Home > Backend Development > Golang > golang reflection acquisition method

golang reflection acquisition method

王林
Release: 2023-05-14 15:15:08
Original
728 people have browsed it

In the Go language, reflection is a very important feature. It can obtain the type, value, method and other information of variables at runtime, and implement many advanced functions, such as object serialization, automatic code generation, etc. Among them, the method of obtaining objects through reflection is also very useful.

In the Go language, we can use the Type and Value structures provided in the reflection package to obtain the object's methods. The Type structure can obtain the type information of the object, including the method collection of the type; the Value structure can obtain the value information of the object, including the parameter information required to call the method.

Below, we use an example to demonstrate how to use reflection to obtain an object.

First, suppose we have a structure type Person, which contains two methods GetAge and GetName:

type Person struct {
    name string
    age int
}

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

func (p *Person) GetName() string {
    return p.name
}
Copy after login

Next, we define an object p of type Person and use reflection to obtain its method Collection:

p := &Person{"Alice", 18}

t := reflect.TypeOf(p)
for i := 0; i < t.NumMethod(); i++ {
    m := t.Method(i)
    fmt.Println(m.Name)
}
Copy after login

In the above code, we first use the reflect.TypeOf function to get the type information t of p, then use the t.NumMethod function to get the number of methods of type p, and then use the t.Method function to get each one one by one. Information about each method, and finally prints the name of the method m.Name.

The output result is:

GetAge
GetName
Copy after login

As you can see, we successfully obtained all methods of the Person type through reflection.

Next, we can use reflection to obtain the value information of the method so that the method can be called dynamically. Specifically, we define a string variable name and dynamically call the corresponding method according to the name entered by the user:

var name string
fmt.Println("Please enter method name:")
fmt.Scanln(&name)

v := reflect.ValueOf(p)
m := v.MethodByName(name)
if m.IsValid() {
    result := m.Call(nil)
    fmt.Println(result)
} else {
    fmt.Println("Method not found")
}
Copy after login

In the above code, we first use the reflect.ValueOf function to obtain the value information v, and then use The v.MethodByName function obtains the method information m based on the name entered by the user. If the method exists, the m.Call function is used to dynamically call the method and print the result. Note: The Call function needs to pass an empty parameter list nil.

Now, we can test this code. If we enter "GetAge", 18 will be printed. If we enter "GetName", "Alice" will be printed.

So far, we have successfully obtained the object's method through reflection and implemented the function of dynamically calling the method.

Summary

Through the study of this article, we have learned about the method of reflection to obtain object methods in Go language. Reflection is a very important feature that can help us achieve many advanced functions. Although reflection has a certain performance loss, reflection is very useful in scenarios that require dynamic code generation, deserialization, and framework design. Therefore, we need to learn the knowledge of reflection in depth and flexibly apply it to actual development.

The above is the detailed content of golang reflection acquisition method. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template