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
+8.8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+11.1k Golang : Byte format example
+5.7k nginx : force all pages to be SSL
+4.8k Swift : Convert (cast) Float to Int or Int32 value
+4.5k Facebook : How to place save to Facebook button on your website
+9.4k Golang : Detect number of active displays and the display's resolution
+7.8k Golang : Variadic function arguments sanity check example
+24.2k Golang : Change file read or write permission example
+7.6k Golang : Get today's weekday name and calculate target day distance example
+12.1k Golang : Extract part of string with regular expression
+20.4k Golang : Underscore or snake_case to camel case example
+6.1k PHP : Proper way to get UTF-8 character or string length