Go Switch Statement

In this tutorial you will learn about the Go Switch Statement and its application with practical example.

Go Switch Statement

The switch statement is simplified form of the nested if … else if statement , it avoids long chain of if..else if..else statements. A switch statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Syntax:-

Example:-

Output:-

go_switch_statement_1

In Go, you can have multiple values in a single case statement separated with comma (,) as following –

Example :-

Output:-

go_switch_statement_2

Go Switch fallthrough

In switch statement a fallthrough statement allows to execute all the following statements after a match found.

Example:-

Output:-

go_switch_fallthrough

Go Switch with a short statement

In Go, a switch statement can also contain a short declaration statement like the If statement before the conditional expression as following –

Example:-

Output:-

go_switch_with_short_statement

Go switch with no expression

Syntax:-

Example:-

Output:-

go_switch_no_expression

Note:- In Go, a break statement can be used to inside your matched case to exit the switch statement.

In this tutorial we have learn about the Go Switch Statement and its application with practical example. I hope you will like this tutorial.