SoFunction
Updated on 2025-03-05

Introduction to the flexible writing method of golang switch statement

Switch is easy to understand. Let's start with a code and run it

See what your operating system is

package main 
import (
    "fmt"
    "runtime"
)
 
func main() {
    ("Go runs on ")
    switch os := ; os {
    case "darwin":
        ("OS X.")
    case "linux":
        ("Linux.")
    default:
        ("%s", os)
    }
}

Runtine runtime gets the current operating system, using GOOS. Like habits like if for, you can declare assignment variables before. We are here to get the operating system information.

os := ;

The case in {} is easier to understand. If the operating system is "darwin", print "OS X."; if the operating system is "linux", print "Linux"; the others are directly printed in the system category.

In the switch in the go language language, the branch will automatically terminate unless it ends with a fallthrough statement.

So modify the above code and run it:

package main 
import (
    "fmt"
    "runtime"
)
 
func main() {
    ("Go runs on ")
    switch os := ; os {
    case "darwin":
        ("OS X.")
    case "linux":
        ("Linux.")
    case "windows":
        ("win")
        fallthrough
    default:
        ("%s", os)
    }
}

The case option "windows" of the current system has been added, and fallghrough is also used in this branch.

If you comment out fallthrough, or simply delete fallthrough, and run it, you will find that the penetration effect is gone.

Summarize

The condition of the switch is executed from top to bottom and stops when the match is successful. If the successful match ends with fallthrough, the next next adjacent branch will also be executed.

There is no conditional usage of switch. This is actually equivalent to switch true. Each case option is a bool expression, and a branch with a value of true will be executed, or a default will be executed.

package main 
import (
    "fmt"
    "time"
)
 
func main() {
    t := ()
    switch  {
    case () > 12:
        ("Morning was passed.")
    case () > 17:
        ("Afternoon was passed.")
    default:
        ("Now too early.")
 
    }
}

Supplement: Remember a "pit" in Golang switch

Switch branch statements are commonly used in various programming languages. They can select one or more case statements according to the conditions for execution. Those who are accustomed to programming in C and C++ know that in C and C++, the switch branch judgment conditions will traverse each case in sequence. When a case that meets the conditions is encountered, the statement in the case will be executed. The switch process will not be terminated until the break statement displayed in the case is encountered. If this case is not displayed in a case, the program will execute the next case (if it exists).

For programmers who have just transferred from C and C++ to Go, there is a "pit" here. I have also stepped on this "pit" during the development process. That is, Go will automatically add a break statement to each case of the switch branch statement. That is to say, in Go, the program enters a case process. Regardless of whether there is a break case displayed in the program, the program will not continue to execute other case processes, but will directly exit the entire switch process. Here is a simple experiment.

Add a break statement to each case as shown:

package main
import "fmt"
func main() {
    var num = 10
    switch num {
    case 5:
        ("num is 5")
        break
    case 10:
        ("num is 10")
        break
    case 15:
        ("num is 15")
        break
    default:
        ("num is default branch")
    }
    return
}

The running results of the program are as follows:

num is 10

Let’s remove the break statement displayed in each case to see what the result is.

package main
import "fmt"
func main() {
    var num = 10
    switch num {
    case 5:
        ("num is 5")
    case 10:
        ("num is 10")
    case 15:
        ("num is 15")
    default:
        ("num is default branch")
    }
    return
}

The running results of the program are as follows:

num is 10

It is obvious that whether the added break statement displayed in each case statement will only execute one of the branch processes, which is indeed a small "pit" for programmers who want to control the number of cases executed each time through break.

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.