Golang : Convert string to array/slice
Just a note for my own self. Hope it will be useful for you.
Problem :
You have a string like this :
apple orange durian pear
and you want to convert this string into an array
Solution :
Use strings.Fields()
function to instantly convert the string into an array.
package main
import (
"fmt"
"strings"
)
func main() {
str := "apple orange durian pear"
strArray := strings.Fields(str)
fmt.Println(strArray)
fmt.Println(strArray[1:3])
}
Output :
[apple orange durian pear]
[orange durian]
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.7k Golang : Convert(cast) string to int64
+11.6k Golang : How to check if IP address is in range
+14.1k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+8.1k Golang : ISO8601 Duration Parser example
+9.7k Swift : Convert (cast) Int or int32 value to CGFloat
+16.4k Golang : Secure(TLS) connection between server and client
+12.2k Golang : Basic authentication with .htpasswd file
+9.2k Golang : Remove or trim extra comma from CSV
+6.4k Golang : Handle sub domain with Gin
+4.1k Golang : Error handling methods
+8.6k Golang : Get local time and equivalent time in different time zone
+9.3k How to test Facebook App on localhost ?