0PricingLogin
Go Academy · Lesson

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

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