码迷,mamicode.com
首页 > 其他好文 > 详细

A Tour of Go Switch evaluation order

时间:2014-10-28 08:10:58      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   for   sp   strong   

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

标签:style   blog   io   color   os   ar   for   sp   strong   

原文地址:http://www.cnblogs.com/ghgyj/p/4055703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!