Code examples from Go Tutorial for Beginners #7 - Control Flow: switch
basic_switch.go- Basic switch statements with multiple case valuesexpressionless_switch.go- Switch without expression (like if-else chains)fallthrough.go- Using the fallthrough keywordtype_switch.go- Type switches for interface types
go run basic_switch.go
go run expressionless_switch.go
go run fallthrough.go
go run type_switch.goswitch day {
case "Monday", "Tuesday":
fmt.Println("Weekday")
case "Saturday", "Sunday":
fmt.Println("Weekend")
default:
fmt.Println("Unknown")
}switch {
case score >= 90:
fmt.Println("Grade: A")
case score >= 80:
fmt.Println("Grade: B")
default:
fmt.Println("Grade: F")
}switch num {
case 1:
fmt.Println("One")
fallthrough
case 2:
fmt.Println("Two")
}switch v := i.(type) {
case int:
fmt.Println("Integer:", v)
case string:
fmt.Println("String:", v)
default:
fmt.Printf("Unknown: %T\n", v)
}Go Tutorial for Beginners #7 - Control Flow: switch
MIT