|
|
|
|
|
by hsn915
1748 days ago
|
|
This is fine: s2 := append(s1, item)
It's only not fine if you some how assume that s2 and s1 will always refer to the same data.> other language separate slices and vectors Go has a separate type for arrays, which is a value type var arr [10]int
|
|
No, it is not.
> It's only not fine if you some how assume that s2 and s1 will always refer to the same data.
It's not fine if you assume anything about the interaction of s1 and s2. It's not fine if you assume they do alias, it's not fine if you assume they don't.
There is, fundamentally, no situation in which that construct is anything other than a footgun. `append` should only ever be used with the same slice on the LHS and RHS[0], or a brand new slice object constructed for the occasion on the RHS.
> Go has a separate type for arrays, which is a value type
An array is neither a vector nor a slice.
The issue is that Go's slices serve as both a vector and an actual slice, and the union of these interfaces creates footguns which don't exist in either.
[0] IFF that RHS is a either a non-parameter local, or a pointer to a slice