Skip to content

Instantly share code, notes, and snippets.

@asmedrano
Created November 28, 2013 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asmedrano/7694449 to your computer and use it in GitHub Desktop.
Save asmedrano/7694449 to your computer and use it in GitHub Desktop.
Turn a comma delmited string of ints into a slice of actual ints
package main
import "fmt"
import "strings"
import "strconv"
func main() {
fmt.Println("Hello, playground")
s := "1,2,4"
n := commaStringToIntSlice(s)
fmt.Println(n)
}
func commaStringToIntSlice(cs string) []int{
st := strings.Split(cs, ",")
out := []int{}
for i:=0; i< len(st); i++ {
fmt.Println(st[i])
val, err:= strconv.Atoi(st[i])
if err == nil{
out = append(out, val)
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment