Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Screenshot
What did you do?
The official golang specification (selectors section) states the following two rules:
....
As an exception, if the type of x is a defined pointer type and (x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (x).f.
In all other cases, x.f is illegal.
....
So, based on that, only the following should work:
package main
import "fmt"
type Human *struct {
name string
}
func main() {
var a Human = &struct{ name string }{"leabit1"}
fmt.Println(a.name)
a.name = "leabit2"
fmt.Println(a.name)
}
What did you see happen?
However, the following two cases also work (even though, based on documentation, they are illegal):
1) Pointer to defined non-pointer type
package main
import "fmt"
type Human struct {
name string
}
func main() {
var a *Human = &Human{"leabit1"}
fmt.Println(a.name)
a.name = "leabit2"
fmt.Println(a.name)
}
Pointer to non-defined (unnamed) type
package main
import "fmt"
func main() {
var a *struct{ name string } = &struct{ name string }{"leabit1"}
fmt.Println(a.name)
a.name = "leabit2"
fmt.Println(a.name)
}
What did you expect to see?
I expect the documentation to include these two cases and not to consider them illegal.
What is the URL of the page with the issue?
https://go.dev/ref/spec#Selectors
What is your user agent?
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Screenshot
What did you do?
The official golang specification (selectors section) states the following two rules:
So, based on that, only the following should work:
What did you see happen?
However, the following two cases also work (even though, based on documentation, they are illegal):
1) Pointer to defined non-pointer type
What did you expect to see?
I expect the documentation to include these two cases and not to consider them illegal.