daily pastebin goal
18%
SHARE
TWEET

[Go] Type assertion with uncertainty

cwchen Oct 7th, 2017 212 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
Top