switch
The switch statement in Go handles multi-way branching based on a condition. Unlike many other languages, each case breaks automatically — execution does not fall through to the next case unless you explicitly use fallthrough.
Syntax
// Basic switch
switch expression {
case value1:
// code
case value2, value3: // multiple values can be grouped with a comma
// code
default:
// runs when no case matches
}
// Switch without an expression (alternative to if-else if)
switch {
case condition1:
// code
case condition2:
// code
}
// Switch with an initialization statement
switch init; expression {
}
// Type switch (determines the concrete type of an interface{} value)
switch v := i.(type) {
case int:
case string:
}
Syntax Reference
| Syntax | Description |
|---|---|
| switch expression { case ... } | Executes the case that matches the value of the expression. If no case matches, the default block runs. |
| case value1, value2: | Groups multiple values into a single case using a comma-separated list. |
| default: | Runs when no case matches. It is optional. |
| fallthrough | Continues execution into the next case. It must appear at the end of a case block. |
| switch { case condition: } | An expression-less form where each case holds a boolean condition. Useful as an alternative to if-else if chains. |
| switch v := i.(type) | A type switch. Branches based on the concrete type of an interface value. |
Sample Code
package main
import "fmt"
func dayType(day string) string {
switch day {
case "Saturday", "Sunday":
return "Weekend"
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
return "Weekday"
default:
return "Unknown"
}
}
func classify(score int) string {
// Expression-less switch (alternative to if-else if)
switch {
case score >= 90:
return "Excellent"
case score >= 70:
return "Good"
case score >= 50:
return "Pass"
default:
return "Fail"
}
}
func main() {
fmt.Println(dayType("Saturday")) // Weekend
fmt.Println(dayType("Monday")) // Weekday
fmt.Println(classify(95)) // Excellent
fmt.Println(classify(60)) // Pass
// Example of fallthrough
n := 2
switch n {
case 1:
fmt.Println("It's 1")
case 2:
fmt.Println("It's 2")
fallthrough // also run the next case
case 3:
fmt.Println("It's 2 or 3")
}
// Type switch
values := []interface{}{42, "hello", true, 3.14}
for _, v := range values {
switch t := v.(type) {
case int:
fmt.Printf("int: %d\n", t)
case string:
fmt.Printf("string: %s\n", t)
default:
fmt.Printf("other: %v\n", t)
}
}
}
Notes
Because Go's switch breaks automatically after each case, the common bug of forgetting a break — as in C — does not apply. Use fallthrough only when you intentionally want execution to continue into the next case.
fallthrough executes the next case's code unconditionally, regardless of whether its condition matches. Keep this behavior in mind when using it.
The type switch is a powerful feature used together with type assertions. It is commonly used in functions that accept an interface to branch on the concrete type of the value.
If you find any errors or copyright issues, please contact us.