Gaaker Go

Golang Fallthrough

If you want to continue to execute subsequent branches after executing a branch, you can use the fallthrough keyword to achieve.

fallthrough needs to be the last statement in the case branch. Otherwise the compiler will throw an error: fallthrough statement out of place.

The following is a complete code example.

package main

import "fmt"

func main() {
    switch i := 45; {
    case i < 10:
        fmt.Printf("%d is less than 10\n", i)
        fallthrough
    case i < 50:
        fmt.Printf("%d is less than 50\n", i)
        fallthrough
    case i < 100:
        fmt.Printf("%d is less than 100\n", i)
    }
}

Below is the program output:

45 is less than 50
45 is less than 100
< Fallthrough Fallthrough >