ZetCode

Golang fallthrough keyword

last modified May 7, 2025

This tutorial explains how to use the fallthrough keyword in Go. We'll cover switch statement control with practical examples of fallthrough.

The fallthrough statement transfers control to the next case clause in a switch statement. It's a unique feature in Go's switch control flow.

In Go, fallthrough must be the last statement in a case clause. Unlike other languages, Go's switch doesn't automatically fall through cases.

Basic fallthrough example

This example demonstrates the simplest use of fallthrough in a switch statement. It shows how execution flows to the next case.

basic_fallthrough.go
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")
    }
}

When num is 2, it prints "Two" and then falls through to print "Three". Without fallthrough, only "Two" would be printed.

Multiple fallthroughs

You can chain multiple fallthrough statements to continue execution through several case clauses. This example shows the behavior.

multi_fallthrough.go
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")
    }
}

Starting at case 'B', execution continues through all subsequent cases due to the fallthrough statements. Each case's code is executed in sequence.

Fallthrough with expression cases

fallthrough works with expression cases too. This example shows how it behaves with non-constant expressions.

expression_fallthrough.go
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")
    }
}

The first case matches and executes, then falls through to the second case. Note that the second case's condition isn't evaluated due to fallthrough.

Fallthrough in type switches

Type switches can also use fallthrough. This example demonstrates its behavior with different types.

type_fallthrough.go
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")
    }
}

When val is float64, it prints "Float64" and falls through to print "Float32". The type check for float32 isn't performed due to fallthrough.

Fallthrough with empty cases

Empty cases can use fallthrough to share code between cases. This example shows a practical use case.

empty_case_fallthrough.go
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")
    }
}

For Q1 months, it prints "Q1" and falls through to print "Q2". This shows how to handle hierarchical categorization.

Fallthrough vs no fallthrough

This example contrasts switch behavior with and without fallthrough to highlight the difference.

compare_fallthrough.go
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")
    }
}

The first switch prints both cases due to fallthrough. The second only prints the matching case. This demonstrates Go's default no-fallthrough behavior.

Practical example: Days categorization

This practical example uses fallthrough to categorize days while maintaining hierarchical relationships between categories.

days_fallthrough.go
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")
    }
}

For Tuesday, it prints all three categories due to fallthrough. This shows how to implement hierarchical classification in a clean way.

Source

Go language specification

This tutorial covered the fallthrough keyword in Go with practical examples of switch control in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.