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
+9.5k Golang : Quadratic example
+7.3k Golang : Scanf function weird error in Windows
+5k Golang : Experimental Jawi programming language
+14.8k Golang : Basic authentication with .htpasswd file
+19.4k Golang : Example for DSA(Digital Signature Algorithm) package functions
+16.3k Golang : Get IP addresses of a domain name
+21.7k Golang : Use TLS version 1.2 and enforce server security configuration over client
+6k Golang : Grab news article text and use NLP to get each paragraph's sentences
+5.8k AWS S3 : Prevent Hotlinking policy
+6.8k Golang : Decode XML data from RSS feed
+18.5k Golang : Generate thumbnails from images
+3.6k Java : Random alphabets, alpha-numeric or numbers only string generator