Golang is a statically typed language with object-oriented features. Reflection is the ability to obtain the type of a value and operate on it at runtime. Golang has a built-in reflection mechanism, which can modify the attribute value of an object through reflection. This article will introduce the relevant knowledge points of Golang reflection settings.
First of all, you need to understand the commonly used reflection types in Golang. In Golang, the reflection type (reflect.Type) is an interface type. It defines the type information of an object, including type name, type size, alignment, method set, etc.
In Golang, we can obtain the type information of an object through reflection. For example, the following code can obtain the type information of variable a:
package main import ( "fmt" "reflect" ) func main() { var a = 10 t := reflect.TypeOf(a) fmt.Println("TypeOf a:", t) }
The output result is as follows:
TypeOf a: int
As you can see, we obtained the variable through the reflect.TypeOf
function The type of a, the result obtained is of type int
.
In addition to basic types, you can also obtain structure, function, pointer and other types of information through reflection. For example, the following code can obtain the type information of the structure:
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { var p = Person{"John", 30} t := reflect.TypeOf(p) fmt.Println("TypeOf p:", t) }
The output result is as follows:
TypeOf p: main.Person
As you can see, we obtained the structure through the reflect.TypeOf
function Type information of bodyPerson
.
In addition to the reflection type, Golang also has the concept of reflection value (reflect.Value). A reflected value is an interface type that contains the value and type information of an object. In Golang, we can obtain the value and type information of an object through reflection. For example, the following code can obtain the value and type information of variable a:
package main import ( "fmt" "reflect" ) func main() { var a = 10 v := reflect.ValueOf(a) fmt.Println("ValueOf a:", v) fmt.Println("TypeOf a:", v.Type()) }
The output result is as follows:
ValueOf a: 10 TypeOf a: int
As you can see, we obtain it through the reflect.ValueOf
function The value and type information of variable a are obtained.
Similarly, in addition to basic types, you can also obtain values and type information of structures, functions, pointers and other types through reflection. For example, the following code can obtain the value and type information of the structure:
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { var p = Person{"John", 30} v := reflect.ValueOf(p) fmt.Println("ValueOf p:", v) fmt.Println("TypeOf p:", v.Type()) }
The output result is as follows:
ValueOf p: {John 30} TypeOf p: main.Person
As you can see, we obtain it through the reflect.ValueOf
function The value and type information of structure Person
is obtained.
After we obtain the type and value of an object, we can use reflection to modify the attribute value of the object. Generally speaking, we can obtain the value pointed to by the pointer through the Elem()
method of reflection. For example, the following code can modify the attribute value of the structure:
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { var p = &Person{"John", 30} v := reflect.ValueOf(p).Elem() nameValue := v.FieldByName("Name") nameValue.SetString("Tom") fmt.Println(p) }
The output result is as follows:
&{Tom 30}
As you can see, we obtained the structure through the FieldByName
method of reflection The value of the body attribute, and the value of the Name
attribute was modified using the SetString
method.
In addition to modifying the values of structure attributes, you can also modify function parameter values through reflection. For example, the following code can modify the parameter value of the function:
package main import ( "fmt" "reflect" ) func Add(a, b int) int { return a + b } func main() { f := reflect.ValueOf(Add) args := []reflect.Value{reflect.ValueOf(10), reflect.ValueOf(20)} f.Call(args) fmt.Println("Before:", args) args[0] = reflect.ValueOf(100) args[1] = reflect.ValueOf(200) f.Call(args) fmt.Println("After:", args) }
The output result is as follows:
Before: [10 20] After: [100 200]
As you can see, we called the function through the reflection Call
method Add
And modified the parameters of the function.
When using Golang reflection settings, you need to pay attention to the following points:
Golang reflection settings are a powerful feature that can obtain the type of an object and the ability to operate on it at runtime. In this article, we introduce the relevant knowledge points about reflection types, reflection values and reflection settings in Golang. Through studying this article, I believe that readers have a deeper understanding of Golang reflection settings.
The above is the detailed content of Detailed explanation of relevant knowledge points of Golang reflection settings. For more information, please follow other related articles on the PHP Chinese website!