Struct method promotion

March 15, 2023

Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted.

Struct types

Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows:

  • If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.
  • If S contains an embedded field *T, the method sets of S and *S both include promoted methods with receiver T or *T.

Let’s illustrate with an example.

type Person struct {
	Name string
	Age  int
}

func (p Person) Greeting() {
	fmt.Printf("Hello, %s", p.Name)
}

type Employee struct {
	Person
	StartDate time.Time
}

func main() {
	var e Employee
	e.Name = "Bob"
	e.Greeting() // Prints: Hello, Bob.
}

See it in the playground.

Oh! That’s just like inheritance! Right?

No! It’s not! This is composition. Don’t be confused.

There’s a subtle, but very important distinction… The method sets of the “parent” struct (Employee in our case, or S in the spec), include the promoted methods, but with receiver T or *T. That is, the promoted methods receive the “child” struct value, not the parent.

In concrete terms, this means that the Greeting() method has no visibility to the StartDate value, or any other values that are not part of the Person type. This is perhaps made more obvious with a more explicit example:

type Person struct {
	Position string
	Name     string
}

func (p Person) Greeting() {
	fmt.Printf("Hello, %s %s", p.Position, p.Name)
}

type Employee struct {
	Person
	Position string
}

func main() {
	p := Person{
		Name:     "Bob",
		Position: "Homeowner",
	}
	e := Employee{
		Person:   p,
		Position: "Go Developer",
	}
	e.Greeting() // Prints: Hello, Homeowner Bob
}

See it in the playground.

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

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

Unsure? Browse the archive .