golang append() 被评估但未使用

huangapple go评论65阅读模式
英文:

golang append() evaluated but not used

问题

func main() {
    var array [10]int
    sliceA := array[0:5]
    append(sliceA, 4)
    fmt.Println(sliceA)
}

> 错误:append(sliceA, 4) 被计算但未使用

我不知道为什么?切片的追加操作没有执行...

英文:
func main(){
     var array [10]int
     sliceA := array[0:5]
     append(sliceA, 4)
     fmt.Println(sliceA)
}

> Error : append(sliceA, 4) evaluated but not used

I don't Know why? The slice append operation is not run...

答案1

得分: 134

参考:追加和复制切片

在Go语言中,参数是按值传递的。

append 的典型用法是:

a = append(a, x)

你需要写成:

func main(){
	var array [10]int
	sliceA := array[0:5]
	// append(sliceA, 4)  // 丢弃
	sliceA = append(sliceA, 4)  // 保留
	fmt.Println(sliceA)
}

输出

[0 0 0 0 0 4]
英文:

Refer: Appending to and copying slices

In Go, arguments are passed by value.

Typical append usage is:

a = append(a, x)

You need to write:

func main(){
	var array [10]int
	sliceA := array[0:5]
	// append(sliceA, 4)  // discard
	sliceA = append(sliceA, 4)  // keep
	fmt.Println(sliceA)
}

Output:

[0 0 0 0 0 4]

答案2

得分: 15

sliceA = append(sliceA, 4)

append() 返回一个包含一个或多个新值的切片。
请注意,我们需要接收 append 的返回值,因为我们可能会得到一个新的切片值。

英文:
sliceA = append(sliceA, 4)

append() returns a slice containing one or more new values.
Note that we need to accept a return value from append as we may get a new slice value.

答案3

得分: 4

你可以尝试这样做:

sliceA = append(sliceA, 4)

内置函数append([]type, ...type)返回一个类型为数组/切片的值,应该将其赋给你想要的值,而输入的数组/切片只是一个源。简单来说,outputSlice = append(sourceSlice, appendedValue)

英文:

you may try this:

sliceA = append(sliceA, 4)

built-in function append([]type, ...type) returns an array/slice of type, which should be assigned to the value you wanted, while the input array/slice is just a source. Simply, outputSlice = append(sourceSlice, appendedValue)

答案4

得分: 2

根据Go文档:

append的返回值是一个包含原始切片所有元素以及提供的值的切片。

因此,'append'的返回值将包含原始切片以及追加的部分。

英文:

Per the Go docs:

> The resulting value of append is a slice containing all the elements of the original slice plus the provided values.

So the return value of 'append', will contain your original slice with the appended portion.

答案5

得分: 1

理解的关键是,切片(slice)只是底层数组的一个"视图"。
你通过值传递将该视图传递给append函数,底层数组被修改,最后append函数的返回值给你底层数组的不同视图,也就是说切片(slice)中有更多的元素。

你的代码
sliceA := array[0:5]  // sliceA指向[0,5)
append(sliceA, 4) // 直到你执行以下操作,sliceA仍然是原始视图[0,5)

sliceA = append(sliceA, 4)

参考:https://blog.golang.org/slices

英文:

The key to understand is that slice is just a "view" of the underling array.
You pass that view to the append function by value, the underling array gets modified, at the end the return value of append function gives you the different view of the underling array. i.e. the slice has more items in it

your code
sliceA := array[0:5]  // sliceA is pointing to [0,5)
append(sliceA, 4) // sliceA is still the original view [0,5) until you do the following

sliceA = append(sliceA, 4)

reference: https://blog.golang.org/slices

huangapple
  • 本文由 发表于 2017年7月10日 14:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/45004854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定