Home >Backend Development >Golang >Why does `append(x, x...)` copy the slice into a new backing array in Go?
php editor Youzi will answer a common question for everyone: Why does `append(x, x...)` copy the slice to the new support array in Go? In the Go programming language, the `append` function is used to append elements to a slice. When we use the `append` function, if the slice capacity is insufficient, Go will create a new underlying array and copy the elements in the original slice to the new underlying array. This is because in Go, a slice is a reference to a dynamic array. When the slice capacity is not enough, a new array must be created to accommodate more elements. This mechanism ensures the continuity and scalability of slices, but also brings some performance losses.
In go's slicing tips wiki and go libraries (such as this example), you will sometimes see code similar to the following for copying a slice to a new backing array middle.
// In a library at the end of a function perhaps... return append(whateverSlice[:0:0], whateverSlice...) // In an assignment, as in the wiki example... b = append(a[:0:0], a...)
Here's what I think I understand:
append
will be copied to the new backing array. append
, the code uses a full slice expression. (We could rewrite the first parameter as a[0:0:0]
, but if omitted, the first 0
will be provided. I think this is more consistent with here The big meaning has nothing to do with it.) copy
instead of append
and it reads clearer.) However, I still don't quite understand why the syntax append(someslice[:0:0], someslice...)
creates a new backing array. I was also initially confused as to why the append
operation didn't mess up (or truncate) the original slice.
Now my guess:
newslice := oldslice
then changes to one will be reflected in the other. Normally, you don't want this. append
to the original slice (which is normal in go), nothing happens to the original slice. It is not truncated or altered in any way. anyslice[:0:0]
are zero, if go wants to assign the elements of anyslice
to the result, a new support must be created array. Is this the reason to create a new backing array? anyslice...
has no elements? A snippet on the go playground shows that if you use this additional trick on an empty slice, the copy and the original initially have the same backing array. (Edit: As the commenter explained, I misunderstood the snippet. The snippet shows that both items are initially the same, but neither supports arrays. They both point to the original is a universal zero value.) Since both slices have zero length and capacity, when you add anything to one of the slices, that slice gets a new backing array. So I guess, the effect is still the same. That is, append
After copying, the two slices cannot affect each other. append
copy method immediately generates a new backing array. In this case, the two resulting slices are immediately separated, so to speak. I'm probably overly worried about this, but I'd like a fuller explanation of why append(a[:0:0], a...)
The trick works is so.
Since the length and capacity of anySlice[:0:0] are zero, if Go wants to assign the elements of anySlice to the result, it must create a new backing array . Is this why the new backing array was created?
Because the capacity is 0
, yes.
https://pkg.go.dev/[email protected]#append
If there is sufficient capacity, the target will be resliced to accommodate the new elements. If not, a new underlying array will be allocated.
cap=0
is not enough for non-empty slices, a new array needs to be allocated. The above is the detailed content of Why does `append(x, x...)` copy the slice into a new backing array in Go?. For more information, please follow other related articles on the PHP Chinese website!