A hands-on workshop about writing Go that works with the machine instead of against it. Every module explains a mechanism inside the Go compiler, runtime or the hardware, and then you measure the before and after yourself with benchmarks and profiles.
This workshop uses stock Go 1.27. You don't need to build Go from source.
benchstat: go install golang.org/x/perf/cmd/benchstat@latestThis workshop has 19 modules that build on each other. You start by learning to measure, then remove allocations, then tune data for the CPU caches and branch predictor, and then work with the GC and scheduler. Every module pairs one concept with a lab where you measure the before and the after.
What mechanical sympathy means, how the workshop is organized, and how to prepare your environment.
Write benchmarks that measure what you think they measure, and compare results with benchstat.
Learn how the CPU and heap profilers sample your program, and use pprof to find the hot spot.
Watch growslice copy your data over and over, then size slices and maps up front.
Immutable strings, hidden copies in conversions, and the compiler tricks that avoid them.
Learn how the compiler decides between stack and heap, and keep your values on the stack.
Split shopping-cart pricing into a small common case and a coupon helper, then measure inlining.
Keep customer signup validation in a helper so a small constructor can avoid heap allocations.
Two small price exercises: avoid boxing allocations, then inline a budget check.
Calculate receipt totals while helping the compiler avoid repeated slice bounds checks.
Reorder shopping-order fields to reduce padding and memory use.
Reuse temporary buffers safely and measure the allocations and GC work you avoid.
Reduce pointer-heavy storage and explore the memory and CPU tradeoff of GC settings.
Apply sale discounts in batches and compare workers with a simple loop.
The cost of crossing into the kernel and what the scheduler does meanwhile. Buffer your I/O.
Feed a CPU profile back to the compiler for hot-path inlining and devirtualization.
Total daily sales and account balances using memory-friendly access patterns.
Compare shared counters, split frequent writes and keep independent counters on separate cache lines.
Filter product prices and compare branches on sorted and random data.
labs/NN-name/. You edit after.go, and solution.go is the reference