当前位置:网站首页>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
边栏推荐
- Simpledateformat thread unsafe
- 伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
- 全上链哈希游戏dapp系统定制(方案设计)
- After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
- Material management system based on SSM (source code + document + database)
- Pytest testing framework
- Leetcode (146) - LRU cache
- The Google File System (GFS) learning notes
- NPM download speed is slow
- List set Introduction & common methods
猜你喜欢
Nifi quick installation (stand-alone / cluster)
Sleep revolution - find the right length of rest
基于QT+MySQL的相机租赁管理系统
Shrimp skin test surface treated
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
Sequential stack traversal binary tree
The four stages of cloud computing development have finally been clarified
Background of master data construction
得物多活架构设计之路由服务设计
图像PANR
随机推荐
After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked
After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
网络安全审查办公室对知网启动网络安全审查
Pytest test framework II
Geek University cloud native training camp
Procedural life: a few things you should know when entering the workplace
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
The difference between RPC and restful
Packaging_ Conversion between basic type and string type
Sequential stack traversal binary tree
Set up your own website (14)
Several common command operations in win system
Grating diffraction
An example illustrates restful API
Nifi quick installation (stand-alone / cluster)
Learn together and make progress together. Welcome to exchange
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
C language to realize mine sweeping (simple version)
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
Enjoy yuan mode -- a large number of flying dragons