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

unexpected defer + slice behaviour #26413

Closed
dkostenko opened this issue Jul 17, 2018 · 1 comment
Closed

unexpected defer + slice behaviour #26413

dkostenko opened this issue Jul 17, 2018 · 1 comment

Comments

@dkostenko
Copy link

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

go version go1.8 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/dkostenko/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tq/8tjk5zj92yx85_d7s_lsrkzh0000gn/T/go-build732852120=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

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

What did you expect to see?

I expect to see such output:

plain1: 0x10414020; len(a) = 2, cap(a) = 3, value: [0 0]
plain2: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 0]
plain3: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 0]
plain4: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 123]
plain5: 0x10414020; len(a) = 3, cap(a) = 3, value: [345 123 1]
defer : 0x10414020; len(a) = 3, cap(a) = 3, value: [345 123 1]

There is a difference in the last output line with a prefix "defer": i expect to see the same array as in the previous line with a prefix "plain5", because, as i understand, defer must run after all commands and output the latest state of slice (even after append). And it's confusing to see the same pointer address of the slice in all output lines.

What did you see instead?

plain1: 0x10414020; len(a) = 2, cap(a) = 3, value: [0 0]
plain2: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 0]
plain3: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 0]
plain4: 0x10414020; len(a) = 2, cap(a) = 3, value: [345 123]
plain5: 0x10414020; len(a) = 3, cap(a) = 3, value: [345 123 1]
defer : 0x10414020; len(a) = 2, cap(a) = 3, value: [345 123]

In the defer output I see not the last state of the slice, but slice has the same address in all output lines.

@davecheney
Copy link
Contributor

defer captures the value of a by value, that is, it makes a copy of a, thus changes to a later on are not reflected in the slice header captured by defer.

Here is another example, https://play.golang.org/p/BC8rePavZRw

Unlike many projects on GitHub, the Go project does not use its bug tracker for general discussion or asking questions. We only use our bug tracker for tracking bugs and tracking proposals going through the Proposal Process. I'm going to close this issue as it is not a bug in Go.

Please see https://golang.org/wiki/Questions for good places to ask questions.

@golang golang locked and limited conversation to collaborators Jul 17, 2019
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