-
Notifications
You must be signed in to change notification settings - Fork 18k
builtin: append replaces values #53795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Working as intended, you need to use the result of append as it may reallocate the underlying array if there is insufficient space for new elements. |
I disagree. |
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only. For questions please refer to https://github.com/golang/go/wiki/Questions |
@Vector-Hector https://play.golang.com/p/UNFoS592mq5 prints "3 vs 2":
|
What version of Go are you using (
go version
)?What did you do?
Play
Result:
["a", "b", "e"]
What did you expect to see?
I expected append to create a new slice, that is
["a", "b", "e"]
. I did not expectappend
to change the original slicearr
as it does not target that slice. If I instead useappend(arr[:2], "e", "f")
, it does not changearr
(as expected).What did you see instead?
The slice
arr
changed its third value from"c"
to"e"
.The text was updated successfully, but these errors were encountered: