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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
switch expression { case value1, value2, value3: // this block runs if expression equals ANY of value1/value2/value3 case value4, value5: // runs if expression == value4 OR value5 case value6: // single value — still allowed default: // no match } |
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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package main import ( "fmt" "time" ) func main() { day := time.Now().Weekday() switch day { case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday: fmt.Println("It's a weekday → work / study day 💼") case time.Saturday, time.Sunday: fmt.Println("Weekend! Time to relax 🏖️🍿") default: fmt.Println("What kind of day is this?!") } fmt.Printf("Today is %s\n", day) } |
Why this is better than alternatives?
Alternative with if-else if:
|
0 1 2 3 4 5 6 7 8 9 |
if day == time.Monday || day == time.Tuesday || day == time.Wednesday || day == time.Thursday || day == time.Friday { // ... } |
→ 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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
func handleResponse(status int) string { switch status { case 200, 201, 204: return "Success (2xx)" case 400, 401, 403, 404, 429: return "Client error (4xx)" case 500, 502, 503, 504: return "Server error (5xx)" default: return fmt.Sprintf("Unknown status: %d", status) } } |
This style is extremely common in API handlers, web servers, CLI tools — grouping similar outcomes.
4. Multi-case with Short Variable Declaration
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
input := "y" switch choice := strings.ToLower(input); choice { case "yes", "y", "ok", "confirm", "true": fmt.Println("User confirmed action") case "no", "n", "cancel", "abort", "false": fmt.Println("User cancelled") case "maybe", "unsure", "later": fmt.Println("User is undecided") default: fmt.Println("Invalid choice:", choice) } |
‘choice’ variable is scoped only to the switch block — very clean.
5. Mixing Single-value & Multi-value Cases
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
color := "red" switch color { case "red", "crimson", "scarlet": fmt.Println("Warm color – red family") case "blue": fmt.Println("Cool color – blue") case "green", "lime", "emerald": fmt.Println("Nature color – green family") case "yellow": fmt.Println("Bright color – yellow") default: fmt.Println("Unknown color") } |
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! 💪🇮🇳🚀
