Guides writing, refactoring, and reviewing table-driven tests in Go. Covers struct design, subtest naming, test matrices, shared setup, and when to avoid tables.
Ships in go-agent-skills. 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.
Table-driven tests are a powerful Go idiom — when used correctly. Most codebases either underuse them (10 copy-paste tests) or overuse them (complex branching logic in a 200-line struct). This skill covers the sweet spot.
Community default. A project skill or agent-config rule that explicitly supersedes
go-test-table-driventakes precedence over this one.
Detailed reference material, loaded on demand:
references/patterns.md — full worked examples: canonical tables,
wantErr/wantErrIs, parallel tables, map-based tables, error-only
tables, struct alignment for readability.references/refactoring.md — recognizing bloated tables and rewriting
them as explicit subtests, with before/after examples.Read a reference file only when the summary below is not enough for the task at hand.
Use a table only when ALL of these are true:
Canonical use case: pure functions, parsers, validators, formatters.
func TestParseSize(t *testing.T) {
tests := []struct {
name string
input string
want int64
wantErr bool
}{
{name: "plain bytes", input: "1024", want: 1024},
{name: "kilobytes suffix", input: "4KB", want: 4096},
{name: "empty string", input: "", wantErr: true},
{name: "negative size", input: "-1", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseSize(tt.input)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
Every case has the same shape, the loop body is a few lines, and adding a case is one struct literal. No branching, no conditionals.
setupMock/setupFunc function fields in
the struct mean the table is hiding complexity. Write explicit subtests.if tt.shouldError / if tt.wantRedirect
in the loop body means each branch is a different test pretending to
share a structure. Split it.See references/refactoring.md for before/after rewrites of each smell.
name field as a short sentence describing the scenario:
"returns error for negative amount", not "case1" or "success".wantErr bool for "should it error?" — check it first and return
early in the loop body.wantErrIs error with a sentinel when the caller must detect a
specific error; assert with require.ErrorIs.Full field-pattern examples are in references/patterns.md.
The point of a table test is identical execution logic for every case. Keep the loop body under ~10 lines: call, error check, comparison. If it accumulates conditionals or per-case setup, the table has outgrown its usefulness — refactor into explicit subtests.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := Transform(tt.input)
assert.Equal(t, tt.want, got)
})
}
tt := tt capture is
unnecessary. For Go <1.22 the capture is still required.t.Parallel() when the function under test has no side
effects and no shared mutable state.| Symptom | Fix |
|---|---|
| Struct has 8+ fields | Split into multiple test functions by scenario |
setupFunc field in struct | Extract to separate subtests with explicit setup |
if tt.shouldX in loop body | Each branch is a different test — split it |
| Same 3 fields identical in every case | Move to shared setup outside the table |
| Adding a case requires understanding all others | Table has grown beyond its useful life |
Is the function pure (input → output, no side effects)? Yes → table test is probably ideal. Go to 2. No → consider explicit subtests first.
Do all cases share the exact same assertion pattern? Yes → table test. Go to 3. No → explicit subtests.
Can each case be expressed in ≤5 struct fields? Yes → table test. No → split by scenario into separate test functions.
Is the loop body ≤10 lines? Yes → you're golden. No → the table is hiding complexity. Refactor.
name fieldsetupFunc or mockFunc fields in the structwantErr is a simple bool or sentinel, not a string matcht.Run wraps each case for named subtestst.Parallel() used only when function is side-effect-freeWhat SKILL.md sets; muted values are Claude Code's defaults
/go-agent-skills:go-test-table-drivenfrom go-agent-skills
npx claudepluginhub eduardo-sl/go-agent-skills --plugin go-agent-skills35 skills in this plugin
/git-commitStructured git commit messages following Conventional Commits format for Go projects. Generates well-scoped, atomic commits with clear descriptions. Use when committing changes, writing commit messages, preparing PRs, or reviewing commit history quality. Trigger examples: "commit these changes", "create commit", "commit message", "prepare PR", "squash commits". Not for: code review (go-code-review), changelog generation.
/go-api-designHTTP/REST API design for Go services: handlers, ServeMux routing, middleware, request validation, pagination, idempotency, health checks, error responses, and graceful shutdown. Use when designing APIs, writing HTTP handlers, implementing middleware, or structuring REST endpoints. Trigger examples: "design API", "REST endpoints", "HTTP handler", "middleware pattern", "graceful shutdown", "API versioning". Not for: gRPC services (go-grpc), spec-first OpenAPI work (go-openapi), general architecture (go-architecture-review), concurrency in handlers (go-concurrency-review).
/go-architecture-reviewReview Go project architecture: package structure, dependency direction, layering, separation of concerns, domain modeling, and module boundaries. Use when reviewing architecture, designing package layout, evaluating dependency graphs, or refactoring monoliths into modules. Trigger examples: "review architecture", "package structure", "project layout", "dependency direction", "clean architecture Go", "module boundaries". Not for: code-level style (go-coding-standards), endpoint design (go-api-design).
/go-binary-sizeReduce the size of compiled Go binaries and container images: linker flags, inlining budget, CGO and build tags, embedded assets, dependency weight, and measuring what actually costs bytes. Use when a binary or image is too large, when shrinking a CLI for distribution, or when auditing what a dependency adds to the build. Trigger examples: "binary is too big", "shrink the binary", "reduce image size", "strip symbols", "what is making my binary large", "ldflags -s -w". Not for: runtime speed and allocations (go-performance-review), CI setup (go-ci), release pipelines and signing (go-build-release), dependency CVEs (go-dependency-audit).
/go-build-releaseBuild and release Go binaries that are reproducible and verifiable: version stamping, -trimpath builds, cross-compilation, GoReleaser, checksums, SBOMs, cosign signing, build provenance attestations, and container images with ko or a multi-stage Dockerfile. Use when: "release a Go binary", "set up GoReleaser", "cross-compile", "reproducible build", "sign the release", "generate an SBOM", "build provenance", "publish a container image", "version command shows (devel)". Not for: test and lint pipelines (go-ci), shrinking the binary or image (go-binary-size), auditing dependencies (go-dependency-audit).
/go-ciContinuous integration for Go projects: GitHub Actions pipelines, caching, golangci-lint setup, test/coverage gates, vulnerability scanning, SAST (CodeQL, gosec), Dependabot, action pinning, build matrices, Makefile targets. Use when: "set up CI", "GitHub Actions for Go", "add lint to the pipeline", "CI is slow", "coverage gate", "build matrix", "write a Makefile", "run govulncheck in CI". Not for: commit conventions (git-commit), choosing or auditing dependencies (go-dependency-audit), writing the tests themselves (go-test-quality), releases and signing (go-build-release).
Other plugins' skills for the same job