Golang Switch Statements (A Complete Guide)
What is Switch Statement in Go?
The switch statement in Go is a control flow statement that is used to execute a block of code among many alternatives. Switch statements are declared using the switch
keyword followed by the condition to be evaluated. The condition can be a variable, constant, or expression.
The switch
statement is similar to the if-else
statement. The difference is that the switch
statement can have multiple cases. The switch
statement is also known as the switch-case
statement.
Syntax of Switch Statement in Go
The syntax of the switch
statement in Go is as follows:
The switch
statement is followed by the case
keyword. The case
keyword is followed by the value to be compared with the condition. The case
keyword is followed by the code block to be executed if the condition is equal to the value. The default
keyword is used to execute a block of code if none of the cases is true.
Example of Switch Statement in Go
The following example shows the use of the switch
statement in Go:
The output of the above program is:
Switch Statement with Multiple Cases
The switch
statement can have multiple cases, you can have as many cases as you want. The following example shows the use of the switch
statement with multiple cases:
The output of the above program is:
Switch Statement with Fallthrough
Fallthrough is a keyword that is used to execute the next case in a switch
statement. The fallthrough
keyword is used to execute the next case even if the condition is not true. The following example shows the use of the fallthrough
keyword:
The output of the above program is:
In the above example, case 10 is executed and then the fallthrough
keyword is used to execute the next case which is case 20.
golang switch case break
The break
keyword is used to terminate the switch
statement when a given condition is true.
The following example shows the use of the break
keyword:
The output of the above program is:
Nothing is printed because the switch statement is terminated after the break
keyword.
golang switch channel
A channel is a communication mechanism that allows one goroutine to pass values of a specified type to another goroutine.
The following example shows the use of the switch
statement with a channel:
The output of the above program is:
In the above example, the select
statement is used to receive the values from the channel. The select
statement is used to receive the values from the channel. The select
statement is used to receive the values from the channel.
Switch Statement with Multiple Conditions
The switch
statement can have multiple conditions using the ||
, &&
, and !
operators. The following example shows the use of the switch
statement with multiple conditions:
The output of the above program is:
In the above example, the switch
statement is used to check if the num
variable is equal to 10 and the x
variable is equal to 20.
Switch Statement with Type Switch
The switch
statement can be used to check the type of a variable using the type
keyword. By combining type
and :=
we can create a type switch guard . The following example shows the use of the switch
statement with the type
keyword:
The output of the above program is:
if we change the value of the x
variable to 10
the output will be:
Tl;dr
- The
switch
statement is used to execute a block of code based on the value of a variable. - The
switch
statement can have multiple cases, you can have as many cases as you want. - The
switch
statement can have multiple conditions using the||
,&&
, and!
operators. - The
switch
statement can be used to check the type of a variable using thetype
keyword.