Hooking into the Go toolchain with -toolexec

The Go command exposes a powerful interception point for observing and rewriting compiler inputs without modifying the compiler, with practical caveats around build-cache correctness.

Logged at IST: 2026-10-06 17:03 IST

What it is: A hands-on explanation of go build -toolexec, written by Kemal Akkoyun and hosted by Jesús Espino.

Gist: -toolexec routes each compiler, assembler, and linker invocation through a wrapper you choose. The article builds from a simple build-time stopwatch to source rewriting, injecting imports, //go:linkname hooks, and cache pitfalls, then connects the techniques to otelc. The build cache can make rewritten builds surprising, so the experiments isolate their caches and discuss keeping cache identity honest.

Why it matters: This is a surprisingly capable seam for build instrumentation and experiments, but it is not a stable, first-class source-rewriting API. The runnable examples make the risks concrete rather than hiding them behind an abstraction.

Source read: https://internals-for-interns.com/posts/hooking-into-the-go-toolchain/; https://github.com/kakkoyun/hooking-into-the-go-toolchain