当前位置:网站首页>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
边栏推荐
- Nifi quick installation (stand-alone / cluster)
- What does virtualization mean? What technologies are included? What is the difference with private cloud?
- [performance tuning basics] performance tuning standards
- Summary of message protocol problems
- Open programmable infrastructure (OPI) project, redefining dpu/ipu
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
- Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
- Postman assertion
- The four stages of cloud computing development have finally been clarified
- Map跟object 的区别
猜你喜欢

Background operation retry gave up; KeeperErrorCode = ConnectionLoss

After a few years in the testing industry, do you still know a little?

Haitai Advanced Technology | application of privacy computing technology in medical data protection

Curl command

How to apply agile development ideas to other work

After screwing the screws in the factory for two years, I earned more than 10000 yuan a month by "testing" and counterattacked

Adding subscribers to a list using mailchimp's API V3

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

The Google File System (GFS) learning notes

Procedural life: a few things you should know when entering the workplace
随机推荐
What will you do if you have been ignored by your leaders at work?
Mapstacks: data normalization and layered color layer loading
Background operation retry gave up; KeeperErrorCode = ConnectionLoss
Map跟object 的区别
List set Introduction & common methods
opds sql组件能不能将流程参数通过上下文传给下一个组件
Static routing job supplement
图像PANR
After a few years in the testing industry, do you still know a little?
Design of routing service for multi Activity Architecture Design
Use the transparent [x] cross button image in the dialog
全上链哈希游戏dapp系统定制(方案设计)
Web automation: summary of special scenario processing methods
Enjoy yuan mode -- a large number of flying dragons
A/b test helps the growth of game business
Comprehensive comparison of the most popular packet capturing tools in the whole network
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
浅谈MySql update会锁定哪些范围的数据
Handling of garbled JMeter response data - three solutions
Common member methods of the calendar class