A Tour of Go Switch with no condition

时间:2014-10-28 08:09:51   收藏:0   阅读:112

Switch without a condition is the same as switch true.

This construct can be a clean way to write long if-then-else chains.

package main 

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("Good morning!")
    case t.Hour() < 17:
        fmt.Println("Good afternoon")
    default:
        fmt.Println("Good evening.")
    }
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!