16 Go Reflection

Overview

        In the previous section, we introduced Go’s concurrency, including: Goroutines, Channels, WaitGroups, Mutex, Select, etc. In this section, we will introduce reflection in Go. Reflection in the Go language is the ability to check type information and operate objects at runtime. Through reflection, you can dynamically check the type of variables, call functions, and modify the values ​​of variables. In the Go language, reflection is mainly implemented through the reflect package. This package provides a set of functions for dynamically analyzing and manipulating object types and values ​​at runtime.

        Reflection is a powerful and complex feature, and it is generally not recommended to use it too much in regular programs. Therefore, only some common reflection operations are introduced below.

reflect.TypeOf

        The reflect.TypeOf function is used to dynamically obtain the type information of a value at runtime. Its syntax is as follows:

          func reflect.TypeOf(value) reflect.Type

        ​​​​​Among them, value is the value to obtain type information. reflect.TypeOf returns a value of type reflect.Type, indicating the type information of the value. reflect.Type is an interface type that defines some functions for manipulating type information. For example, the String function is used to obtain the string representation of the type, the Kind function is used to obtain the type of the type, etc.

package main

import (
 "fmt"
 "reflect"
)  
  
func main() {
    var num float64 = 3.14
    t := reflect.TypeOf(num)
    // 均输出: float64
    fmt.Println(t)
    fmt.Println(t.String())
    fmt.Println(t.Kind())
}

reflect.ValueOf

        The reflect.ValueOf function is used to obtain the reflection object of a value. Its syntax is as follows:

          func ValueOf(v interface{}) reflect.Value

        Where, v is a value of interface{} type, which can be a value of any type. The function returns an object of type reflect.Value, which represents the reflection object of the value. The reflect.Value type is one of the most important types in the reflection package. It provides a set of functions for manipulating the value of the type, such as getting the type, address, value, etc. of the value.

package main

import (
 "fmt"
 "reflect"
)

func main() {
    var text string = "Hello, CSDN"
    value := reflect.ValueOf(text)
    // 输出: Hello, CSDN
    fmt.Println(value)
}

reflect.Value

        The value of a variable can be modified through the Set function of reflect.Value. Note: The Set function can only succeed if the variable is addressable and modifiable.

package main

import (
 "fmt"
 "reflect"
)

func main() {
    var num float64 = 66.6
    value := reflect.ValueOf(&num).Elem()  
    value.SetFloat(88.8)
    // 输出: 88.8
    fmt.Println(num)
}

        In addition, you can also call the method of the structure through the MethodByName function of reflect.Value.

        In the following example code, we define a structure named Person and bind the function OuputInfo. Then, we use the reflect.ValueOf() function to obtain the reflection object value of the person object. Next, we obtain the pointer of the function OuputInfo through the MethodByName function of value. Finally, we call the function using the Call() method and pass it the parameter list.

package main

import (
 "fmt"
 "reflect"
)

type Person struct {
    name string
}

func (person Person) OuputInfo(age int) {
    fmt.Println("name is", person.name, ", age is", age)
}

func main() {
    person := Person{name: "Mike"}
    value := reflect.ValueOf(person)
    method := value.MethodByName("OuputInfo")
    // 创建参数列表  
    args := []reflect.Value{
        reflect.ValueOf(15),
    }
    
    // 输出: name is Mike , age is 15
    method.Call(args)
}

Guess you like

Origin blog.csdn.net/hope_wisdom/article/details/134495063
Recommended