Skip to content

appending two slices with default ends modifies base slice #37604

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

Closed
xeviknal opened this issue Mar 2, 2020 · 3 comments
Closed

appending two slices with default ends modifies base slice #37604

xeviknal opened this issue Mar 2, 2020 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@xeviknal
Copy link

xeviknal commented Mar 2, 2020

What version of Go are you using (go version)?

$ go version
1.13.7

Does this issue reproduce with the latest release?

Yes, go1.14

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xcanalma/.cache/go-build"
GOENV="/home/xcanalma/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xcanalma/.gvm/pkgsets/go1.14/global"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/xcanalma/.gvm/gos/go1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/xcanalma/.gvm/gos/go1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build278629003=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/ys0shvSiHkW

What did you expect to see?

I expect that both append and slice[] operator don't modify the slice variable.

[2 3 5 7 11 13]
[2 3 11 13]
[2 3 5 7 11 13]

What did you see instead?

[2 3 5 7 11 13]
[2 3 11 13]
[2 3 11 13 11 13]
@renthraysk
Copy link

If don't want to modify the backing memory of a slice with append, limit the capacity, which will force append to allocate a new slice plus memory.

s2 := append(s[0:2:2], s[4:]...)

@dmitshur
Copy link
Member

dmitshur commented Mar 2, 2020

I believe the behavior you're seeing matches what is written in the Go specification. Do you see an inconsistency?

Append operates on a slice, and a slice has an underlying array. If two slices share common parts of an underlying array, then modifying one slice can cause the other slice to have different data. To avoid that, the two slices should not share the same underlying array.

Also see https://golang.org/wiki/SliceTricks.

@dmitshur dmitshur added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 2, 2020
@dmitshur dmitshur added this to the Backlog milestone Mar 2, 2020
@xeviknal
Copy link
Author

xeviknal commented Mar 2, 2020

Thanks for the explanation. Once the behind the scenes of a slice is clear, the append methods makes more sense. However, it looks like the passed array is read-only and a new array is returned.

Didn't know the 3-index operand. Interesting one. Thanks and closing the issue.

@xeviknal xeviknal closed this as completed Mar 2, 2020
@golang golang locked and limited conversation to collaborators Mar 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants