英文:
Built-In source code location
问题
在Go的源代码中,我可以在哪里找到make
的实现?
事实证明,“代码搜索”功能对于语言的这个核心特性几乎没有用处,我没有好的方法来确定我应该搜索C函数、Go函数还是其他什么。
另外,在将来,我如何在不求助于这里的情况下找到这种信息?(即:教我如何自己查找)
编辑
附言:我已经找到了http://golang.org/pkg/builtin/#make,但与其他Go包不同,它没有包含源代码的链接,可能是因为它在编译器领域的某个深处。
英文:
Where in Go's source code can I find their implementation of make
.
Turns out the "code search" functionality is almost useless for such a central feature of the language, and I have no good way to determine if I should be searching for a C function, a Go function, or what.
Also in the future how do I figure out this sort of thing without resorting to asking here? (i.e.: teach me to fish)
EDIT
P.S. I already found http://golang.org/pkg/builtin/#make, but that, unlike the rest of the go packages, doesn't include a link to the source, presumably because it's somewhere deep in compiler-land.
答案1
得分: 48
没有make()
这样的函数。简单来说,发生了以下过程:
- Go代码:
make(chan int)
- 符号替换:
OMAKE
- 符号类型检查:
OMAKECHAN
- 代码生成:
runtime·makechan
gc
是一个Go风格的C解析器,根据上下文解析make
调用(以便更容易进行类型检查)。
这个转换是在cmd/compile/internal/gc/typecheck.go中完成的。
之后,根据符号的不同(例如,OMAKECHAN
表示make(chan ...)
),在cmd/compile/internal/gc/walk.go中替换相应的运行时调用。对于OMAKECHAN
,可能是makechan64
或makechan
。
最后,在运行代码时,调用了被替换的函数pkg/runtime。
如何找到这个过程
我通常通过想象这个特定事物在过程的哪个阶段可能发生来找到这些内容。对于make
,根据在pkg/runtime
(最基本的包)中没有make
的定义这一事实,它必须在编译器层面上,并且很可能被替换为其他内容。
然后,你需要搜索各个编译器阶段(gc、*g、*l),随着时间的推移,你会找到定义。
英文:
There is no make()
as such. Simply put, this is happening:
- go code:
make(chan int)
- symbol substitution:
OMAKE
- symbol typechecking:
OMAKECHAN
- code generation:
runtime·makechan
gc
, which is a go flavoured C parser, parses the make
call according to context (for easier type checking).
This conversion is done in cmd/compile/internal/gc/typecheck.go.
After that, depending on what symbol there is (e.g., OMAKECHAN
for make(chan ...)
),
the appropriate runtime call is substituted in cmd/compile/internal/gc/walk.go. In case of OMAKECHAN
this would be makechan64
or makechan
.
Finally, when running the code, said substituted function in pkg/runtime is called.
How do you find this
I tend to find such things mostly by imagining in which stage of the process this
particular thing may happen. In case of make
, with the knowledge that there's no
definition of make
in pkg/runtime
(the most basic package), it has to be on compiler level
and is likely to be substituted to something else.
You then have to search the various compiler stages (gc, *g, *l) and in time you'll find
the definitions.
答案2
得分: 18
实际上,make
是一个由不同函数组合而成的功能,使用 Go 语言在运行时实现。
-
makeslice 用于创建切片,例如
make([]int, 10)
-
makemap 用于创建映射,例如
make(map[string]int)
-
makechan 用于创建通道,例如
make(chan int)
对于其他内置函数如 append
和 copy
,也是类似的情况。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论