← Workshop home / Exercise 01
EXERCISE 01

Build Go from Source

Bootstrap the Go toolchain from source and explore the compiler and runtime directories.

Exercise 1: Building Go From Source

Welcome to the first hands-on exercise of the Go Mechanics Workshop! Here you’ll compile the entire Go toolchain from source and explore its key directories while the build runs.

Learning Objectives

By the end of this exercise, you will:

  • Understand how the Go bootstrap process works
  • Know the layout of the Go source tree and what lives where
  • Have a custom-built Go binary ready to use in subsequent exercises
  • Be able to rebuild the toolchain after making changes

Introduction: The Bootstrap Process

Go is a self-hosting language — the Go compiler is written in Go. This raises an obvious question: how do you compile a Go compiler if you don’t have a Go compiler?

The answer is bootstrapping. You need an existing Go installation (version 1.24.6 or newer) on your machine — the bootstrap compiler. make.bash uses it to compile the Go 1.26.1 source, then has Go rebuild itself repeatedly so the final binaries owe nothing to that older compiler.

The diagram below traces the chain. The bootstrap Go 1.24.6 first builds toolchain1, a working 1.26.1 compiler. That compiler then rebuilds the toolchain a second time, now self-hosted (compiled by 1.26.1 itself), producing toolchain2. A third pass — rebuild -a with clean build IDs — produces toolchain3, making the result reproducible and built entirely by the new version. Finally that toolchain compiles the standard library and tools, and everything is installed as $GOROOT/bin/go. The bootstrap compiler is only used for that very first step.

The Go bootstrap chain: Bootstrap Go 1.24.6 builds the new compiler (toolchain1), which rebuilds itself self-hosted (toolchain2), rebuilds clean (toolchain3), then builds the standard library and tools into $GOROOT/bin/go

Step 1: Start the Build

From your working directory (where you cloned the Go repository), run:

cd go/src && ./make.bash

This will take a few minutes. While it builds, let’s explore the source tree.

What make.bash Does

make.bash orchestrates the entire build:

  1. Detects your bootstrap compiler and platform
  2. Compiles the compiler tools (cmd/compile, cmd/link, cmd/asm, etc.)
  3. Compiles the standard library packages
  4. Outputs the final binaries to go/bin/

You’ll see lines like:

Building Go cmd/dist using /usr/local/go. (go1.24.6 linux/amd64)
Building Go toolchain1 using /usr/local/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for linux/amd64.

Each stage represents a compilation step. “toolchain1” is compiled by your bootstrap Go. “toolchain2” is compiled by toolchain1, and “toolchain3” is rebuilt by toolchain2 — toolchain3 is the final output.

Step 2: Explore Key Directories

While the build runs, open a second terminal and explore the source layout.

The Compiler: src/cmd/compile/

ls go/src/cmd/compile/internal/

This is where the Go compiler lives. Key subdirectories:

  • syntax/ — Scanner and parser: converts source text to tokens and AST
  • typecheck/ — Type checker: validates types and resolves identifiers
  • ir/ — Intermediate representation: the compiler’s internal AST
  • inline/ — Inlining optimization: decides which functions to inline
  • escape/ — Escape analysis: determines stack vs heap allocation
  • ssa/ — SSA backend: converts IR to SSA form and runs optimization passes
  • ssagen/ — SSA code generation: produces architecture-specific SSA

The Runtime: src/runtime/

ls go/src/runtime/*.go | head -20

The runtime provides everything a Go program needs at execution time:

  • malloc.go — Memory allocator (mallocgc, size classes, span management)
  • proc.go — Goroutine scheduler (the G/M/P model)
  • mgc*.go — Garbage collector
  • chan.go — Channel implementation
  • map.go — Map runtime entry points (the Swiss-table hash table itself lives in internal/runtime/maps)
  • slice.go — Slice operations

The go Tool: src/cmd/go/

ls go/src/cmd/go/internal/

The go command you use every day (go build, go test, go mod) lives here. It orchestrates compilation by invoking cmd/compile, cmd/link, and other tools.

The Linker: src/cmd/link/

ls go/src/cmd/link/internal/

The linker takes compiled object files and links them into a final executable. It handles symbol resolution, relocation, and produces ELF/Mach-O/PE binaries depending on the target platform.

Step 3: Verify the Build

Once make.bash completes, verify your new binary:

../bin/go version
# Should show: go version go1.26.1 linux/amd64

You now have a fully functional Go 1.26.1 toolchain built from source, ready to modify.

Step 4: Test With Hello World

Create a simple test to confirm everything works end to end. Stay in the go/src directory you built from (so the ../bin/go path below resolves correctly).

Create hello.go:

package main

func main() {
    println("Hello from our custom-built Go!")
}

Run it with your newly built binary:

../bin/go run hello.go

Expected output:

Hello from our custom-built Go!

For the rest of the workshop, go/bin/go refers to the binary inside the cloned go/ directory in your working directory. From go/src it’s ../bin/go; from your working directory it’s go/bin/go. Adjust the relative path to wherever you’re running from.

Summary

Bootstrap compiler (go1.24.6+)
    ↓ compiles
Go 1.26.1 toolchain1
    ↓ compiles itself (self-hosted)
Go 1.26.1 toolchain2
    ↓ rebuilds clean (reproducible)
Go 1.26.1 toolchain3 → go/bin/go

Build command:  cd go/src && ./make.bash
Verify:         ../bin/go version
Test:           ../bin/go run hello.go

You now have a solid foundation for the rest of the workshop. Every subsequent exercise will modify something in go/src/ and rebuild with ./make.bash.

Continue to Exercise 2

← Previous
Introduction and Setup
Next →
Lexer & Scanner — Arrow Operator