Description
Go version
go version go1.23.2 linux/amd64
Output of go env
in your module/workspace:
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/yaxum62/.cache/go-build'
GOENV='/home/yaxum62/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/yaxum62/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/yaxum62/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.23'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.23/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/yaxum62/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/yaxum62/src/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build812913915=/tmp/go-build -gno-record-gcc-switches'
What did you do?
Original post: https://groups.google.com/g/golang-nuts/c/anNFVPtCGyQ/
Example code: https://go.dev/play/p/6RulNQwIk4n
The example code contains an assignment statement:
phase_2, foo.bar.value = "??????", compute_value()
The assignment statement will trigger nil pointer panic and contains visible side effects in both phases as described in golang spec:
The assignment proceeds in two phases. First, the operands of index expressions and pointer indirections (including implicit pointer indirections in selectors) on the left and the expressions on the right are all evaluated in the usual order. Second, the assignments are carried out in left-to-right order.
What did you see happen?
The nil pointer panic happened after compute_value()
expression is evaluated in phase 1 but before phase_2
variable is updated in phase 2.
What did you expect to see?
The nil pointer panic happened before compute_value()
expression is evaluated in phase 1, as mentioned in original post.