🏗️

Go Struct Embedding

Verified

by Community

Explains Go struct embedding for composition over inheritance, including method promotion, field shadowing, interface satisfaction through embedding, and when to use embedding vs explicit fields.

gostructsembeddingcompositiontypes

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