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) +
return (*num);
}
func main() {
num :=
total :=
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 + :
fmt.Println("Today.")
case today + :
fmt.Println("Tomorrow.")
case today + :
fmt.Println("In tow days.")
default:
fmt.Println("Too far away.")
}
}

case中的语句可以是返回值的语句

上一篇:ngOptions


下一篇:Socket学习总结系列(二) -- CocoaAsyncSocket