8 Star 47 Fork 21

Lucky / go-hutool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
07-switch.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Lucky 提交于 2020-02-19 16:20 . go 例子排版顺序
package main
/**
* Go by Example 中文版:分支结构
*/
import (
"fmt"
"time"
)
func main() {
//一个基本的 switch。
i := 2
fmt.Print("write ", i, " as ")
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("It's the weekend")
default:
fmt.Println("It's a weekday")
}
//不带表达式的 switch 是实现 if/else 逻辑的另一种 方式。这里还展示了 case 表达式也可以不使用常量。
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
//类型开关 (type switch) 比较类型而非值。可以用来发现一个接口值的 类型。在这个例子中,变量 t 在每个分支中会有相应的类型。
whatAmI := func(i interface{}) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type %T\n", t)
}
}
whatAmI(true)
whatAmI(1)
whatAmI("hey")
}
Go
1
https://gitee.com/huangbosbos/go-hutool.git
git@gitee.com:huangbosbos/go-hutool.git
huangbosbos
go-hutool
go-hutool
81f09a275c3b

搜索帮助

53164aa7 5694891 3bd8fe86 5694891