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 sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

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

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