[英]Appending to a slice with enough capacity using value receiver
can someone help me understand what happens here? 有人可以帮助我了解这里发生的情况吗?
package main
import (
"fmt"
)
func appendString(slice []string, newString string) {
slice = append(slice, newString)
}
func main() {
slice := make([]string, 0, 1)
appendString(slice, "a")
fmt.Println(slice)
}
I know about the slice header and the need to use a pointer receiver. 我知道切片头和使用指针接收器的需要。 But here, as the underlying array has enough capacity I would expect append to work anyways (just adding the new value to the underlying array and the original [copied] header working as expected) 但是在这里,由于基础数组具有足够的容量,因此我希望追加仍可以正常工作(只需将新值添加到基础数组中,并且原始[复制的]标头将按预期工作)
What is wrong with my assumptions? 我的假设有什么问题?
Let's add a final print statement to see the result: 让我们添加最终的打印语句以查看结果:
slice := make([]string, 0, 1)
fmt.Println(cap(slice))
appendString(slice, "a")
fmt.Println(slice)
And the output will be (try it on the Go Playground ): 输出将是(在Go Playground上尝试):
1
[]
Which is correct. 哪个是对的。 One could expect the output to be: 可以期望输出为:
1
[a]
The reason why this is not the case is because even though a new backing array will not be allocated, the slice header in the slice
variable inside main()
is not changed , it will still hold length = 0
. 之所以不是这种情况,是因为即使不分配新的支持数组, main()
内slice
变量中的slice头也不会更改 ,它仍将保持length = 0
。 Only the slice header stored in the slice
local variable inside appendString()
(the parameter) is changed, but this variable is independent from main's slice
. 仅更改了存储在appendString()
(参数)内部的slice
局部变量中的slice头,但是此变量独立于main的slice
。
If you were to reslice main's slice
, you will see that the backing array does contain the new string: 如果要重新slice
main的slice
,您将看到backing数组确实包含新字符串:
slice := make([]string, 0, 1)
fmt.Println(cap(slice))
appendString(slice, "a")
fmt.Println(slice)
slice = slice[:1]
fmt.Println(slice)
Now output will be (try it on the Go Playground ): 现在输出将是(在Go Playground上尝试):
1
[]
[a]
This is why the builtin append()
has to return the new slice: because even if no new backing array is needed, the slice header (which contains the length) will have to be changed (increased) if more than 0 elements are appended. 这就是为什么内置的append()
必须返回新的slice的原因:因为即使不需要新的后备数组,如果添加的元素超过0个,slice头(包含长度)也将不得不更改(增加)。
This is why appendString()
should also return the new slice: 这就是为什么appendString()
也应返回新切片的原因:
func appendString(slice []string, newString string) []string {
slice = append(slice, newString)
return slice
}
Or short: 或简称:
func appendString(slice []string, newString string) []string {
return append(slice, newString)
}
Which you have to reassign where you use it: 您必须在哪里使用它来重新分配它:
slice := make([]string, 0, 1)
fmt.Println(cap(slice))
slice = appendString(slice, "a")
fmt.Println(slice)
And then you get the expected outcome right away (try it on the Go Playground ): 然后立即获得预期的结果(在Go Playground上尝试):
1
[a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.