0Pricing
Go Academy · Lesson

Method Promotion

Inherit methods via embedding.

Method Promotion is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Inherit Methods via Embedding

When you embed a type, its methods are promoted too. The outer type can call them as if it defined them — Go's flavor of inheritance.

A Promoted Method

Car embeds Engine and gains its Start method for free.

package main

import "fmt"

type Engine struct{}

func (Engine) Start() { fmt.Println("engine started") }

type Car struct{ Engine }

func main() {
	c := Car{}
	c.Start()
}

Methods Operate on the Embedded Value

A promoted method runs against the embedded field, not the outer struct. It cannot see the outer struct's other fields.

package main

import "fmt"

type Counter struct{ n int }

func (c *Counter) Inc() { c.n++ }
func (c Counter) Get() int { return c.n }

type Widget struct{ Counter }

func main() {
	w := Widget{}
	w.Inc()
	w.Inc()
	fmt.Println(w.Get())
}

Overriding a Method

Define a method with the same name on the outer struct to override the promoted one. The outer method takes precedence.

package main

import "fmt"

type Animal struct{}

func (Animal) Speak() string { return "..." }

type Dog struct{ Animal }

func (Dog) Speak() string { return "Woof" }

func main() {
	fmt.Println(Dog{}.Speak())
}

Calling the Embedded Version

Even after overriding, you can still reach the original through the embedded type name.

package main

import "fmt"

type Animal struct{}

func (Animal) Speak() string { return "generic" }

type Dog struct{ Animal }

func (d Dog) Speak() string { return "Woof + " + d.Animal.Speak() }

func main() {
	fmt.Println(Dog{}.Speak())
}

Promotion and Interfaces

Because promoted methods are part of the outer type's method set, the outer type can satisfy interfaces using the embedded type's methods.

package main

import "fmt"

type Stringer interface{ String() string }
type Base struct{ name string }

func (b Base) String() string { return b.name }

type Wrapper struct{ Base }

func main() {
	var s Stringer = Wrapper{Base{"hi"}}
	fmt.Println(s.String())
}

Pointer vs Value Method Sets

If the embedded type has pointer-receiver methods, only a pointer to the outer struct (or an addressable value) gets those methods promoted into its method set.

Embedding for Default Behavior

Embed a type that provides sensible default methods, then override only the few you need to customize — a powerful reuse pattern.

package main

import "fmt"

type DefaultHandler struct{}

func (DefaultHandler) Handle() string { return "default" }

type CustomHandler struct{ DefaultHandler }

func (CustomHandler) Handle() string { return "custom" }

func main() {
	fmt.Println(CustomHandler{}.Handle())
}

No True Polymorphism

Unlike classical OOP, the embedded method does not dispatch back to the outer override. Go has no virtual method dispatch through embedding.

Promotion Through Multiple Levels

Methods promote transitively: if A embeds B and B embeds C, A gains C's methods too, as long as there is no ambiguity.

When Promotion Helps

  • Reuse behavior without rewriting
  • Provide defaults you can override
  • Satisfy interfaces via embedded methods

Quick Check

Test your method promotion knowledge.

Recap

You learned method promotion.

  • Embedded methods are promoted to the outer type
  • Outer methods override promoted ones
  • Reach the original via the embedded type name
  • Promotion helps satisfy interfaces, but there is no virtual dispatch

Frequently asked questions

Is the “Method Promotion” lesson free?

Yes — the full text of “Method Promotion” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Method Promotion”?

Inherit methods via embedding. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Method Promotion” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Struct Embedding
  2. Interface Embedding
  3. Method Promotion
  4. Composition over Inheritance
← Back to Go Academy