Skip to content
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

bug: look like to watch same address when assign to another variable from sub-slice. #56617

Closed
tomoyukikino opened this issue Nov 7, 2022 · 1 comment

Comments

@tomoyukikino
Copy link

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

$ go version
go version go1.19.3 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

intel MacOS Monterey version 12.6, yes I use go env.

go env Output
$ go env
GO111MODULE="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/tomoyuki/Library/Caches/go-build"
GOENV="/Users/tomoyuki/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/tomoyuki/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/tomoyuki/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wz/r55pw77x0_1dv15r4w3wtds00000gn/T/go-build1838909510=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I assign y slice variable from x sub-slice.
I try to append 30 to y variable. I expect to append y variable only. but append y and x.
perhaps, y and x is look like watch some address.
In this case, should be pass by value, but it's call by pointer.

package main

import "fmt"

func main() {
	x := []int{1, 2, 3, 4}
	y := x[:2]

	fmt.Println("x:", x) // x: [1 2 3 4]
	fmt.Println("y:", y) // y: [1 2]

	y = append(y, 30)
	fmt.Println("x:", x) // x: [1 2 30 4] ← I think x should be [1 2 3 4].
	fmt.Println("y:", y) // y: [1 2 30]
}

play : https://go.dev/play/p/HWwI5WCjDXm

What did you expect to see?

Of course, I know how to fix this bug, should use copy.

package main

import "fmt"

func main() {
	x := []int{1, 2, 3, 4}
	y := make([]int, len(x[:2]))
	copy(y, x[:2])

	fmt.Println("x:", x) // x: [1 2 3 4]
	fmt.Println("y:", y) // y: [1 2]

	y = append(y, 30)
	fmt.Println("x:", x) // x: [1 2 3 4] ← no problem
	fmt.Println("y:", y) // y: [1 2 30]
}

play : https://go.dev/play/p/IayY-PdoM0l

@ianlancetaylor
Copy link
Member

That is how the language works. Please see https://go.dev/blog/slices-intro.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
@golang golang locked and limited conversation to collaborators Nov 7, 2023
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