Go test coverage exclude: files, packages and mocks
Go has no exclude flag or ignore comment. Two ways that work, measured on Go 1.27.4, and what does not.

Go has no exclude flag and no ignore comment. Choose the packages to measure
with -coverpkg, or delete lines from the profile with grep before you read
it. Files marked // Code generated … DO NOT EDIT. are not left out for you.
Exclude patterns at a glance
| To exclude | Do |
|---|---|
| Packages, such as mocks | -coverpkg=$(go list ./... | grep -v /mocks | paste -sd, -) |
| Files or folders | grep -v -E '/(mocks|gen)/' cover.out > cover.filtered.out |
| Generated files | Filter them the same way; Go does not skip them |
| Packages with no tests | Leave them out of the go test package list |
| One function or line | Move it to a file you filter out |
We ran every setup here with Go 1.27.4. The module has five packages:
pricing with three tests, a generated gen package, mocks used by the
tests, an unused legacy package and a cmd/shop main.
Choose the packages to measure
go test -coverprofile=cover.out ./...
This counted each package only through its own tests. pricing had 8 of 9
statements; the four packages with no tests showed at 0%, even the gen and
mocks code the pricing tests ran. Total: 8 of 20 (40%).
go test -coverpkg=./... -coverprofile=cover.out ./...
-coverpkg counts a package whenever any test runs it. Now gen had 1 of 2
statements and mocks 3 of 5: 12 of 20.
To leave packages out, list the ones to keep:
go test -coverpkg=$(go list ./... | grep -v -e /mocks -e /cmd/ | paste -sd, -) \
-coverprofile=cover.out ./...
The profile then had pricing, gen and legacy: 9 of 12 statements.

Filter the profile
A coverage profile is a text file with one line per code block, starting with the file path. Delete the lines you do not want, then report:
go test -coverpkg=./... -coverprofile=cover.out ./...
grep -v -E '/(mocks|gen)/' cover.out > cover.filtered.out
go tool cover -func=cover.filtered.out
go tool cover -func printed 61.5%: 8 of 13 statements, without the mocks
and the generated code. Keep the first line of the file, mode: set; a
grep -v on paths leaves it in place. Codecov and other services accept the
filtered file.
Why your Go coverage exclude isn’t working
| What we tried | What happened |
|---|---|
A // Code generated … DO NOT EDIT. header | Still counted |
go test ./... without -coverpkg | Code run from another package’s tests counted as 0% |
go test ./pricing/... | Only pricing measured; the rest simply missing |
An ignore comment such as // coverage:ignore | Go has no such comment |

Lines inside a function cannot be excluded. If a block must not count, move
it into its own file, such as debug_gen.go, and filter that file out.
What Supercov adds
go run github.com/supercorp-ai/supercov/cmd/supercov@latest -- go test ./...
go run github.com/supercorp-ai/supercov/cmd/supercov@latest runs latest gaps
Supercov counts code in every package your tests reach, as -coverpkg=./...
does, and records which test covered each line, with branches and MC/DC,
which go test -cover does not report. It decides what counts from your
source directories. See Go test coverage for the
tools side by side and Go covermode for set,
count and atomic.
More coverage guides
- Jest coverageThreshold: global, per-folder and per-file
- JaCoCo aggregate report for multi-module Maven and Gradle builds
- pytest ignore directory: --ignore, norecursedirs and collect_ignore
- Merge coverage reports from shards: Vitest, Jest and coverage.py
- Statement vs branch coverage, with decision, line and path coverage
- JaCoCo report not generated: the causes in Maven and Gradle
- Kover exclude: classes, packages and generated code
- sonar.coverage.exclusions: why SonarQube ignores your JaCoCo excludes
- istanbul ignore next vs c8 ignore next: every hint, measured
- Jest coverage exclude: files, folders and lines
- pytest coverage exclude: omit files, folders and lines
- Vitest coverage exclude: files, folders and lines
- JaCoCo exclude in Maven and Gradle: packages, classes and generated code