英文:
How does golang implement reflection?
问题
使用反射,我们可以获取给定类型(如uint64、用户定义的结构体等)的类型名称、存储大小和函数。甚至可以修改给定类型的某些字段。
Golang是如何实现反射的呢?我猜想以下几种方式:
-
在Golang中,每种类型,包括用户定义的类型,都有关于类型名称、字段名称和函数名称的信息。Golang的反射只是读取这些信息或调用函数。
-
通过某种机制,Golang可以获取类型名称、存储大小等信息。而类型本身并不具备这些信息。
我大致阅读了Golang的反射代码,我猜想Golang使用了第二种方式。
谁能描述一下反射的具体实现?或者推荐一些文档给我?阅读所有的代码对我来说有些困难。
英文:
Using reflection, we can get the type name, storage size and the function of the given type(such as uint64, user-defined struct and so on). Even, we can modify some fields of the given type.
How does golang implement reflections? I guess the following ways:
-
Every type in golang, including user-defined type, itself has the information about type name, fields name and the function name. Golang reflection just reads these information or call the function.
-
Through some mechanism, Golang can get the type name, storage size and so on. And the type itself doesn’t have these information.
I have read the golang reflection code roughly. I guessed that golang used the second way.
Who can describe the concrete implement of reflection? Or recommend me some documents? Reading all code is difficult for me.
答案1
得分: 9
这只是一个概述,可能不是100%准确,但希望对你有所帮助。
在构建时,Go链接器将应用程序使用的所有类型的信息嵌入到可执行文件中(https://github.com/golang/go/blob/master/src/runtime/symtab.go#L39)。
每个接口值都包含一个指向数据类型描述符的指针(https://github.com/golang/go/blob/master/src/runtime/type.go#L14)。
在从编译时已知的类型转换为接口值时,Go编译器将该接口值的类型描述符指向具体类型描述符(在编译时已知!)。
例如,当你调用reflect.TypeOf(uint(87))
:
- 编译器创建一个引用
uint
类型描述符的接口值 - 将此接口值作为参数传递给
reflect.TypeOf
函数 reflect.TypeOf
函数使用链接器在可执行文件中存储的类型描述符来获取关于uint
类型的对齐(和其他)信息。
英文:
This is just an overview and it might be not 100% accurate but hopefully you will find it helpful.
At build time Go linker will embed information about all types used by the application into the executable (https://github.com/golang/go/blob/master/src/runtime/symtab.go#L39)
Each interface value contains a pointer to the data type descriptor (https://github.com/golang/go/blob/master/src/runtime/type.go#L14)
During conversion from a type that is known at compile time to an interface value Go compiler will point type descriptor of this interface value to the concrete type descriptor (it is known at compile time!).
E.g. when you call reflect.TypeOf(uint(87))
:
- an interface value is created by the compiler that references
uint
type descriptor - this interface value is passed to
reflect.TypeOf
function as argument reflect.TypeOf
function uses type descriptor that has been stored by the linker in the executable to get the align (and other) information about uint type.
答案2
得分: 2
接口的描述在这里很好地描述了:反射定律。
接口类型的变量存储了一对值:分配给变量的具体值和该值的类型描述符。
基本上,类型是从你的代码中静态地知道的。更灵活的接口类型保留了原始的底层类型,以便返回到原始的数据类型。
英文:
The description of interfaces is well described here: The Laws of Reflection.
> A variable of interface type stores a pair: the concrete value
> assigned to the variable, and that value's type descriptor.
Basically, type are known statically from your code. More flexible interface types keep the original underlying type for getting back to the original data type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论