标签:index ring print -o range its struct UNC interface
package main
import "fmt"
type Elementer interface{}
type Person struct {
name string
age int
}
func main() {
var list []Elementer = make([]Elementer, 3) //空接口类型
list[0] = 1
list[1] = "ads"
list[2] = Person{"mike", 12}
for index, element := range list {
if value, ok := element.(int); ok == true {
fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
} else if value, ok := element.(string); ok == true {
fmt.Printf("list[%d] is an int and its value is %s\n", index, value)
} else if value, ok := element.(Person); ok == true {
fmt.Printf("list[%d] is an int and its value is [%s,%d]\n", index, value.name, value.age)
}
}
}
标签:index ring print -o range its struct UNC interface
原文地址:https://www.cnblogs.com/zhaopp/p/11565533.html