Search⌘ K
AI Features

Composition vs Inheritance

Explore how Go replaces traditional inheritance with composition and interfaces, enabling you to build flexible structs and implement interfaces efficiently. This lesson helps you understand struct embedding, method promotion, and how composition can simplify coding in Go.

Composition as an Alternative to Inheritance

Coming from an OOP background a lot of us are used to inheritance, something that isn’t supported by Go. Instead you have to think in terms of composition and interfaces. In the previous lessons, we learnt about structs in Go, and that is what we will be using for composition.

The Go team wrote a short but good segment on this topic.

Composition

Composition (or embedding) is a well understood concept for most OOP programmers and Go ...