go语言中fallthrough用法
来源:互联网 发布:猎鹿帽为什么前后 知乎 编辑:程序博客网 时间:2024/06/15 02:42
switch sExpr {case expr1: some instructionscase expr2: some other instructionscase expr3: some other instructionsdefault: other code}
sExpr和expr1、expr2、expr3的类型必须一致。Go的switch非常灵活,表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项;而如果switch没有表达式,它会匹配true。 Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码,fallthrough不会判断下一条case的expr结果是否为true。
http://play.golang.org/p/LySjbnt53q
package mainimport "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 <= 5The integer was <= 6The integer was <= 7
从本例可以看出:switch人第一个expr为true的case开始执行,如果case带有fallthrough,程序会继续执行下一条case,不会再判断下一条case的expr,如果之后的case都有fallthrough,default出会被执行。
0 0
- go语言中fallthrough用法
- go语言fallthrough的用法心得
- Swift的switch结构中,fallthrough的用法注意总结
- swift语言之fallthrough
- Go语言中string的用法(转载)
- Go语言中Time的用法[1]
- Go语言中Time的用法[2]
- fallthrough
- go语言MAP用法
- swift中fallthrough的作用
- Swift中fallthrough的使用
- (摘抄)GO语言中template的用法
- go语言defer的用法
- GO语言range的用法
- Go语言中的map用法
- 数据库中 go的用法
- go中select的用法
- Go语言中Channel机制
- RelativeLayout的属性
- (7)DTD的声明与引用
- 给TextView加上多彩效果:改变部分字体的大小和颜色
- 将List转成树的两种方式(递归、循环)
- LTE信号相关的调试
- go语言中fallthrough用法
- ajax文件上传
- SpringJDBC中DataSource的实现
- FFMPEG命令集
- 关于git(SourceTree)的几个疑问及解答(待自己学会来解答)
- Jtester框架初体验
- js插入表行 js设置tr属性
- 用ImageMagick转换缩小JPG图像提高图像质量的方法
- VS2010旗舰版+LUA环境配置