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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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")
   }
     
}

chevron_right


Output:

Thursday

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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")
   }
  
}

chevron_right


Output:

Bonjour

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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")
   }  
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
         
   }
}

chevron_right


Output:

value is of type: <nil>


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.