Source file tour/methods/type-assertions.go

     1  // +build no-run OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  func main() {
     8  	var i interface{} = "hello"
     9  
    10  	s := i.(string)
    11  	fmt.Println(s)
    12  
    13  	s, ok := i.(string)
    14  	fmt.Println(s, ok)
    15  
    16  	f, ok := i.(float64)
    17  	fmt.Println(f, ok)
    18  
    19  	f = i.(float64) // panic
    20  	fmt.Println(f)
    21  }
    22  

View as plain text