proposal: slices: add Push #64090
Labels
Proposal
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
Milestone
Motivation:
Appending a new element to a slice is probably the most common slice operation.
The semantics of the append operator might lead to unwanted bugs due to the return parameter.
Consider the following code:
When assigning to a variable different from the source slice, anotherSlice might or might not end up pointing to the same array as in existingSlice.
Since append is such a common operation in Go, it's very easy to miss this nuance and run into a strange bug with some elements "disappearing" from the slice due to another slice holding a pointer to the same array. This issue is very hard to debug.
While there is a linter in gocritic that tackles the exact same problem, it'd be more efficient to exclude this problem on the language level
Solution:
We can improve the situation by introducing a new function for "just appending" to the slice
Then, you can use it like this:
This solution has the following benefits:
existingSlice
and only then append what they intended to)append
is still available as an operatorThe text was updated successfully, but these errors were encountered: