2
花了几个小时试图找到一个解决方案,以解决将一串数字转换为单个数字的数组整数到Go中的“简单”问题。尝试了许多不同的方法,但总是遇到问题。这是最后一次尝试。它会建立,但会在运行时在指定的行上提供超出范围的索引。可能会有几个AH会让我失望,因为我会问愚蠢的问题,但是在多个Google搜索的前50个结果中我找不到解决方案。所以来吧,纪念我,你是男人。对其他99%:感谢您的耐心和帮助。将字符串转换为Go中的单个数字整数数组
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
s := "876567747896354336739443262"
var dstr []string = strings.SplitAfterN(s,"",len(s))
var dint []int
for i := 0; i < len(s); i++ {
dint[i], _ = strconv.Atoi(dstr[i]) //index out of range at runtime
fmt.Printf("dstr[%v] is: %s\n", i, dstr[i])
fmt.Printf("dint[%v] is: %v\n", i, dint[i])
}
}