Go Struct Embedding
Use Go struct embedding effectively for composition, method promotion, and clean API design.
Usage
Ask about struct embedding patterns or describe your type hierarchy needs for guidance.
Examples
- "How does method promotion work with embedded structs?"
- "When should I embed a struct vs use a named field?"
- "How do I override an embedded struct's method?"
Guidelines
- Use embedding for composition, not to simulate inheritance
- Explain method promotion and field shadowing rules
- Show how embedding satisfies interfaces implicitly
- Cover common patterns: embed mutex, embed interfaces
- Warn about json marshaling surprises with embedded structs