Go Switch Statement
A switch statement tests a value against multiple cases and runs the matching block. It is a cleaner alternative to writing many if-else if chains when checking a single value against fixed options.
Basic Switch Statement
package main
import "fmt"
func main() {
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
default:
fmt.Println("Weekend")
}
}
Output:
Wednesday
Switch Flow Diagram
value = 3
│
▼
case 1? → No
│
case 2? → No
│
case 3? → Yes ──► "Wednesday"
│
(stops)
Go automatically stops after the first matching case. No break statement is needed — unlike C or Java.
Switch with String
package main
import "fmt"
func main() {
lang := "Go"
switch lang {
case "Python":
fmt.Println("You chose Python")
case "Go":
fmt.Println("You chose Go")
case "Java":
fmt.Println("You chose Java")
default:
fmt.Println("Unknown language")
}
}
Output:
You chose Go
Multiple Values in One Case
Multiple values can be listed in a single case, separated by commas. If the tested value matches any of them, that case runs.
package main
import "fmt"
func main() {
day := "Saturday"
switch day {
case "Saturday", "Sunday":
fmt.Println("Weekend!")
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
fmt.Println("Weekday")
}
}
Output:
Weekend!
Switch Without a Condition (Expression Switch)
When no value is given after switch, Go treats it like an if-else chain. Each case holds a boolean condition.
package main
import "fmt"
func main() {
score := 82
switch {
case score >= 90:
fmt.Println("Grade A")
case score >= 75:
fmt.Println("Grade B")
case score >= 60:
fmt.Println("Grade C")
default:
fmt.Println("Grade F")
}
}
Output:
Grade B
Switch with Initialization
A variable can be declared directly inside the switch statement. It is scoped only to the switch block.
package main
import "fmt"
func getStatus() int {
return 404
}
func main() {
switch code := getStatus(); code {
case 200:
fmt.Println("OK")
case 404:
fmt.Println("Not Found")
case 500:
fmt.Println("Server Error")
default:
fmt.Println("Unknown Status")
}
}
Output:
Not Found
The fallthrough Keyword
By default, Go stops at the first matching case. The fallthrough keyword forces execution to continue into the next case regardless of its condition.
package main
import "fmt"
func main() {
level := 1
switch level {
case 1:
fmt.Println("Beginner")
fallthrough
case 2:
fmt.Println("Intermediate")
case 3:
fmt.Println("Advanced")
}
}
Output:
Beginner
Intermediate
Switch vs if-else
| Feature | switch | if-else |
|---|---|---|
| Best for | Checking one value against many options | Complex range conditions |
| Readability | Cleaner for many fixed options | Cleaner for range checks |
| Auto-stop | Yes — no break needed | N/A |
| Multiple values | Yes — in one case | Needs || |
Key Points
- Switch tests a value against multiple cases and runs the first match
- No
breakis needed — Go stops at the matching case automatically - Multiple values fit in one case separated by commas
- Switch without a condition works like a chain of if-else conditions
fallthroughforces execution to continue to the next case
