Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
1. get []string
https://golang.org/pkg/strings/#Split
2. create []int with length like []string.
There are 2 "correct" options:
If we want to fill through append, then we use:
Or if we want to fill through an index, then we do this:
https://golang.org/ref/spec#Slice_types
https://golang.org/ref/ spec#Length_and_capacity
3. iterate for range over []string and fill in []int.
https://golang.org/ref/spec#For_statements
https://golang.org/pkg/strconv/#Atoi
Don't forget to handle errors.
https://play.golang.org/p/gGU5-rsySS
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
str := "1,3,5,6,8,42"
strs := strings.Split(str, ",")
var ints []int
for _, s := range strs {
num, err := strconv.Atoi(s)
if err == nil {
ints = append(ints, num)
}
}
fmt.Println(ints)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question