English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String (String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception (Error)

Golang Other Miscellaneous

Go Language Switch Statement

The switch statement is a multi-way branch statement. It provides an effective method to transfer execution to different parts of the code based on the value of an expression (also known as case). Go language supports two types of switch statements:

  1. expression switch

  2. Type switch

expression switch

The expression switch is similar to C, C ++The switch statement in Java. It provides a simple method to dispatch execution to different parts of the code based on the value of the expression.

Syntax:

switch optstatement; optexpression {
    case expression1: Statement...
    case expression2: Statement...
    ...
    default: Statement...
}

Important Note:

  • The optstatement and optexpression in the expression switch are optional statements.

  • If both existoptstatementandoptression, then a semicolon (;) is needed between them.

  • If the switch does not contain any expressions, the compiler assumes the expression to be true.

  • Optional statements, that is, optstatement, contain simple statements, such as variable declarations, increment or assignment statements, or function calls, etc.

  • If there is a variable in the optional statement, the scope of that variable is limited to the switch statement.

  • In the switch statement, case and default statements do not contain any break statements. However, if your program requires, you can use break and fallthrough statements.

  • The default (default) statement is optional in the switch statement.

  • If a case can contain multiple values, and these values are separated by commas (,).

  • If a case does not contain any expressions, the compiler assumes this expression to be true.

Example of getting the day of the week for a specified number:

package main
import "fmt"
func main() {
    // A switch statement uses both statements, such as optional statements, day :=4 and expressions such as: day
    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")
    case 7:
        fmt.Println("Sunday")
    default:
        fmt.Println("Invalid")
    }
}

Output:

Thursday

Example of a switch statement without optional statements and expressions:

package main 
  
import "fmt"
  
func main() { 
    var value int = 2 
      
      // A switch statement without optional statements and expressions
   switch { 
       case value == 1: 
       fmt.Println("Hello") 
       case value == 2: 
       fmt.Println("Bonjour") 
       case value == 3: 
       fmt.Println("Namstay") 
       default:  
       fmt.Println("Invalid") 
   } 
  
}

Output:

Bonjour

This is a switch statement without a default statement, multiple values in the case statement:

package main
import "fmt"
func main() {
    var value string = "five"
    //Switch statements with multiple values in case clauses without a default statement
    switch value {
    case "one":
        fmt.Println("C#")
    case "two", "three":
        fmt.Println("Go")
    case "four", "five", "six":
        fmt.Println("Java")
    }
}

Output:

Java

Type switch

Use type switch when you want to compare types. In this switch, the case contains types to be compared with the types appearing in the switch expression.

Syntax:

switch optstatement; typeswitchexpression{
    case typelist 1: Statement...
    case typelist 2: Statement...
    ...
    default: Statement...
}

Important Note:

  • Optional statement, i.e., optstatement, which is similar to that in a switch expression.

  • If a case can contain multiple values, and these values are separated by commas (,).

  • In a type switch statement, case and default statements do not contain any break statements. However, break and fallthrough statements can be used if needed by the program.

  • The default statement is optional in a type switch statement.

  • typeswitchexpressionis an expression that results in a type.

  • If an expression is assigned to a variable in a typeswitch expression, the type of the variable depends on the type specified in the case clauses. If the case clauses contain two or more types, the type of the variable is the type created when the variable is created in the typeswitch expression.

package main
import "fmt"
func main() {
    var value interface{}
    switch q := value.(type) {
    case bool:
        fmt.Println("The value is boolean")
    case float64:
        fmt.Println("The value is float")64type)
    case int:
        fmt.Println("The value is of int type")
    default:
        fmt.Printf("The type of the value is: %T", q)
    }
}

Output:

The type of the value is: <nil>