🧬 Go – Inheritance via Composition – Struct Embedding Explained (2025 Guide)
🧲 Introduction – Does Go Support Inheritance?
Go does not support classical inheritance like object-oriented languages such as Java or C++. Instead, Go uses composition via struct embedding to achieve code reuse and polymorphic behavior. This allows one struct to include another and promote its fields and methods, forming a “has-a” relationship rather than an “is-a” hierarchy.
🎯 In this section, you’ll learn:
- How composition replaces inheritance in Go
- How struct embedding works with fields and methods
- How method promotion enables polymorphism
- Best practices and real-world use cases
✅ Basic Struct Composition (Embedding)
type Animal struct {
Name string
}
type Dog struct {
Animal // embedded struct
Breed string
}
✅ The Dog
struct embeds Animal
, promoting its fields and methods.
🧪 Access Embedded Fields Directly
func main() {
d := Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
}
fmt.Println(d.Name) // Output: Buddy
fmt.Println(d.Breed) // Output: Golden Retriever
}
✅ d.Name
is promoted from Animal
to Dog
—no need for d.Animal.Name
.
🔁 Method Inheritance via Composition
type Animal struct {
Name string
}
func (a Animal) Speak() string {
return a.Name + " makes a sound."
}
type Cat struct {
Animal
}
func main() {
c := Cat{Animal: Animal{Name: "Whiskers"}}
fmt.Println(c.Speak()) // Output: Whiskers makes a sound.
}
✅ Cat
automatically inherits the Speak()
method from Animal
.
🧱 Override Embedded Method
type Dog struct {
Animal
}
func (d Dog) Speak() string {
return d.Name + " barks."
}
func main() {
d := Dog{Animal: Animal{Name: "Rex"}}
fmt.Println(d.Speak()) // Output: Rex barks.
}
✅ Dog.Speak()
overrides Animal.Speak()
even though Animal
is embedded.
🧠 Composition vs Inheritance
Feature | Inheritance (OOP) | Composition in Go |
---|---|---|
Relationship | “is-a” | “has-a” |
Method Sharing | Explicit via inheritance | Implicit via embedding |
Field Access | Inherited from parent | Promoted from embedded |
Overriding | Supported | Supported via shadowing |
Multiple Parents | Not allowed | Multiple embedding allowed |
📚 Real-World Use Case – Reusable Logger
type Logger struct{}
func (l Logger) Log(msg string) {
fmt.Println("[LOG]:", msg)
}
type Service struct {
Logger
Name string
}
func main() {
s := Service{Name: "AuthService"}
s.Log("Started") // Output: [LOG]: Started
}
✅ Service
now has logging functionality without explicitly redefining it.
📦 Embedding Multiple Structs
type A struct {
ValA string
}
type B struct {
ValB string
}
type Combo struct {
A
B
}
func main() {
c := Combo{}
c.ValA = "Hello"
c.ValB = "World"
fmt.Println(c.ValA, c.ValB)
}
✅ Structs can embed multiple structs—a form of multiple inheritance.
🧠 Best Practices
Best Practice | Why It Helps |
---|---|
✅ Use embedding for code reuse | Clean, idiomatic way to share functionality |
✅ Avoid deep embedding chains | Keeps code readable and manageable |
❌ Don’t simulate full inheritance | Go prefers simplicity over inheritance trees |
✅ Combine with interfaces | Boosts flexibility and testability |
📌 Summary – Recap & Next Steps
While Go lacks classical inheritance, struct embedding provides composition, enabling reusable, testable, and clean code. It supports method promotion, field access, and selective overriding, allowing types to share behavior without rigid hierarchies.
🔍 Key Takeaways:
- Use
struct embedding
to promote fields and methods - Embedded methods can be overridden
- Embedding supports code reuse and “has-a” relationships
- Combine with interfaces for powerful abstractions
⚙️ Next: Learn how interfaces + composition empower Go’s dependency injection and mocking capabilities.
❓ FAQs – Go Inheritance via Composition
❓ Does Go support class-based inheritance?
❌ No. Go uses composition via struct embedding, not inheritance.
❓ How do I reuse fields and methods in Go structs?
✅ Use embedding (type B struct { A }
) to promote A
‘s fields/methods into B
.
❓ Can I override methods from embedded structs?
✅ Yes. Simply define the method in the embedding struct with the same name.
❓ Is embedding the same as inheritance?
❌ Not exactly. Embedding is composition, which Go uses to promote flexibility over hierarchy.
❓ Can I embed multiple structs in one?
✅ Yes. Go supports multiple struct embedding.
Share Now :