‘Behind the scenes of Go language functions: from symbol tables to stack frames’

Beck Moulton
4 min read8 hours ago

Go functions are the basic modules for building Go programs, and we use them every day. But have you ever thought about how Go functions work during compilation and runtime? This article will delve into the internal mechanisms of Go functions, from symbol tables to stack frames, revealing the secrets of how Go functions run.

Naming and symbol table of functions

In Go, each function has a unique name because the Go compiler creates a Symbol table To record the names of all variables and functions. When we define a function in the code, its name is added to the symbol table. If two functions have the same name, it can lead to conflicts because there can only be one entry with the same name in the symbol table.

func a() {}func a(b string) {}//a redeclared in this block is the error I get

So, how do I view the symbol table of a Go program?

We can usego tool nmCommand to view the symbol table of Go executable files. For example, suppose we have a name calledmainIn the Go program, we can generate a symbol table using the following command:

go tool nm ./main &> logs.txt

This will output the symbol table information tologs.txtIn the document. Each entry in the symbol table consists of three parts: addresstype and name

100343920 T main.getURL
1003439b0 T main.main
100343f30 T main.main.func1
100343fd0 T

--

--

Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton