/golang:go-performancefrom golang
Guides Go performance work as a measure-first discipline — never optimize from intuition; profile or benchmark first, then optimize only what the data proves is hot, without sacrificing clarity for unmeasured speed. Covers benchmarking with go test -bench -benchmem and comparing runs with benchstat (not eyeballing one noisy run); profiling with pprof (CPU -cpuprofile, heap -memprofile, net/http/pprof for live servers; top/list/web); escape analysis via go build -gcflags=-m (stack vs heap). Auto-invokes when optimizing Go performance, reducing allocations, profiling with pprof, reading escape analysis, or enabling PGO, and on "make this faster" / "why is this allocating" / "profile this" requests. Routes benchmark mechanics to go-testing-advanced, sync.Pool to go-sync-primitives.
Ships in golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
"You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is." "Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest." — Rob Pike, Notes on Programming in C
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." — Donald Knuth, Structured Programming with go to Statements (Computing Surveys, 1974)
Performance work in Go is a measure-first discipline, not a bag of tricks. The single most expensive mistake is optimizing code that was never hot — paying readability to speed up a path the program barely runs. This skill makes the loop explicit: profile or benchmark, find the real hot path, change one thing, measure again. Everything below serves that loop.
All Go below builds clean under gofmt, go vet, and go test on Go 1.26.
The order is non-negotiable, because "bottlenecks occur in surprising places" (Pike):
This is the same dual-axis judgment as the policy root (go-idiomatic-discipline): "clear AND correct." An unmeasured micro-optimization that trades clarity for speed you never proved is the ceiling failure — it ships, it's correct, and the next reader pays for cleverness that bought nothing. The bar for sacrificing readability is a profile or a benchmark that names the cost. No profile, no trade. "Clear is better than clever" (Go Proverbs) loses only to evidence.
| Tool | Question it answers | Command |
|---|---|---|
Benchmark (-benchmem) | How fast / how many allocs is this function? | go test -bench=. -benchmem |
| benchstat | Is the change real or noise? | benchstat old.txt new.txt |
| CPU profile | Where does wall/CPU time go? | go test -cpuprofile=cpu.prof |
| Heap profile | Where do allocations come from? | go test -memprofile=mem.prof |
| Escape analysis | What is forced onto the heap? | go build -gcflags=-m |
| PGO | Free inlining of measured hot funcs | default.pgo in the main package |
-benchmem, Compare with benchstatA benchmark is the unit of evidence. go test -bench=. -benchmem adds the allocation columns; b.ReportAllocs() turns them on per-function. The benchmark mechanics — for b.Loop() (Go 1.24), the dead-code/sink trap, b.ResetTimer, b.RunParallel — are owned by go-testing-advanced §2–3; read it for how to write the loop. This skill owns what you do with the numbers.
BenchmarkJoinNaive-32 6748058 179.3 ns/op 216 B/op 7 allocs/op
BenchmarkJoinBuilder-32 24888777 49.11 ns/op 48 B/op 1 allocs/op
The allocs/op column is the lever: 7 → 1 allocations is a real, durable win (string += in a loop reallocates every iteration; strings.Builder with one Grow does not). But one run is not a measurement — CPU scaling, GC, and noise swamp small deltas. To know a change is real, run each side ≥10 times and let benchstat do the statistics:
go test -run='^$' -bench=. -count=10 > old.txt
# ... make the change ...
go test -run='^$' -bench=. -count=10 > new.txt
benchstat old.txt new.txt
"Benchstat computes statistical summaries and A/B comparisons of Go benchmarks" and "Each benchmark should be run at least 10 times to gather a statistically significant sample" (benchstat). It reports the delta with a p-value: a ~ "indicates no statistically significant difference," while -17.20% (p=0.000 n=10) is a change you can trust (benchstat). Reporting a single before/after pair as proof is the classic eyeball error.
When you don't already know which function is hot, profile; don't guess. "Profiling tools analyze the complexity and costs of a Go program such as its memory usage and frequently called functions to identify the expensive sections" (Diagnostics). The two profiles you reach for most:
The fastest way to get both is straight from a benchmark — no code changes:
go test -bench=. -cpuprofile=cpu.prof -memprofile=mem.prof
go tool pprof -top cpu.prof # the heaviest functions
go tool pprof cpu.prof # interactive: top, list Fn, web
Inside go tool pprof, three commands carry the work: "The most important command is topN, which shows the top N samples"; list Fn shows per-line counts where "the first three columns are the number of samples taken while running that line, the number … in code called from that line, and the line number"; and "the web command writes a graph of the profile data in SVG format and opens it in a web browser" (needs graphviz) (Profiling Go Programs). Use -cum to sort by cumulative cost up the call tree.
Live servers: import net/http/pprof for its side effect — "Package pprof serves via its HTTP server runtime profiling data" under paths that "all begin with /debug/pprof/" (net/http/pprof):
import _ "net/http/pprof" // registers /debug/pprof/ handlers on the default mux
// go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30 # 30s CPU
It is "safe to profile programs in production, but enabling some profiles (e.g. the CPU profile) adds cost" (Diagnostics). One caveat: "Use tools in isolation to get more precise info" — precise memory profiling skews CPU profiles (Diagnostics). block and mutex profiles are off by default (runtime.SetBlockProfileRate, runtime.SetMutexProfileFraction).
Stack allocations are reclaimed for free when the function returns; heap allocations create GC work. The compiler decides per value, and it will show you its reasoning: -m is documented as "Print optimization decisions" (cmd/compile).
go build -gcflags=-m ./... # add a second -m (-gcflags='-m -m') for more detail
The lines that matter read like moved to heap: x, ... escapes to heap, or the reassuring ... does not escape. A verified example — returning the address of a local forces it onto the heap:
// escapes: the local p outlives the call, so it must live on the heap
func NewPointHeap(x, y int) *Point { p := Point{x, y}; return &p }
// ./perf.go:51:2: moved to heap: p
// stays on the stack: the value is copied in, nothing outlives the call
func SumPointValue(p Point) int { return p.X + p.Y }
The recurring escape triggers to recognize in a hot path:
any/interface{} (and fmt.Sprint(x), whose ...any boxes every argument) heap-allocates it.Escape analysis tells you why the heap profile looks the way it does — read them together.
Only after a heap profile or -benchmem names the cost. The high-leverage moves, each verified by an alloc delta:
make([]T, 0, n) gives append its backing array once instead of growing-and-copying. Measured: appending 1024 ints with no prealloc was 25208 B/op, 12 allocs/op; with make([]int, 0, n) it was 0 B/op, 0 allocs/op. Same for maps: make(map[K]V, n). (Slice/map mechanics: go-slices-and-maps.)strings.Builder, not +=. s += p in a loop reallocates the whole string every iteration. Measured: naive += was 216 B/op, 7 allocs/op; strings.Builder with one Grow(n) was 48 B/op, 1 allocs/op. (Builder + conversion cost: go-strings-bytes-runes.)[]byte↔string conversions in hot loops — each conversion copies. Work in one representation; reach for strconv over fmt for number formatting.buf = buf[:0] keeps the capacity and resets the length.sync.Pool for transient objects. "A Pool is a set of temporary objects that may be individually saved and retrieved"; its "purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector" (sync.Pool). The caveat: "Any item stored in the Pool may be removed automatically at any time without notification" — so it's for fungible scratch (byte buffers), never for objects that must persist. Mechanics and copylock rules: go-sync-primitives.Profile-Guided Optimization feeds a real CPU profile back into the compiler. "The Go toolchain will automatically enable PGO when it finds a profile named default.pgo in the main package directory" (PGO blog); "By default, go build will detect default.pgo files automatically and enable PGO" (PGO guide). Profiles "from runtime/pprof and net/http/pprof can be used directly as the compiler input" — so the CPU profile you already collected is the input.
go tool pprof -proto ... > default.pgo # a representative production CPU profile
go build ./... # auto-detected; no flag needed
With it, "the compiler may decide to more aggressively inline functions which the profile indicates are called frequently" (PGO guide). The payoff is modest and free: "benchmarks for a representative set of Go programs show that building with PGO improves performance by around 2-14%" (PGO guide). Commit default.pgo to the repo — it "ensures that users automatically have access to the profile … and that builds remain reproducible" (PGO blog). Collect it from production, not a synthetic micro-benchmark, and refresh it occasionally as the code drifts.
Two recent releases cut overhead with no code change — they raise the floor before you optimize anything:
GOMAXPROCS (Go 1.25). "On Linux, the runtime considers the CPU bandwidth limit of the cgroup containing the process … If the CPU bandwidth limit is lower than the number of logical CPUs available, GOMAXPROCS will default to the lower limit" (Go 1.25). This stops a containerized binary from spawning a thread per host core and thrashing under a Kubernetes CPU limit. It "periodically updates GOMAXPROCS if … the cgroup CPU bandwidth limit change[s]," and is disabled if you set GOMAXPROCS manually.GOEXPERIMENT=nogreenteagc (expected removed in 1.27).Gate any version-specific idiom on the module's go directive — see go-version-feature-map.
go-idiomatic-discipline. The price of clever is a profile that names the win.-count and benchstat; a single delta is within the noise.GOGC, GOMEMLIMIT) before measuring that GC is your bottleneck — a mutex or a syscall usually isn't fixed by GC knobs.sync.Pool objects that must persist — pooled items can vanish at any time.go-idiomatic-discipline — the policy root; unmeasured micro-opt vs clarity is its axis-2 ("clear AND correct") failure.go-testing-advanced — benchmark mechanics: for b.Loop(), the sink/dead-code trap, b.ReportAllocs, b.RunParallel. This skill interprets the numbers it produces.go-sync-primitives — sync.Pool mechanics, copylock, and the rest of shared-memory synchronization.go-slices-and-maps — preallocation (make([]T,0,n)), capacity, append/aliasing semantics.go-strings-bytes-runes — strings.Builder and the cost of []byte↔string conversion.go-version-feature-map — version floors: PGO GA 1.21, container-aware GOMAXPROCS 1.25, Green Tea GC default 1.26.High-frequency performance anti-patterns in LLM-generated Go, each with wrong/right code and citations:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ctoth/golang-skills-plugin --plugin golang25 skills in this plugin
/go-channels-selectGuides Go channel mechanics — declare directionality in signatures (chan less than - send-only, less than -chan receive-only), let only the sender close and only when sends are done (closing is a broadcast, not cleanup), read the comma-ok / range-ends-on-close idiom so a closed channel's zero value is not mistaken for data, choose unbuffered (synchronization) vs buffered (decoupling) deliberately, use chan struct{} for pure signals, and drive select with default for non-blocking and the nil-channel trick to disable a case. Auto-invokes when writing or editing channels, chan declarations, close(), select statements, or buffered channels, and on "send on closed channel panic" / "why does this select block" / "why does my range over a channel never end" requests. The depth behind the policy root's "don't fight Go" for the communication primitive./go-concurrency-goroutinesGuides goroutine lifetime and ownership — never start a goroutine you can't stop, give every goroutine a defined exit (context cancellation, a closed channel, or bounded work), avoid the blocked-forever leak, use sync.WaitGroup correctly (Add before the go, or wg.Go in 1.25) and golang.org/x/sync/errgroup for fallible fan-out and SetLimit worker pools, and keep library functions synchronous so the caller owns concurrency. Auto-invokes when writing or editing `go` statements, goroutines, sync.WaitGroup, errgroup, worker pools, or on "is this goroutine leaking" / "how do I wait for these" requests. The depth behind the policy root's "never start a goroutine you can't stop."