Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Built-In source code location

Tags:

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.

like image 380
Chris Pfohl Avatar asked Aug 29 '13 13:08

Chris Pfohl


People also ask

Where can I find Python source code?

The source for python 2.7 itself can be found at http://hg.python.org/cpython. Other versions of python have had their source imported onto Launchpad. You can see them here.

How can I see the source code of a built in function in Python?

Since Python is open source you can read the source code. To find out what file a particular module or function is implemented in you can usually print the __file__ attribute. Alternatively, you may use the inspect module, see the section Retrieving Source Code in the documentation of inspect .

Where are Python built in functions stored?

Everything in python is object. Python stores object in heap memory and reference of object in stack. Variables, functions stored in stack and object is stored in heap.


1 Answers

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.

like image 76
nemo Avatar answered Oct 10 '22 18:10

nemo