我想知道为什么在golang中的类型switch语句中不允许使用fallthrough.
根据规范:"类型切换中不允许使用"fallthrough"语句.",这并没有解释为什么不允许这样做.
附加的代码是模拟一个可能的场景,类型转换语句中的一个漏洞可能是有用的.
注意!这段代码不起作用,会产生错误:"无法通过类型切换".我只是想知道在类型切换中不允许使用fallthrough语句可能有什么原因.
//A type switch question
package main
import "fmt"
//Why isn't fallthrough in type switch allowed?
func main() {
//Empty interface
var x interface{}
x = //A int, float64, bool or string value
switch i := x.(type) {
case int:
fmt.Println(i + 1)
case float64:
fmt.Println(i + 2.0)
case bool:
fallthrough
case string:
fmt.Printf("%v", i)
default:
fmt.Println("Unknown type. Sorry!")
}
}
Run Code Online (Sandbox Code Playgroud)