Struct embedding
Struct embedding is a Go language feature that includes an anonymous field of one type in a struct and promotes the embedded type’s fields and methods.
100 Go Mistakes and How to Avoid Them
Type embedding is a Go language feature that promotes the fields and methods of an embedded type into the embedding type's method set and selector space.
- How it is used
- Uses embedding to reuse fields and methods while diagnosing unintended API visibility in Go structs.
- How deep it goes
- Explains selector promotion, receiver behavior, interface composition, and a mutex visibility failure, then stops short of broader embedding rules.
- Examples
- Uses Foo and Bar promotion, io.ReadWriter composition, an InMem mutex, and Logger forwarding or embedding.
- What you can do after
- Diagnose accidental method exposure and choose between embedded fields, named fields, and explicit forwarding methods.
Read how 100 Go Mistakes and How to Avoid Them explains struct embedding →
Go by Example
Field embedding is the inclusion of an anonymous field in a Go struct so the embedded field and its members can be promoted.
- How it is used
- Uses embedding to compose reusable HTTP middleware, satisfy interfaces, intercept methods, and extend behavior without modifying handlers.
- How deep it goes
- Explains promotion, forwarding, interface satisfaction, method hiding, and serialization hazards through an HTTP interceptor, while deferring broader pitfalls to type assertions.
- Examples
- Demonstrates an HTTP ResponseWriter interceptor, status-code logging middleware, curl requests, and resulting server logs.
- What you can do after
- Build composable Go types that promote methods, intercept selected behavior, satisfy interfaces, and preserve access to underlying capabilities.
Learn Go with Pocket-Sized Projects
Embedding is a Go struct feature that incorporates an unnamed field's type and promotes its methods and fields to the containing type.
- How it is used
- Uses embedding to compose Go types and satisfy generated interfaces, especially when implementing a gRPC server.
- How deep it goes
- Defines embedding through a server example and explains its required generated method, but does not explore method promotion mechanics in depth.
- Examples
- Uses Server, UnimplementedHabitsServer, Logger, and CreateHabit to demonstrate embedded and named fields together.
- What you can do after
- Build it from scratch by embedding a generated implementation and registering the containing server against its interface.
Read how Learn Go with Pocket-Sized Projects explains struct embedding →
Go in Action, Second Edition
Struct embedding is a Go language feature that embeds one struct type in another and promotes the embedded type's fields and methods.
- How it is used
- Uses embedding to build complex Go domain types through composition instead of class-based inheritance.
- How deep it goes
- Explains promotion and composition, then applies them to measurements and recipes without detailing compiler resolution or embedding rules.
- Examples
- Demonstrates embedded metric and imperial measurements inside a recipe application with conversion, scaling, and grocery-list generation.
- What you can do after
- Build domain models that reuse fields and methods through composition and extend behavior with wrappers and interfaces.
Read how Go in Action, Second Edition explains struct embedding →
Related concepts
- inheritancecontrasts with · Inheritance is a mechanism by which a child type or element receives properties, values, or behavior from a parent, subject to system rules and possible overrides.
- structpart of · A struct is a composite data type or structure that groups related fields or values into a single unit.
- compositionequivalent to · Composition is a technique for combining simpler functions, components, or operations into a larger structure or behavior.
- aspectused by · An aspect is a modular unit of cross-cutting behavior applied around selected program operations or method invocations.
- http.ResponseWriteruses · http.ResponseWriter is the Go interface through which an HTTP handler constructs a response by writing its headers, status, and body.
- Marshaler interfaceimplements · The Marshaler interface is a Go interface requiring a MarshalJSON method that supplies a type’s custom JSON encoding.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



