Switch Statement is another way of controlling the flow in GoLang. It is pretty much similar to if
statements but both have their advantages and disadvantages depending on your use case. Below is a simple switch structure:
switch {
case condition-1:
// Code block 1 - Executed if condition-1 is true
case condition-2:
// Code block 2 - Executed if condition-2 is true
case condition-3:
// Code block 3 - Executed if condition-3 is true
default:
// Executed if all other cases are false
}
Switch Statement in GoLang follows different rules in comparison to other programming languages. So, let's take a look at the following examples to understand these rules.
Example 1:
func exampleOne() {
fmt.Println(" ----- Example One: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
case 2 == 2:
fmt.Println("2 is equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
Result:
----- Example One: -----
1 is equal to 1
Here as you can see, we didn't even need to define a variable for switch
to evaluate. The first case statement that becomes true will executed.
Interesting fact is that even if the next case is also true
, it is not evaluated anymore. Here, it only evaluates the first true
case. In other languages, we also need to have break
for every case block, otherwise, the next cases (even if false) will be executed. In GoLang, break
is not needed as it will only execute the code block within the first case that is true
.
Example 2:
func exampleTwo() {
fmt.Println(" ----- Example Two: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
fallthrough
case 2 == 2:
fmt.Println("2 is equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
Result:
----- Example Two: -----
1 is equal to 1
2 is equal to 2
In case we want the next case block to be executed as well, we need to add fallthrough
keyword.
Another interesting fact is that even if the next case evaluates to false
it will still be executed.
Example 3:
func exampleThree() {
fmt.Println(" ----- Example Three: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
fallthrough
case 1 == 2:
fmt.Println("1 is not equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
Result:
----- Example Three: -----
1 is equal to 1
1 is not equal to 2
As mentioned in previous example, even if the next case evaluates to false
, as long as the previous case is executed and has fallthrough
keyword, it will still be executed.
Example 4
func exampleFour() {
fmt.Println(" ----- Example Four: -----")
switch {
case 1 == 2:
fmt.Println("1 is not equal to 2")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
Result:
----- Example Four: -----
Reached default
default
keyword works the same way as other programming languages. If none of the previous cases are true
, default
block will be executed.
Example 5:
func exampleFive() {
fmt.Println(" ----- Example Five: -----")
name := "Darwin"
switch name {
case "Darwin":
fmt.Println("Name is Darwin")
case "Test":
fmt.Println("Name is Test")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
Result:
----- Example Five: -----
Name is Darwin
In this example we have a name to be evaluated in the switch
line. This checks which case has the same value as the name
and executes that code block.
Example 6:
func exampleSix() {
fmt.Println(" ----- Example Six: -----")
name := "Yehey"
switch name {
case "Darwin", "Mark", "Yehey":
fmt.Println("Name is Darwin")
case "Test":
fmt.Println("Name is Test")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
Result:
----- Example Six: -----
Name is Darwin
Our case
can have multiple possible options. From what you can see above, if the name
is equal to "Darwin", "Mark", or "Yehey", the case block will be executed.
Complete Code:
package main
import "fmt"
func main() {
exampleOne()
exampleTwo()
exampleThree()
exampleFour()
exampleFive()
exampleSix()
}
func exampleOne() {
fmt.Println(" ----- Example One: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
case 2 == 2:
fmt.Println("2 is equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
func exampleTwo() {
fmt.Println(" ----- Example Two: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
fallthrough
case 2 == 2:
fmt.Println("2 is equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
func exampleThree() {
fmt.Println(" ----- Example Three: -----")
switch {
case 1 == 1:
fmt.Println("1 is equal to 1")
fallthrough
case 1 == 2:
fmt.Println("1 is not equal to 2")
case 3 == 3:
fmt.Println("3 is equal to 3")
case 2 == 3:
fmt.Println("2 is not equal to 3")
}
fmt.Println()
}
func exampleFour() {
fmt.Println(" ----- Example Four: -----")
switch {
case 1 == 2:
fmt.Println("1 is not equal to 2")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
func exampleFive() {
fmt.Println(" ----- Example Five: -----")
name := "Darwin"
switch name {
case "Darwin":
fmt.Println("Name is Darwin")
case "Test":
fmt.Println("Name is Test")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
func exampleSix() {
fmt.Println(" ----- Example Six: -----")
name := "Yehey"
switch name {
case "Darwin", "Mark", "Yehey":
fmt.Println("Name is Darwin")
case "Test":
fmt.Println("Name is Test")
default:
fmt.Println("Reached default")
}
fmt.Println()
}
Result:
----- Example One: -----
1 is equal to 1
----- Example Two: -----
1 is equal to 1
2 is equal to 2
----- Example Three: -----
1 is equal to 1
1 is not equal to 2
----- Example Four: -----
Reached default
----- Example Five: -----
Name is Darwin
----- Example Six: -----
Name is Darwin