Go – The Switch Statement

  • Post author:
  • Post category:GO
  • Post comments:1 Comment
Go - The Switch Statement

This topic is about Go – The Switch Statement.

switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

In Go programming, switch statements are of two types −

  • Expression Switch − In expression switch, a case contains expressions, which is compared against the value of the switch expression.
  • Type Switch − In type switch, a case contain type which is compared against the type of a specially annotated switch expression.

Expression Switch

The syntax for expression switch statement in Go programming language is as follows −

switch(boolean-expression or integral type){
   case boolean-expression or integral type :
      statement(s);      
   case boolean-expression or integral type :
      statement(s); 
   
   /* you can have any number of case statements */
   default : /* Optional */
      statement(s);
}

The following rules apply to a switch statement −

  • The expression used in a switch statement must have an integral or boolean expression, or be of a class type in which the class has a single conversion function to an integral or boolean value. If the expression is not passed then the default value is true.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  • When the variable being switched on is equal to a case, the statements following that case will execute. No break is needed in the case statement.
  • switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Flow Diagram

Example

package main

import "fmt"

func main() {
   /* local variable definition */
   var grade string = "B"
   var marks int = 90

   switch marks {
      case 90: grade = "A"
      case 80: grade = "B"
      case 50,60,70 : grade = "C"
      default: grade = "D"  
   }
   switch {
      case grade == "A" :
         fmt.Printf("Excellent!\n" )     
      case grade == "B", grade == "C" :
         fmt.Printf("Well done\n" )      
      case grade == "D" :
         fmt.Printf("You passed\n" )      
      case grade == "F":
         fmt.Printf("Better try again\n" )
      default:
         fmt.Printf("Invalid grade\n" );
   }
   fmt.Printf("Your grade is  %s\n", grade );      
}

When the above code is compiled and executed, it produces the following result −

Excellent!
Your grade is  A

Type Switch

The syntax for a type switch statement in Go programming is as follows −

switch x.(type){
   case type:
      statement(s);      
   case type:
      statement(s); 
   /* you can have any number of case statements */
   default: /* Optional */
      statement(s);
}

The following rules apply to a switch statement −

  • The expression used in a switch statement must have an variable of interface{} type.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • The type for a case must be the same data type as the variable in the switch, and it must be a valid data type.
  • When the variable being switched on is equal to a case, the statements following that case will execute. No break is needed in the case statement.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example

package main

import "fmt"

func main() {
   var x interface{}
     
   switch i := x.(type) {
      case nil:	  
         fmt.Printf("type of x :%T",i)                
      case int:	  
         fmt.Printf("x is int")                       
      case float64:
         fmt.Printf("x is float64")           
      case func(int) float64:
         fmt.Printf("x is func(int)")                      
      case bool, string:
         fmt.Printf("x is bool or string")       
      default:
         fmt.Printf("don't know the type")     
   }   
}

When the above code is compiled and executed, it produces the following result −

type of x :<nil>

To know more, Click Here

This Post Has One Comment

Leave a Reply