The article number originated in public links: Michael mo coding 】 【 mp.weixin.qq.com/s/vFt06c9he…

Go fire the first shot links: typeOf use 】 【 mp.weixin.qq.com/s/F8yZyqC5U…

Go reflection second link: valuef use 】 【 mp.weixin.qq.com/s/lgZykTL8l…

directory

  • isNil()/isValid()
  • Reflection creates object
  • Call () function
  • Small talk
  • Welcome to join my official account [Maimocoding] pk big factory together

isNil()/isValid()

In go, when the reflect.valueof () method is used to obtain the reflect.value object, some methods are provided in the reflect.value object to determine whether the Value is null, as shown in the following table:

The sample

package main
import (
   "fmt"
   "reflect"
)
type User struct{
   Name string
   Age int
}
func main(a){
   user := &User{
      Name : "Michael mo coding",
   }
   values := reflect.ValueOf(user)
   fmt.Println("Is the user object empty:", values.IsNil())
   values = values.Elem()
   fmt.Println(Does the Name field attribute exist:,           values.FieldByName("Name").IsValid())
}
Copy the code

Results:

Whether the user object is empty:falseDoes the Name field attribute exist:true
Copy the code

Code description:

- the first11Line: Instantiate the User object - line15Line: Determines whether the reflection object values is null - number17Line: Determines the reflection object"Name"Whether the field attribute is validCopy the code

Reflection creates object

In go, if the reflect.type Type is known, an instance can be created by reflection with the reflect.new () method, but only an object of Type reflect.type can be passed in.

The sample

package main

import (
  "fmt"
  "reflect"
)

type User struct{
   Name string
}
func main(a) {
   types := reflect.TypeOf(&User{})
   values := reflect.New(types)
fmt.Println("Reflection creates the type and kind of object :", values.Type(), values.Kind())
}
Copy the code

The results of

Reflection creates the type and kind of object: ** main.user PTRCopy the code

Call () function

Reflect. Value can be called from reflect.Value if the Value in the reflect.Value object is of type function. When a function is called using reflection, the argument is passed to the Call() method using a slice of the reflection Value object [] reflect.value. When the Call is complete, the return Value of the function is returned via [] reflect.value.

The sample

package main

import (
  "fmt"
  "import"
)

func add(x, y int) int {
  return x + y 
}

func main(a) {
  addValue := reflect.ValueOf(add)
   // Constructor argument, passing in two integer values
    paramList := []reflect.Value{reflect.ValueOf(10),   reflect.ValueOf(20)}
    // reflection calls the function
    retList := funcValue.Call(paramList)
    // Get the first return value, integer value
    fmt.Println(retList[0].Int())
}
Copy the code

The code description is as follows:

The first9~12Line: Define a normal addition function. The first17Line: Wrap the Add function as a reflection value object. The first20Line:1020ValueOf wraps two integer values as reflect.Value, and takes a slice of the reflected Value object []reflect.Value as an argument to the function. The first23Line: Call add() using the Call() method of the funcValue value object, passing in the parameter list paramList. The first26Line: After the call is successful, use retList[0] takes the first argument of the return value, using Int to take the integer value of the return value.Copy the code

prompt

The process of reflection calling a function requires the construction of a large number of reflects. values and intermediate variables, the checking of function parameter values one by one, and the copying of the call parameters into the parameter memory of the calling function. After the call, the return Value needs to be converted to reflect.Value, and the user needs to fetch the call Value from it. Therefore, the performance of reflection calling functions is particularly problematic and extensive use of reflection function calls is not recommended.

Small talk

  • After reading the article, oneself is not and the cp rate of reflection has increased again
  • I’m Maimo, welcome to talk to me

If you think the article is well written, please click 👍 to encourage you

Welcome to join my official account [Maimo Coding] pk Dachan-Maimo Coding welcome the arrival of guest officers