Advertisement
cwchen

[Go] Type assertion with switch statement.

Oct 7th, 2017
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.47 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. func main() {
  8.     var t interface{}
  9.     t = 3
  10.     switch t := t.(type) {
  11.     default:
  12.         fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t has
  13.     case bool:
  14.         fmt.Printf("boolean %t\n", t) // t has type bool
  15.     case int:
  16.         fmt.Printf("integer %d\n", t) // t has type int
  17.     case *bool:
  18.         fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
  19.     case *int:
  20.         fmt.Printf("pointer to integer %d\n", *t) // t has type *int
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement