Struct embedding is a Go language feature: you list a type inside a struct without a field name, and its fields and methods are promoted to the outer type. Composition is the wider design principle of building types out of other types, and it also covers ordinary named fields. Embedding is one way to compose; a named field is another.
The practical difference is access and the method set. With embedding, car.Start() works directly. With a named field, you write car.Engine.Start(), or you write a wrapper method yourself.
Composition means a struct holds other values as fields and delegates work to them. Go has no inheritance, so this is the default way to reuse behaviour.
Car does not have a Start method. Callers must go through c.Engine, which keeps the relationship visible and lets Car decide what to expose.
When a field has a type but no name, it is embedded. The type name becomes the field name, and the embedded type's exported and unexported fields and methods are promoted to the outer struct (within the same package for unexported ones).
The embedded value is still a separate value inside the struct. Embedding copies nothing and creates no subclass relationship: Person is not an Address, and you cannot pass a Person where an Address is required.
You can embed interfaces and pointer types too.
Embedding an interface is a common way to decorate or partially override a type: you implement only the methods you change and inherit the rest by delegation. Embedding *T makes the method sets of both T and *T available, and a nil embedded pointer panics when a promoted method dereferences it.
outer.Embedded.Name.outer.Name is a compile error as soon as you use it. Resolve it by qualifying the selector.| Aspect | Embedding | Named field |
|---|---|---|
| Syntax | Address | Addr Address |
| Access to members | p.City or p.Address.City | p.Addr.City |
| Methods promoted to outer type | Yes | No |
| Satisfies interfaces of inner type | Yes, through promotion | No, unless you write wrappers |
| API surface of outer type | Grows with inner type | Fully controlled |
| JSON encoding | Fields are flattened into the parent object | Nested under the field name |
| Risk | Leaks inner API, name collisions | More boilerplate |
Embed when the outer type really should expose the inner type's behaviour, for example wrapping an interface, adding a mutex to a struct, or building small mixins. Prefer a named field when the inner type is an implementation detail, because embedding an exported type makes all its exported methods part of your public API and any later change to it becomes a breaking change for you.
A common idiom is embedding sync.Mutex, but it exposes Lock and Unlock to callers. Many teams prefer an unexported named field mu sync.Mutex for that reason.
No. Embedding is automatic delegation. There is no subtype relationship and no virtual method dispatch, so a promoted method cannot be overridden from the outside.
Yes. Composition is the principle, and embedding is syntax that makes composition more convenient by promoting fields and methods.
With encoding/json, an embedded struct's exported fields appear as if they belonged to the outer struct, unless you give the embedded field a JSON tag, which makes it a nested object.
Embed a value unless you need sharing, optional presence or an interface-like substitute. An embedded nil pointer panics when you call a promoted method that uses it.
Composition builds types from other types, and embedding is the Go feature that promotes the inner type's fields and methods without writing wrappers. Use named fields to hide implementation details and embedding to deliberately expose or extend behaviour. Remember that there is no inheritance or virtual dispatch, so shadowed methods are not called from promoted ones.