Tutorialsteacher

Follow Us

Go Switch Statements

The switch statement is a shorter way of writing an if-else statement. It allows you to execute one among many code blocks based on a condition. It runs the first case where the expression matches the value.

The Go switch statement is similar to the switch statements in other programming languages such as C#, C++, and java.

In Go, the switch statements can have a single case or multiple case values for each case.

In single value switch statements, each case will have a single value to be compared with the expression, as shown in the following example.

Syntax
switch expression {
	case 1:
		//code block 1
	case 2:
		//code block 2
	case 3:
		//code block 3
	default:
		//default code block
}

In the above syntax, the expression is evaluated once. Its value is compared with the value of each case. When the expression value matches the case value, the code block of that case is executed. If none of the cases matches the expression then the default code block is executed.

In the following example, the month is passed as a number and the case displays the actual month in words.

Example: Switch
package main
import "fmt"

func main() {
	thisMonth := 5

	switch thisMonth {
		case 1:
			fmt.Println("January")
		case 2:
			fmt.Println("February")
		case 3:
			fmt.Println("March")
		case 4:
			fmt.Println("April")
		case 5:
			fmt.Println("May") 
		case 6:
			fmt.Println("June")
	}  
}
Output:
May

The following example contains multiple values in each case.

Example: Switch with Multiple Case Values
package main
import "fmt"
	 
func main() {
	thisMonth := 6

	switch thisMonth {
		case 1, 3, 5, 7, 8, 10,12:
			fmt.Println("31 days")
		case 4,6,9,11:
			fmt.Println("30 days")
		case 2:
			fmt.Println("28 days")
	}  
}
Output:
30 days

The switch statements can be used with any datatype.

Example: Switch
package main
import "fmt"

func main() {
	thisMonth := "May"

	switch thisMonth {
		case "January":
			fmt.Println("1")
		case "February":
			fmt.Println("2")
		case "March":
			fmt.Println("3")
		case "April":
			fmt.Println("4")
		case "May":
			fmt.Println("5") 
		case "June":
			fmt.Println("6")
	}  
}
Output:
5

Conditional Case

The comparison operators can be used in the switch case statements, as shown below.

Example: Conditional Cases
package main
import "fmt"
	 
func main() {
    x := 20
    y := 10 

    switch {
	    case x > y:
		    fmt.Println("x is greater than y")
	    case x < y:
		    fmt.Println("x is less than y")
	    default:
		    fmt.Println("x is equal to y")
    }  
}
Output:
x is greater than y

Fallthrough Case Statement

The fallthrough keyword in the case statement transfers the execution to the next case.

Example:
package main
import "fmt"
	 
func main() {
    x := 3 

    switch x {
	    case 1:
		    fmt.Println("1")
		    fallthrough
	    case 3:
		    fmt.Println("3")
		    fallthrough
	    case 5:
		    fmt.Println("5")
	}
}
Output:
3 5

Go Switch Initializer Statement

Like for loop and if statement, the switch statement can start with the short statement. For example, a variable local to the switch block can be declared and initialized immediately after the switch keyword, as shown below.

Example: Short Statement with Switch
package main
import "fmt"
	 
func main() {
    
    switch day:= 4; day {
	    case 1:
		    fmt.Println("Monday")
	    case 2:
		    fmt.Println("Tuesday")
	    case 3:
		    fmt.Println("Wednesday")
	    case 4:
		    fmt.Println("Thursday") 
	    case 5:
		    fmt.Println("Friday")
	    case 6:
		    fmt.Println("Saturday")
	    default:
		    fmt.Println("Sunday")
    }  
}
Output:
Thursday

In the above example, a variable day is declared and initialized immediately after the switch keyword. It is also passed as the expression to the switch block. It is local to the switch block where it is declared.