We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
go version go1.23.8 darwin/arm64
go env
GO111MODULE='on' GOARCH='arm64' GOBIN='' GOCACHE='/Users/bytedance/Library/Caches/go-build' GOENV='/Users/bytedance/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/bytedance/go/pkg/mod' GONOPROXY='*.byted.org,*.everphoto.cn,git.smartisan.com' GONOSUMDB='*.byted.org,*.everphoto.cn,git.smartisan.com' GOOS='darwin' GOPATH='/Users/bytedance/go' GOPRIVATE='*.byted.org,*.everphoto.cn,git.smartisan.com' GOPROXY='https://goproxy.byted.org|https://goproxy.cn|direct' GOROOT='/Users/bytedance/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.8.darwin-arm64' GOSUMDB='sum.golang.google.cn' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/Users/bytedance/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.8.darwin-arm64/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.23.8' GODEBUG='' GOTELEMETRY='local' GOTELEMETRYDIR='/Users/bytedance/Library/Application Support/go/telemetry' GCCGO='gccgo' GOARM64='v8.0' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='1' GOMOD='/dev/null' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config'
package main type Person struct { Name string Age int TestSlice []string } // main demo5 - 测试 marshal 和 unmarshal 方法对 nil 值的实践 func main() { pList1 := []Person{ { Name: "1", }, { Name: "2", }, } pList2 := []Person{ { Name: "3", }, { Name: "4", }, } println("1111", &pList1, &pList2, &pList1[0]) test(pList1, pList2) println("1111", &pList1, &pList2, &pList1[0]) } func test(personList ...[]Person) { println(&personList[0], &personList[1], &personList[0][0]) for _, persons := range personList { for i := range persons { persons[i].Name = "test" } } testPerson := []Person{ { Name: "5", }, } println("2222", &testPerson) personList[0] = testPerson }
1111 0x14000050628 0x14000050610 0x14000050640 0x140000506a8 0x140000506c0 0x14000050640 2222 0x1400009cdf8 1111 0x1400009ce28 0x1400009ce10 0x1400009ce40
After calling the test function, the addresses of pList1 and pList2 do not change. However, if the following lines:
println(&personList[0], &personList[1], &personList[0][0]) println("2222", &testPerson)
are omitted, the addresses of pList1 and pList2 are the same.
The text was updated successfully, but these errors were encountered:
What makes you think the addresses won't change?
We do stack copying, so if plist1 and plist2 are on the stack, their addresses will change.
plist1
plist2
Sorry, something went wrong.
Related Issues
Related Discussions
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
No branches or pull requests
Go version
go version go1.23.8 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
What did you expect to see?
After calling the test function, the addresses of pList1 and pList2 do not change.
However, if the following lines:
are omitted, the addresses of pList1 and pList2 are the same.
The text was updated successfully, but these errors were encountered: