上一篇文章中,我们详细了解了 golang 中反射机制的实现原理。 golang 中的反射(上) — 反射的原理与实现
本文,我们就来详细介绍 golang 中反射的使用。
经过上文的介绍,我们可以通过 ValueOf 拿到了内存中实际的值,从原理上来说,只要通过强制类型转换,就可以将他转换为我们需要的类型了。
Value 类型绑定了以下几种基本类型的转换方法:
func (v Value) Bool() bool
func (v Value) Bytes() []byte
func (v Value) Int() int64
func (v Value) Float() float64
func (v Value) Interface() (i interface{})
func (v Value) Pointer() uintptr
func (v Value) Uint() uint64
如果该类型已知为上述某种基本类型,通过上述方法就可以直接转换为对应的值了。
package main
import (
"fmt"
"reflect"
)
type temprature int
func main() {
var temp interface{} = temprature(5)
fmt.Printf("temprature is %d\n", temp.(temprature))
itype := reflect.TypeOf(temp)
ivalue := reflect.ValueOf(temp)
fmt.Printf("%s: %d", itype, ivalue.Int())
}
但如果你想将他转换为其他任意类型呢?也很简单,因为 Value 提供了转换为 interface{} 类型的方法,在这之后,通过类型断言,我们可以轻易将变量转换为我们需要的类型:
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func main() {
var temp interface{} = User{Id: 1, Name: "Nico", Age: 17}
itype := reflect.TypeOf(temp)
ivalue := reflect.ValueOf(temp)
fmt.Printf("%s: %v", itype, ivalue.Interface().(User))
}
打印出了:
main.User: {1 Nico 17}
反射最为常用的场景是在运行时推断类型,从而获取到传递的实际数据:
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func (u User) OutputSelf() {
fmt.Printf("No.%d OutputSelf name: %s, age: %d\n", u.Id, u.Name, u.Age)
}
func main() {
var temp interface{} = User{Id: 1, Name: "Nico", Age: 17}
itype := reflect.TypeOf(temp)
ivalue := reflect.ValueOf(temp)
// 获取字段类型与字段值
fmt.Println("reflect get fields:")
for i := 0; i < itype.NumField(); i++ {
field := itype.Field(i)
value := ivalue.Field(i).Interface()
fmt.Printf("%s: %v = %v\n", field.Name, field.Type, value)
}
// 获取方法
fmt.Println("reflect get methods:")
for i := 0; i < itype.NumMethod(); i++ {
m := itype.Method(i)
fmt.Printf("%s: %v\n", m.Name, m.Type)
}
}
打印出了:
reflect get fields: Id: int = 1 Name: string = Nico Age: int = 17 reflect get methods: OutputSelf: func(main.User)
正如前面所说,反射一个非常重要的作用就是动态改变变量的值,从而在运行时实现通用性极强的一些功能。
设置一个基本类型变量的值是最基本的操作,主要有以下几步:
package main
import (
"fmt"
"reflect"
)
func main() {
iVal := 5
rVal := reflect.ValueOf(&iVal) // 必须取地址,否则会抛出 panic: reflect: call of reflect.Value.Elem on int Value
valElem := rVal.Elem()
valElem.Set(reflect.ValueOf(13)) // Set 传递的参数类型必须与原值类型一致,否则抛出 panic: reflect.Set: value of type string is not assignable to type int
fmt.Printf("new iVal is: %v\n", iVal)
}
打印出了:
new iVal is: 13
与上述设置基本类型的程序十分类似,只是获取内存地址并设为可寻址的 Elem() 方法改为 Index(i int) 方法。 需要注意的是,在获取目标类型指针对应的 Value 对象时,我们需要区分:
package main
import (
"fmt"
"reflect"
)
func main() {
aVal := []int{1}
rVal := reflect.ValueOf(aVal) // slice 本身持有数组的指针,所以此处无需取地址
valElem := rVal.Index(0)
valElem.Set(reflect.ValueOf(13))
fmt.Printf("new aVal[0] is: %v\n", aVal[0])
}
打印出了:
new aVal[0] is: 13
对于结构体,我们必须要指定需要设置的字段,Value 类型提供了 FieldByName 方法用来实现这个功能。
package main
import (
"fmt"
"reflect"
)
type Student struct {
Id int
Name string
Score int
}
func main() {
student := Student{Id: 1, Name: "Nico", Score: 80}
rVal := reflect.ValueOf(&student) // 必须取地址,否则会抛出 panic: reflect: call of reflect.Value.Elem on int Value
valElem := rVal.Elem()
score := valElem.FieldByName("Score")
score.SetInt(99)
fmt.Printf("student is: %v\n", student)
}
打印出了:
student is: {1 Nico 99}