πŸ”¨ Exercise 1: Compiling Go Without Changes

In this exercise, you’ll learn how to build the Go toolchain from the source code without making any modifications. This is an essential skill before we start making changes to the language! ⚑

🎯 Learning Objectives

By the end of this exercise, you will:

πŸ₯Ύ Step 1: Understanding the Bootstrap Process

Go is written in Go itself! πŸ€” This creates a “chicken and egg” problem - how do you compile Go without having Go? The solution is bootstrapping:

  1. πŸ“¦ The Go team provides pre-compiled binaries
  2. πŸ”¨ These binaries compile the current Go source code
  3. ✨ The newly compiled version can then be used for development

Let’s check if you have Go installed (needed for bootstrapping): πŸ”

go version
# Must show version 1.24.6 or newer

⚠️ Critical: You must have Go 1.24.6 or newer installed to build Go 1.25.1. If you don’t have Go installed or your version is too old, install the latest version from https://golang.org/dl/ πŸ“₯

πŸ“‚ Step 2: Navigate to the Go Source Directory

cd go/src
pwd
# Should show: /path/to/workshop/go/src

# Verify you're on the correct Go version
git describe --tags
# Should show: go1.25.1

πŸš€ Step 3: Start the Build Process

Go provides different scripts for building. Let’s start with make.bash which builds the toolchain, then explore the source code while it’s running! ⚑

On Unix-like systems (Linux, macOS):

./make.bash

On Windows:

make.bat

This script will: πŸ“‹

  1. πŸ”¨ Build the Go toolchain (compiler, linker, runtime, standard library)
  2. ⏱️ Take approximately 2-10 minutes depending on your system

πŸ“ Note: The first time you run this, it will take longer as it needs to compile everything from scratch.

πŸ€” What about all.bash and run.bash?

You might wonder about other scripts in the src/ directory:

For this workshop, make.bash is perfect because:

πŸ” Step 4: Explore Source Code While Building

While the build is running, open a new terminal or IDE and let’s explore the Go source code structure! This is a great time to understand what we’re building. 🧭

In your new terminal:

cd /path/to/workshop/go  # Navigate to your Go source directory
ls -la

πŸ“ Repository Structure

Key directories you should see:

πŸ—οΈ Examine the Go Compiler Structure

The Go compiler is located in src/cmd/compile/. Let’s explore it: πŸ”§

cd src/cmd/compile
ls -la

Key files and directories:

πŸ“Š Step 5: Understanding the Build Output

Switch back to your original terminal where the build is running. As the build progresses, you should see output like: πŸ‘€

Building Go cmd/dist using /usr/local/go. (go1.25.1 darwin/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 darwin/amd64.

This shows the multi-stage bootstrap process:

πŸ“ Step 6: Locate Your Compiled Go Binary

After successful compilation, your new Go binary will be in: 🎯

ls -la /path/to/workshop/go/bin

You should see:

πŸ§ͺ Step 7: Test Your Custom Go Build

Let’s test your newly compiled Go: ✨

# Check version of your compiled Go
../bin/go version

Create a hello.go in the a temporary directory, for example /tmp.

package main

import "fmt"

func main() {
    fmt.Println("Hello from my custom Go build!")
}
# Compile and run with your custom Go
/path/to/workshop/go/bin/go run /tmp/hello.go

πŸŽ“ What We Learned

πŸŽ‰ Next Steps

Congratulations! 🎊 You now have a working Go toolchain built from source.

You can now proceed with any of the following exercises to learn about different parts of Go:

Or return to the main workshop to choose an exercise.