ProgramGuru

Struct Embedding

Start with Struct Embedding

Embedding puts a type into a struct without giving it a separate explicit field name, enabling promoted field and method selectors.

What you’ll learn

  • Explain the main purpose of struct embedding.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

With type Employee struct { Person; Role string }, the embedded Person is still a real field accessible as employee.Person. Its fields can also be accessed through promoted selectors such as employee.Name when unambiguous.

Embedding is composition, not subclass inheritance. It does not make Employee a subtype of Person, and promoted methods follow Go method-set rules. Selectors can be shadowed or become ambiguous, so choose meaningful types and names.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for embeddingEmbedding promotes accessible selectors without inheritance.Employee valueStep 1Embedded IdentityStep 2Promoted NameStep 3Explicit Identity.NameStep 4Embedding promotes accessible selectors without inheritance.
Embedding promotes accessible selectors without inheritance.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

type Identity struct{ Name string }
type Employee struct {
	Identity
	Role string
}

func main() {
	employee := Employee{Identity: Identity{Name: "Sam"}, Role: "Engineer"}
	fmt.Println("Promoted:", employee.Name)
	fmt.Println("Explicit:", employee.Identity.Name)
	fmt.Println("Role:", employee.Role)
}

Output

Promoted: Sam
Explicit: Sam
Role: Engineer

Trace what the program does

Employee embeds Identity, making its Name field accessible both with employee.Name and employee.Identity.Name. The Role field remains a direct Employee field.

Avoid common mistakes

  • An embedded struct is not a superclass; Go does not provide class inheritance.
  • Promotion can be ambiguous when embedded types offer conflicting field or method names.

Try it yourself

Embed a Details struct with an ID in an Order struct, then print the promoted ID.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What is true about employee.Name here?
Choose one answer. You can try again.

Answer and explanation

It is promoted from the embedded Identity field

Correct. Employee embeds Identity, making its Name field accessible both with employee.

What you should remember

You have practiced Struct Embedding in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Methods in Go →