Chapter 38: Single-Case
Single-Case — which in the context of Go almost always refers to the basic / classic / single-case switch statement (as opposed to multi-value cases, type switches, tagless switches, etc.).
Many beginner tutorials (especially W3Schools, TutorialsTeacher, Programiz, etc.) use the exact phrase “Single-Case switch” to describe the simplest form of switch — where each case checks against one single value.
Let me explain it like your friendly offline Go teacher — slowly, clearly, with lots of runnable examples and comparisons so you really feel the difference.
1. What is a “Single-Case” switch?
- It’s the most basic form of the switch statement
- Each case clause contains exactly one value (or constant expression)
- The switch expression is compared equality-wise (==) against each case value
- First match wins → its block runs → switch ends (no automatic fallthrough)
- Very similar to a chain of if … else if … == … but usually more readable when you have 4+ cases
Official / tutorial name Many learning sites literally title this section “Single-Case switch” to contrast it with:
- multi-value cases (case “Mon”, “Tue”: …)
- type switch (switch v := x.(type) {)
- tagless switch (switch { case age > 18: … })
2. Syntax – Single-Case Style
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
switch expression { case value1: // code runs if expression == value1 case value2: // code runs if expression == value2 case value3: // ... default: // runs if no case matched (optional) } |
Key points:
- No parentheses around the expression
- No mandatory braces {} if only one statement per case
- No break needed — cases do not fall through by default
- default is optional and can be anywhere (but usually last)
3. Classic Single-Case Example – Weekday Name
|
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 27 28 29 30 31 32 33 34 35 36 37 38 |
package main import ( "fmt" "time" ) func main() { day := time.Now().Weekday() // e.g. time.Tuesday var dayName string switch day { case time.Monday: dayName = "Monday" case time.Tuesday: dayName = "Tuesday" case time.Wednesday: dayName = "Wednesday" case time.Thursday: dayName = "Thursday" case time.Friday: dayName = "Friday" case time.Saturday: dayName = "Saturday" case time.Sunday: dayName = "Sunday" default: dayName = "Unknown day?!" } fmt.Printf("Today is %s\n", dayName) } |
Why this is better than if-else chain?
- Easier to scan (all cases aligned vertically)
- Less typing (no repeated day ==)
- Compiler can sometimes optimize it better
4. Single-Case with Short Variable Declaration (very common)
You can declare variables right in the switch — scoped only to the switch.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
input := "yes" switch answer := strings.ToLower(input); answer { case "yes", "y", "ok": fmt.Println("User confirmed") case "no", "n": fmt.Println("User cancelled") case "maybe": fmt.Println("User is unsure") default: fmt.Println("Invalid response:", answer) } // 'answer' is not visible after the switch ends |
5. Single-Case vs If-Else Chain – Quick Comparison
| Situation | if-else if chain | switch single-case | Winner in Go style 2026 |
|---|---|---|---|
| 2–3 conditions | Very fine | Also fine | Either |
| 5+ equality checks | Becomes repetitive | Cleaner & more readable | switch |
| Need short variable declaration | Only in first if | Available in every case (but usually in switch expr) | switch |
| Conditions are complex (>, &&) | Better | Harder (use tagless switch instead) | if-else |
| Many constant values | Tedious | Perfect | switch |
6. Your Quick Practice Exercise
Try writing these two versions of the same logic:
Version A – using if-else if chain Version B – using single-case switch
Task: Given a number 1–7, print the weekday name (1 = Monday, 2 = Tuesday, …, 7 = Sunday)
Which version do you prefer when there are 7 cases? Now imagine 12 months — which one stays cleaner?
Any part still confusing?
- Difference between single-case switch vs tagless switch?
- When to choose switch over if-else chain?
- Why no automatic fallthrough in Go?
- Or ready to see multi-value cases (case “Mon”, “Tue”: …) next?
Keep typing these examples — switch is one of those things that once you start using it regularly, you’ll wonder how you ever lived without the clean single-case style.
You’re killing it — keep going! 💪🇮🇳🚀
