I want to ask a question. Is there func that the len of slice is not resized, but the cap is not resized. Because I don’t want reassign memory, it can cost .
for example ,
slice’ len = 5, slice cap = 10; after resize slice’ len = 0, slice’ cap = 10.
In the example a and b are two slices pointing to the same array, which is why appending to b is a visible change when printing a…
Edit: I just now fully read the question about reallocation. From this other example it would seem there isn’t and internally just the len is modified: https://play.golang.org/p/xGlzY2xd2Lg. But Johan and Norbert mention there is, so I’ll have to take a better look.
Further edit: In this post it’s mentioned that Slicing does not copy the slice’s data. It creates a new slice value that points to the original array. But Effective Go mentions this: The length of a slice may be changed as long as it still fits within the limits of the underlying array; just assign it to a slice of itself. It seems to me the latter is the case we are dealing with.