|
|
|
|
|
by hsn915
1758 days ago
|
|
Go tells you very explicitly how it resizes slices by forcing you to write this: slice = append(slice, item)
By merely typing this all the time when you add elements, you intuitively understand that appending can potentially re-allocate the slice data in a totally different location, so any pointers you have taken before the resize are not guaranteed to be pointing to items in the new slice; just the old slice. |
|
No, what most readers intuit from that is that `append` performs no mutation and that this is fine:
because it looks very much like, say, and often it will look like it works, especially at the smaller sizes, or if you never modify (or even use) s1.Except it's absolutely not fine.
That's why other language separate slices and vectors and avoid confusing two objects which have different behaviours and uses even if their representation is very similar.