What I talked about last time was only in ordinary variables Application, relatively speaking, there are not too many usage scenarios.
But the application of reflection in the structure will basically run through the entire Go language foundation.
Sample code
type Student struct { Name string `json:"name" describe:"姓名"` Age int `json:"age" describe:"年龄"` Gender bool `json:"gender" describe:"性别"` Hobby []string `json:"hobby" describe:"爱好"` }
func main() { //实例化结构体 var s1 = Student{ Name: "张三", Age: 18, Gender: true, Hobby: []string{"吃", "喝", "pia", "玩"}, } var t = reflect.TypeOf(s1) fmt.Println(t.Name()) //Student fmt.Println(t.Kind()) //struct fmt.Println(t.NumField()) //结果:4,表示多少个字段 for i := 0; i < t.NumField(); i++ { field := t.Field(i)//每个结构体对象 /* {Name string json:"name" describe:"姓名" 0 [0] false} {Age int json:"age" describe:"年龄" 16 [1] false} {Gender bool json:"gender" describe:"性别" 24 [2] false} {Hobby []string json:"hobby" describe:"爱好" 32 [3] false} */ //fmt.Println(field) fmt.Println("------") fmt.Printf("field.Name:%v\n",field.Name) fmt.Printf("field.Index:%v\n",field.Index) fmt.Printf("field.Type:%v\n",field.Type) fmt.Printf("field.Tag:%v\n",field.Tag.Get("describe")) } }
func main() { //实例化结构体 var s1 = Student{ Name: "张三", Age: 18, Gender: true, Hobby: []string{"吃", "喝", "pia", "玩"}, } var t = reflect.TypeOf(s1) genderField, ok := t.FieldByName("Gender") if ok { fmt.Println(genderField.Name) //Gender fmt.Println(genderField.Index) //[2] fmt.Println(genderField.Type) //bool fmt.Println(genderField.Tag.Get("describe")) //性别 } }
上述的代码只能用的是TypeOf
,只能返回类型等信息,相对来说不是太智能,ValueOf
可以获取值,同样也能获取类型,相对来说比TypeOf
好一点。
示例代码
func main() { //实例化结构体 var s1 = Student{ Name: "张三", Age: 18, Gender: true, Hobby: []string{"吃", "喝", "pia", "玩"}, } var v = reflect.ValueOf(s1) for i := 0; i < v.NumField(); i++ { field :=v.Field(i) fmt.Println("------") fmt.Printf("Kind:%v\n",field.Kind()) fmt.Printf("值:%v\n",field.Interface()) } }
上述我们反射的都是值,有没有反射是否可以反射函数,并且调用函数呢??
type Student struct { Name string `json:"name" describe:"姓名"` Age int `json:"age" describe:"年龄"` Gender bool `json:"gender" describe:"性别"` Hobby []string `json:"hobby" describe:"爱好"` } //无参方法 func (this Student) Say() { fmt.Printf("我是%v,我的年龄是%v,我的性别是%v,我的爱好是%v\n", this.Name, this.Age, this.Gender, this.Hobby) } //有参数方法 func (this Student) Jump(distance int) { fmt.Printf("我是%v,我跳远跳了%v米\n", this.Name, distance) }
func main() { //实例化结构体 var s1 = Student{ Name: "张三", Age: 18, Gender: true, Hobby: []string{"吃", "喝", "pia", "玩"}, } var t = reflect.TypeOf(s1) var v = reflect.ValueOf(s1) fmt.Println(v.NumMethod(),v.NumField()) for i := 0; i < v.NumMethod(); i++ { method := v.Method(i) fmt.Println("--------") fmt.Println(method)//0x48c4e0 函数地址 fmt.Println(method.Type())//func(int) 函数类型,形参和返回值 fmt.Println(t.Method(i).Name)//Jump,函数名,注意,由t来调用的 } }
func main() { //实例化结构体 var s1 = Student{ Name: "张三", Age: 18, Gender: true, Hobby: []string{"吃", "喝", "pia", "玩"}, } var v = reflect.ValueOf(s1) //通过反射调用函数 //调用Jump函数 //反射调用函数必须传一个参数,不管有没有形参都要传 //var args = []reflect.Value{} //v.MethodByName("Say").Call(args) //如果需要传参数 //参数需要用reflect.ValueOf(1) 强转一下 var args = []reflect.Value{reflect.ValueOf(2)} v.MethodByName("Jump").Call(args) }
注:注意第14行和20行代码区别,如果要传参数,参考第20行代码。
在平常开发中,尽量慎用反射,原因如下。
The reflection performance may be relatively low. After all, it travels in the forward direction and is generally one or two levels slower than the forward operation.
The more reflection, the worse the code, TypeOf
and ValueOf
has Kind
, in many cases it is TypeOf
and ValueOf
are mixed, so it is extremely unfriendly to those with poor foundation.
In Go, there is no #try
, if reflection is not handled well Exception, the program will crash directly, possibly in an unexpected place.
# Above we mainly talked about GoReflective structurerelated knowledge, including
Application of reflection in structures
How to reflect structure field information individually
##ValueOfOther operations
How to radiate structure binding method
The above is the detailed content of An article teaches you the basics of Go language structure reflection. For more information, please follow other related articles on the PHP Chinese website!