How do I convert strings in an array to integers in an array in go?
如何将数组中的字符串转换成数组中的整数?
["1", "2", "3"]
to
来
[1, 2, 3]
I've searched for some solutions online but couldn't find it. I've tried to loop through the array and did strconv.ParseFloat(v, 64) where v is the value but it didn't work.
我在网上搜索了一些解决方案,但找不到。我试着遍历数组,并做了strconv。ParseFloat(v, 64)其中v是值,但它不起作用。
3 个解决方案
#1
13
You will have to loop through the slice indeed. If the slice only contains integers, no need of ParseFloat
, Atoi
is sufficient.
你将不得不循环穿过这片区域。如果切片仅包含整数,则不需要ParseFloat, Atoi就足够了。
import "fmt"
import "strconv"
func main() {
var t = []string{"1", "2", "3"}
var t2 = []int{}
for _, i := range t {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
t2 = append(t2, j)
}
fmt.Println(t2)
}
On Playground.
在操场上。
#2
7
For example,
例如,
package main
import (
"fmt"
"strconv"
)
func sliceAtoi(sa []string) ([]int, error) {
si := make([]int, 0, len(sa))
for _, a := range sa {
i, err := strconv.Atoi(a)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
func main() {
sa := []string{"1", "2", "3"}
si, err := sliceAtoi(sa)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%q %v\n", sa, si)
}
Output:
输出:
["1" "2" "3"] [1 2 3]
Playground:
操场上:
http://play.golang.org/p/QwNO8R_f90
http://play.golang.org/p/QwNO8R_f90
#3
0
A slice is a descriptor of an array segment
It consists of
- a pointer to the array,
- the length of the segment, and
- its capacity (the maximum length of the segment)
slice是一个数组段的描述符,它包括一个指向数组的指针,即段的长度,以及它的容量(段的最大长度)
Below, string Array/Slice is converted to int Array/Slice:
下面,将字符串数组/切片转换为int数组/切片:
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
func Slice_Atoi(strArr []string) ([]int, error) {
// NOTE: Read Arr as Slice as you like
var str string // O
var i int // O
var err error // O
iArr := make([]int, 0, len(strArr))
for _, str = range strArr {
i, err = strconv.Atoi(str)
if err != nil {
return nil, err // O
}
iArr = append(iArr, i)
}
return iArr, nil
}
func main() {
strArr := []string{
"0 0 24 3 15",
"0 0 2 5 1 5 11 13",
}
for i := 0; i < len(strArr); i++ {
iArr, err := Slice_Atoi(strings.Split(strArr[i], " "))
if err != nil {
log.Print("Slice_Atoi failed: ", err)
return
}
fmt.Println(iArr)
}
}
Output:
输出:
[0 0 24 3 15]
[0 0 2 5 1 5 11 13]
I used in a project, so did a small optimizations from other replies, marked as // O for above, also fixed a bit in readability for others
我在一个项目中使用过,所以从其他回复中做了一个小的优化,在上面标记为// O,也为其他人修改了一些可读性。
Best of luck
最好的运气
#1
13
You will have to loop through the slice indeed. If the slice only contains integers, no need of ParseFloat
, Atoi
is sufficient.
你将不得不循环穿过这片区域。如果切片仅包含整数,则不需要ParseFloat, Atoi就足够了。
import "fmt"
import "strconv"
func main() {
var t = []string{"1", "2", "3"}
var t2 = []int{}
for _, i := range t {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
t2 = append(t2, j)
}
fmt.Println(t2)
}
On Playground.
在操场上。
#2
7
For example,
例如,
package main
import (
"fmt"
"strconv"
)
func sliceAtoi(sa []string) ([]int, error) {
si := make([]int, 0, len(sa))
for _, a := range sa {
i, err := strconv.Atoi(a)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
func main() {
sa := []string{"1", "2", "3"}
si, err := sliceAtoi(sa)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%q %v\n", sa, si)
}
Output:
输出:
["1" "2" "3"] [1 2 3]
Playground:
操场上:
http://play.golang.org/p/QwNO8R_f90
http://play.golang.org/p/QwNO8R_f90
#3
0
A slice is a descriptor of an array segment
It consists of
- a pointer to the array,
- the length of the segment, and
- its capacity (the maximum length of the segment)
slice是一个数组段的描述符,它包括一个指向数组的指针,即段的长度,以及它的容量(段的最大长度)
Below, string Array/Slice is converted to int Array/Slice:
下面,将字符串数组/切片转换为int数组/切片:
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
func Slice_Atoi(strArr []string) ([]int, error) {
// NOTE: Read Arr as Slice as you like
var str string // O
var i int // O
var err error // O
iArr := make([]int, 0, len(strArr))
for _, str = range strArr {
i, err = strconv.Atoi(str)
if err != nil {
return nil, err // O
}
iArr = append(iArr, i)
}
return iArr, nil
}
func main() {
strArr := []string{
"0 0 24 3 15",
"0 0 2 5 1 5 11 13",
}
for i := 0; i < len(strArr); i++ {
iArr, err := Slice_Atoi(strings.Split(strArr[i], " "))
if err != nil {
log.Print("Slice_Atoi failed: ", err)
return
}
fmt.Println(iArr)
}
}
Output:
输出:
[0 0 24 3 15]
[0 0 2 5 1 5 11 13]
I used in a project, so did a small optimizations from other replies, marked as // O for above, also fixed a bit in readability for others
我在一个项目中使用过,所以从其他回复中做了一个小的优化,在上面标记为// O,也为其他人修改了一些可读性。
Best of luck
最好的运气