GolangNote

Golang笔记

GoLang Switch语句里的fallthrough 介绍

Permalink

GoLang Switch语句里的fallthrough 关键字比较特殊。下面举个例子形象说明。

GoLang Switch fallthrough

Go: Switch fallthrough
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import "fmt"

func main() {
    switch a := 3; {
    case a >= 2:
        fmt.Println(">=2")
        fallthrough
    case a >= 3:
        fmt.Println(">=3")
        fallthrough
    case a >= 4:
        fmt.Println(">=4")
        fallthrough
    case a >= 5:
        fmt.Println(">=5")
        fallthrough
    default:
        fmt.Println("default")
    }
}

输出:

Bash: output
1
2
3
4
5
>=2
>=3
>=4
>=5
default

可以看出,fallthrough 语句是不退出当前 case 语段去实行下一个 case 语段,且不去判断 case 条件的去实行。

本文网址: https://golangnote.com/topic/43.html 转摘请注明来源

Related articles

golang Selenium WebDriver 使用记录

Selenium WebDriver 直接通过浏览器自动化的本地接口来调用浏览器,以达到模拟浏览器行为的操作,如点击、选择、鼠标移动等。下面是记录个人使用golang 驱动的记录。...

Write a Comment to "GoLang Switch语句里的fallthrough 介绍"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 4ms