类型断言
package main import ( "fmt" ) func main() { var p *person = new(person) fmt.Println(p) // i := make([]interface{}, 3) //切片类型:空接口,长度:3 i[0] = 9 i[1] = "string" i[2] = person{"张三", 30} fmt.Printf("%T\n", i) for index, data := range i { //if实现断言 if value, ok := data.(int); ok == true { fmt.Printf("i[%d] 的类型为int, 值为 :%d\n", index, value) } else if value, ok := data.(string); ok == true { fmt.Printf("i[%d] 的类型为string, 值为 :%s\n", index, value) } else if value, ok := data.(person); ok == true { fmt.Printf("i[%d] 的类型为student, 值为 :%+v\n", index, value) } //switch实现断言 switch value := data.(type) { case int: fmt.Printf("i[%d] 的类型为int, 值为 :%d\n", index, value) case string: fmt.Printf("i[%d] 的类型为string, 值为 :%s\n", index, value) case person: fmt.Printf("i[%d] 的类型为student, 值为 :%+v\n", index, value) } } } type person struct { name string age int }