How exactly are function values ('function pointers') implemented in Go?

huangapple go评论97阅读模式
英文:

How exactly are function values ('function pointers') implemented in Go?

问题

根据reflect.Value.Pointer()的文档

如果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.

huangapple
  • 本文由 发表于 2017年6月7日 00:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/44395698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定