文章目录
1、从类型对象中获取名称和种类
const (
Zero int = 0
)
func main() {
type stu struct{}
typeofCat := reflect.TypeOf(stu{})
fmt.Println(typeofCat.Name(), typeofCat.Kind()) //显示反射类型对象名称和种类
typeOfA := reflect.TypeOf(Zero)
fmt.Println(typeOfA.Name(), typeOfA.Kind())
}
2、使用反射获取结构体的成员类型
| 方法 |
说明 |
| Field(i int) StructField |
根据索引规返回索引对应的结构字段的信息,当值不是结构体或索引越界会发生宕机 |
| NumField() int |
返回结构体成员字段数量,当类型不是结构体或索引越界时发生宕机 |
| FieldByName(name string) (StructField bool) |
根据给定字段返回字符串对应的结构体字段的信息,没有找到时bool返回false,当类型不是结构体或索引越界时发生宕机 |
| FieldByIndex(index\[\]int) StructField |
多层成员访问时,根据\[\]iny 提供的每个结构体的字段索引,返回字段的信息,没有找到返回零值。当类型不是结构体或索引越界时发生宕机 |
| FieldByNameFunc(match func(string) bool)(StructField,bool) |
根据匹配函数匹配需要的字段,当值不是结构体或索引越界时发生宕机 |
3、从反射值对象中获取值的方法
| 方法名 |
说明 |
| Interface() interface{} |
将值以interface{}类型返回,可以通过类型断言为指定类型 |
| Int() int64 |
将值以int类型返回,所有有符号整数均可以此方式返回 |
| Uint() uint64 |
将值以uint类型返回,所有无符号整数均可以此方式返回 |
| Float() float64 |
将值以双精度类型返回,所有浮点数(float32,float64)均可以此方式返回 |
| Bool() bool |
将值以bool类型返回 |
| Bytes() \[\]bytes |
将值以字节数组\[\]bytes类型返回 |
| String() string |
将值以字符串类型返回 |
func main() {
var m int = 2021
valueOfA := reflect.ValueOf(m)
var getA int = valueOfA.Interface().(int)
var getA2 int = int(valueOfA.Int())
fmt.Println(getA, getA2)
}
4、使用反射访问结构体的成员字段的值
| 方法 |
说明 |
| Field(i int) Value |
根据索引对应的结构体成员字段的反射对象,当值不是结构体或索引越界时发生宕机 |
| NumField() int |
返回给结构体成员字段数量 ,当值不是结构体或索引越界时发生宕机 |
| FieldByName(name string) Value |
根据给定字符串返回字符串对应的结构体字段,没有找到时返回零值,当值不是结构体或索引越界时发生宕机 |
| FieldByIndex(index \[\]int) Value |
多层成员返回时,根据\[\]int提供的每个结构体的字段索引,返回字段的值,没有找到时返回零值,当值不是结构体或索引越界时发生宕机 |
| FieldByNameFunc(match func(string) bool) value |
根据匹配函数匹配需要的字段,找到时返回零值,当值不是结构体或索引越界时发生宕机 |
type stu struct {
a int
b string
float32
bool
next *stu
}
func main() {
d := reflect.ValueOf(stu{
next: &stu{},
})
fmt.Println("numField", d.NumField())
floatField := d.Field(2)
fmt.Println("floatField", floatField.Type())
//根据名称查找字段
fmt.Println("FieldsByName().Type", d.FieldByName("b").Type())
fmt.Println("FieldByIndex()", d.FieldByIndex([]int{4, 0}).Type())
}
5、反射对象的空和有效性判断
| 方法 | 说明 |
| IsNil() bool | 返回值是否为nil,如果值类型不是通道(channel)、函数、接口、map、指针切片,类似于语言层的v==nil操作|
| IsValid() bool | 判断值是否有效。当值本身非法时,返回false,如reflect Value不包含任何值,值为nil等|
func main() {
var a *int
fmt.Println("var a *int:", reflect.ValueOf(a).IsNil())
fmt.Println("nil:", reflect.ValueOf(nil).IsValid())
fmt.Println("*int nil:", reflect.ValueOf((*int)(nil)).Elem().IsValid())
s := struct{}{}
fmt.Println("struct:", reflect.ValueOf(s).FieldByName("").IsValid())
fmt.Println("并不存在的结构体方法:", reflect.ValueOf(s).MethodByName("").IsValid())
m := map[int]int{}
fmt.Println("不存在的键:", reflect.ValueOf(m).MapIndex(reflect.ValueOf(3)).IsValid())
}
6、判定及获取元素的相关方法
| 方法名 |
说明 |
| Elem() Value |
取值指向的元素值,类似于语言层* 操作,当值类型不是指针或接口时发生宕机,空指针时返回nil的Value |
| Addr() Value |
对寻址的值返回其地址,类似于语言层&操作,当值不可寻址时发生宕机 |
| CanAddr() bool |
表示值是否可寻址 |
| CanSet() bool |
返回值能否被修改,要求值可寻址且是导出的字段 |
|
|
7、值修改的相关方法
| Set(x Value) |
将值设置为传入反转值对象的值 |
| SetUint(x uint64) |
使用uint64设置值,当值的类型不是uint,uint8,uint16,uint32,uint64会发生宕机 |
| SetInt(x int64) |
比上面少了u |
| SetFloat(x float64) |
使用float54设置值,当值的类型不是float32,float64发生宕机 |
| SetBool(x bool) |
使用bool设置值,当值的类型不是bool时发生宕机 |
| SetBytes(x \[\]byte) |
设置字节数组\[\] bytes值,当值的类型不是\[\]byte 会发生宕机 |
| SetString(x string) |
设置字符串值.当值的类型不是string时会发生宕机 |
func main() {
a := 1024
valueOfA := reflect.ValueOf(&a)
valeOfEl := valueOfA.Elem()
valeOfEl.SetInt(100)
fmt.Println(a)
}
func main() {
type Dog struct {
LegCount int
}
valueOfDog := reflect.ValueOf(&Dog{})
valueOfDog = valueOfDog.Elem()
vLegCount := valueOfDog.FieldByName("LegCount")
vLegCount.SetInt(3)
fmt.Println(vLegCount.Int())
}
8、通过类型创建类型
创建了新的类型
func main() {
var a int
typeOfA := reflect.TypeOf(a)
aIns := reflect.New(typeOfA)
fmt.Println(aIns.Type(), aIns.Kind())
}
9、使用反射调用函数
func add(a, b int) int {
return a + b
}
func main() {
funcValue := reflect.ValueOf(add)
paramList := []reflect.Value{reflect.ValueOf(10), reflect.ValueOf(20)}
retList := funcValue.Call(paramList)
fmt.Println(retList[0].Int())
}
10、常用反射API
从实例到Value
func ValueOf(i interface{}) Value
从实例到Type
func TypeOf (i interface{}) Type
从Type到Value
func NewAt (type Type,p unsafe.Pointer) Value
从Value到Type
func (v Value) Type() Type
从Value到实例
func (v Value) Interface() (i interface{})
func (v Value) Bool() bool
func (v Value) Float() float64
func (v Value) Int() int64
func (v Value) Uint() uint64
从Value的指针到值
func (v Value) Elem() Value
func Indirect(v Value) Value
Type指针和值相互转换
t.Elem() Type //指针类型Type到值类型Type
func PtrTo(t Type) Type
Value值的可修改性
func (v Value) CanSet() bool
func (v Value) Set(x Value)