Go中的switch fallthrough
2017-03-01 16:38
106 查看
Go基础
switch sExpr { case expr1: some instructions case expr2: some other instructions case expr3: some other instructions default: other code }
sExpr和expr1、expr2、expr3的类型必须一致。Go的switch非常灵活,表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项;而如果switch没有表达式,它会匹配true。 Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码。
http://play.golang.org/p/LySjbnt53q
package main import "fmt" func main() { switch { case false: fmt.Println("The integer was <= 4") fallthrough case true: fmt.Println("The integer was <= 5") fallthrough case false: fmt.Println("The integer was <= 6") fallthrough case true: fmt.Println("The integer was <= 7") case false: fmt.Println("The integer was <= 8") fallthrough default: fmt.Println("default case") } }
运行结果:
The integer was <= 5
The integer was <= 6
The integer was <= 7
从本例可以看出:switch人第一个expr为true的case开始执行,如果case带有fallthrough,程序会继续执行下一条case,不会再判断下一条case的expr,如果之后的case都有fallthrough,default出会被执行。
相关文章推荐
- fall through switch的用法
- 浅谈Swift编程中switch与fallthrough语句的使用
- switch语句中的Fall-through、静态导入
- switch fall-through一题
- 【swift】15-0525 控制流---switch break continue fallthrough
- Switch Fall Through
- java - switch case statement fall through and b...
- 33.笔记go语言——switch关键字
- Go语言的for和switch
- Go语言switch语句
- go-if、for、switch、goto、break、continue
- Go语言基础:if,goto,for,switch
- Swift中fallthrough的使用
- GOIP connects with Elastix through “config by line”
- Go基础系列:接口类型断言和type-switch
- go语言中select与switch的区别
- A Tour of Go Switch
- GO语言练习:switch基本用法
- 《iOS移动开发从入门到精通》图书连载14: if条件判断语句、switch条件判断语句、 continue语句和forthrough语句
- Go switch 语句