Advertisement
cwchen

[Go] Type assertion with uncertainty

Oct 7th, 2017
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.29 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6. )
  7.  
  8. func main() {
  9.     var im interface{}
  10.     var in interface{}
  11.  
  12.     im = 3
  13.     in = 2
  14.  
  15.     // type assertion
  16.     m, ok := im.(int)
  17.     if !ok {
  18.         log.Fatal("Wrong type")
  19.     }
  20.  
  21.     n, ok := in.(int)
  22.     if !ok {
  23.         log.Fatal("Wrong type")
  24.     }
  25.  
  26.     fmt.Println(m + n)
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement