Javatpoint Logo
Javatpoint Logo

Go switch

The Go switch statement executes one statement from multiple conditions. It is similar to if-else-if chain statement.

Syntax:

The switch statement in Go is more flexible. In the above syntax, var1 is a variable which can be of any type, and val1, val2, ... are possible values of var1.

In switch statement, more than one values can be tested in a case, the values are presented in a comma separated list

like: case val1, val2, val3:

If any case is matched, the corresponding case statement is executed. Here, the break keyword is implicit. So automatic fall-through is not the default behavior in Go switch statement.

For fall-through in Go switch statement, use the keyword "fallthrough" at the end of the branch.

Go Switch Example:

Output:

Enter Number: 20
the value is 20

or

Output:

Enter Number: 35
 It is not 10,20,30,40

Go switch fallthrough example

Output:

was <= 30
was <= 40
was <= 50
was <= 60
default case

Next TopicGo For




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA