An open API service indexing awesome lists of open source software.

https://github.com/jespino/having-fun-with-the-go-source-code-workshop

Having Fun with the Go Source Code Workshop
https://github.com/jespino/having-fun-with-the-go-source-code-workshop

go internals workshop

Last synced: 4 months ago
JSON representation

Having Fun with the Go Source Code Workshop

Awesome Lists containing this project

README

          

# ๐Ÿš€ Having Fun with the Go Source Code Workshop

Welcome to an interactive workshop where you'll learn how to modify and experiment with the Go programming language source code! This hands-on workshop will guide you through understanding, building, and making changes to the Go compiler and runtime. ๐ŸŽฏ

**๐Ÿ“Œ This workshop uses Go version 1.25.1** - we'll check out the specific release tag to ensure consistency across all exercises.

## ๐Ÿ“‹ Prerequisites

- ๐Ÿน Basic knowledge of Go programming
- ๐Ÿ’ป Familiarity with command line tools
- ๐Ÿ—‚๏ธ Git installed on your system
- **โšก Go compiler version 1.24.6 or newer** (required for bootstrapping the build process)
- ๐Ÿ’พ At least 4GB of free disk space

## ๐ŸŽ“ Workshop Overview

This workshop consists of 10 exercises that will take you through the process from building Go from source, and making modifications at different places in the compiler, tooling and runtime. You'll gain some insights about the Go internals, from things like the lexer or parser, to runtime behaviors:

### ๐ŸŒฑ [Exercise 0: Introduction and Setup](./exercises/00-introduction-setup.md)

Get started by cloning and setting up the Go source code environment.

### ๐Ÿ”จ [Exercise 1: Compiling Go Without Changes](./exercises/01-compile-go-unchanged.md)

Learn to build the Go toolchain from source without any modifications.

### โšก [Exercise 2: Adding the "=>" Arrow Operator for Goroutines](./exercises/02-scanner-arrow-operator.md)

Learn scanner/lexer modification by adding "=>" as an alternative syntax for starting goroutines.

### ๐Ÿ”„ [Exercise 3: Multiple "go" Keywords - Parser Enhancement](./exercises/03-parser-multiple-go.md)

Learn parser modification by enabling multiple consecutive "go" keywords (go go go myFunction).

### โš™๏ธ [Exercise 4: Inline Parameters - Function Inlining Experiments](./exercises/04-compiler-inlining-parameters.md)

Explore the inliner behavior by modifying function inlining parameters.

### ๐ŸŽจ [Exercise 5: gofmt Transformation - "hello" to "helo"](./exercises/05-gofmt-ast-transformation.md)

Learn about Go's tools by modifying gofmt to modify "hello" to "helo" in code.

### ๐Ÿ” [Exercise 6: SSA Pass - Detecting Division by Powers of Two](./exercises/06-ssa-power-of-two-detector.md)

Create a custom SSA compiler pass that detects division operations by powers of two that could be optimized to bit shifts.

### ๐Ÿ•ฐ๏ธ [Exercise 7: Patient Go - Making Go Wait for Goroutines](./exercises/07-runtime-patient-go.md)

Modify the Go runtime to wait for all goroutines to complete before program termination.

### ๐Ÿ•ต๏ธโ€โ™‚๏ธ [Exercise 8: Goroutine Sleep Detective - Runtime State Monitoring](./exercises/08-goroutine-sleep-detective.md)

Add logging to the Go scheduler to monitor goroutines going to sleep.

### ๐ŸŽฏ [Exercise 9: Predictable Select - Removing Randomness from Go's Select Statement](./exercises/09-predictable-select.md)

Modify Go's select statement implementation to be deterministic instead of random.

### โ˜• [Exercise 10: Java-Style Stack Traces - Making Go Panics Look Familiar](./exercises/10-java-style-stack-traces.md)

Transform Go's verbose stack traces into Java-style formatting.

## ๐Ÿš€ Getting Started

1. ๐ŸŒฑ Start with [Exercise 0](./exercises/00-introduction-setup.md) to set up your environment
2. ๐Ÿ“š Work through the exercises in order
3. ๐Ÿ”— After exercise 1, you can pick and choose the exercise that you want.

## ๐Ÿ“ Repository Structure

```
.
โ”œโ”€โ”€ README.md # This file
โ”œโ”€โ”€ exercises/ # Individual exercise files (markdown)
โ”‚ โ”œโ”€โ”€ 00-introduction-setup.md
โ”‚ โ”œโ”€โ”€ 01-compile-go-unchanged.md
โ”‚ โ”œโ”€โ”€ 02-scanner-arrow-operator.md
โ”‚ โ””โ”€โ”€ ...
โ”œโ”€โ”€ website-generator/ # Go program to generate website from markdown
โ”‚ โ”œโ”€โ”€ main.go
โ”‚ โ”œโ”€โ”€ templates.go
โ”‚ โ””โ”€โ”€ README.md
โ”œโ”€โ”€ website/ # Generated website (HTML)
โ”‚ โ”œโ”€โ”€ index.html
โ”‚ โ”œโ”€โ”€ 00-introduction-setup.html
โ”‚ โ””โ”€โ”€ ...
โ”œโ”€โ”€ Makefile # Build automation
โ””โ”€โ”€ go/ # Go source code (cloned during setup)
```

## ๐ŸŒ Website Generator

This repository includes a Go program that automatically generates a static website from the markdown exercise files.

### Generate the Website

```bash
# Using make (recommended)
make website

# Or run directly
cd website-generator
go run . -exercises ../exercises -output ../website
```

### Serve Locally

```bash
# Start a local web server
make serve

# Then open http://localhost:8000 in your browser
```

The website generator:
- โœ… Converts markdown to HTML using [blackfriday](https://github.com/russross/blackfriday)
- โœ… Preserves all formatting, emojis, and code blocks
- โœ… Generates navigation between exercises
- โœ… Creates an index page with exercise overview
- โœ… Includes responsive CSS styling

See [website-generator/README.md](website-generator/README.md) for more details.

## ๐Ÿ’ก Tips for Success

- โฐ Take your time with each exercise - compiler internals are complex!
- ๐Ÿ” Don't hesitate to explore the Go source code beyond what's required
- ๐ŸŒฟ Use `git` to track your changes and revert when needed
- ๐Ÿงช Test your modifications thoroughly with various Go programs

## ๐Ÿ“– Resources

- ๐Ÿ—๏ธ [Go Compiler Overview](https://github.com/golang/go/tree/master/src/cmd/compile)
- ๐Ÿ“‹ [Go Language Specification](https://go.dev/ref/spec)
- ๐Ÿ”ง [Go Runtime Documentation](https://pkg.go.dev/runtime)
- ๐Ÿ“š [Go Internal Documentation](https://go.dev/src/)

### ๐ŸŽฅ Video References

These workshop exercises are based on insights from my talks:

- ๐ŸŽฌ [Understanding the Go Compiler](https://www.youtube.com/watch?v=qnmoAA0WRgE) - Deep dive into Go's compilation process
- ๐ŸŽฌ [Understanding the Go Runtime](https://www.youtube.com/watch?v=YpRNFNFaLGY) - Exploration of Go's runtime system

## ๐Ÿ† Workshop Completion

Upon completing all exercises, you'll have:

- โœ… **Built Go from source** and understood the bootstrap process
- โœ… **Modified language syntax** by changing scanner and parser behavior
- โœ… **Customized development tools** like gofmt and compiler optimizations
- โœ… **Implemented SSA optimizations** in the compiler backend
- โœ… **Modified runtime behavior** including program entry points and scheduler monitoring
- โœ… **Altered concurrency algorithms** like select statement randomization
- โœ… **Customized error reporting** with Java-style stack trace formatting

**Congratulations!** You'll have gained the confidence to keep exploring the Go source code. This knowledge enables you to:

- Start small contributions to the Go project
- Build custom language variants and tools
- Understand some trade-offs in language and runtime design

## ๐Ÿค Contributing

Found an issue, have an improvement idea or want to add more exercises? Please [open an issue](https://github.com/jespino/having-fun-with-the-go-source-code-workshop/issues) or submit a pull request!

---

**Happy coding and welcome to the world of Go internals!** ๐Ÿš€โœจ