You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Appending values to a slice should work as expected.
What did you see instead?
Some of the existing values in the slice change after appending a new value.
Example from the play.golang code:
A slice in this state: [[] [1] [2] [3] [4] [5] [1 2]]
Gets a call to append a []int{1,3}
The resulting slice is: [[] [1] [2] [3] [4] [5] [1 3] [1 3]]
Expected: [[] [1] [2] [3] [4] [5] [1 2] [1 3]]
The perplexing part is this is only happening for some values and running the same code in different versions of go produces different outputs:
Apologies for the somewhat convoluted example but I'm not sure how to reduce it any further.
The text was updated successfully, but these errors were encountered:
myartsev
changed the title
Appending a new values to a slice sometimes(!) changes existing slice values
Appending a new value to a slice sometimes changes existing slice values
Dec 5, 2018
This is working as intended. Doing ret = append(ret, set) does not copy the backing store for set. You modify that backing store on the next iteration. You may want to read this if you haven't already: https://blog.golang.org/go-slices-usage-and-internals
Unlike many projects on GitHub, the Go project does not use its bug tracker for general discussion or asking questions. We only use our bug tracker for tracking bugs and tracking proposals going through the Proposal Process.
What version of Go are you using (
go version
)?1.9.2 and 1.11.1
(results are different between versions!)
Does this issue reproduce with the latest release?
Yes
What did you do?
https://play.golang.org/p/EZ5u42kxiMy
What did you expect to see?
Appending values to a slice should work as expected.
What did you see instead?
Some of the existing values in the slice change after appending a new value.
Example from the play.golang code:
A slice in this state:
[[] [1] [2] [3] [4] [5] [1 2]]
Gets a call to append a
[]int{1,3}
The resulting slice is:
[[] [1] [2] [3] [4] [5] [1 3] [1 3]]
Expected:
[[] [1] [2] [3] [4] [5] [1 2] [1 3]]
The perplexing part is this is only happening for some values and running the same code in different versions of go produces different outputs:
1.9.2
[[] [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 4] [2 3 4 5] [1 2 3 4 5]]
1.11.1
[[] [1] [2] [3] [4] [5] [1 2] [1 2] [1 2] [1 2] [2 3] [2 4] [2 5] [3 4] [3 4] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 4] [2 3 4 5] [1 2 3 4 5]]
Expected
[[] [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [2 3 4] [2 3 5] [3 4 5] [1 2 3 4] [1 2 3 5] [2 3 4 5] [1 2 3 4 5]]
Apologies for the somewhat convoluted example but I'm not sure how to reduce it any further.
The text was updated successfully, but these errors were encountered: