当前位置:网站首页>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
边栏推荐
- Agency mode -- Jiangnan leather shoes factory
- Web automation: web control interaction / multi window processing / Web page frame
- Rename and delete files
- Does the developer want to change to software testing?
- The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
- 大一女生废话编程爆火!懂不懂编程的看完都拴Q了
- I just purchased a MySQL database and prompted that there are already instances. The console login instance needs to provide a database account. How do I know the database account.
- Sequential stack traversal binary tree
- Leetcode (146) - LRU cache
- Background operation retry gave up; KeeperErrorCode = ConnectionLoss
猜你喜欢

Camera rental management system based on qt+mysql

What does virtualization mean? What technologies are included? What is the difference with private cloud?

Grating diffraction

Image panr

Static routing job supplement
浅谈MySql update会锁定哪些范围的数据

在Dialog中使用透明的【X】叉叉按钮图片

物聯網?快來看 Arduino 上雲啦

传统的IO存在什么问题?为什么引入零拷贝的?

Background of master data construction
随机推荐
Simpledateformat thread unsafe
IDEA Dashboard
How to enhance influence
After idea installs these plug-ins, the code can be written to heaven. My little sister also has to arrange it
Appium introduction and environment installation
Selenium crawl notes
Nifi quick installation (stand-alone / cluster)
After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
微信小程序中使用vant组件
Sequence stack version 1.0
VMware virtual machine setting static IP
JUnit unit test
Grating diffraction
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
Format method and parse method of dateformat class
Appium desktop introduction
Map跟object 的区别
Reflection - class object function - get method (case)
Leetcode(455)——分发饼干
伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI