Skip to content

Syntax issue with Go compiler when using two-value assignment from map to struct member #17871

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
peteArnt opened this issue Nov 10, 2016 · 4 comments

Comments

@peteArnt
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version devel +73497c7 Thu Nov 10 01:34:03 2016 +0000 linux/amd64

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

GOARCH="amd64"
GOBIN="/home/peter/mygo/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/peter/mygo"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="/usr/bin/gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build003323332=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
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?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

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

What did you expect to see?

A successful compilation

What did you see instead?

tmp/sandbox205861443/main.go:18: non-name x.S on left side of :=

It appears that using the idiomatic map lookup pattern, there is a problem assigning the retrieved map value to a structure member (as opposed to a simple local variable). This occurs when a status variable (i.e. "ok") is used with the ":=" operator.
x.S, ok := m[2]
where: x is a local structure with member S, and ok is created within the statement due to the :=
See the playground link above.

@bradfitz
Copy link
Contributor

That's how the language is defined. This isn't an implementation bug.

@peteArnt
Copy link
Author

Brad,
Thanks for your timely response; however I am still confused about this.
This seems inconsistent. In one case I am assigning into a declared struct (x); in the other I am assigning to a declared scalar (s). Used in the context of v, ok := m[n] where v is either x.S or s, it looks like you're telling me I can only use a scalar variable for v? If this is truly how it is defined, obviously I'll abide by that. I just do not see why there's a distinction.
After looking at the language spec for a while, it still wasn't obvious to me that it was defined like this.

If there is a better forum to discuss this topic, I'd be glad to migrate. Let me know.
Thanks,
-Pete

@zigo101
Copy link

zigo101 commented Nov 10, 2016

@peteArnt
this problem is not related to map. It is a famous known design behaviour/issue/flaw (or whatever).
But now the implementation is really consistent with Go spec: the right of := must be pure identifiers, can't be object fields, array/slice/map elements or pointer dereferences.

https://golang.org/ref/spec#Short_variable_declarations

#6842

@bradfitz
Copy link
Contributor

If there is a better forum to discuss this topic, I'd be glad to migrate. Let me know.

See https://golang.org/wiki/Questions

@golang golang locked and limited conversation to collaborators Nov 11, 2017
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

4 participants