Reflection in detail

-Basic introduction

    *Reflection can dynamically obtain all kinds of traversal information at runtime, such as the type and kind of variables
    *If it is a structure variable, you can also obtain the information of the structure itself (including the fields and methods of the structure)
    *Through reflection, you can modify the value of the variable and call the associated method
    *To use reflection, you need to import ("reflect")

-There are two common application scenarios of reflection
    *I don't know which function the interface calls. I determine the specific interface to call at run time according to the incoming parameters. I need to reflect the function or method, such as the following bridging mode, such as
        func bridge(funcPtr interface{},args ...interface{})

    *The first parameter funcptr is passed into the function pointer in the form of interface, and the function parameter args is passed in the form of variable parameter. The funptr function can be dynamically executed by reflection in the bridge function
    *When serializing a structure, if the structure has a specified tag, reflection is also used to generate the corresponding string

-Reflecting important functions and concepts

    *Reflect. Typeof (variable name), get the type of the variable and return the type of reflect. Type
    *Reflect. Valueof (variable name) to get the value of the variable. A struct type when reflect. Value is returned. You can get a lot of information about the variable through reflect. Value
    *Variables, interface {} and reflect. Value can be converted to each other. They are often used in practical development

    var studuentStu
    var num int
    //It's specially used for reflection
    func test (b interface{}){
      //1. How to convert interface {} to reflect. Value
      //2. How to change reflect. Value > interface {}
      //3. How to convert interface {} to original variable type and use type assertion


More details about golang reflection and case exercises, check the links below, if you think it’s useful, help point a star

Reflection theory and operation cases