Skip to content

Appending a new value to a slice sometimes changes existing slice values #29115

Closed
@myartsev

Description

@myartsev

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions