Promotion
Promotion is the rule that lets you use an embedded type's fields and methods as if they were declared on the outer type. s.Lock() works when s embeds sync.Mutex, because Lock is promoted.
Detailed Explanation
Embedding puts a type in a struct without a field name. Promotion is what that unnamed field does at every selector. The compiler looks for Lock on Server, then on each embedded type, then on their embedded types. The first unique match wins. You still have a real field — s.Mutex — and promotion is sugar over s.Mutex.Lock().
Fields promote the same way. type Point struct{ X, Y int }; type Pixel struct{ Point; Color int } lets you write p.X. p.Point.X is the same cell. JSON and other encodings see X as a field of Pixel unless you hide Point with a tag on the embedded field.
Depth and ambiguity. If A embeds B and C, and both B and C have Name, then a.Name does not compile. If only B has Name, a.Name is B.Name even if C embeds something else that also has Name at a greater depth — shallower wins. An explicit field or method on the outer type always wins over any promoted name.
Method sets include promoted methods. A *Pixel has Pixel's methods plus the methods of Point and *Point that promote. That is why embedding bytes.Buffer makes Write appear on your type and why your type suddenly implements io.Writer. Promotion is the mechanism; interface satisfaction is the consequence.
Receivers still follow pointer rules. If the inner method has a pointer receiver, the outer value must be addressable (or already a pointer) for the promoted call to compile. A Pixel value in a map cannot call a promoted pointer-receiver method without copying out.
What it costs. Promotion is compile-time selector rewriting. It is free at runtime. The cost is a method set you did not write down in one place. When a promoted method's behaviour is wrong for the outer type, define an outer method of the same name and implement the forwarding (or do not embed).
Code Examples
package main
import "fmt"
type Point struct{ X, Y int }
func (p Point) String() string {
return fmt.Sprintf("(%d,%d)", p.X, p.Y)
}
type Pixel struct {
Point
Color int
}
func main() {
p := Pixel{Point: Point{1, 2}, Color: 9}
fmt.Println(p.X, p.String()) // 1 (1,2)
fmt.Println(p.Point.X) // same X
}type A struct{ Name string }
type B struct{ Name string }
type Both struct {
A
B
}
func ambiguous(b Both) {
// fmt.Println(b.Name) // compile error
fmt.Println(b.A.Name, b.B.Name)
}func (p Pixel) String() string {
// hides Point.String
return fmt.Sprintf("%s#%d", p.Point.String(), p.Color)
}Common Gotchas
Ambiguous promoted names do not compile
qualify with the inner type, or add an outer field/method that hides them.
Promoted methods satisfy interfaces you may not want
embedding a rich type is an API decision, not only a storage decision.
`encoding/json` inlines an exported anonymous struct
`Pixel`'s JSON object contains `X` and `Y` at the top level, not a nested `Point`. Use a named field if you want a nested object.
`s.Mutex` still exists
promotion does not remove the inner field. Use it when you need the inner type.