golang: reflect reflection

Because C++ has been the main development language before, I felt very confused when I first came into contact with reflect in the go language, so I decided to find materials to study it thoroughly.

What exactly is reflection?

https://blog.golang.org/laws-of-reflection

According to the introduction in this article:

Reflection in computing is the ability of a program to examine its own structure, particularly through types; it's a form of metaprogramming. 

Reflection is the ability for a program to inspect its own structure, especially through types, which is a form of metaprogramming.

This article explains the concept of reflection by introducing how reflect works in Go.

 

The main points of the text are as follows:

1. Representation of the interface

An interface type variable stores a pair of values: the concrete value and the type description of the value.

2. The three rules of reflection

1. From interface to reflection object

Obtain the content in the interface through two methods: reflect.TypeOf and reflect.ValueOf, and return Type and Value respectively.

2. From reflective objects to interfaces

Returns an empty interface{} from a Value type via the Interface method

3. If you want to modify a reflection object, the value must be settable

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324874846&siteId=291194637