使用值接收器附加到具有足够容量的切片 [英] Appending to a slice with enough capacity using value receiver

查看:77
本文介绍了使用值接收器附加到具有足够容量的切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解这里发生的事情吗?

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)
}

我了解切片标头以及使用指针接收器的需要.但是在这里,由于基础数组具有足够的容量,因此我希望追加仍可以正常工作(只需将新值添加到基础数组中,并且原始的[copied]标头可以按预期工作)

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)

我的假设有什么问题?

推荐答案

让我们添加最终的打印语句以查看结果:

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):

1
[]

这是正确的.可以期望输出为:

Which is correct. One could expect the output to be:

1
[a]

之所以不是这样,是因为即使不分配新的后备数组,main()内的slice变量中的slice头也不会不变,它将仍然按住length = 0.仅存储在appendString()(参数)内部的slice局部变量中的slice头,但是此变量独立于main的slice.

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. 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.

如果要重新分割main的slice,您将看到支持数组确实包含新字符串:

If you were to reslice main's slice, you will see that the backing array does contain the new string:

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):

1
[]
[a]

这就是为什么内置 append() 必须返回新切片的原因:即使不需要新的后备数组,如果追加了0个以上的元素,也必须更改(增加)切片头(包含长度).

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.

这就是为什么appendString()还应该返回新切片的原因:

This is why appendString() should also return the new slice:

func appendString(slice []string, newString string) []string {
    slice = append(slice, newString)
    return slice
}

或更短:

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):

1
[a]

这篇关于使用值接收器附加到具有足够容量的切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆