Judge the interface{} type through switch, and then force it through xxx.(type)
type Person struct{
name string
age int}funccheckType(i interface{
}){
switch v := i.(type){
//这里是通过i.(type)来判断是什么类型 下面的case分支匹配到了 则执行相关的分支caseint:
fmt.Printf("%v is an int\n", v)casestring:
fmt.Printf("%v is string", v)case Person:
fmt.Println("Person", reflect.TypeOf(i))
i2 := i.(Person)//将 i 强转为 Person
fmt.Println(i2.name)casebool:
fmt.Printf("%v is bool", v)}}funcmain(){
var info Person
info.name ="zs"
info.age =20checkType(info)}