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中的语句可以是返回值的语句

时间: 2024-10-24 13:59:42

A Tour of Go Switch evaluation order的相关文章

A Tour of Go Switch

You probably knew what switch was going to look like. A case body breaks automatically, unless it ends with a fallthrough statement. package main import ( "fmt" "runtime" ) func main() { fmt.Print("Go runs on ") switch os :=

A Tour of Go Switch with no condition

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(

golang基础练习(一)

//遍历map package main import "fmt" func main() { x := make(map[string]int) x["zhangsan"] = 3 x["lisi"] = 4 x["wangwu"] = 5 //#丢弃值 for i,_ := range x { fmt.Println(i) } } //匿名函数 package main import "fmt" fun

[ES7] Descorator: evaluated &amp; call order

When multiple decorators apply to a single declaration, their evaluation is similar to function composition in mathematics. In this model, when composing functions f and g, the resulting composite (f ° g)(x) is equivalent to f(g(x)). As such, the fol

推荐!国外程序员整理的 PHP 资源大全

iadoz 在 Github 发起维护的一个 PHP 资源列表,内容包括:库.框架.模板.安全.代码分析.日志.第三方库.配置工具.Web 工具.书籍.电子书.经典博文等等. 依赖管理 依赖和包管理库 Composer/Packagist:一个包和依赖管理器 Composer Installers:一个多框架Composer库安装器 Pickle:一个PHP扩展安装器 其他的依赖管理 其他的相关依赖管理 Satis:一个静态Composer存储库生成器 Composition:一个在运行时检查C

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

【PHP开发】国外程序员收集整理的 PHP 资源大全

依赖管理 依赖和包管理库 Composer/Packagist:一个包和依赖管理器 Composer Installers:一个多框架Composer库安装器 Pickle:一个PHP扩展安装器 其他的依赖管理 其他的相关依赖管理 Satis:一个静态Composer存储库生成器 Composition:一个在运行时检查Composer环境的库 Version:语义版本的解析和比较库 NameSpacer -转化下划线到命名空间的库 Patch Installer -使用Composer安装补丁

优秀的PHP开源项目集合

包管理Package Management Libraries for package and dependency management. Composer/Packagist– A package and dependency manager. Composer Installers– A multi framework Composer library installer. Package Management Related Libraries related to package ma