go-tour/type-assertions.go

20 lines
262 B
Go
Raw Normal View History

2020-02-08 22:39:18 +00:00
package main
import "fmt"
func main() {
var i interface{} = "hello"
s := i.(string)
fmt.Println(s)
s, ok := i.(string)
fmt.Println(s, ok)
f, ok := i.(float64)
fmt.Println(f, ok)
f = i.(float64) // panic
fmt.Println(f)
}