Updated April 12, 2023
Introduction to Golang Switch
Switch case in go language allow us to deal with multiple conditional code execution, as in many situations it can either execute one code or another on the basis of the multiple codes, but with the help of the switch case statement we can execute many cases, here switch is a keyword in go language which take the input value and compare that value with various cases and in case any case match with value statement belongs to that case will be executed, in case of non of the case match with the value of switch, in that case, it has default option where we can write something to execute in case none of the case matches.
Syntax of Golang Switch
In the below we have written a very simple syntax for the switch case in the go language, we can explain each attribute of the below syntax,
- switch: Switch is a keyword which takes parameters which will be compared for each case.
- output-statement: This is the value that will be used for each case of the switch case to compare and match the case statement will execute.
- output-expression: This is the statement value we are assigning to the output-statement.
- case: This is the case that will compare with each output-statement value which we assigned and match aces statement will execute.
- default: In case none of the cases match with the output-statement then whatever written as the statement, in this case, will execute.
switch output-statement; output-expression{
case condition 1: Statement 1
case condition 2: Statement 2
case condition 2: Statement 3
...
default: Statement N
}
Flowchart of Golang Switch
In the below flow diagram we can see the basic flow of the switch cases in the go languages. We can explain the diagram below in a few steps, the steps are given below.
- Switch case starts from statement condition 1, her condition will be any value which we are comparing or some string and numeric value.
- If condition 1 matches then it will go for the execution of statement 1 and condition 2 matches then it will go execution of statement 2 and so on it will check for each condition.
- At the end of the switch case finally, the end of the statement will happen and the next statement of the switch case will execute.
Please see the below flowchart to understand go language switch case.
How Switch Statements Works in Go language?
We can explain the working of the switch case in the go language with below steps:
- Every switch case will get a value first, this value can be anything either any string, integer, or any boolean value.
- Then inside the switch statement, there will be a case, these cases will be compared with the value passed to the switch and in case anyone will match with the value, the statement written inside that case will be executed.
- Because of the switch case, we can write multiple conditions for executions, and handling is also very easy.
- Finally, we have a default case that will execute in case none of the cases matches the value passed to the switch at the time of initial.
Examples of Golang Switch
To execute the below code we can make a file with name switch.go and copy paste the code given in the example and run the command go run switch.go.
Example #1
Below is a very simple example where we are checking days by passing a numeric value to the switch case, like we passed 3 and case which match with the three statements of that case will execute.
Code:
package main
import "fmt"
func main() {
var Day =3;
switch d:=Day; d{
case 1:
fmt.Println("Today day is Monday")
case 2:
fmt.Println("Today day is Tuesday")
case 3:
fmt.Println("Today day is Wednesday")
case 4:
fmt.Println("Today day is Thursday")
case 5:
fmt.Println("Today day is Friday")
case 6:
fmt.Println("Today day is Saturday")
case 7:
fmt.Println("Today day is Sunday")
default:
fmt.Println("The enter number for day is Invalid")
}
}
Output:
Example #2
Below is another example where we are getting marks of students and giving output as the grade of the student according to the match case, so we passed 50 and the case which matches for 50 will be the output. In case none of the case match output will be from default case.
Code:
package main
import "fmt"
func main() {
var marks int = 56
switch {
case marks >= 50:
fmt.Println("You got more than 50 so your grade is C")
case marks >= 70:
fmt.Println("You got more than 70 so your grade is B")
case marks >= 90:
fmt.Println("You got more than 90 so your grade is A")
default:
fmt.Println("The marks which enter is not valid marks")
}
}
Output:
Example #3
Below is an example where we are showing which language we are going to learn today on the basis of the value passed to the switch case, here in the below example we have passed seven and output will be default one.
Code:
package main
import "fmt"
func main() {
var language string = "seven"
switch language {
case "one":
fmt.Println("Today we are going to learn Javascript language")
case "two":
fmt.Println("Today we are going to learn Javascript language")
case "three":
fmt.Println("Today we are going to learn Javascript language")
case "four":
fmt.Println("Today we are going to learn Javascript language")
case "five":
fmt.Println("Today we are going to learn Javascript language")
case "six":
fmt.Println("Today we are going to learn Javascript language")
default:
fmt.Println("Today we are going to learn go language")
}
}
Output:
Conclusion
From this tutorial we learned the basic concept of the switch case of the go language, we understand working of the switch case with example and with the flow chart. We focus on the simple syntax of the switch case in the go language.
Recommended Article
This is a guide to Golang Switch. Here we discuss the introduction, syntax, flowchart, and working of Switch in Golang along with different examples and code implementation. You can also go through our other suggested articles to learn more –