Gopher

Switch statements in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

Switch statement is one of the control flow statements in Golang. Switch statement helps you to write concise code. Switch statement can be used for replacing multiple if else statements or if else ladder with one switch statement.

Golang only runs the selected case statement that is matched and does not execute remaining cases following the case that is matched.

Golang automatically provides a break statement for all the cases that are defined, however this behaviour can be overridden by manually placing a fallthrough statement.

In Golang switch cases don’t have to be constant.

Evaluation order of a switch statement

It is important to know that evaluation order of a switch statement is from top to bottom. This means that statements provided at the top of the switch statement will be evaluated before statements provided at the bottom of the switch statement.

This can be confirmed by taking a look at various examples given on this page.

Program to demonstrate a simple switch statement in Golang

Program given below makes use of simple switch statement to evaluate the value of variable a and print appropriate message to the console.

package main

import "fmt"

func main() {
	var a = 4
	// simple switch case statement
	switch a {
	case 1:
		fmt.Println("a is 1")
	case 2:
		fmt.Println("a is 2")
	case 3:
		fmt.Println("a is 3")
	case 4:
		fmt.Println("a is 4")
	}
}

Program output

Above program produces following output:

a is 4

Switch with no condition

A switch statement with no condition is same as switch true. It means that switch statement acts like a concise way to define if else statements with multiple conditions.

// switch with no condition
package main

import "fmt"

func main() {
	var a = 4
	// simple switch case statement
	switch { // same as switch true
	case a==1:
		fmt.Println("a is 1")
	case a==2:
		fmt.Println("a is 2")
	case a==3:
		fmt.Println("a is 3")
	case a==4:
		fmt.Println("a is 4")
	}
}

Program output

Above program produces following output:

a is 4

Switch statement with missing expression

Program given below demonstrates a switch statement with a missing expression.

This way of defining switch statement is same as switch true

// switch with missing expression
package main

import "fmt"

func main() {
	// simple switch case statement
	switch b := 4; { // missing expression, same as switch true
	case b == 1:
		fmt.Println("b is 1")
	case b == 2:
		fmt.Println("b is 2")
	case b == 3:
		fmt.Println("b is 3")
	case b == 4:
		fmt.Println("b is 4")
	}
}

Program output

Above program produces following output:

b is 4

Switch statement using fallthrough

By default, Golang adds a break statement for every case statement, however if we want to change this behaviour and instead want to continue evaluating the next case statement then we can use the fallthrough statement for achieving similar results.

Program given below effect of follthrough on how cases are evaluated.

// switch with missing expression
package main

import "fmt"

func main() {
	var c = 1
	// simple switch case statement
	switch { // missing expression, same as switch true
	case c==1:
		fmt.Println("c is 1")
		fallthrough
	case c==2:
		fmt.Println("c is 2")
		fallthrough
	case c==3:
		fmt.Println("c is 3")
	}
}

Program output

Above program produces following output:

c is 1
c is 2
c is 3

Using the default case

If a switch statement fails to match any case then code inside default case block will be executed. This behaviour is demonstrated in the example given below.

In the program given below, no match for x is found therefore the default case block is executed.

// switch with default case block
package main

import "fmt"

func main() {
	var x = 0
	// simple switch case statement
	switch { // missing expression, same as switch true
	case x ==1:
		fmt.Println("x is 1")
		fallthrough
	case x ==2:
		fmt.Println("x is 2")
		fallthrough
	case x ==3:
		fmt.Println("x is 3")
	default:
		fmt.Println("no matches found")
	}
}

Program output

Above program produces following output:

no matches found