【Go】Reflection in Go language

Table of contents

Reflection in Go language


Reflection in Go language

Reflection in the Go language refers to the ability to dynamically check, access, and manipulate the type of a program at runtime. Through reflection, we can obtain type information, call its methods, modify its field values, etc. without knowing the specific type.

Reflection is very useful in some scenarios, such as:

  1. Dynamically parse and process data of unknown types.
  2. Check the field, method and label information of the structure at runtime.
  3. Dynamically call a function based on its name.
  4. Modify the value of a variable through reflection.

Here are some common functions and operations using reflection:

  1. reflect.TypeOf(): Returns the type information of a variable.
  2. reflect.ValueOf(): Returns the value information of a variable.
  3. Value.Interface(): Convert the reflected value to the interface type.
  4. Type.Kind(): The underlying type of the return type.
  5. Type.Field(): Returns the field information of the structure.
  6. Type.Method(): The method information of the return type.
  7. Value.FieldByName(): Obtain the field value of the structure according to the field name.
  8. Value.MethodByName(): The method to obtain the type according to the method name.
  9. Value.SetString(): Set the value of string type.
  10. Value.SetInt(): Set the value of integer type.
  11. Value.Call(): Call a function or method.

Here is a simple example that demonstrates how to use reflection to get the type and value of a variable, and to modify the value of a variable:

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	// 创建一个Person对象
	person := Person{
		Name: "John",
		Age:  30,
	}

	// 获取变量的类型信息
	personType := reflect.TypeOf(person)
	fmt.Println("类型:", personType)

	// 获取变量的值信息
	personValue := reflect.ValueOf(person)
	fmt.Println("值:", personValue)
}

 In the Go language, Value.SetString()when using a method to modify a value of string type, it is required that the parameter passed in must be addressable, not a value whose address cannot be obtained.

In your example code, Namethe fields are part of the struct Person, and struct fields are not addressable by default. Therefore, after directly using FieldByName()the method to get Namethe field, the object it returns Valueis not addressable.

To solve this problem, you can use reflect.Value.Addr()methods to get the addressable value of the field, and then call SetString()the method to modify it. Here is the modified sample code:

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	// 创建一个Person对象
	person := Person{
		Name: "John",
		Age:  30,
	}

	// 获取变量的值信息
	personValue := reflect.ValueOf(&person).Elem()

	// 修改变量的值
	nameField := personValue.FieldByName("Name")
	if nameField.IsValid() && nameField.Kind() == reflect.String {
		nameField.SetString("Jane")
	}

	ageField := personValue.FieldByName("Age")
	if ageField.IsValid() && ageField.Kind() == reflect.Int {
		ageField.SetInt(35)
	}

	fmt.Println("修改后的值:", person)
}

 In the above code, we use the addressable value of the ValueOf(&person).Elem()obtained object, and then perform the field modification operation.Person

Guess you like

Origin blog.csdn.net/fanjufei123456/article/details/132103604