Three laws of reflection: 1. Reflection can convert "interface type variables" into "reflection type objects", where the reflection type refers to "reflect.Type" and "reflect.Value"; 2. Reflection can convert " "Reflection type object" is converted into "interface type variable"; 3. If you want to modify the "reflection type object", its value must be "writable".
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
In the world of reflection, we have the ability to obtain the type, properties and methods of an object.
In the world of Go reflection, there are two types that are very important and are the basis for the entire The core of reflection, when learning how to use the reflect package, you must first learn these two types:
reflect.Type
reflect. Value
They correspond to type and value in the real world respectively, but in the reflection object, they have more content.
From the source code point of view, reflect.Type exists in the form of an interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
and reflect.Value exists in the form of a structure,
1 2 3 4 5 |
|
At the same time It accepts a lot of methods (see the table below), and due to space limitations, there is no way to introduce them one by one here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
Through the content of the previous section (), we know that an interface variable is actually composed of a pair (type and data). The pair records the value and value of the actual variable. type. That is to say, in the real world, type and value are combined to form interface variables.
In the world of reflection, type and data are separated, and they are represented by reflect.Type and reflect.Value respectively.
There is a Three Laws of Reflection in the Go language, which is a very important reference when you are learning reflection:
Reflection goes from interface value to reflection object.
Reflection goes from reflection object to interface value.
To modify a reflection object, the value must be settable.
Translated, it is:
Reflection can convert "interface type variable" to "Reflection type object";
Reflection can convert "reflection type object" into "interface type variable";
If you want to modify" Reflection type object" its type must be "writable";
Reflection goes from interface value to reflection object.
In order to realize the conversion from interface variables to reflection objects, two very important methods in the reflect package need to be mentioned:
reflect. TypeOf(i): Get the type of the interface value
reflect.ValueOf(i): Get the value of the interface value
These two methods The returned objects are called reflection objects: Type object and Value object.
For example, let’s see how these two methods are used?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
The output is as follows
1 2 |
|
In this way, we have completed the conversion from interface type variables to reflection objects.
Wait a minute, isn’t the age we defined above an int type? How come it is said to be an interface type in the first rule?
Regarding this point, in fact, it has been mentioned in the previous section (Three "hidden rules" about interfaces). Since the two functions TypeOf and ValueOf receive interface{ } Empty interface type, and Go language functions are all passed by value, so Go language will implicitly convert our type into an interface type.
1 2 3 |
|
Reflection goes from reflection object to interface value.
Just the opposite of the first law, The second law describes the conversion from reflected objects to interface variables.
It can be seen from the source code that the structure of reflect.Value will receive the Interface
method and return a variable of type interface{}
(Note: Only Value can be reverse-converted, but Type cannot. This is also easy to understand. If Type can be reverse-converted, what can it be reverse-converted into? )
1 2 3 4 5 6 7 8 |
|
This function is what we use To implement a bridge that converts reflection objects into interface variables.
Examples are as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
输出如下
1 2 3 4 |
|
当然了,最后转换后的对象,静态类型为 interface{}
,如果要转成最初的原始类型,需要再类型断言转换一下,关于这点,我已经在上一节里讲解过了,你可以点此前往复习:()。
1 |
|
至此,我们已经学习了反射的两大定律,对这两个定律的理解,我画了一张图,你可以用下面这张图来加强理解,方便记忆。
To modify a reflection object, the value must be settable.
反射世界是真实世界的一个『映射』,是我的一个描述,但这并不严格,因为并不是你在反射世界里所做的事情都会还原到真实世界里。
第三定律引出了一个 settable
(可设置性,或可写性)的概念。
其实早在以前的文章中,我们就一直在说,Go 语言里的函数都是值传递,只要你传递的不是变量的指针,你在函数内部对变量的修改是不会影响到原始的变量的。
回到反射上来,当你使用 reflect.Typeof 和 reflect.Valueof 的时候,如果传递的不是接口变量的指针,反射世界里的变量值始终将只是真实世界里的一个拷贝,你对该反射对象进行修改,并不能反映到真实世界里。
因此在反射的规则里
CanSet()
来获取得知1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
输出如下
1 |
|
要让反射对象具备可写性,需要注意两点
创建反射对象时传入变量的指针
使用 Elem()
函数返回指针指向的数据
完整代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
输出如下
1 2 |
|
知道了如何使反射的世界里的对象具有可写性后,接下来是时候了解一下如何对修改更新它。
反射对象,都会有如下几个以 Set
单词开头的方法
这些方法就是我们修改值的入口。
来举个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
输出如下
1 2 |
|
The above is the detailed content of What are the three laws of reflection in go language. For more information, please follow other related articles on the PHP Chinese website!