Open In App

Type Switches in GoLang

Last Updated : 15 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A switch is a multi-way branch statement used in place of multiple if-else statements but can also be used to find out the dynamic type of an interface variable. 
A type switch is a construct that performs multiple type assertions to determine the type of variable (rather than values) and runs the first matching switch case of the specified type. It is used when we do not know what the interface{} type could be.
Example 1:
 

C




// Golang program to illustrate the
// concept of type switches
package main
  
import (
    "fmt"
)
  
// main function
func main() {
      
    // an interface that has
    // a string value
    var value interface{} = "GeeksforGeeks"
      
    // type switch to find
    // out interface{} type
    switch t := value.(type){
      
        case int64:
 
            // if type is an integer
            fmt.Println("Type is an integer:", t)
 
        case float64:
 
            // if type is a floating point number
            fmt.Println("Type is a float:", t)
 
        case string:
 
            // if type is a string
            fmt.Println("Type is a string:", t)
 
        case nil:
 
            // if type is nil (zero-value)
            fmt.Println("Type is nil.")
 
        case bool:
 
            // if type is a boolean
            fmt.Println("Type is a bool:", t)
 
        default:
 
            // if type is other than above
            fmt.Println("Type is unknown!")
    }
}


Output: 
 

Type is a string: GeeksforGeeks

The switch can have multiple value cases for different types and is used to select a common block of code for many similar cases.
Note: Golang does not needs a ‘break’ keyword at the end of each case in the switch.
Example 2:
 

C




// Golang program to illustrate the
// concept of type switch with
// multiple value cases
package main
  
import (
    "fmt"
)
  
// function which implements type
// switch with multiple cases value
func find_type(value interface{}) {
      
    // type switch to find
    // out interface{} type
    switch t := value.(type) {
      
        case int, float64:
 
            // type is an int or float
            fmt.Println("Type is a number, either an int or a float:", t)
              
        case string, bool:
 
            // type is a string or bool
            fmt.Println("Type is either a string or a bool:", t)
              
        case *int, *bool:
 
            // type is either an int pointer
            // or a bool pointer
            fmt.Println("Type is a pointer to an int or a bool:", t)
              
        default:
 
            // type of the interface is unknown
            fmt.Println("Type unknown!")
    }
}
  
// main function
func main() {
      
    // an interface that has
    // a string value
    var v interface{} = "GeeksforGeeks"
      
    // calling the find_type method
    // to determine type of interface
    find_type(v)
      
    // re-assigning v with a
    // float64 value
    v = 34.55
      
    find_type(v)
}


Output:
 

Type is either a string or a bool: GeeksforGeeks
Type is a number, either an int or a float: 34.55

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads