45_类型断言之switch的使用

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
", index, value)
		case string:
			fmt.Printf("list[%d] is an int and its value is %d
", index, value)
		case Person:
			fmt.Printf("list[%d] is an int and its value is [%s,%d]
", index, value.name, value.age)

		}
	}
}

每天的价值就是不停息的前进!!!
原文地址:https://www.cnblogs.com/zhaopp/p/11565538.html