compare Struct Definition with an alternative
Fast recall
Quick revision
Core answer
Practical understanding of Struct Definition.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
Embedding is a field declaration with no name — the type name becomes the field name:
type User struct {
Name string
Age int
}
func (u User) Greet() string { return "hi " + u.Name }
type Admin struct {
User // embedded — no field name
Level int
}
a := Admin{User: User{Name: "Root", Age: 40}, Level: 9}
fmt.Println(a.Name) // "Root" — User's field, promoted
fmt.Println(a.Greet()) // "hi Root" — User's method, promotedExecuted: both the field and the method arrive on Admin. That promotion is the whole feature — the inner type's exported fields and methods become reachable on the outer type as if declared there (unless shadowed by an outer declaration of the same name).
What embedding is NOT: inheritance. There's no subtype relationship (Admin is not a User — no assignment, no interface satisfaction through the outer type unless the method is promoted onto it), no overriding (an outer Greet doesn't 'override' — it shadows at the outer level; the inner's Greet still exists and is reachable via a.User.Greet()), no dynamic dispatch (the promoted method is the inner type's method, statically bound).
The interface consequence (the interview follow-up): if User satisfies Greeter and Greet is promoted, then Admin ALSO satisfies Greeter — through promotion. That's how embedding composes interface implementations: embed a FileLogger (has Log) and a MemStore (has Save) into App, and App satisfies both Logger and Store — verified with an AppStore interface embedding both. Interface satisfaction composes through struct embedding; type hierarchies don't exist.
compare Struct Definition with an alternative
Direct Answer
type Admin struct { User; Level int } — the inner struct's fields and methods are PROMOTED: admin.Name and admin.Greet() work directly. Composition, not inheritance — no polymorphism, no virtual dispatch.
Deep Dive & Architecture
The mechanics of promotion — how the compiler actually resolves a.Name: name lookup walks the struct's fields at increasing depth. Level and User are at depth 0 on Admin; Name is at depth 1 (inside User). A reference to a.Level finds depth 0; a.Name finds nothing at depth 0, descends into the embedded User, finds Name — promoted. Two rules keep this unambiguous: a shallower name always wins (a shadowing outer Name hides User's), and two same-named things at the SAME depth are an ambiguity — usable only through the explicit path (a.User.Name).
The promoted METHOD is a wrapper, not an alias: a.Greet() compiles to a.User.Greet() — a real method value with the receiver being the inner struct. That's why the method's own promoted lookups resolve within the INNER type's scope (a.Greet() using u.Name reads User's field even if the outer had its own Name at depth 0 — the receiver's type governs).
The composition pattern this enables — the standard-library proof: bufio.Reader embeds io.Reader (an INTERFACE — embedding works for interfaces too, promoting the interface's methods as compile-time-checked delegation); ReadSeeker-style composed interfaces embed io.Reader + io.Writer. The idiom: embed what you delegate to; the promotion IS the delegation; the interface satisfaction follows for free.
The inheritance-vs-composition payoff, stated once: embedding solves 'reuse the inner's fields/methods and satisfy its interfaces' without any of inheritance's problems (fragile base, deep hierarchies, accidental override). The cost: no polymorphism — when you need an inner-typed value to behave differently per outer type, that's an interface + method on the outer — the Go-shaped answer to the same problem inheritance was solving.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Struct Definition, and when would you use it?
- When would you use Struct Definition in a real project?
- What is a common mistake when using Struct Definition?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.