golang-101-hacks(19)——switch
羊羽shine · · 1392 次点击 · · 开始浏览注:本文是对golang-101-hacks中文翻译。
和其他编程语言(例如C)相比,Go语音的switch-case语句不需要显式的添加"break",也没有fall-though。如下面代码所示:
Compared to other programming languages (such as C), Go's switch-case statement doesn't need explicit "break", and not have fall-though characteristic. Take the following code as an example:
package main
import (
"fmt"
)
func checkSwitch(val int) {
switch val {
case 0:
case 1:
fmt.Println("The value is: ", val)
}
}
func main() {
checkSwitch(0)
checkSwitch(1)
}
输出结果是
The value is: 1
期望当val为0或1时,执行fmt.Println("The value is: ", val),但实际上,该语句只在val为1时生效。为了得到期望结果,有两种方法:
使用fallthrough:
func checkSwitch(val int) {
switch val {
case 0:
fallthrough
case 1:
fmt.Println("The value is: ", val)
}
}
(2)将0和1放在同一个的case语句下:
func checkSwitch(val int) {
switch val {
case 0, 1:
fmt.Println("The value is: ", val)
}
}
与if-else相比,switch语句表达判断更清晰和简单:
package main
import (
"fmt"
)
func checkSwitch(val int) {
switch {
case val < 0:
fmt.Println("The value is less than zero.")
case val == 0:
fmt.Println("The value is qual to zero.")
case val > 0:
fmt.Println("The value is more than zero.")
}
}
func main() {
checkSwitch(-1)
checkSwitch(0)
checkSwitch(1)
}
输出结果
The value is less than zero.
The value is qual to zero.
The value is more than zero.
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
注:本文是对golang-101-hacks中文翻译。
和其他编程语言(例如C)相比,Go语音的switch-case语句不需要显式的添加"break",也没有fall-though。如下面代码所示:
Compared to other programming languages (such as C), Go's switch-case statement doesn't need explicit "break", and not have fall-though characteristic. Take the following code as an example:
package main
import (
"fmt"
)
func checkSwitch(val int) {
switch val {
case 0:
case 1:
fmt.Println("The value is: ", val)
}
}
func main() {
checkSwitch(0)
checkSwitch(1)
}
输出结果是
The value is: 1
期望当val为0或1时,执行fmt.Println("The value is: ", val),但实际上,该语句只在val为1时生效。为了得到期望结果,有两种方法:
使用fallthrough:
func checkSwitch(val int) {
switch val {
case 0:
fallthrough
case 1:
fmt.Println("The value is: ", val)
}
}
(2)将0和1放在同一个的case语句下:
func checkSwitch(val int) {
switch val {
case 0, 1:
fmt.Println("The value is: ", val)
}
}
与if-else相比,switch语句表达判断更清晰和简单:
package main
import (
"fmt"
)
func checkSwitch(val int) {
switch {
case val < 0:
fmt.Println("The value is less than zero.")
case val == 0:
fmt.Println("The value is qual to zero.")
case val > 0:
fmt.Println("The value is more than zero.")
}
}
func main() {
checkSwitch(-1)
checkSwitch(0)
checkSwitch(1)
}
输出结果
The value is less than zero.
The value is qual to zero.
The value is more than zero.