反射
反射的介绍:
程序中会使用type关键字定义很多自定义类型,可以用反射来区别是否是自定义类型
reflect包的俩种方法
//reflect包中定义的全部底层的类型
type Kind uint
const (
Invalid Kind = iota // 非法类型
Bool // 布尔型
Int // 有符号整型
Int8 // 有符号8位整型
Int16 // 有符号16位整型
Int32 // 有符号32位整型
Int64 // 有符号64位整型
Uint // 无符号整型
Uint8 // 无符号8位整型
Uint16 // 无符号16位整型
Uint32 // 无符号32位整型
Uint64 // 无符号64位整型
Uintptr // 指针
Float32 // 单精度浮点数
Float64 // 双精度浮点数
Complex64 // 64位复数类型
Complex128 // 128位复数类型
Array // 数组
Chan // 通道
Func // 函数
Interface // 接口
Map // 映射
Ptr // 指针
Slice // 切片
String // 字符串
Struct // 结构体
UnsafePointer // 底层指针
)
TypeOf
使用
reflect.TypeOf()
函数可以获得任意值的类型对象(reflect.Type),程序通过类型对象可以访问任意值的类型信息
//使用reflect.TypeOf( )函数可以获得任意值得类型对象(reflect.Type),程序通过类型对象可以访问任意值的类型信息
func reflectType(x interface{}) {
v := reflect.TypeOf(x)
fmt.Printf("type:%v\n", v)
}
func typeMain() {
var a float32 = 3.14
reflectType(a) //type:float32
var b int64 = 100
reflectType(b) //type:int64
}
Type Name和Type Kind
种类:
Kind
就是指底层的类型,Name
就是底层的类型的名称类型:
type
当前的类型
注意:array、slice、Map、pointer等类型的变量,``type都是空`
type myInt int64
func reflectType2(x interface{}) {
t := reflect.TypeOf(x)
fmt.Printf("type:%v kind:%v\n", t.Name(), t.Kind())
}
func type2Main() {
var a *float32
var b myInt
var c rune
reflectType2(a) //type: kind:ptr
reflectType2(b) //type:myInt kind:int64
reflectType2(c) //type:int32 kind:int32
type person struct {
name string
age int
}
var d = person{
name: "lqx",
age: 20,
}
reflectType2(d) //type:person kind:struct
var e map[string]int
reflectType2(e) //type: kind:map
var f [1]int
reflectType2(f) //type: kind:array
//数组、切片、Map、指针等类型的变量,type都是空
}
ValueOf
方法 | 说明 |
---|---|
Interface() interface {} | 将值以 interface{} 类型返回,可以通过类型断言转换为指定类型 |
Int() int64 | 将值以 int 类型返回,所有有符号整型均可以此方式返回 |
Uint() uint64 | 将值以 uint 类型返回,所有无符号整型均可以此方式返回 |
Float() float64 | 将值以双精度(float64)类型返回,所有浮点数(float32、float64)均可以此方式返回 |
Bool() bool | 将值以 bool 类型返回 |
Bytes() []bytes | 将值以字节数组 []bytes 类型返回 |
String() string | 将值以字符串类型返回 |
通过反射获取值
//通过反射获取值
func reflectValue(x interface{}) {
v := reflect.ValueOf(x)
k := v.Kind()
switch k {
case reflect.Int64:
// v.Int()从反射中获取整型的原始值,然后通过int64()强制类型转换
fmt.Printf("type is int64, value is %d\n", int64(v.Int()))
case reflect.Float32:
// v.Float()从反射中获取整型的原始值,然后通过float32()强制类型转换
fmt.Printf("type is float32, value is %f\n", float32(v.Float()))
case reflect.Float64:
// v.Float()从反射中获取整型的原始值,然后通过float64()强制类型转换
fmt.Printf("type is float64, value is %f\n", float64(v.Float()))
}
}
func reflectValueMain() {
var a float32 = 3.14
var b int64 = 100
reflectValue(a) // type is float32, value is 3.140000
reflectValue(b) // type is int64, value is 100
// 将int类型的原始值转换为reflect.Value类型
c := reflect.ValueOf(10)
fmt.Printf("type c :%T\n", c) // type c :reflect.Value
}
通过反射设置变量的值
// 通过反射设置变量的值,使用专有的Elem()方式来获取指针的值
func reflectSetValue1(x interface{}) {
v := reflect.ValueOf(x)
if v.Kind() == reflect.Int64 {
v.SetInt(200) //修改的是副本,reflect包会引发panic
}
}
func reflectSetValue2(x interface{}) {
v := reflect.ValueOf(x)
//反射中使用Elem()方式获取指针对应的值
if v.Elem().Kind() == reflect.Int64 {
v.Elem().SetInt(200)
}
}
func reflectSetValueMain() {
var a int64 = 100
// reflectSetValue1(a) //panic: reflect: reflect.Value.SetInt using unaddressable value
reflectSetValue2(&a)
fmt.Println(a)
}
isNil()和isValid()
// isNil() 和isValid()
// isNil() 报告v持有的值是否为nil,v持有的值必须为,channl、func、interface、map、pointer、slice,否则会导致panic
// isValid() 返回v是否持有一个值。如果v是value零值会返回假,v处理isValid、String、Kind之外的方法都导致panic
// IsNil()常被用于判断指针是否为空;IsValid()常被用于判定返回值是否有效。
func NilValid() {
// *int 类型空指针
var a *int
fmt.Println("var a *int isnil:", reflect.ValueOf(a).IsNil())
//nil值
fmt.Println("nil isvalid:", reflect.ValueOf(nil).IsValid())
//实例化一个匿名结构体
b := struct{}{}
//尝试从结构体中查找“abc”字段
fmt.Println("不存在的结构体成员:", reflect.ValueOf(b).FieldByName("abc").IsValid()) //不存在的结构体成员: false
//尝试从结构体中查询“abc”方法
fmt.Println("不存在的结构体方法:", reflect.ValueOf(b).MethodByName("abc").IsValid()) //不存在的结构体方法: false
//map
c := map[string]int{}
//尝试从map中查询一个不存在的键
fmt.Println("不存在的map键:", reflect.ValueOf(c).MapIndex(reflect.ValueOf("lqx")).IsValid()) //不存在的map键: false
}
结构体反射
方法 | 说明 |
---|---|
Field(i int) StructField | 根据索引,返回索引对应的结构体字段的信息。 |
NumField() int | 返回结构体成员字段数量。 |
FieldByName(name string) (StructField, bool) | 根据给定字符串返回字符串对应的结构体字段的信息。 |
FieldByIndex(index []int) StructField | 多层成员访问时,根据 []int 提供的每个结构体的字段索引,返回字段的信息。 |
FieldByNameFunc(match func(string) bool) (StructField,bool) | 根据传入的匹配函数匹配需要的字段。 |
NumMethod() int | 返回该类型的方法集中方法的数目 |
Method(int) Method | 返回该类型方法集中的第i个方法 |
MethodByName(string)(Method, bool) | 根据方法名返回该类型方法集中的方法 |
StructField类型
//结构体反射
type student struct {
Name string `json:"name"`
Age int `json:"age"`
}
func studentMain() {
stu1 := student{
Name: "lqx",
Age: 20,
}
t := reflect.TypeOf(stu1)
fmt.Println(t.Name(), t.Kind())
//通过for循环遍历结构体的所有字段信息
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fmt.Printf("name:%s index:%d type:%v json tag:%v\n", field.Name, field.Index, field.Type, field.Tag.Get("json"))
}
//通过字段名称 获取指定结构体字段信息
if ageField, ok := t.FieldByName("Age"); ok {
fmt.Printf("name:%s index:%d type:%v json tag:%v\n", ageField.Name, ageField.Index, ageField.Type, ageField.Tag.Get("json"))
}
}
通过结构体反射获取实例的全部方法
//结构体反射 打印 s 包含的方法
func (s student) Study() string {
msg := "好好学习,天天上线"
fmt.Println(msg)
return msg
}
func (s student) Sleep() string {
msg := "好好睡觉,天天玩耍"
fmt.Println(msg)
return msg
}
func printMethod(x interface{}) {
t := reflect.TypeOf(x)
v := reflect.ValueOf(x)
fmt.Println(t.NumMethod()) //有几个方法 2
for i := 0; i < v.NumMethod(); i++ {
methodType := v.Method(i).Type()
methodName := t.Method(i).Name
fmt.Printf("method name: %s\n", methodName) //method name: Sleep // method name: Study
fmt.Printf("method: %s\n", methodType) //是一个什么样的函数 method: func() string //method: func() string
var args = []reflect.Value{}
v.Method(i).Call(args) //执行相关的方法
}
}
func printMethodMain() {
stu2 := student{
Name: "lqx",
Age: 20,
}
printMethod(stu2)
}
来源:https://blog.csdn.net/qq_34857250/article/details/100569679