标签:imp pack import range 使用 type main 断言 printf
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 {
switch value := element.(type) { //注意这里是type
case int:
fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
case string:
fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
case Person:
fmt.Printf("list[%d] is an int and its value is [%s,%d]\n", index, value.name, value.age)
}
}
}
标签:imp pack import range 使用 type main 断言 printf
原文地址:https://www.cnblogs.com/zhaopp/p/11565538.html