![]() |
VOOZH | about |
A switch is a multi-way branch statement used in place of multiple if-else statements but can also be used to find out the dynamic type of an interface variable.
A type switch is a construct that performs multiple type assertions to determine the type of variable (rather than values) and runs the first matching switch case of the specified type. It is used when we do not know what the interface{} type could be.
Example 1:
Output:
Type is a string: GeeksforGeeks
The switch can have multiple value cases for different types and is used to select a common block of code for many similar cases.
Note: Golang does not needs a 'break' keyword at the end of each case in the switch.
Example 2:
Output:
Type is either a string or a bool: GeeksforGeeks Type is a number, either an int or a float: 34.55