接口是一般类型,不知道具体类型如果要转换成具体类型,需要使用类型断言
在进行类型断言时,若类型不匹配,会报panic错误,带上检测机制则不会。
//类型断言--带检测
var aa interface{}
var v = 0.12
aa = v //空接口接受任意类型
//带检测的类型断言
if c,ok := aa.(float64); ok {
fmt.Println("assert success")
fmt.Printf("c的类型%T,值为:%v", c, c)
}else {
fmt.Println("assert fail")
}