-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: cannot use struct fields in short variable declaration i.e. f.field, err := fn() #37134
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
Comments
Yeah you can't do this.
The The workaround is writing Closing here (see #377). |
I do not think it is scope is the reason. Probably i did not explain better |
Okay, can you explain better? |
Because what you wrote here:
looks like the example I wrote above. Ignore the scoping stuff, as I said := will ignore the already defined variable, that's the issue here. |
Thank you for the report @saritseal, @ALTree thanks for the response. For clarity and others looking at this issue in the future, here is a full example https://play.golang.org/p/PSUFXGSVTWQ or inlined below package main
type Foo struct {
err error
}
func f1() (string, error) {
return "here", nil
}
func main() {
n1, err := f1()
if err != nil {
panic("Expected an error")
}
_ = n1
f := new(Foo)
n2, f.err := f1()
_ = n2
} which won't compile as per ./prog.go:19:7: non-name f.err on left side of := Yes indeed, this is forbidden by the language's specification under the section "Short variable declarations" https://golang.org/ref/spec#Short_variable_declarations; a struct field being used is not an IDENTIFIER -- which must be composed only of free standing variables from letters and unicode digits. The language's grammar expects only identifiers for these short variable declarations to be redefined using I don't think that we can change this for Go1 at least, but perhaps @griesemer might want to take a look. |
more related: #30318 |
What version of Go are you using (
go version
)?go 1.12.6
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?"darwin"
go env
OutputWhat did you do?
Option1:
if I have two variables one defined and other undefined then the := does not identify the defined variable and results in compile error "non-name fs.err on left side of :=" The line of code is as follows
d, fs.err := fs.client.Collection(fs.CollectionName).Doc(fs.ID).Get(fs.ctx)
In order to make it work d needs to be defined
var d *firestore.DocumentSnapshot
Option 2:
while the following fragment works and go infer the type for d
var err error
d, err := fs.client.Collection(fs.CollectionName).Doc(fs.ID).Get(fs.ctx)
In the line above fs is a struct and has been defined. In this case I am receiving it as an argument
What did you expect to see?
I expect go to infer types for non defined parameters, as it does in the second scenario above
What did you see instead?
Option 1 does not work while Option 2 works
The text was updated successfully, but these errors were encountered: