Friday, November 22, 2024
Google search engine
HomeLanguagesGolangSwitch Statement in Go

Switch Statement in Go

A switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value(also called case) of the expression. Go language supports two types of switch statements:

  1. Expression Switch
  2. Type Switch

Expression Switch

Expression switch is similar to switch statement in C, C++, Java language. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Syntax:

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

Important Points:

  • Both optstatement and optexpression in the expression switch are optional statements.
  • If both optstatementand optexpression are present, then a semi-colon(;) is required in between them.
  • If the switch does not contain any expression, then the compiler assume that the expression is true.
  • The optional statement, i.e, optstatement contains simple statements like variable declarations, increment or assignment statements, or function calls, etc.
  • If a variable present in the optional statement, then the scope of the variable is limited to that switch statement.
  • In switch statement, the case and default statement does not contain any break statement. But you are allowed to use break and fallthrough statement if your program required.
  • The default statement is optional in switch statement.
  • If a case can contain multiple values and these values are separated by comma(,).
  • If a case does not contain any expression, then the compiler assume that te expression is true.

Example 1:




// Go program to illustrate the 
// concept of Expression switch
// statement
package main
  
import "fmt"
  
func main() {
      
    // Switch statement with both 
    // optional statement, i.e, day:=4
    // and expression, i.e, 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 2:




// Go program to illustrate the 
// concept of Expression switch
// statement
package main
  
import "fmt"
  
func main() {
    var value int = 2
      
    // Switch statement without an     
    // optional statement and 
    // expression
   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

Example 3:




// Go program to illustrate the 
// concept of Expression switch
// statement
package main
  
import "fmt"
  
func main() {
    var value string = "five"
      
    // Switch statement without default statement
    // Multiple values in case 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

Type switch is used when you want to compare types. In this switch, the case contains the type which is going to compare with the type present in the switch expression.

Syntax:

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

Important Points:

  • The optional statement, i.e., optstatement is similar as in the switch expression.
  • If a case can contain multiple values and these values are separated by comma(,).
  • In type switch statement, the case and default statement do not contain any break statement. But you are allowed to use break and fallthrough statement if your program required.
  • The default statement is optional in type switch statement.
  • The typeswitchexpression is an expression whose result is a type.
  • If an expression is assigned in typeswitchexpression using := operator, then the type of that variable depends upon the type present in case clause. If the case clause contains two or more types, then the type of the variable is the type in which it is created in typeswitchexpression.

Example:




// Go program to illustrate the 
// concept of Type switch
// statement
package main
  
import "fmt"
  
func main() {
    var value interface{}
    switch q:= value.(type) {
       case bool:
       fmt.Println("value is of boolean type")
       case float64:
       fmt.Println("value is of float64 type")
       case int:
       fmt.Println("value is of int type")
       default:
       fmt.Printf("value is of type: %T", q)
         
   }
}


Output:

value is of type: <nil>

RELATED ARTICLES

Most Popular

Recent Comments