Type Switches in GoLang

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



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.