英文:
How exactly are function values ('function pointers') implemented in Go?
问题
如果v的Kind是Func,则返回的指针是一个底层代码指针,但不一定足以唯一标识单个函数。唯一的保证是,当且仅当v是nil func Value时,结果为零。
显然,函数值变量必须包含不仅仅是一个代码指针。鉴于Go支持方法指针,这并不令人惊讶,但实际的底层实现是什么呢?
(如果有的话,使用反射创建的函数值与之有何不同?请参见Value.Pointer()的源代码注释。)
英文:
From the documentation for reflect.Value.Pointer()
:
> If v's Kind is Func, the returned pointer is an underlying code pointer, but not necessarily enough to identify a single function uniquely. The only guarantee is that the result is zero if and only if v is a nil func Value.
It seems clear that function-valued variables must contain more than just a code pointer. Given that Go supports method pointers, this is hardly surprising - but what is the actual underlying implementation?
(And how does it differ, if at all, for function values created using reflection? See comment in the source for Value.Pointer().)
答案1
得分: 1
“新实现”部分的设计文档描述了函数指针的表示方式。
英文:
The "New Implementation" section of the design document describes how function pointers are represented.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论