Created
November 28, 2013 16:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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