当前位置:网站首页>Reflect package

Reflect package

2022-06-24 21:07:00 AcTarjan

  • golang The reflection of is through reflect Package to complete , Or through operation reflect The package Type and Value Two structures

Type

Function introduction

//TypeOf  Return one to reflect.Type It means i, This is where type reflection is used 
reflect.TypeOf(i interface) Type

//Elem  Return to recipient t The inner element of Type
// Be careful : The receiver t Of Kind yes Array Chan Map Ptr or Slice, Other types will panic
func (t *rtype) Elem() Type

//Implements  Return to recipient t Is it implemented u Represented by Interface
func (t *rtype) Implements(u Type) bool

//Kind  Return to recipient t The type of , Yes reflect.String、reflect.Struct、reflect.Ptr and reflect.Interface etc. 
func (t *rtype) Kind() Kind

//NumFiled  Return to recipient t The number of fields in the structure of the 
//NumFiled  Be careful : The receiver t Of Kind Must be Struct, Other ( Including structure pointer ) Meeting panic
func (t *rtype) NumFiled() int

//Filed  Return to StructField The recipient of the t In the structure of i A field 
//Filed  Be careful : The receiver t Of Kind Must be Struct, Other ( Including structure pointer ) Meeting panic
func (t *rtype) Filed(i int) StructField

//NumMethod  Return to recipient t The number of ways 
//NumMethod  Note whether the receiver of the method is a struct or a pointer to a struct  
func (t *rtype) NumMethod() int

//Method  Return to Method The recipient of the t Represented by i A way 
func (t *rtype) Method(i int) Method

//NumIn  Return to recipient t The number of arguments to the function represented 
//NumIn  The receiver t Of Kind Must be Func, Other types will panic
func (t *rtype) NumIn() int

//In  Return to recipient t The th... Of the function represented i Parameters Type
//In  The receiver t Of Kind Must be Func, Other types will panic
func (t *rtype) In(i int) Type

//NumOut  Return to recipient t The number of return values of the function represented 
//NumOut  The receiver t Of Kind Must be Func, Other types will panic
func (t *rtype) NumOut() int

//Out  Return to recipient t The th... Of the function represented i Of a return value Type
//Out  The receiver t Of Kind Must be Func, Other types will panic
func (t *rtype) Out(i int) Type

//StructField  Cutting part 
type StructField struct {
    
	Name string			// Name is the field name.
	Type      Type      // field type
	Tag       StructTag // field tag string
	Anonymous bool      // is an embedded field
}

Example

type Person struct {
    
	name string		`default:"Bob"`
	age int			`default:"10"`
}
// Customize tag
p := Person{
    
	name: "AcTarjan",
	age:  22,
}
ptype := reflect.TypeOf(&p).Elem()
for i := 0;i < ptype.NumField();i++ {
    
	field := ptype.Field(i)
	val,ok := field.Tag.Lookup("default")
	if ok {
    
		fmt.Println(field.Name,val)	
	}
}
/*  Running results : name Bob age 10 

Value

Function introduction

//ValueOf  Return one to reflect.Value It means i, This is the entry to use value reflection 
reflect.ValueOf(i interface) Value

//Interface  return Value Of Interface{} Express , Will reallocate memory , Then it can be strongly converted to a certain type 
//Interface  Be careful : If the recipient is obtained by accessing the non exported field , will panic
func (v value) Interface() (i interface{
    })

//Kind  Return to recipient v The type of , Yes reflect.String、reflect.Struct、reflect.Ptr and reflect.Interface etc. 
func (v Value) Kind() Kind

//Elem  If the recipient v Of Kind yes Ptr, This method will return an Value The structure pointed to by this pointer 
// If the recipient v Of Kind yes Interface, This method will return an Value It means Interface The structure contained 
func (v Value) Elem() Value

//NumFiled  Return to recipient v The number of fields in the structure of the 
//NumFiled  Be careful : The receiver v Of Kind Must be reflect.Struct, Other ( Including structure pointer ) Meeting panic
func (v Value) NumFiled() int

//Filed  Return to Value The recipient of the v In the structure of i A field 
//Filed  Be careful : The receiver v Of Kind Must be reflect.Struct, Other ( Including structure pointer ) Meeting panic
func (v Value) Filed(i int) Value

//NumMethod  Return to recipient v The number of ways 
//NumMethod  Note whether the receiver of the method is a struct or a pointer to a struct  
func (v Value) NumMethod() int

//Method  Return to Value The recipient of the v Represents the second i A way 
func (v Value) Method(i int) Value

//Call  Call recipient v The function represented ,in Is the parameter of the function 
//Call  Be careful : The receiver v Of Kind Must be reflect.Func
func (v Value) Call(in []Value) []Value

//CanSet  Return to recipient v Of Value Is it modifiable , If CanSet is false, call Set Method Society panic
//CanSet  Be careful : The recipient must be addressable ( The pointer ), And Set The fields of are exported 
func (v Value) CanSet() bool

//SetString  Modify recipient v Value , among v Of Kind Must be reflect.String, Otherwise panic
func (v Value) SetString(x string)

//String  Return to recipient v Value , among v Of Kind If reflect.String, Others will return "<T Value>",T by v Of Kind
func (v Value) String() string

Example

type Person struct {
    
	name string
	age int
}

func (p *Person) Age() int {
    
	return p.age
}

func (p *Person) SetAge(age int) {
    
	p.age = age
}

// Modify the value of the structure field 
p := Person{
    
	Name: "AcTarjan",
	age:  22,		//field age is unexported 
}
val := reflect.ValueOf(&p).Elem()
field := val.FieldByName("Name")
field.SetString("Guo")		// here p = {"Guo",22}
fmt.Println(field.String())	// Output :Guo

// Call the method of the struct 
p := Person{
    
	Name: "AcTarjan",
	age:  22,
}
val := reflect.ValueOf(&p)
method := val.MethodByName("SetAge")
in := []reflect.Value{
    reflect.ValueOf(30)}
method.Call(in)						// here p = {"AcTarjan",30}
method = val.MethodByName("Age")
fmt.Println(method.Call(nil)[0])	// Output :30
原网站

版权声明
本文为[AcTarjan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211319437990.html