Apr 202020

ADM

Switch case statements in Golang

  • 20 April 2020
  • ADM

 

Switch case statements in Golang - images/logos/golang.jpg

 

The switch statement is an easier way to have a sequence of if-else statements. In Golang you don't need to have break at the end of each case, it breaks implicitly. Also the golang switch statement work on values of any type.

 

Golang switch case statement

The switch statement is used to select and execute one out of many blocks of code.

package main

import "fmt"

func switchStringTest(str string) {
	switch str {
	case "test1":
		fmt.Println("This is switch case 1.")
	case "test2":
		fmt.Println("This is switch case 2.")
	case "test3":
		fmt.Println("This is switch case 3.")
	case "test4":
		fmt.Println("This is switch case 4.")
	default:
		fmt.Println("This is the default case.")
	}
}

func main() {
	switchStringTest("test1")
	switchStringTest("test2")
	switchStringTest("test3")
	switchStringTest("test4")
	switchStringTest("test")
}

The default case is used if there is no case matched.

Output

This is switch case 1.
This is switch case 2.
This is switch case 3.
This is switch case 4.
This is the default case.

 

Golang switch multiple case statement

A switch with multiple case statements is useful when the same code need to be executed for multiple conditions.

package main

import "fmt"

func switchStringTest(str string) {
	switch str {
	case "test1", "test2":
		fmt.Println("This is switch case 1 and 2.")
	case "test3", "test4":
		fmt.Println("This is switch case 3 and 4.")
	default:
		fmt.Println("This is the default case.")
	}
}

func main() {
	switchStringTest("test1")
	switchStringTest("test2")
	switchStringTest("test3")
	switchStringTest("test4")
	switchStringTest("test")
}

Output

This is switch case 1 and 2.
This is switch case 1 and 2.
This is switch case 3 and 4.
This is switch case 3 and 4.
This is the default case.

Note: As you can see first two calls of the switchStringTest goes to the same case (the first) and the last two call goes to the same case (the second).

 

Golang switch fallthrough case statement

The fallthrough keyword is used to force the execution of the code to fall through the next case block.

package main

import "fmt"

func switchStringTest(str string) {
	switch str {
	case "test1":
		fmt.Println("This is switch case 1.")
		fallthrough
	case "test2":
		fmt.Println("This is switch case 2.")
		fallthrough
	case "test3":
		fmt.Println("This is switch case 3.")
		fallthrough
	case "test4":
		fmt.Println("This is switch case 4.")
		fallthrough
	default:
		fmt.Println("This is the default case.")
	}
}

func main() {
	switchStringTest("test1")
	switchStringTest("test2")
	switchStringTest("test3")
	switchStringTest("test4")
	switchStringTest("test")
}

Output

This is switch case 1.
This is switch case 2.
This is switch case 3.
This is switch case 4.
This is the default case.
This is switch case 2.
This is switch case 3.
This is switch case 4.
This is the default case.
This is switch case 3.
This is switch case 4.
This is the default case.
This is switch case 4.
This is the default case.
This is the default case.

Note: As you can see for each calls of the switchStringTest method, the cases from the entry case to default case are executed.

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

References