Anatomy of Conditional Statements and Loops in Go
By: Uday Hiwarale
Tags: Golang, Go Tutorial, Golang Tutorial, Go, Programming

The switch conditional statement

Tswitch statement is generally used to replace multiple if-else conditions.

The syntax of the switch statement

The switch statement takes one input value and matches against many cases. Unlike C , Go doesn't need a break statement to terminate the case block. case block will terminate as soon as the last statement in the case block executes and so is the switch statement. Duplicate cases (values) are not allowed in Go.

switch input {

case value_1:

...

case value_1:

...

}

Let’s look at a simple example of finding the finger name from the finger index.

In the above program, we are passing finger variable as an input value to switch block. Then switch try to find case block which matches that value. In the above program, since finger == 2 , case 2 will be executed and logs the Index message in the terminal.

If finger value is not between 1-5, nothing will get printed.

The default case

Sometimes, we need else kind of condition where we need to execute some piece of code when none of the cases match the input value. In that case, default block is used.

In the above program, since the input finger value is 6 and none of the cases matches that value, the default block will be executed printing No fingers matched on the screen.

The default case does not have to be the last case. It can be anywhere in the switch block.

Multiple case values

You can present multiple ( , ) comma-separated case values to match the input switch value.

The above program will print Letter is a vovel since variable letter has value i which belongs to case values "a", "e", "i", "o", "u" .

The initial statement

There is another variant of switch statement where you can add initial statement before the input value just like if conditional statement.

switch statement; input {

...

}

Let’s see the previous example where in this case, we will define letter variable in the switch statement itself, restricting its scope to switch block.

Expressionless switch statement

Input value in Go’s switch statement is optional. When there is no input value to switch statement, case will be an expression that returns a boolean value.

Since number is greater than 20, number > 5 condition will meet and number is greater than 5 will be printed to the console.

We can use the initial statement without input value of switch statement. In the above program, we could initialize number variable in switch statement.

A semicolon is necessary after the initial statement because if we don’t add a semicolon, then our switch statement is no longer expressionless and number := getnumber() becomes the input value expression which is invalid.

The fallthrough statement

As normally, when a case block executes, no other cases are tried. In the above program, the number is greater than 5, 10 and 15 but since number > 5 and first case meets that condition and only that case block is executed. To execute another case blocks, we use fallthrough statement.

When a case condition is evaluated and returns true or matches the input value, that case block is executed. If the compiler finds fallthrough statement, the next case block is executed without even evaluating the case.

The fallthrough should be the last statement in a case block, else Go compiler will throw fallthrough statement out of place error.