/*
go switch语句
当条件判断分支太多的时候
我们会使用switch语句来优化
*/
package main
import (
"fmt"
"time"
)
func main() {
i := 2
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
// 你可以使用逗号来在 case中分开多个条件
// 还可以使用default
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("is is the weekend")
default:
fmt.Println("it is a weekday")
}
//switch后面可以没有变量
//case后面可以是表达式
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("")
default:
fmt.Println("")
}
}