Switch Statement in Go

Learn via video courses
Topics Covered

Overview

A switch statement is generally a multi-way branch statement that provides an efficient way for transferring the execution to different parts of code depending on the value of the expression.

Introduction

Switch evaluates an expression and compares it with a list of possible matches and executes blocks of code according to the match.

Important points:

  • Duplicate cases with the same constant value are not allowed.
  • It is possible to include multiple expressions in the case by separating them with commas.
  • The expression is optional (can be omitted) and in such case, each of the case expressions is evaluated for truth and the corresponding block of code is executed.
  • Fallthrough transfers the control to the first statement of the next case.

Note: fallthrough should be the last statement in a case.

Switch Statement Flow

switch-statement-flow

In this flow chart, when an expression is evaluated, it checks the first case, if it matches then the statement is executed else it moves on to case 2. Here it again checks if the case matches the expression, if it does then the statement is executed, else it goes on to the next statement and this process goes on for all the cases. If the expression doesn’t match any of the cases then the default statement is executed.

Syntax

The syntax is:

Note: No need for a writing break. Fallthrough should be the last statement of the case. The expression written after the switch statement is evaluated and if the result of the expression is equal to case value_1 then the statement of code block 1 is executed, similarly for case value_2, the statement of code block 2 is executed, and so on.

Example

Program to print the day of the week using switch case

Output:

Explanation: In this example, the package is imported and the main function is initialized. In this, the day of the week is declared as number 4. This is passed to a switch block which is matched with each case of the switch block. Here since the number is 4, it matches with case 4, and the statement is executed which prints Wednesday.

Conditional Switch Case

Go Switch Cases by Using Fallthrough

Fallthrough is used when we have to execute another case after matching a case. To understand this better, let's take an example.

Program to print the day of the week using fallthrough in switch

Output:

Explanation: In this example, the expression matches with case 3 and it prints Tuesday but Wednesday is also printed even though the case doesn’t match because of fallthrough which is mentioned inside case 3.

Go Program by Using Multiple Cases

Inside a single case block, multiple statements can be used for evaluating the expression. The case block is executed if it matches any one value from the case value.

Program to check if the day is a weekend or a weekday

Output:

Explanation: In this example, multiple values are used for each case. The first case executes if the dayOfWeek is either Saturday or Sunday whereas case 2 is executed if the value of dayOfWeek matches with the options like Monday, Tuesday, Wednesday, Thursday, and Friday. If no case is matched against the expression then the default case is executed.

Optional Statement

Optional statements in Golang can be used with expression by separating them through semi-colons. Program to check the day of the week using an optional statement

Output:

Explanation: In this example, an optional statement which is day:= 2 is used along with an expression day. Since the day matches with case 2, hence Monday is printed.

Type Switch

There are two types of switch statements that Golang supports:

  • Expression switch
  • Type switch

Expression switch

It is similar to the switch statement present in C/C++ and Java language. It provides an easy method to dispatch the execution to different parts of code in Golang based on the value of the expression.

The syntax of this is as follows:

Example: Go program to illustrate the concept of an Expression switch statement

Output:

Explanation: In this, both opexpression and opstatement are optional and if both are present, then a semi-colon is required between them. If there is no expression in the switch then it is assume as true by the compiler. If there are multiple values in the case then they are separated by comma and the default statement in switch is optional.

Type switch

It is used for comparing the types. In tis switch, the case contains type and matches it with the type of expression. The syntax for this is as follows:

Example: Go program to illustrate the concept of Type switch statement

Output:

Explanation: If an expression in Golang is assigned using this operator :=, then the type of that variable is dependen on that clause.

Break keyword

The break statement in switch is used for two main reasons: When a break is encountered in the switch, the loop is terminated immediately and the control resumes back to the next statement in the loop. It is also used for terminating a case in a switch statement. The break isn’t needed in Golang because once a case is evaluated, the control goes out of the loop.

Conclusion

  • A switch statement is generally a multi-way branch statement that provides an efficient way for transferring the execution to different parts of code depending on the value of the expression.
  • The syntax is:
  • The break isn’t needed in Golang because once a case is evaluated, the control goes out of the loop.