Struct embedding is a specific Go feature: you list a type inside a struct without a field name, and its fields and methods are promoted to the outer type. Struct composition is the general idea of building a type from other types, which in Go you can do either by embedding or by giving the inner value a named field. Embedding is therefore one form of composition, and the difference is whether you get promotion or must write outer.field.Member explicitly.
Go has no class inheritance, and embedding is not inheritance. It is automatic delegation, and choosing between it and a named field affects your public API.
An embedded field is declared with a type only. Its exported and unexported fields and methods become accessible directly on the outer struct.
The embedded field is still a real field named after its type (p.Address), so you can reach it explicitly and set it in a composite literal.
When the inner struct is a normal named field, nothing is promoted. Callers must go through the field name.
This keeps the inner type an implementation detail and avoids leaking its methods into the outer type's API.
Promoted methods count towards interface satisfaction, which makes embedding useful for reuse and decoration.
You can also embed an interface in a struct, a common pattern for wrapping or partially overriding a dependency, for example in tests.
An outer type can define a field or method with the same name as a promoted one. The shallower name wins, and the embedded one stays reachable through its type name.
If two embedded types at the same depth provide the same name, using the ambiguous selector is a compile error until you qualify it. There is also no virtual dispatch: a promoted method runs with the embedded value as its receiver, not the outer type, so it cannot call overridden methods of the outer struct.
| Aspect | Embedding | Named field |
|---|---|---|
| Access | outer.Member and outer.Inner.Member | outer.field.Member only |
| Method promotion | Yes | No |
| Satisfies inner's interfaces | Yes | No |
| Public API exposure | Inner API leaks into outer | Hidden behind the field |
| Name conflicts | Possible, resolved by depth | None |
| Best for | Reuse with an "is a kind of" or decorator relationship | A "has a" relationship |
Prefer a named field by default; it is explicit and keeps your API small. Embed when you deliberately want to expose the inner type's behaviour, such as embedding sync.Mutex in a private struct or embedding a base type to satisfy an interface. Be careful embedding in exported structs: every exported method of the embedded type becomes part of your API, and removing it later is a breaking change. Embedding a pointer (*Logger) is allowed but the pointer may be nil, so initialise it.
No. It is delegation with syntactic sugar. There is no subtype relationship, so you cannot pass a Person where an Address is expected.
Yes, but watch for ambiguous selectors if two embedded types share a field or method name at the same depth.
Yes. encoding/json flattens exported fields of an embedded struct into the outer object unless the embedded field has a JSON tag.
sync.Mutex?It works, but it exposes Lock and Unlock on the outer type. A named, unexported mu sync.Mutex field is safer for exported types.
Embedding promotes the inner type's fields and methods to the outer struct, while a named field keeps them behind an explicit path. Both are composition, and neither is inheritance. Default to named fields and embed when you intentionally want to expose or satisfy the inner type's behaviour.