go的fallthrough玩一下

版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84678871

     遇到了,看一下:

package main

import "fmt"

func main() {
    b := false

    switch b {
    case false:
            fmt.Println("1")
            fallthrough
    case true:
            fmt.Println("2")
            fallthrough
    case false:
            fmt.Println("3")
            fallthrough
    case true:
            fmt.Println("4")
    case false:
            fmt.Println("5")
            fallthrough
    default:
            fmt.Println("default case")
    }
}

         结果:

1
2
3
4

        不过b为true, 结果为:

2
3
4

       没什么好说的。

猜你喜欢

转载自blog.csdn.net/stpeace/article/details/84678871