Go is an object-oriented language, but it deliberately leaves out one of OOP's most recognizable features: inheritance. Go has no base classes, no extends keyword and no type hierarchy. It gives you composition instead, plus a mechanism called embedding that makes composition feel almost as natural as inheritance.

What is embedding

Embedding is a way to include one type inside another by writing the type name without a field name. When you embed a type, all of its exported fields and methods become directly accessible on the containing struct as if they were defined there:

Go
type Animal struct {    Name string}func (a Animal) Speak() string {    return a.Name + " makes a sound"}type Dog struct {    Animal    Breed string}

Animal is embedded inside Dog. There is no field name, only the type name. Go automatically creates a field named Animal behind the scenes, but the visible effect is that Dog now has access to Name and Speak directly:

Go
dog := Dog{    Animal: Animal{Name: "Rex"},    Breed:  "Labrador",}fmt.Println(dog.Name)    // Rex — promoted from Animalfmt.Println(dog.Speak()) // Rex makes a sound — promoted from Animalfmt.Println(dog.Breed)   // Labrador — defined on Dog directly

This is called promotion. The fields and methods of the embedded type are lifted to the outer struct's surface. You can still access them explicitly through the embedded type's name if needed: dog.Animal.Name.

Shadowing promoted fields and methods

Promotion has an exception. If the outer struct defines a field or method with the same name as one in the embedded type, the outer definition takes precedence and the promoted one is shadowed:

Go
type Dog struct {    Animal    Name string // shadows Animal.Name}dog := Dog{    Animal: Animal{Name: "Rex"},    Name:   "Buddy",}fmt.Println(dog.Name)        // Buddy — Dog.Name shadows Animal.Namefmt.Println(dog.Animal.Name) // Rex — accessed explicitly

The same applies to methods. If Dog defines its own Speak, it shadows the promoted one:

Go
func (d Dog) Speak() string {    return d.Name + " barks"}// Dog's own Speakfmt.Println(dog.Speak())        // Buddy barks// Animal's Speak, accessed explicitlyfmt.Println(dog.Animal.Speak()) // Rex makes a sound

You use shadowing on purpose when the outer type needs to override a promoted method. The promoted version is still reachable if you name the embedded type explicitly.

Embedding is not inheritance

Embedding looks like inheritance, but the two types remain entirely separate. A Dog value is not an Animal, and Go will not let you use one where the other is expected:

Go
func describe(a Animal) {    fmt.Println("Animal:", a.Name)}dog := Dog{Animal: Animal{Name: "Rex"}, Breed: "Labrador"}describe(dog)        // compile error: cannot use Dog as Animaldescribe(dog.Animal) // works — you are passing the Animal explicitly

That is the main difference from classical inheritance. In a language with inheritance, a Dog is an Animal and can be passed wherever an Animal is expected. In Go, a Dog has an Animal, and you cannot substitute one for the other.

This can feel limiting at first, but Go made the trade-off on purpose. Type relationships stay explicit, and the fragile base-class problem doesn't come up, because two types that share behavior through embedding are still two independent types.

Satisfying interfaces through embedding

Embedding is most useful for satisfying interfaces. When you embed a type, its promoted methods become part of the outer struct's method set, so the outer type automatically satisfies any interface that the embedded type satisfies:

Go
type Speaker interface {    Speak() string}

Animal satisfies Speaker because it has a Speak method. And because Dog embeds Animal, Speak is promoted into Dog's method set, so Dog satisfies Speaker too, without any extra code:

Go
var s Speakers = Animal{Name: "Cat"}fmt.Println(s.Speak()) // Cat makes a sounds = Dog{Animal: Animal{Name: "Rex"}, Breed: "Labrador"}fmt.Println(s.Speak()) // Rex makes a sound — promoted from Animal

If Dog defines its own Speak, that overrides the promoted one. Dog still satisfies Speaker, now through its own method rather than the promoted one:

Go
func (d Dog) Speak() string {    return d.Name + " barks"}s = Dog{Animal: Animal{Name: "Rex"}, Breed: "Labrador"}fmt.Println(s.Speak()) // Rex barks — Dog's own Speak

You compose a struct from small, self-contained types, and the interfaces each embedded type satisfies carry over to the containing struct. A type that embeds a logger, a cache and a metrics reporter satisfies any interface one of those components satisfies, and you don't write a single delegation method by hand.

Embedding vs named fields

You do not have to embed a type to compose structs. A named field is always an option:

Go
type Dog struct {    animal Animal // named field, not embedded    Breed  string}

With a named field, nothing is promoted. To access Name, you must write dog.animal.Name. To call Speak, you must write dog.animal.Speak(). The struct does not automatically satisfy Speaker.

The choice comes down to intent. Use a named field when you want to hold a value as an internal implementation detail, without exposing its fields or methods at the outer level. Use embedding when you want the inner type's behavior to become part of the outer type's surface, either for developer convenience or to participate in interfaces without writing delegation methods by hand.

Embedding is how Go reuses behavior across types, the job inheritance does in other languages. You compose types, promote what you need, and the type system keeps the line between what a type is and what it has.