反射的示意图
反射的重要函数
1.reflect.TypeOf()获取变量类型,返回reflect.Type类型
2.reflect.ValueOf()获取变量的值,返回reflect.Value类型
3.反射中 变量 空接口 reflect.Value可以相互转换
注意细节
1.reflect.Value.Kind(),获取变量的类别,返回一个常量
type Kind
2.Type是类型,Kind是类别
var num int = 10 num Type int Kind int
var stu Student stu Type pkg1.Student Kind Struct
3.使用反射的方式来获取变量的值(并返回对应的类型),要求数据类型匹配。如x是int,那么应该使用reflect.Value(x).Int(),而不能用其他
4.用反射来修改变量,需要先用Elem()方法,再用SetXxx()方法,演示如下
package main
import(
"fmt"