Back to Concepts Roadmap
🟣 Level 4 — Composite Data
Memory & Types

Structs & Composition

Grouping related data fields, struct tags for JSON/DB serialization, and composition via embedding.

Real-World Analogy (Mental Model)

A passport: contains labeled fields (Name, Birthdate, Country). Embedding is like stamping a Visa inside the passport — all visa permissions are attached directly to your passport.

Key Concepts & Rules To Remember

  • Primary building block for custom data structures in Go.
  • Composition over inheritance: embed structs to promote inner fields.
  • Struct tags (e.g. `json:"id"`) guide serialization reflection.
Step-by-Step Code

1. Understanding Structs & Composition

Grouping related data fields, struct tags for JSON/DB serialization, and composition via embedding. In Go, structs & composition is designed around clarity and high runtime efficiency.

example.goGo 1.24+
type User struct {
    ID    int    `json:"id"`
    Email string `json:"email"`
}

type Admin struct {
    User  // Embedded struct (Composition)
    Level int `json:"level"`
}

Common Beginner Pitfalls & Mistakes

Mistake: Misusing structs & composition without understanding its memory or concurrency semantics.
✅ Correct Way: Always follow standard Go idioms and verify with tests.
Self Assessment

Knowledge Check

Verify your understanding with these interactive practice questions.

Quizzes

Quick checks for understanding

Multiple-choice with inline explanations—expand to see why.

What is the primary concept behind Structs & Composition?

Finished this lesson?

Mark it as complete to track your overall Go mastery.