Embedding instead of inheritance

Contents
Embed a struct
Promoted methods
Shadow a method
Compose, do not inherit

Embed a struct

Embedding places one struct inside another without a field name. The outer struct then reads the inner fields as if they were its own, which is the Go replacement for simple reuse through inheritance.

package main import "fmt" type Person struct { Name string } type Employee struct { Person ID int } func main() { e := Employee{Person: Person{Name: "Ann"}, ID: 7} fmt.Println(e.Name, e.ID) }

Employee has no Name field of its own, yet e.Name reaches into the embedded Person. When two embedded structs expose the same field name, spell out the path, as in e.Person.Name.

Promoted methods

Methods of the embedded struct are promoted to the outer struct automatically. Callers use the short path, while the method still runs with the inner value as its receiver.

package main import "fmt" type Person struct { Name string } func (p Person) Greet() string { return "hello, I am " + p.Name } type Employee struct { Person ID int } func main() { e := Employee{Person: Person{Name: "Ann"}, ID: 7} fmt.Println(e.Greet()) }

Greet is defined once on Person, but e.Greet works directly on the employee. Promotion also carries interfaces along: an Employee satisfies every interface its embedded Person satisfies.

Shadow a method

When the outer struct defines a method with the same name, the outer one wins and the inner one is shadowed. The hidden version stays reachable through the explicit embedded field, so you can wrap it instead of losing it.

package main import "fmt" type Person struct { Name string } func (p Person) Greet() string { return "hello, I am " + p.Name } type Employee struct { Person ID int } func (e Employee) Greet() string { return fmt.Sprintf("employee %d says: %s", e.ID, e.Person.Greet()) } func main() { e := Employee{Person: Person{Name: "Ann"}, ID: 7} fmt.Println(e.Greet()) fmt.Println(e.Person.Greet()) }

The outer Greet shadows the promoted one, then reuses it through e.Person.Greet. This pattern mirrors overriding with a call to super, minus the class hierarchy.

Compose, do not inherit

Embedding shines when you assemble behavior from small parts instead of growing a deep tree. Give each struct one job, embed the pieces you need, and let the outer type stay thin.

package main import "fmt" type Logger struct{} func (Logger) Log(msg string) { fmt.Println("log:", msg) } type Store struct { items int } func (s *Store) Add() { s.items++ } type Service struct { Logger Store } func main() { svc := Service{} svc.Log("start") svc.Add() fmt.Println(svc.items) }

Service combines a Logger and a Store with two short lines and no base class. Prefer this flat composition: it keeps each piece testable and every dependency visible.

Next: Generics

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: