✅ When you install Go, you install:
cmd/compile
) — responsible for translating your .go
source code into machine code.go
— which manages the project lifecycle: compiling, testing, downloading dependencies, running, etc.cmd/link
) — which combines the compiled files and creates the executable binary.gofmt
, godoc
, goimports
, among others.In summary: You install the language and the entire toolchain required to write, compile, and run Go programs.
⚙️ How the Go Compilation Process Works
Let’s suppose you have a main.go
file. Here’s what happens when you run go run main.go
or go build
:
Parsing (Lexical and Syntactic Analysis)
The compiler reads the code and transforms it into an abstract syntax tree (AST).
Type Checking
It verifies that the types are correct: whether variables are used properly, whether functions return what they promise, etc.
Compilation to Intermediate Code (SSA)
Go converts the AST into a format called SSA (Static Single Assignment), an intermediate representation that facilitates optimizations.
Machine Code Generation
The compiler then translates this into native machine code for your platform (e.g., x86_64, ARM, etc.).
Linking
The linker joins all the necessary object files (.o
) — including those from the standard library — and creates a single static binary (by default).
💡 go run
vs go build
vs go install
go run file.go
→ compiles and temporarily runs the program (the binary is deleted after execution).go build
→ compiles and generates a binary in the current directory.go install
→ compiles and places the binary in $GOBIN
or $GOPATH/bin
.