100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Struct and Interface Embedding in Go

Understand how Go uses struct and interface embedding to compose behavior instead of using inheritance.

Interfaces & CompositionIntermediate10 min readJul 8, 2026
Analogies

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

go
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

go
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

go
Rex
Rex makes a sound
Labrador

Key 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

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#StructAndInterfaceEmbeddingInGo#Struct#Interface#Embedding#Syntax#StudyNotes#SkillVeris