glang中switch ,case 无需break

在使用switch结构时,我们需要注意以下几点: 

示例

i := 0
switch i{
case 0:
   fmt.Printf("0")
   fallthrough
case 1:
   fmt.Printf("1")
case 2:
   fallthrough
case 3:
   fmt.Printf("3")
case 4, 5, 6:
   fmt.Printf("4, 5, 6")
default:
   fmt.Printf("Default")
}

1.左花括号{必须与switch处于同一行; 
2.条件表达式不限制为常量或者整数; 
3.单个case中,可以出现多个结果选项;

4.与C语言等规则相反,Go语言不需要用break来明确退出一个case; 
5.只有在case中明确添加fallthrough关键字,才会继续执行紧跟的下一个case;

猜你喜欢

转载自blog.csdn.net/xia_2017/article/details/79650561
今日推荐