Chapter 39: Multi-case

Multi-case switch (also called “multiple values per case” or “comma-separated cases”).

In Go, one of the nicest features of switch is that a single case clause can match multiple values at once — you just list them separated by commas.

This makes code much shorter, cleaner and more readable compared to writing many separate case lines or long if-else if chains.

Let me explain everything about multi-case switch like we’re sitting together with laptop open — theory, syntax, real examples, style tips, common patterns, when to use it, and when not to.

1. Basic Syntax – Multi-case (multiple values in one case)

Go

Key rules:

  • Values in one case are separated by commas (no limit, but usually 2–8 is readable)
  • All values in one case must have compatible types with the switch expression
  • Order matters — first matching case runs (top to bottom)
  • No automatic fallthrough — even with multiple values, only that block executes
  • You can mix single-value and multi-value cases freely

2. Classic Example – Weekdays vs Weekends (most common first example)

Go

Why this is better than alternatives?

Alternative with if-else if:

Go

→ The switch version is much more readable and easier to maintain (add/remove days without touching || mess)

3. Very Common Real-World Pattern – HTTP Status Code Groups

Go

This style is extremely common in API handlers, web servers, CLI tools — grouping similar outcomes.

4. Multi-case with Short Variable Declaration

Go

‘choice’ variable is scoped only to the switch block — very clean.

5. Mixing Single-value & Multi-value Cases

Go

6. When to Use Multi-case switch vs Other Approaches

Situation Recommended choice Why
3–10 equality checks on same value multi-case switch Cleanest & most readable
Conditions are complex (>, &&, etc.) tagless switch or if-else if switch needs exact equality
Only 2–3 conditions if-else if or short switch Either fine
Many cases with same action multi-case switch Avoid long `
Need fallthrough behavior explicit fallthrough (rare) Better to group in one case

7. Your Quick Practice Exercise

Write a switch that classifies a month number (1–12) into seasons:

  • 12, 1, 2 → “Winter”
  • 3, 4, 5 → “Spring”
  • 6, 7, 8 → “Summer”
  • 9, 10, 11 → “Autumn”
  • anything else → “Invalid month”

Use multi-case style — how many cases did you need?

Now try the same logic with if-else if chain — which one do you find easier to read?

Any part still unclear?

  • Difference between multi-case switch vs if … || … || … ?
  • Can you put expressions in case values? (answer: no — only constants)
  • When is fallthrough useful (and why it’s rare)?
  • Or ready to see type switch (switch v := x.(type) {) next?

Keep playing with multi-case switch — it’s one of those small features that makes Go code feel so much cleaner once you start using it regularly.

You’re progressing really fast — keep going! 💪🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *