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 单实例实现网站多域名请求

有时候写网站,为了统一的后端,把不同业务都集中到一个后端,这时就需要处理多域名的请求,在 Go http server 里实现很简单,只需把不同域名映射到不同的 `http.Handler`。...

golang snappy 的使用场合

google 自家的 snappy 压缩优点是非常高的速度和合理的压缩率。压缩率比 gzip 小,CPU 占用小。...

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

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

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 1ms