Golang fallthrough 关键字
最后修改于 2025 年 5 月 7 日
本教程将解释如何在 Go 中使用 fallthrough 关键字。我们将通过 fallthrough 的实际示例涵盖 switch 语句的控制。
fallthrough 语句将控制转移到 switch 语句中的下一个 case 子句。它是 Go 的 switch 控制流中一项独特的特性。
在 Go 中,fallthrough 必须是 case 子句中的最后一个语句。与其他语言不同,Go 的 switch 不会自动贯穿 case。
基本的 fallthrough 示例
此示例演示了在 switch 语句中最简单的 fallthrough 用法。它展示了执行如何流向下一个 case。
package main
import "fmt"
func main() {
num := 2
switch num {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
fallthrough
case 3:
fmt.Println("Three")
default:
fmt.Println("Other number")
}
}
当 num 为 2 时,它打印 "Two" 然后贯穿到打印 "Three"。如果没有 fallthrough,则只会打印 "Two"。
多个 fallthroughs
您可以链接多个 fallthrough 语句以继续执行多个 case 子句。此示例展示了其行为。
package main
import "fmt"
func main() {
letter := 'B'
switch letter {
case 'A':
fmt.Println("A")
fallthrough
case 'B':
fmt.Println("B")
fallthrough
case 'C':
fmt.Println("C")
fallthrough
default:
fmt.Println("Default case")
}
}
从 case 'B' 开始,由于 fallthrough 语句,执行将继续穿过所有后续的 case。每个 case 的代码按顺序执行。
带表达式 case 的 fallthrough
fallthrough 也适用于表达式 case。此示例展示了它在非常量表达式下的行为。
package main
import "fmt"
func main() {
x := 10
switch {
case x > 5:
fmt.Println("Greater than 5")
fallthrough
case x > 8:
fmt.Println("Greater than 8")
case x > 10:
fmt.Println("Greater than 10")
default:
fmt.Println("Default case")
}
}
第一个 case 匹配并执行,然后贯穿到第二个 case。请注意,由于 fallthrough,第二个 case 的条件未被评估。
类型 switch 中的 fallthrough
类型 switch 也可以使用 fallthrough。此示例演示了它在不同类型下的行为。
package main
import "fmt"
func main() {
var val interface{} = 3.14
switch val.(type) {
case int:
fmt.Println("Integer")
case float64:
fmt.Println("Float64")
fallthrough
case float32:
fmt.Println("Float32")
default:
fmt.Println("Unknown type")
}
}
当 val 为 float64 时,它打印 "Float64" 并贯穿到打印 "Float32"。由于 fallthrough,未执行 float32 的类型检查。
带空 case 的 fallthrough
空 case 可以使用 fallthrough 在 case 之间共享代码。此示例展示了一个实际用例。
package main
import "fmt"
func main() {
month := "Feb"
switch month {
case "Jan", "Feb", "Mar":
fmt.Println("Q1")
fallthrough
case "Apr", "May", "Jun":
fmt.Println("Q2")
case "Jul", "Aug", "Sep":
fmt.Println("Q3")
case "Oct", "Nov", "Dec":
fmt.Println("Q4")
}
}
对于第一季度的月份,它打印 "Q1" 并贯穿到打印 "Q2"。这展示了如何处理分层分类。
Fallthrough 与无 fallthrough 的对比
此示例对比了有和没有 fallthrough 的 switch 行为,以突出其差异。
package main
import "fmt"
func main() {
fmt.Println("With fallthrough:")
value := 1
switch value {
case 1:
fmt.Println("Case 1")
fallthrough
case 2:
fmt.Println("Case 2")
}
fmt.Println("\nWithout fallthrough:")
value = 1
switch value {
case 1:
fmt.Println("Case 1")
case 2:
fmt.Println("Case 2")
}
}
第一个 switch 由于 fallthrough 打印了两个 case。第二个只打印匹配的 case。这演示了 Go 默认不 fallthrough 的行为。
实际示例:日期分类
此实际示例使用 fallthrough 对日期进行分类,同时保持类别之间的分层关系。
package main
import "fmt"
func main() {
day := "Tuesday"
switch day {
case "Monday":
fmt.Println("Start of work week")
fallthrough
case "Tuesday", "Wednesday", "Thursday":
fmt.Println("Midweek day")
fallthrough
case "Friday":
fmt.Println("Weekday")
case "Saturday", "Sunday":
fmt.Println("Weekend")
default:
fmt.Println("Invalid day")
}
}
对于星期二,由于 fallthrough,它打印了所有三个类别。这展示了如何以清晰的方式实现分层分类。
来源
本教程通过在各种场景下 switch 控制的实际示例,涵盖了 Go 中的 fallthrough 关键字。