Back to Concepts Roadmap
🟠 Level 2 — Flow Control
Flow Control

Switch Statements

Clean multi-way branching without automatic fallthrough bugs and dynamic type inspection.

Real-World Analogy (Mental Model)

A vending machine coin slot: it checks the coin against multiple sizes and routes it immediately to the right bucket without falling into all other buckets.

Architecture & Memory MapVisual Model
switch value {
case "A", "B": // matches either
    // breaks automatically
case "C":
    // breaks automatically
default:
    // fallback
}

Key Concepts & Rules To Remember

  • Cases break automatically by default (no manual `break` keyword needed).
  • Tagless switches (`switch { ... }`) serve as a cleaner, more readable alternative to chained `if-else if` blocks.
  • Type switches (`switch v := i.(type)`) safely inspect the underlying dynamic type of an interface value.
Step-by-Step Code

1. Tagless and Multi-Value Switch

Go switch cases can evaluate expressions, check multiple matching values separated by commas, or omit the switch variable entirely.

example.goGo 1.24+
// 1. Multiple values per case
role := "admin"
switch role {
case "admin", "moderator":
    fmt.Println("Staff access granted")
case "guest":
    fmt.Println("Read-only access")
}

// 2. Tagless switch (Clean replacement for if-else chains)
score := 85
switch {
case score >= 90:
    fmt.Println("Grade A")
case score >= 80:
    fmt.Println("Grade B")
default:
    fmt.Println("Grade C")
}

Common Beginner Pitfalls & Mistakes

Mistake: Adding manual `break` at the end of every case.
✅ Correct Way: Go cases break automatically! `break` is redundant unless breaking out of an outer loop.
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.

Do Go switch cases automatically fall through to the next case like in C/Java?

Finished this lesson?

Mark it as complete to track your overall Go mastery.