失败的类型断言,返回的值为最近断言类型的零值
代码入下:
func main() {
var data interface{} = "ehoo"
if res, ok := data.(int); ok {
fmt.Printf("int res:%d\n", res)
} else if res, ok := data.(bool); ok {
fmt.Printf("bool res:%b\n", res)
} else {
fmt.Printf("other res:%v\n", res) // 断言类型的零值,结果:false
}
}