Introduction
Go has no classes and no inheritance. Instead, it favors composition, and embedding is the language feature that makes composition convenient. When you embed a struct or interface inside another struct or interface, the outer type gains access to the embedded type's fields and methods as if they were its own, a mechanism called method and field promotion. Embedding is not inheritance: there is no shared base class, no dynamic dispatch through a parent pointer, and the outer type is a distinct type from the embedded one.
Cricket analogy: There's no 'parent club' a franchise inherits from in the IPL auction system, but a franchise can compose its squad by embedding specialists from different backgrounds, gaining their skills directly onto the roster without a shared base class.
Syntax
type Animal struct {
Name string
}
func (a Animal) Describe() string {
return a.Name + " is an animal"
}
type Dog struct {
Animal // embedded struct, no field name
Breed string
}
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type ReadWriter interface {
Reader // embedded interface
Writer // embedded interface
}Explanation
When Animal is embedded in Dog without a field name, Dog automatically gets a promoted Name field and a promoted Describe() method, so d.Name and d.Describe() both work on a Dog value even though they are actually defined on Animal. If Dog defines its own Describe() method, that method takes precedence over the promoted one, similar to overriding. Interface embedding works differently but achieves a similar goal: ReadWriter combines the method sets of Reader and Writer, so any type satisfying ReadWriter must implement both Read and Write. Embedding is purely compositional; the outer struct literally contains an instance of the inner type as an unnamed field.
Cricket analogy: When a franchise embeds an unnamed 'Overseas Player' template into a squad slot, the squad automatically gets that player's promoted skills, like d.Name and d.Describe() on a Dog; but if the squad defines its own specialist role that overrides the template's description, its own version takes precedence, similar to overriding.
Example
package main
import "fmt"
type Animal struct {
Name string
}
func (a Animal) Describe() string {
return a.Name + " makes a sound"
}
type Dog struct {
Animal
Breed string
}
func main() {
d := Dog{Animal: Animal{Name: "Rex"}, Breed: "Labrador"}
fmt.Println(d.Name) // promoted field
fmt.Println(d.Describe()) // promoted method
fmt.Println(d.Breed)
}Output
Rex
Rex makes a sound
LabradorKey Takeaways
- Embedding promotes fields and methods from the inner type to the outer type.
- Struct embedding is composition, not inheritance; there is no shared base type.
- Interface embedding combines multiple interfaces' method sets into one.
- An outer type can override a promoted method by defining its own with the same name.
Practice what you learned
1. What happens when you embed a struct Animal inside a struct Dog?
2. Which best describes the relationship created by struct embedding in Go?
3. What does interface embedding, as in 'type ReadWriter interface { Reader; Writer }', accomplish?
4. If Dog defines its own Describe() method while also embedding Animal (which has Describe()), which method is called on d.Describe()?
Was this page helpful?
You May Also Like
Interfaces in Go
Learn how Go interfaces define behavior through implicit, structural typing without classes or inheritance.
Structs in Go
A composite type that groups related fields together, forming the basis for custom data modeling in Go.
Methods in Go
Learn how to attach methods to types in Go using value and pointer receivers.
Struct Tags in Go
Learn how backtick-delimited struct tags attach metadata to fields for use by reflection-based libraries.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics