Even though we have if...else construct in Go, there may be situation when we have to check so many conditions and even we may want to execute two if blocks than just one. switch-case statements provide this kind of flexibility and are more readable when we have to write multiple if blocks.

Common Switch Case Statement

Switch..case statements provide multi-way control flow. This also has a default block which is executed if none of the case conditions match. The syntax for this looks like below.

1switch expression {
2    case value1:
3        // execute block
4    case value2:
5        // block 2
6    default:
7        // default block if no match found
8}

This is a typical switch...case block syntax. Let’s see an example of this.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// normal switch case
 7	var month string = "March"
 8	switch month {
 9	case "January":
10		fmt.Println("This is January")
11	case "February", "March": // check for multiple values
12		fmt.Println("This is feb or March")
13	default:
14		fmt.Println("This is neither first or second month of the year.")
15	}
16}

In this code, we are checking for the month variable in switch statement. As you can see on line 11, we can also compare multiple values in a single case statement.

Output:

This is feb or March

Switch Case with Fallthrough

Unlike other programming languages, in Go, the switch statement does not fallthrough by default. That is we do not have to write break statement after each case block. If we want this behavior to change, then we have to explicitly write fallthrough keyword at the end of the case block.

Interestingly, we can specify this fallthrough keyword only in some of the case block and have pretty complex control flow with switch..case.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// use of fallthrough will move control to next case block
 7	var month = "February"
 8	switch month {
 9	case "January":
10		fmt.Println("This is January")
11	case "February", "March": // check for multiple values
12		fmt.Println("This is feb or March")
13		fallthrough
14	default:
15		fmt.Println("This is default block.")
16	}
17}

In above code block, we have used fallthrough keyword in the second case block. This means when this condition matches (i.e. when month variable is either February or March), it will fall through and check for other conditions which matches. In this case, no other conditions after second case block matches, so it will execute the default block statements.

Output:

This is feb or March
This is default block.

Switch Case as Conditional

We can also omit the expression in the switch block and actually use the variable for verifying a condition in the case block. This will behave like an if..else structure.

1switch {
2    case condition1:
3        // statements
4    case condition2:
5        // statements 2
6    default:
7        // default statements
8}

In below example, we are checking multiple conditions with a variable num. If the number is greater than 100 or 50 or 10, it will print corresponding message from the case block. One thing to note here is that this variable is also accessible inside the case or default statement, as we can see on line 16.

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// using switch case as if..else
 7	var num int = 9
 8	switch {
 9	case num > 100:
10		fmt.Println("The number is greater than 100")
11	case num > 50:
12		fmt.Println("The number is greater than 50")
13	case num > 10:
14		fmt.Println("The number is greater than 10")
15	default:
16		fmt.Println("The number is", num)
17	}
18}

Output:

The number is 9

Conclusion

In this lesson, we looked at various ways in which switch...case statement may be used. This can be used as a conditional statement or it can also fallthrough and execute multiple matching statements.