Embedding
Embedding places a type inside a struct (or interface) without a field name. The outer type gains the inner type's fields and methods by promotion. It is composition, not subclassing.
Detailed Explanation
You want a Server that has a mutex and a logger. You could write mu sync.Mutex and call s.mu.Lock(). You can also write:
type Server struct {
sync.Mutex
log *slog.Logger
}
Then s.Lock() is legal. The mutex is a field whose name is the type name, Mutex. Embedding is that unnamed-field syntax. Promotion is the extra rule that lets you skip the field name at the call site.
There is no subtype relationship. A Server is not a sync.Mutex. You cannot pass Server where sync.Mutex is required. You can pass &s.Mutex if you need the inner type. Interfaces are satisfied by the outer type when the promoted methods cover the interface, which feels like inheritance and is not: there is no super, no virtual dispatch table on the struct, and no "is-a" check.
The outer method wins. If Server defines Lock, that method hides the promoted Mutex.Lock. There is no automatic call-through. If two embedded types promote the same name and the outer type does not define it, using that name is a compile error (ambiguous). Name the fields if both need to stay.
Embedding a pointer. type Server struct { *log.Logger } promotes Logger's methods. A zero Server then has a nil *Logger, and calling s.Info(...) panics. Initialize the pointer in NewServer. Embedding a pointer is useful when the inner value is shared or optional; embedding a value is useful when the inner value belongs to the outer one (sync.Mutex).
Interfaces embed interfaces. interface { io.Reader; io.Writer } is the set of types that have both method sets. That is the usual way to compose interfaces. Embedding a concrete type in an interface is illegal.
What it costs. Promotion keeps call sites short and can accidentally export a wide method set. Embedding os.File on a public struct makes Read, Write, Seek, and a dozen other methods part of your type's method set, which may satisfy interfaces you never meant to satisfy. Embed small types you own, or unexported fields, when the promoted API would leak.
Code Examples
package main
import (
"fmt"
"sync"
)
type Counter struct {
sync.Mutex
n int
}
func (c *Counter) Inc() {
c.Lock()
c.n++
c.Unlock()
}
func main() {
var c Counter
c.Inc()
fmt.Println(c.n)
}type Reader interface {
io.Reader
io.Closer
}type FileView struct {
*os.File // promoted methods; must be non-nil before use
}Common Gotchas
Two embeddings that promote the same name conflict
the compiler rejects `s.Write` if both inners have `Write` and the outer does not.
Embedding is not inheritance
`Server` does not substitute for the embedded type. There is no polymorphic "base class" pointer.
A nil embedded pointer panics on promoted calls
initialize it, or use a named field and nil-check.
Promoted exported methods become part of the outer method set
they count for interface satisfaction even if you never mention them.