Built-In source code location

前端 未结 2 1491
太阳男子
太阳男子 2020-12-04 23:54

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 fea

2条回答
  •  无人及你
    2020-12-05 00:37

    There is no make() as such. Simply put, this is happening:

    1. go code: make(chan int)
    2. symbol substitution: OMAKE
    3. symbol typechecking: OMAKECHAN
    4. 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.

提交回复
热议问题