Go Switch
Elegant way to handle multiple conditions
🔄 What is Go Switch?
Go switch statements provide a clean way to execute different code blocks based on variable values, replacing long if-else chains with more readable and efficient code structure.
// Simple switch example
day := "Monday"
switch day {
case "Monday":
fmt.Println("Start of work week!")
case "Friday":
fmt.Println("TGIF!")
default:
fmt.Println("Regular day")
}
Output:
Start of work week!
Types of Switch Statements
Basic Switch
Compare variable against multiple values
switch grade {
case "A":
fmt.Println("Excellent")
case "B":
fmt.Println("Good")
}
Expression Switch
Switch on expressions and conditions
switch {
case score >= 90:
fmt.Println("A")
case score >= 80:
fmt.Println("B")
}
Type Switch
Switch based on variable type
switch v := x.(type) {
case int:
fmt.Println("Integer")
case string:
fmt.Println("String")
}
Fallthrough
Continue to next case
switch num {
case 1:
fmt.Println("One")
fallthrough
case 2:
fmt.Println("Small number")
}
🔹 Basic Switch Statement
The most common form of switch in Go:
package main
import "fmt"
func main() {
month := 3
switch month {
case 1:
fmt.Println("January")
case 2:
fmt.Println("February")
case 3:
fmt.Println("March")
case 4:
fmt.Println("April")
default:
fmt.Println("Other month")
}
}
Output:
March
🔹 Multiple Values in Case
Handle multiple values in a single case:
package main
import "fmt"
func main() {
day := "Saturday"
switch day {
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
fmt.Println("Weekday - Time to work!")
case "Saturday", "Sunday":
fmt.Println("Weekend - Time to relax!")
default:
fmt.Println("Invalid day")
}
}
Output:
Weekend - Time to relax!
🔹 Expression Switch
Switch without a variable (like if-else chain):
package main
import "fmt"
func main() {
score := 85
switch {
case score >= 90:
fmt.Println("Grade: A - Outstanding!")
case score >= 80:
fmt.Println("Grade: B - Great job!")
case score >= 70:
fmt.Println("Grade: C - Good work!")
case score >= 60:
fmt.Println("Grade: D - Need improvement")
default:
fmt.Println("Grade: F - Study harder!")
}
}
Output:
Grade: B - Great job!
🔹 Switch with Short Statement
Declare and switch on a variable in one line:
package main
import (
"fmt"
"time"
)
func main() {
switch hour := time.Now().Hour(); {
case hour < 12:
fmt.Println("Good morning!")
case hour < 17:
fmt.Println("Good afternoon!")
default:
fmt.Println("Good evening!")
}
}
Output:
Good afternoon!
🔹 Fallthrough Statement
Execute the next case even if current case matches:
package main
import "fmt"
func main() {
number := 1
switch number {
case 1:
fmt.Println("One")
fallthrough
case 2:
fmt.Println("Small number")
fallthrough
case 3:
fmt.Println("Less than four")
default:
fmt.Println("Other number")
}
}
Output:
One
Small number
Less than four
🔹 Type Switch
Switch based on the type of a variable:
package main
import "fmt"
func checkType(x interface{}) {
switch v := x.(type) {
case int:
fmt.Printf("Integer: %d\n", v)
case string:
fmt.Printf("String: %s\n", v)
case bool:
fmt.Printf("Boolean: %t\n", v)
default:
fmt.Printf("Unknown type: %T\n", v)
}
}
func main() {
checkType(42)
checkType("Hello")
checkType(true)
}
Output:
Integer: 42
String: Hello
Boolean: true