Find Edge-Case Panics in Go With go test -fuzz
Write FuzzX(f *testing.F) targets and run go test -fuzz to discover edge-case panics and automatically save regression cases under testdata/fuzz. After this, parsers hold up against unexpected inputs.
Table-driven unit tests only verify the inputs you already imagined. In production, parsers, URL splitters, config decoders, and auth header handlers fail on the inputs nobody thought to write by hand: empty keys, odd UTF-8 sequences, repeated delimiters, or off-by-one slices that trigger an index-out-of-range panic.
Go builds coverage-guided fuzzing directly into go test -fuzz. Instead of random noise, Go's fuzzer tracks code coverage branches, mutates seed inputs toward unexplored paths at tens of thousands of runs per second, and saves any failing input under testdata/fuzz/ so normal go test runs replay it as a regression test forever.
Skip ahead
Jump straight to FuzzParseKV or the local vs. CI fuzzing table.
1. Writing a FuzzX(f *testing.F) target
Seed the fuzzer with representative cases using f.Add(...), then assert invariants inside f.Fuzz(...)—such as "never panics" and "never accepts an empty key":
package fuzzdemo
import (
"strings"
"testing"
)
func ParseKV(s string) (string, string, bool) {
k, v, ok := strings.Cut(s, "=")
if !ok || k == "" {
return "", "", false
}
return k, v, true
}
func FuzzParseKV(f *testing.F) {
f.Add("port=8080")
f.Add("=bad")
f.Add("noequals")
f.Fuzz(func(t *testing.T, input string) {
k, v, ok := ParseKV(input)
if ok && k == "" {
t.Fatalf("empty key accepted for %q", input)
}
if ok && !strings.Contains(input, "=") {
t.Fatalf("accepted input without =: %q -> %q=%q", input, k, v)
}
})
}
$ go test -fuzz=FuzzParseKV -fuzztime=2s .
fuzz: elapsed: 0s, gathering baseline coverage: 0/3 completed
fuzz: elapsed: 0s, gathering baseline coverage: 3/3 completed, now fuzzing with 2 workers
fuzz: elapsed: 2s, execs: 186385 (88746/sec), new interesting: 0 (total: 3)
PASS
ok example.com/fuzzdemo 2.102s
In just 2 seconds, Go tested 186,385 mutated inputs. Notice what happens when the fuzzer does find a panic or failing assertion: Go minimizes the input to the shortest possible failing string and writes it into testdata/fuzz/FuzzParseKV/<hash>. From that moment on, every regular go test ./... run replays that exact input automatically—even without -fuzz.
How fuzz tests fit into local dev vs. CI
| Command | What it runs | Where to use |
|---|---|---|
go test ./... |
Runs f.Add(...) seeds + saved testdata/fuzz/ regression inputs once |
Every PR and local edit (milliseconds) |
go test -fuzz=FuzzParseKV -fuzztime=30s |
Actively mutates inputs for 30s searching for new crashes | Nightly CI jobs or before releasing parser/decoder changes |
💡 Tip
Round-trip invariants make the highest-value fuzz targets: take an arbitrary struct or string, encode it, decode it back, and assert equality—or assert that malformed bytes always return an error cleanly instead of panicking.
Common mistakes
- Running
-fuzz=.without-fuzztimein CI: Without-fuzztime, Go fuzzes forever until a crash or manual interrupt. - Fuzzing disk or network I/O: Keep fuzz targets pure in-memory functions; I/O drops execution throughput from ~80,000/sec to ~50/sec.
- Ignoring
testdata/fuzz/files in Git: Always commit minimized failing inputs undertestdata/fuzz/so regulargo testruns protect against regressions.
⚠ Warning
Do not mutate package-global state inside f.Fuzz(...)—Go runs fuzz workers across multiple goroutines concurrently.
What happens if you skip this
Happy-path unit tests miss malformed edge cases, leaving slice-bounds panics and infinite parser loops to be triggered by real external inputs.
Related course: Go Advanced.
About
Write FuzzX(f *testing.F) targets and run go test -fuzz to discover edge-case panics and automatically save regression cases under testdata/fuzz. After this, parsers hold up against unexpected inputs.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 3 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.