3.8 Go switch 语句学习笔记

1. 基本 switch --- 匹配值

Go 的 switch 默认每个 case 自动 break,不需要手动写 break(与 C/Java 不同):

bash 复制代码
package main

import "fmt"

func main() {
    day := 3

    fmt.Print("Day: ")
    switch day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    case 6:
        fmt.Println("Saturday")
    case 7:
        fmt.Println("Sunday")
    default:
        fmt.Println("Invalid day")
    }
}

执行结果:

bash 复制代码
Day: Wednesday

要点:

  • switch day --- 匹配变量 day 的值,day=3 匹配 case 3
  • 每个 case 结束后自动 break,不会继续执行下一个 case(Go 与 C/Java 最大的区别)
  • default --- 所有 case 都不匹配时执行,相当于 else
  • case 的值必须是可比较的类型(int、string、rune 等),且每个 case 的值必须不同

2. 多值匹配 --- 一个 case 匹配多个值

用逗号分隔,一个 case 可以匹配多个值:

bash 复制代码
package main

import "fmt"

func main() {
    grade := 'B' // rune 类型

    fmt.Print("Grade performance: ")
    switch grade {
    case 'A', 'a':
        fmt.Println("Excellence")
    case 'B', 'b':
        fmt.Println("Good")
    case 'C', 'c':
        fmt.Println("Average")
    case 'D', 'd', 'F', 'f':
        fmt.Println("Needs improvement")
    default:
        fmt.Println("Invalid grade")
    }
}

执行结果:

bash 复制代码
Grade performance: Good

要点:

  • case 'A', 'a': --- 只要 grade 是 'A' 或 'a' 就匹配,相当于 grade == 'A' || grade == 'a'
  • 这比写多个独立 case 更简洁,尤其在处理大小写、枚举值分组时很实用
  • 'B' 是 rune 类型(int32 的别名),单引号表示字符,双引号表示字符串
  • case 'D', 'd', 'F', 'f': --- D/d 和 F/f 合并为一个 case,逻辑上它们都是"需要改进"

3. 无表达式 switch --- 等价于 if-else if 链

switch {} 不带表达式,每个 case 写完整的条件表达式:

bash 复制代码
package main

import "fmt"

func main() {
    score := 85

    fmt.Print("Score evaluation: ")
    switch {
    case score >= 90:
        fmt.Println("Outstanding")
    case score >= 80:
        fmt.Println("Very Good")
    case score >= 70:
        fmt.Println("Good")
    case score >= 60:
        fmt.Println("Satisfactory")
    default:
        fmt.Println("Needs work")
    }
}

执行结果:

bash 复制代码
Score evaluation: Very Good

要点:

  • switch {} 没有表达式,每个 case 是一个独立的布尔条件
  • 等价于 if-else if-else 链,但比 if-else 链更清晰易读
  • case 按顺序判断,第一个为 true 的 case 执行后自动 break
  • score=85:score >= 90 为 false → score >= 80 为 true → 输出 "Very Good"
  • default 等价于最终的 else
  • 适用场景:范围判断、复杂条件分支(if-else 嵌套超过 3 层时推荐用 switch)

4. fallthrough --- 强制执行下一个 case

Go 的 switch 默认每个 case 自动 break。fallthrough 显式打破这个规则,强制继续执行下一个 case:

bash 复制代码
package main

import "fmt"

func main() {
    day := 1

    fmt.Print("Day type: ")
    switch day {
    case 1:
        fmt.Print("Start of work week, ")
        fallthrough
    case 2, 3, 4:
        fmt.Println("Weekday")
    case 5:
        fmt.Println("TGIF!")
    case 6, 7:
        fmt.Println("Weekend")
    }
}

执行结果(day=1):

bash 复制代码
Day type: Start of work week, Weekday

要点:

  • day=1 匹配 case 1,输出 "Start of work week, "
  • fallthrough 强制跳到下一个 case(case 2, 3, 4),不管下一个 case 的条件是否匹配
  • 所以继续输出 "Weekday" --- 注意 day=1 并不匹配 case 2, 3, 4,但 fallthrough 无条件继续
  • fallthrough 只能出现在 case 的最后一行,不能放在中间
  • fallthrough 只能跳到下一个 case,不能指定跳到某个特定 case(与 C 的 goto 不同)
  • Go 设计者认为 fallthrough 容易导致 bug,所以默认不加,必须显式写才生效
  • 实际中很少使用 fallthrough,大部分场景用多值匹配(知识点 2)替代

5. switch 初始化语句 --- switch init; value

与 if 类似,switch 可以在条件前加初始化语句,初始化的变量作用域仅限 switch 块:

bash 复制代码
package main

import "fmt"

func main() {
    score := 85

    fmt.Print("Letter grade: ")
    switch x := score / 10; x {
    case 10, 9:
        fmt.Println("A")
    case 8:
        fmt.Println("B")
    case 7:
        fmt.Println("C")
    case 6:
        fmt.Println("D")
    default:
        fmt.Println("F")
    }
}

执行结果:

bash 复制代码
Letter grade: B

要点:

  • switch x := score / 10; x --- 分号前是初始化语句,分号后是 switch 表达式
  • x := 85 / 10 = 8,匹配 case 8,输出 "B"
  • x 的作用域仅限 switch 块内部,switch 外无法访问 x
  • case 10, 9: --- 100分和90-99分都对应 A(多值匹配)
  • 与 if init; condition {} 语法一致,都是 Go 的"先初始化再判断"模式
  • 好处:避免在 switch 外声明只用一次的临时变量,保持代码紧凑

知识点总结

|-------------|--------------------------------|
| 知识点 | 关键概念 |
| 基本 switch | 默认自动 break,不需要手动写(与 C/Java 不同) |
| 多值匹配 | case 'A', 'a': |
| 无表达式 switch | switch {} |
| fallthrough | 强制继续执行下一个 case,不检查条件,必须显式写 |
| switch 初始化 | switch x := score/10; x |

相关推荐
圣殿骑士-Khtangc2 小时前
Go大厂面试真题精讲之并发安全Map的实现方案
golang
圣殿骑士-Khtangc4 小时前
Go-Channel面试精讲从底层结构到select随机性
golang
北漂燕郊杨哥5 小时前
dsh-desktop:DeepSeek Harness 的本地优先桌面应用
golang·wails·deepseek·deepseekharness
朋克洛德的码农20 小时前
Go并发-sync包四剑客:Mutex、RWMutex、WaitGroup、Once-从入门到原理
开发语言·后端·golang
圣殿骑士-Khtangc20 小时前
Go泛型编程实战从类型约束到泛型数据结构
golang
FfHUCisI1 天前
Golang - 信号量模式(Semaphore Pattern)
开发语言·后端·golang
报错小能手1 天前
Go 语言结构 基础语法
开发语言·后端·golang
剩下了什么1 天前
go语言 Ctx:「错误三:在 Context 中存储可变值」
开发语言·后端·golang
运维开发笔记1 天前
3.7 Go panic 与 recover 学习笔记
golang