我想将数字附加到列表中,但我的切片仅在 for 循环中更新值。
外面怎么更新呢?
slice := []int{5,4,3,2,1}
for i := 0; i < len(slice); i++ {
slice := append(slice, i)
fmt.Println(slice)
}
fmt.Println(slice)
实际结果
[5 4 3 2 1 0]
[5 4 3 2 1 1]
[5 4 3 2 1 2]
[5 4 3 2 1 3]
[5 4 3 2 1 4]
[5 4 3 2 1]
预期结果
[5 4 3 2 1 0]
[5 4 3 2 1 1]
[5 4 3 2 1 2]
[5 4 3 2 1 3]
[5 4 3 2 1 4]
[5 4 3 2 1 0 1 2 3 4]
这段代码可以在 Python 中运行,但在 go 中有些东西我没有发现
蓝山帝景
相关分类