Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
An embedded field (often called an anonymous field) is a struct field declared solely by its type, without an explicit identifier. The Go compiler implicitly assigns the unqualified type name as the field’s identifier and promotes the embedded type’s fields and methods to the enclosing struct’s scope.
Syntax and Implicit Naming
When embedding a type, the base name of the type becomes the implicit field name.
type Base struct {
ID int
}
type Container struct {
Base // Embedded field. Implicitly named 'Base'
Name string
}
In memory, Container is a standard struct with two fields: Base (of type Base) and Name (of type string).
Field and Method Promotion
The primary mechanic of an embedded field is promotion. Fields and methods belonging to the embedded type can be accessed directly on the embedding struct, bypassing the implicit field name.
c := Container{}
c.ID = 42 // Promoted access
c.Base.ID = 42 // Explicit access (equivalent)
Shadowing and Name Collisions
If the embedding struct declares a field or method with the same name as a promoted field or method, the outer declaration shadows the embedded one. The shadowed embedded member remains accessible via explicit traversal.
type Base struct {
Value string
}
type Container struct {
Base
Value string // Shadows Base.Value
}
c := Container{
Value: "Outer",
Base: Base{Value: "Inner"},
}
// c.Value evaluates to "Outer"
// c.Base.Value evaluates to "Inner"
If a struct embeds multiple types that contain identically named fields at the same depth, accessing that field directly on the outer struct results in a compile-time ambiguous selector error.
Pointer Embedding
Types can be embedded as pointers. This affects struct initialization and method sets.
type Container struct {
*Base // Implicitly named 'Base', type is '*Base'
}
When embedding a pointer, the embedding struct must initialize the pointer to avoid a nil pointer dereference during promoted field access. Methods with pointer receivers on the embedded type are promoted to the method set of both the embedding struct value and its pointer.
Export Rules
The visibility of promoted fields depends on the export status of the fields themselves, not the embedded type. If an unexported type is embedded, its exported fields and methods are still promoted and accessible outside the package.
type internalStruct struct {
ExportedField int
}
type PublicStruct struct {
internalStruct // Unexported embedded field
}
In this scenario, PublicStruct.ExportedField is accessible from other packages, but PublicStruct.internalStruct is not.
Interface Embedding
Embedding is not restricted to concrete structs. Interfaces can embed other interfaces to construct composite method sets, and structs can embed interfaces to satisfy interface requirements dynamically at runtime.
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
// Interface embedding interfaces
type ReadWriter interface {
Reader
Writer
}
// Struct embedding an interface
type MockStream struct {
Reader // Must be initialized with a concrete implementation at runtime
}
Master Go with Deep Grasping Methodology!Learn More