How to slice a string using a delimiter

sagit picture sagit · Jan 10, 2013 · Viewed 26.1k times · Source

In Go, if I have a string variable s:

var s string = "a,b,c,d,e"

How can I convert or split or explode it into a slice or an array of strings so that it will become:

arr[0] = "a"
...
arr[4] = "e"

Answer

Daniel picture Daniel · Jan 10, 2013

You should use the strings package for that.

stringSlice := strings.Split(s, ",")

http://play.golang.org/p/UKZbcuJUPP