Explain Switch Statement in Go Programming Language | Go Programming Tutorial
In this article, you will learn:
- What is a Switch Statement in Go?
- Types of Switch Statements
What is a Switch Statement in Go Programming Language?
It is a multiway branch statement. It has some conditional statements in the switch statement, whatever branch satisfies the condition will execute in the switch statement.
Types of Switch Statements
There are two types of switch statements:
- Expression Switch
- Type Switch
Expression Switch:
It is so similar to switch statements in C, C++, and Java Language. It evaluates the value of the expression and executes the different parts of the code.
SYNTAX:
switch optstatement; optexpression {
case expression1: //Set of statements
case expression2: //Set of statements
..
default: //set of statements
}
Points To Be Remembered:
- Both optstatement and optexpression is the statement or expression that is optional.
- If both optstatement and optexpression are present, then a semicolon is required between them.
- If the switch statement does not have any expression, then the compiler itself assumes that the written expression is true.
- The optional statement contains only simple statements like variable declarations, function calls, etc.
- If the variable is present in the optional statement, then the scope of the variable is limited only to the switch statement.
- The switch statement has a default statement that does not contain any break statement.
- The default statement is just an optional statement in the switch statement.
- The multiple values are separated by a comma in a case.
- If a case does not have any expression, then the compiler assumes that the expression is true.
Type Switch:
Types switch is used for the comparison. The case contains the type which is going to compare with the data type present in the switch statement.
SYNTAX:
Switch optstatement; typeswitchexpression {
case type list 1: // set of statements
case type list 2: // set of statements
..
default: // set of statements
}
Points to be remembered:
- The optional statement works similarly as it works in the switch statement.
- If a case contains various values and these values are separated by a comma(,).
- The default statement in the switch statement does not contain any break statement.
- The default statement is optional in the type switch statement.


