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
+10.4k Android Studio : Simple input textbox and intercept key example
+36.1k Golang : How to split or chunking a file to smaller pieces?
+7.4k Golang : Get YouTube playlist
+32.5k Golang : Regular Expression for alphanumeric and underscore
+15.4k Golang : rune literal not terminated error
+9.2k Golang : How to get ECDSA curve and parameters data?
+8.4k Golang : Another camera capture GUI application with GTK and OpenCV
+47.5k Golang : Convert int to byte array([]byte)
+7.5k Gogland : Where to put source code files in package directory for rookie
+13.4k Golang : reCAPTCHA example
+32.1k Golang : Convert []string to []byte examples
+5.4k Golang : Stop goroutine without channel