I tackle with this question.
I need to convert strings to int.
In this case, I need to convert "5 2 4 6 1 3" to, for example, [6]int{5,2,4,6,1,3}.
I wrote following this code, especially AizuArray()
.
It seems elements are int here.
Would you please let me know if my way is correct?
Or could you let me know the better ways?
I ask this because I feel my way would be redundant and Java way is much easier.
Thank you.
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
)
func AizuArray(A string, N string) []int {
a := strings.Split(A, " ")
n, _ := strconv.Atoi(N) // int 32bit
b := make([]int, n)
for i, v := range a {
b[i], _ = strconv.Atoi(v)
}
return b
}
func main() {
A := "5 2 4 6 1 3"
N := "6"
j := strings.Split(A, " ")
for _, v := range j {
fmt.Println(reflect.TypeOf(v))
}
b := AizuArray(A, N)
fmt.Println(b)
for _, v := range b {
fmt.Println(reflect.TypeOf(v))
}
}