首页 > 代码库 > A Tour of Go Switch evaluation order
A Tour of Go Switch evaluation order
Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
(For example,
switch i {case 0:case f():}
does not call f
if i==0
.)
Note: Time in the Go playground always appears to start at 2009-11-10 23:00:00 UTC, a value whose significance is left as an exercise for the reader.
package main import ( "fmt" "time")func f(num *int) int{ (*num) = (*num) + 1 return (*num);}func main() { num := 0 total := 3 switch total { case f(&num): fmt.Println(num) case f(&num): fmt.Println(num) case f(&num): fmt.Println(num) } fmt.Println("When‘s Saturday?") today := time.Now().Weekday() switch time.Saturday { case today + 0: fmt.Println("Today.") case today + 1 : fmt.Println("Tomorrow.") case today + 2: fmt.Println("In tow days.") default: fmt.Println("Too far away.") }}
case中的语句可以是返回值的语句
A Tour of Go Switch evaluation order
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。