Reading the Go Runtime Source
Reading the Go Runtime Source
Overview
The ultimate deep dive is src/runtime in the Go tree. You do not need to memorize it — you need a map, a debugger for experiments, and humility about version drift.
Workshop-style learning (modifying the toolchain) is increasingly popular for advanced gophers.
Diagram: Source map
symptom (latency / leak / panic)
│
v
pick runtime area
├── proc.go scheduler
├── chan.go channels
├── mgc*.go GC
└── netpoll*.go network
Entry Points
| Area | Files (names evolve) |
|---|---|
| Scheduler | proc.go, runtime2.go |
| Channels | chan.go |
| Maps | map*.go |
| GC | mgc*.go, mheap.go |
| Netpoll | netpoll*.go |
| Stacks | stack.go |
| Timers | time.go |
| Interface | iface.go / abi |
| Assembly | asm_*.s, sys_*.s |
Clone matching your version:
git clone --depth 1 --branch go1.27.1 https://github.com/golang/go
cd go/src/runtime
rg -n 'type hchan'
rg -n 'func schedule'How To Study
- Pick a symptom (e.g. send on closed panics).
- Find the user-facing panic string in runtime.
- Walk callers one level at a time.
- Write a 10-line program that hits the path.
- Optional:
dlvon a rebuiltgotool (advanced).
go tool objdump / nm
go build -o app .
go tool nm app | rg 'runtime\\.'
go tool objdump -s 'main\\.main' app | headExperiments Without Patching
GODEBUGtracesgo build -gcflags=-m- Race detector as dynamic analysis
- Breakpoints in your code at API boundaries, infer runtime
Experiments With Patching (Optional)
- Add a
printlnin a runtime helper (local toolchain) - Use
go installfrom a branch - Never ship patched runtimes without extreme need
Field Guide: This Book → Source
| Chapter | Start reading |
|---|---|
| 201 GMP | proc.go schedule/findrunnable |
| 203 channels | chan.go |
| 208 GC | mgc.go |
| 213 netpoll | netpoll_*.go |
| 214 timers | time.go |
| 228 cgo | cgocall.go |
Experiment
# on a full go checkout matching your version
rg -n "panic\\(.*close of closed channel" "$(go env GOROOT)/src/runtime"
rg -n "func makeslice" "$(go env GOROOT)/src/runtime"What to notice: Panic strings and helper names are breadcrumbs; follow them instead of random file browsing.
Try next: Pick one production bug class from mistakes and find the runtime check that enforces it.