Go Example--switch

时间:2023-03-09 05:45:38
Go Example--switch
package main

import (
"fmt"
"time"
) func main() {
i := 2
fmt.Print("write ", i, " as ")
//switch 语句第一种使用方式,i与case去逐个匹配
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
} //case 可以通过逗号来分隔多个值
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("it's the wekend")
default:
fmt.Println("it's a weekday")
} t := time.Now()
//第二种switch方式,判断case是否为true
//fallthrough 语句可以让当前case执行完后去执行下一个case
switch {
case t.Hour() < 12:
fmt.Println("it's before noon")
default:
fmt.Println("it's after noon")
}
}