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
+12.3k Golang : Verify token from Google Authenticator App
+7.8k Golang : Capture text return from exec function example
+3.5k Javascript : Detect when console is activated and do something about it
+25.5k Golang : How to check if a connection to database is still alive ?
+12.4k Golang : Check if an integer is negative or positive
+5.2k Golang & Javascript : How to save cropped image to file on server
+10.7k Golang : Pagination with go-paginator configuration example
+5.4k CodeIgniter : form input set_value cause " to become & quot
+7.3k Golang: Prevent over writing file with md5 hash
+7.2k How to show different content from website server when AdBlock is detected?
+25.9k PHP : Count number of JSON items/objects