Field depth

October 20, 2023

Selectors

A selector f may denote a field or method f of a type T, or it may refer to a field or method f of a nested embedded field of T. The number of embedded fields traversed to reach f is called its depth in T. The depth of a field or method f declared in T is zero. The depth of a field or method f declared in an embedded field A in T is the depth of f in A plus one.

The importance of this concept of depth will become apparent in our next installment, but for now let’s look at some examples, just to make this crystal clear.

type Person struct {
  Name string
}

type Employee struct {
  Person
  JobTitle string
}

var e Employee

e.JobTitle // JobTitle's depth is zero
e.Name     // Name's depth is 1
e.Person   // Person's depth is 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 .