分享
go的switch case
梁十八 · · 5353 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
可以一个case带几个参数:
var i = 0
switch i {
case 0, 1:
fmt.Println("1")
case 2:
fmt.Println("2")
default:
fmt.Println("def")
}
默认有break效果,要取消就加上fallthrough:
var i = 0
switch i {
case 0:
fallthrough
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
default:
fmt.Println("def")
}
case还可以是表达式:
var i = 0
switch {
case i > 0 && i < 10:
fmt.Println("i > 0 and i < 10")
case i > 10 && i < 20:
fmt.Println("i > 10 and i < 20")
default:
fmt.Println("def")
}
注意:go的switch case默认有break。如果不需要break,可以加上fallthrough**~~
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信5353 次点击
上一篇:go的time
下一篇:interface{}、类型断言
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
可以一个case带几个参数:
var i = 0
switch i {
case 0, 1:
fmt.Println("1")
case 2:
fmt.Println("2")
default:
fmt.Println("def")
}
默认有break效果,要取消就加上fallthrough:
var i = 0
switch i {
case 0:
fallthrough
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
default:
fmt.Println("def")
}
case还可以是表达式:
var i = 0
switch {
case i > 0 && i < 10:
fmt.Println("i > 0 and i < 10")
case i > 10 && i < 20:
fmt.Println("i > 10 and i < 20")
default:
fmt.Println("def")
}
注意:go的switch case默认有break。如果不需要break,可以加上fallthrough**~~