← Fundamentals
Fundamentals

Go struct embedding and promoted methods

Is embedding inheritance?

Embedding provides shorter selectors while keeping an ordinary field and its receiver.

Outcome

Say where a promoted field or method really lives, predict which name wins, say which receiver a promoted method gets, tell an embedded copy from an embedded pointer, and guard an embedded pointer that may be nil.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Embedded field
A struct field declared with only a type; its name is the type’s name.
Promoted field or method
A field or method of an embedded type that can be used directly on the outer struct.
Depth
How many embedded fields a selector passes through; the shallowest name wins.
Ambiguous selector
A name found at the same depth in two embedded fields; the compiler reports it where it is used.

Promotion follows a contained field

A Go struct can embed a type by declaring a field without a separate field name. Eligible fields and methods of that embedded value become reachable through promoted selectors: outer.Name can stand for outer.Inner.Name. The explicit field path remains available.

Promotion follows selector depth and method-set rules. It does not replace the embedded value with the outer one. Before relying on a promoted method, inspect whether the field stores a value, a pointer or an interface, what a copy retains, and whether the collaborator is present.

01Promoted names

The shallowest name wins

A club administration package quotes member equipment reservations, books teams against one shared coach schedule and displays optional sponsorship on team cards. Its embedded values are ordinary collaborators: a board supplies the billed price, a coach pointer identifies the shared schedule, and a sponsor may be absent.

The equipment quote needs membership data alongside a player record. Before changing a price, use this reduced member record to check whether a short selector names the outer metadata or an embedded field. Predict the three values.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Player struct {
	Name  string
	Level int
}

type Member struct {
	Player
	Level string
}

func main() {
	m := Member{Player: Player{Name: "ana", Level: 2}, Level: "gold"}

	fmt.Println(m.Name, m.Level, m.Player.Level)
}
What is printed?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run main.go

Use the Go setup from the first reading. These examples use Go structs, embedded interfaces and promoted selectors; follow each field path before predicting its result.

02Receivers

A promoted method runs on the inner value

Team bookings must reach a schedule shared with the caller. This reduced wins counter isolates the ownership question: Member embeds a Player value, while Shared embeds a *Player. Predict what remains after the two kinds of copy.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Player struct {
	Wins int
}

func (p *Player) AddWin() { p.Wins++ }

type Member struct {
	Player
}

type Shared struct {
	*Player
}

func main() {
	m := Member{}
	m.AddWin()
	c := m
	c.AddWin()

	a := Shared{Player: &Player{}}
	b := a
	b.AddWin()

	fmt.Println(m.Wins, c.Wins, a.Wins)
}
What is printed?
Loading Copying an embedded field copies its value or its pointer…

03Embedded interfaces

An unset embedded interface panics when called

A directory test replaces only part of a display collaborator. This reduced fake uses a Board interface, separate from the priced Board struct in practice, and supplies only Show. Predict what happens when the caller also uses Clear.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Board interface {
	Show() string
	Clear()
}

type TestBoard struct {
	Board
}

func (TestBoard) Show() string { return "test" }

func clearPanics(b Board) (panicked bool) {
	defer func() { panicked = recover() != nil }()
	b.Clear()

	return false
}

func main() {
	var b Board = TestBoard{}

	fmt.Println(b.Show(), clearPanics(b))
}
What is printed?

04Repair the club administration package

Quote equipment, share the coach schedule, then show optional sponsors

The reservation quote uses the board’s daily price for guests and a member price of 80 percent rounded down before multiplying by days. Team bookings must accumulate into the one coach supplied to all teams. The directory card shows a sponsor’s name and amount when present, or no sponsor when the embedded pointer is nil.

The files keep their supplied readers and callers. Repair the rental’s billed price, preserve the shared schedule’s owner, then keep sponsored and unsponsored directory cards distinct.

Repair the club administration package

Quote member rentals, accumulate bookings into the supplied coach, then render every team’s sponsorship status.

  1. easy · F-EMBED-V4-1 Price the rentals
  2. medium · F-EMBED-V4-2 Share the coach
  3. hard · F-EMBED-V4-3 Print the team cards
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.