go语言中reflect反射机制。详细原文:地址golang
package main import ( "fmt" "reflect" ) func main() { var x int = 1 fmt.Println("type: ", reflect.TypeOf(x)) }
type: int
TypeOf
函数的定义以下,参数为接口类型,返回值为类型函数
func TypeOf(i interface {}) Type
ValueOf
函数的定义以下,参数为接口类型,返回值为Value
code
var x int = 1 fmt.Println("value: ", reflect.ValueOf(x))
value: <int Value>
能够经过Kind
函数来检查类型,对象
fmt.Println("Kind: ", reflect.ValueOf(x).Kind()) fmt.Println("Kind is Int? ", reflect.ValueOf(x).Kind() == reflect.int)
Kind: int Kind is Int? true
经过Interface
函数能够实现反射对象到接口值的转换,blog
func (v Value) Interface() interface {}
// Interface 以 interface{} 返回 v 的值 y := v.Interface().(float64) fmt.Println(y)
修改反射对象的前提条件是其值必须是可设置的接口
var x float64 = 3.4 v := reflect.ValueOf(x) v.SetFloat(7.3) // Error: panic
为了不这个问题,须要使用CanSet
函数来检查该值的设置性,ci
var x float64 = 3.4 v := reflect.ValueOf(x) fmt.Println("settability of v: ", v.CanSet())
settability of v: false
那么如何才能设置该值呢?
这里须要考虑一个常见的问题,参数传递
,传值仍是传引用或地址?
在上面的例子中,咱们使用的是reflect.ValueOf(x)
,这是一个值传递,传递的是x的值的一个副本,不是x自己,所以更新副本中的值是不容许的。若是使用reflect.ValueOf(&x)
来替换刚才的值传递,就能够实现值的修改。get
var x float64 = 3.4 p := reflect.ValueOf(&x) // 获取x的地址 fmt.Println("settability of p: ", p.CanSet()) v := p.Elem() fmt.Println("settability of v: ", v.CanSet()) v.SetFloat(7.1) fmt.Println(v.Interface()) fmt.Println(x)
settability of p: false settability of v: true 7.1 7.1
首先介绍如何遍历结构体字段内容,
假设结构体以下,string
type T struct { A int B string } t := T{12, "skidoo"}
从而,经过反射来遍历全部的字段内容it
s := reflect.ValueOf(&t).Elem() typeOfT := s.Type() for i := 0; i < s.NumField(); i++ { f := s.Field(i) fmt.Printf("%d %s %s = %v\n", i, typeOfT.Field(i).Name, f.Type(), f.Interface()) }
0 A int = 23 1 B string = skidoo
接下来,如何获取结构体的标签内容?
func main() { type S struct { F string `species:"gopher" color:"blue"` } s := S{} st := reflect.TypeOf(s) field := st.Field(0) fmt.Println(field.Tag.Get("color"), field.Tag.Get("species")) }
通常状况下,为了存储多个函数值,通常采用map
来存储。其中key为函数名称,而value为相应的处理函数。
在这里须要定义好函数类型,可是函数的参数以及返回类型就须要是统一的,以下
package main import "fmt" func say(text string) { fmt.Println(text) } func main() { var funcMap = make(map[string]func(string)) funcMap["say"] = say funcMap["say"]("hello") }
若是但愿map
能够存储任意类型的函数(参数不一样,返回值不一样),那么就须要用interface{}而不是func(param...)来定义value。
package main import "fmt" func say(text string) { fmt.Println(text) } func main() { var funcMap = make(map[string]interface{}) funcMap["say"] = say funcMap["say"]("hello") }
cannot call non-function funcMap["say"] (type interface {})
直接调用会报错,提示不能调用interface{}类型的函数。
这时,须要利用reflect
把函数从interface转换到函数来使用,
package main import ( "fmt" "reflect" ) func say(text string) { fmt.Println(text) } func Call(m map[string]interface{}, name string, params ... interface{}) (result []reflect.Value) { f := reflect.ValueOf(m[name]) in := make([]reflect.Value, len(params)) for k, param := range params { in[k] = reflect.ValueOf(param) } result = f.Call(in) return } func main() { var funcMap = make(map[string]interface{}) funcMap["say"] = say Call(funcMap, "say", "hello")