The first rule of Selectors

October 23, 2023

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!](

We're going to talk about Selectors.

The spec lays out 6 rules. We’ll start at the beginning, and discuss the first one.


The following rules apply to selectors:

  1. For a value x of type T or *T where T is not a pointer or interface type, x.f denotes the field or method at the shallowest depth in T where there is such an f. If there is not exactly one f 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 {

type Employee struct {
	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

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .