go语言:switch语句

前端之家收集整理的这篇文章主要介绍了go语言:switch语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


go语言专门用于多条件分支语句。

这里有个基本的switch语句。
在同一个case语句中,可以用逗号分隔不同的条件。在这个例子中,我们使用默认的default语句。
没有表达式的switch语句可以替代if/else语句。这里我们显示了case表达式可以不是常量。
Plain Text code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@H_502_58@ 15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packagemain
import"fmt"
import"time"
funcmain(){
i:=2
fmt.Print("write",i,"as")
switchi{
case1:
fmt.Println("one")
case2:
fmt.Println("two")
case3:
fmt.Println("three")
}
switchtime.Now().Weekday(){
casetime.Saturday,time.Sunday:
fmt.Println("it'stheweekend")
default:
fmt.Println("it'saweekday")
}
t:=time.Now()
switch{
caset.Hour()<12:
fmt.Println("it'sbeforenoon")
default:
fmt.Println("it'safternoon")
}
}

$gorunswitch.go
write2astwo
it'stheweekend
it'sbeforenoon

原文地址: https://gobyexample.com/switch 原文链接:https://www.f2er.com/go/191214.html

猜你在找的Go相关文章