Skip to content

main: chained appends change contents of previous variable #65034

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
daHaimi opened this issue Jan 9, 2024 · 2 comments
Closed

main: chained appends change contents of previous variable #65034

daHaimi opened this issue Jan 9, 2024 · 2 comments

Comments

@daHaimi
Copy link

daHaimi commented Jan 9, 2024

Go version

go version 1.19.1, 1.20, 1.21 linux/amd64

Output of go env in your module/workspace:

GOCACHE="/home/dahaimi/.cache/go-build"
GOENV="/home/dahaimi/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dahaimi/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dahaimi/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.19"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.19/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1433866872=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I reproduced the issue on https://go.dev/play/p/gRUHj2w1DmJ:

  • Create a slice of an initial collection and stored it to variable a -> a has expected value
  • Create a slice b using append to add an item to a -> b has expected value
  • Create a slice c using append to add an item to b -> c has expected value
  • Create a slice d using append to add an other item to b
    • -> d has expected value
    • -> c has the value of d

I tried also with global vars and := variable declarations. The result always was the same, so I suggest this is a problem in the behaviour of append.

What did you see happen?

In my example, I added the output of the pointers, which are correct.

So the last call to append is changing the contents of c, although only d is being touched.

What did you expect to see?

I expect to see the value of c remain untouched when creating d by appending an item to b, as c is not interacting in any way.

@robpike
Copy link
Contributor

robpike commented Jan 9, 2024

It's working as expected. Both c and share the same underlying array, so when you declare d by modifying b, you also modify c.

@daHaimi
Copy link
Author

daHaimi commented Jan 9, 2024

Okay, I did deeper investigations and what to expect. But although it is not intuitive, it is technically correct behaviour.
The weird thing is, that different pointers to the same slice have the same (c and d) or different (b and c/d) contents; b, c and d are different pointers, but c and d point literally to the same content while b does not.

@daHaimi daHaimi closed this as completed Jan 9, 2024
@golang golang locked and limited conversation to collaborators Jan 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants