daily pastebin goal
36%
SHARE
TWEET

[Go] Type assertion with switch statement.

cwchen Oct 7th, 2017 320 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top