Reflection reflect
struct
reflect.TypeOf(i interface{})-->Type
gets the type and field name of the field of the structure
reflect.ValueOf(i interface{})-->Value
gets the value of the field of the structure
//Anonymous field
t1.FieldByIndex([]int{0,0})
v1.FieldByIndex([]int{0,0})
//Change data
reflect.ValueOf(p1)-->value
reflect.Struct
reflect.ValueOf (&p1)-->value
reflect.Ptr
v1 = v1.Elem()
CanSet()-->bool
c1.FieldByName()-->f1
struct
reflect.TypeOf(i interface{})-->Type
gets the type and field name of the field of the structure
the type of the method, the name of the method
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int Sex string } func (p Person)Say(msg string) { fmt.Println("hello,",msg) } func (p Person)PrintInfo() { fmt.Printf( "Name: %s, Age: %d, Gender: %s\n" , p.Name , p.Age , p.Sex) } func main () { //1. Create a struct object p1 := Person{ "Wang Ergou" , 30 , "Male" } //1. Part 1: Manipulate the fields in the structure through reflection: get the fields of the fields Type, name, value. . //Get the type of p1 through TypeOf() under the reflect package t1:=reflect.TypeOf(p1) fmt.Println(t1) //main.Person fmt.Println(t1.Name()) //Person fmt.Println(t1.Kind()) //struct //Get the value of p1 through ValueOf() in the reflect package Value v1:=reflect.ValueOf(p1) //{Wang Ergou 30 male} fmt.Println(v1) //Detailed operation: determine the type of t1 is the structure type if t1.Kind() == reflect. Struct { //Get the number of fields count:=t1.NumField() //3 fields fmt.Println(count) for i:=0;i<count;i++{ filed:=t1.Field(i) val:=v1.Field(i).Interface() //Get the value of the corresponding field according to v1, ---> empty interface type //fmt.Println(filed ) fmt.Println( "Field name:" , filed. Name , "Field Type:" , filed.Type , "Field Value:" , val) } } //2. Through reflection, the operation method fmt.Println(t1.NumMethod()) for i:= 0 ; i<t1.NumMethod() ; i++{ method:=t1.Method(i) fmt.Println(method.Name,method.Type) } //Execute method m1:=v1.MethodByName( "PrintInfo" ) m1.Call(nil) //Execute PrintInfo() corresponding to m1 m2:=v1.MethodByName( "Say" ) m2.Call([]reflect.Value{reflect.ValueOf( "Hello " )}) // string-->Value }
reflect.ValueOf(i interface{})-->Value
gets the value of the field of the structure
Get the object of the method--->Call(), execute the corresponding method
package main import ( "reflect" "fmt" ) type Animal struct { Name string Age int } type Cat struct { Animal Color string } func main() { c1 := Cat{Animal{"猫咪", 1}, "白色"} t1 := reflect.TypeOf(c1) fmt.Println(t1.NumField()) for i := 0; i < t1.NumField(); i++ { field := t1.Field(i) fmt.Println(field) } //获取匿名字段 /* 匿名字段,可以通过下标获取 FieldByIndex([]int{0})-->Animal FieldByIndex([]int{0,0})-->Animal中的第一个字段:Name */ f1:=t1.FieldByIndex([]int{0,0}) fmt.Println(f1.Name,f1.Type)//Name string f2:=t1.FieldByIndex([]int{0,1}) fmt.Println(f2.Name,f2.Type) v1:=reflect.ValueOf(c1) name:=v1.FieldByIndex([]int{0,0}) fmt.Println(name) }
//Anonymous field
t1.FieldByIndex([]int{0,0})
v1.FieldByIndex([]int{0,0})
//Change data
reflect.ValueOf(p1)-->value
reflect.Struct
reflect.ValueOf (&p1)-->value
reflect.Ptr
v1 = v1.Elem()
CanSet()-->bool
c1.FieldByName()-->f1
f1.SetXXX(新value)
package main import ( "reflect" "fmt" ) type Student struct { Name string Age int School string } func main() { /* 通过反射,来更改对象的数值:前提是数据可以被更改 */ s1:=Student{"王二狗",19,"新东方"} fmt.Printf("%T\n",s1) //main.Student p1:=&s1 fmt.Printf("%T\n",p1) //*main.Student fmt.Println(s1.Name) fmt.Println((*p1).Name,p1.Name) v1:= reflect.ValueOf(&s1) // value if v1.Kind()==reflect.Ptr{ fmt.Println(v1.Elem().CanSet()) v1 = v1.Elem() } f1:=v1.FieldByName("Name") f1.SetString("王三狗") f3:=v1.FieldByName("School") f3.SetString("蓝翔学校") fmt.Println(s1) }