Method Promotion
Inherit methods via embedding.
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()
}All lessons in this course
- Struct Embedding
- Interface Embedding
- Method Promotion
- Composition over Inheritance