本文介绍了golang append()已评估,但未使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
func main(){
var array [10]int
sliceA := array[0:5]
append(sliceA, 4)
fmt.Println(sliceA)
}
我不知道为什么?切片附加操作未运行...
I don't Know why? The slice append operation is not run...
推荐答案
参考:附加并复制切片
在Go中,参数按值传递.
In Go, arguments are passed by value.
典型的append
用法是:
a = append(a, x)
您需要写:
func main(){
var array [10]int
sliceA := array[0:5]
// append(sliceA, 4) // discard
sliceA = append(sliceA, 4) // keep
fmt.Println(sliceA)
}
输出:
[0 0 0 0 0 4]
希望对您有帮助.
这篇关于golang append()已评估,但未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!