What is a struct in Go and how does struct embedding provide composition?
Understand Go structs and how struct embedding promotes fields and methods to give composition over inheritance, with method promotion examples.
Expected Interview Answer
A struct in Go is a composite type that groups a fixed set of named fields, each with its own type, into a single value used to model a real-world entity. Struct embedding places one struct (or interface) inside another without a field name, so the outer type promotes the inner type's fields and methods as if they were its own — giving composition instead of inheritance.
Go has no classes or inheritance; it favours composition, and embedding is the language-level tool for it. When you embed a type, its exported fields and methods are promoted to the outer struct, callable directly on the outer value, while the embedded value is still reachable by its type name. This lets you reuse behaviour, satisfy interfaces through the embedded type, and override promoted methods simply by defining a method with the same name on the outer struct.
- Groups related data into one cohesive value
- Enables composition-over-inheritance reuse
- Promotes embedded fields and methods to the outer type
- Lets an outer type satisfy interfaces via its embedded type
- Supports overriding promoted methods on the outer struct
AI Mentor Explanation
A struct is like a player's profile card grouping name, role, and average into one record. Embedding is like an all-rounder card that contains a batter card inside it without renaming the fields: the all-rounder automatically shows the batting average and the batting method, yet you can still reach the inner batter part explicitly. Composition, not a new inherited class — you build a richer player by including an existing one.
Step-by-Step Explanation
Step 1
Define fields
Declare a struct with named, typed fields using type Name struct { Field Type }.
Step 2
Embed a type
Add an inner type without a field name inside the outer struct to embed it.
Step 3
Use promoted members
Access the embedded type's exported fields and methods directly on the outer value.
Step 4
Reach the inner value
Refer to outer.Inner to access the embedded value explicitly when needed.
Step 5
Override when required
Define a method with the same name on the outer struct to shadow the promoted one.
What Interviewer Expects
- That a struct groups named typed fields into one value
- That Go uses composition via embedding, not classical inheritance
- Knowledge that embedded fields and methods are promoted to the outer type
- Understanding that embedding can satisfy interfaces for the outer type
- That an outer method with the same name overrides the promoted one
Common Mistakes
- Calling embedding 'inheritance' and expecting subtype polymorphism
- Thinking the outer type IS the inner type rather than containing it
- Forgetting that only exported (capitalised) members are promoted usefully across packages
- Ambiguous promotion when two embedded types share a field or method name
- Assuming a pointer receiver method is promoted on a non-addressable embedded value
Best Answer (HR Friendly)
“A struct in Go is a way to bundle related pieces of data into one record, like grouping a user's name, age, and email together. Struct embedding lets you build a bigger type by including an existing one, automatically reusing its fields and behaviour — Go's way of composing types instead of using inheritance.”
Code Example
type Animal struct {
Name string
}
func (a Animal) Speak() string {
return a.Name + " makes a sound"
}
type Dog struct {
Animal // embedded, no field name
Breed string
}
func main() {
d := Dog{Animal: Animal{Name: "Rex"}, Breed: "Lab"}
fmt.Println(d.Name) // promoted field
fmt.Println(d.Speak()) // promoted method
fmt.Println(d.Animal.Name) // explicit inner access
}Follow-up Questions
- How is embedding different from classical inheritance?
- What happens when two embedded types have a method with the same name?
- How can embedding an interface inside a struct be useful?
- Which struct members are promoted across package boundaries?
- How do you override a promoted method on the outer struct?
MCQ Practice
1. What does struct embedding provide in Go?
Embedding composes types by promoting the embedded type's fields and methods to the outer struct.
2. How do you access an embedded Animal's field explicitly on a Dog?
The embedded value is reachable by its type name, so d.Animal.Name accesses it directly.
3. What happens if the outer struct defines a method with the same name as a promoted one?
A same-named method on the outer struct shadows the promoted method for calls on the outer value.
Flash Cards
What is a Go struct? — A composite type grouping a fixed set of named, typed fields into one value.
What is struct embedding? — Including a type without a field name so its fields and methods are promoted to the outer struct.
Inheritance or composition? — Composition — Go has no inheritance; embedding reuses behaviour by containment.
How to access the embedded value directly? — Use outer.InnerTypeName, e.g. dog.Animal.
How to override a promoted method? — Define a method of the same name on the outer struct.
Continue Learning
Related Interview Questions
What is the difference between a value receiver and a pointer receiver in Go?
medium
What is the empty interface (interface{} / any) in Go and when is it used?
medium
What are Go interfaces and how does implicit implementation work?
medium
How does error handling work in Go and why does it not use exceptions?
medium