Today I’m live streaming again at the regular time, and I’ll be reviewing your Go code!. I have already received a few submissions, but there’s still time to send yours! And, of course, [join me for the live stream!](https://www.youtube.com/watch?v=pgfyrmWiUCk
We're going to talk about Selectors.
The spec lays out 6 rules. We’ll start at the beginning, and discuss the first one.
Selectors
…
The following rules apply to selectors:
- For a value x of type
T
or*T
whereT
is not a pointer or interface type,x.f
denotes the field or method at the shallowest depth inT
where there is such anf
. If there is not exactly onef
with shallowest depth, the selector expression is illegal.
I promised we’d talk about depth soon. And now here we are. So what does this rule mean in practice? Let’s look at an example. (Forgive the horrible data modeling.)
type Person struct {
Name string
Age int
}
type Department struct {
Name string
}
type Manager struct {
Department
Person
}
type Employee struct {
Person
Manager
JobTitle string
}
var e Employee
var m Manager
e.Name // References Employee.Person.Name, with depth 1
m.Name // Illegal: Both m.Department.Name and m.Person.Name have equal depth of 1
Quotes from The Go Programming Language Specification Version of August 2, 2023