switch, case, fallthrough, and default

The switch statement allows you to branch execution based on the state of a variable. It is similar to the switch statement in C and other languages.

There is no fallthrough by default. This means once the end of a case is reached, the code exits the switch statement completely unless an explicit fallthrough command is provided. A default case can be provided if none of the cases are matched.

You can put a statement in front of the variable to be switched, such as the if statement. This creates a variable whose scope is limited to the switch statement.

This example demonstrates two switch statements. The first one uses hardcoded values and includes a default case. The second switch statement uses an ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.