Go has no class inheritance. Instead it gives you struct embedding, which promotes the fields and methods of an embedded type to the outer type, and interfaces, which describe behavior that any type can satisfy implicitly. Together they cover what inheritance does in Java or C++ (code reuse and polymorphism) through composition, with no parent-child type hierarchy.
The phrase "type-based inheritance" is a common way to describe embedding, but it is not true inheritance: an outer type is never a subtype of the embedded type. This article shows how embedding, method promotion, overriding and interfaces work together, and where the model differs from classical object-oriented languages.
In a classical hierarchy, a Dog class extends Animal and becomes substitutable for it. Deep hierarchies tend to be fragile: a change in the base class ripples into every subclass. Go removes the hierarchy and keeps two separate ideas:
Because these are independent, you can reuse code without committing to a type relationship, and you can satisfy an interface without sharing any implementation.
When you declare a field with only a type name and no field name, the type is embedded. Its exported and unexported fields and methods are promoted to the outer struct, so you can call them directly.
d.Speak() is shorthand for d.Animal.Speak(). The receiver inside Speak is the embedded Animal, not the Dog. That is the key difference from inheritance: the embedded type knows nothing about the outer type that contains it.
If the outer type declares a method with the same name, it shadows the promoted one. The embedded method is still available through the explicit field name.
There is no virtual dispatch. If Animal had another method that called a.Speak(), it would call Animal.Speak, not the Dog version. Do not expect Java-style overriding.
You can embed *Animal instead of Animal. The outer value then holds a pointer, the zero value has a nil embedded pointer, and promoted methods with pointer receivers become callable. Use this when the embedded type must be shared or is expensive to copy, and remember to initialise it before use.
Interfaces are how Go achieves substitutability. A type satisfies an interface when its method set includes every method of the interface. Promoted methods count, so embedding can satisfy an interface for you.
Cat satisfies Speaker through the promoted Speak method, and Robot satisfies it independently. Neither declares that it implements the interface. Keep interfaces small and define them where they are consumed, not where the types live.
An interface can be embedded in a struct or in another interface. Embedding an interface in a struct is useful for decorators and test doubles: you override only the methods you care about and delegate the rest.
If the embedded interface value is nil, calling a delegated method panics, so always set it.
Embedding is a convenience, not a requirement. When you do not want the inner API to leak into the outer type, use a regular named field and forward only what you need.
A good rule: embed when the outer type genuinely "has the behavior of" the inner one and you want it in its public API; use a named field when the inner type is an implementation detail.
| Aspect | Java or C++ inheritance | Go embedding and interfaces |
|---|---|---|
| Type relationship | Subclass is a subtype of the base class | Outer type is unrelated to the embedded type |
| Method overriding | Virtual dispatch | Shadowing only, no virtual dispatch |
| Polymorphism | Through the class hierarchy | Through interfaces, satisfied implicitly |
| Multiple reuse | Limited or via multiple inheritance | Embed several types at once |
| Coupling | Tight, base class is part of the contract | Loose, embedded type is unaware of the outer type |
If two embedded types promote the same name at the same depth, the selector is ambiguous and the compiler reports an error only when you use it. Resolve it by qualifying the call, for example x.A.Do().
No. Go has no class inheritance and no subtyping between structs. Struct embedding provides code reuse, and interfaces provide polymorphism.
Not directly, because Dog is not an Animal. Define an interface such as Speaker, or pass d.Animal explicitly when you need the embedded value.
Embed when you want the inner type's methods and fields in the outer type's public API. Use a named field when the inner type is an internal detail.
Yes. The shallowest declaration wins, and same-depth duplicates are ambiguous. Qualify the selector with the embedded type name to resolve it.
Go replaces inheritance with composition. Embedding promotes fields and methods for reuse, shadowing lets the outer type specialise behavior without virtual dispatch, and interfaces give you implicit, loosely coupled polymorphism. Keep interfaces small, prefer named fields for internal details, and embed only when promoting the inner API is what you want.