# Go test coverage exclude: files, packages and mocks > Go has no coverage exclude flag. Leave out mocks and generated code with -coverpkg or by filtering the profile with grep, measured on Go 1.27.4. Web page: https://supercov.com/blog/go-test-coverage-exclude · Published 2026-11-22 All Supercov pages for agents: https://supercov.com/llms.txt · Full text: https://supercov.com/llms-full.txt 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 ```sh 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%). ```sh 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: ```sh 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. ![Bars for four ways to run the same tests: go test -coverprofile, 8 of 20 statements; -coverpkg=./..., 12 of 20; -coverpkg without mocks and cmd, 9 of 12; -coverpkg=./... then grep -v mocks and gen, 8 of 13.](https://supercov.com/assets/go-test-coverage-exclude-flow-1360.612e93d834.webp) *The same three tests, four profiles.* ## 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: ```sh 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 | ![A grid of six setups against five packages. go test ./... and -coverpkg=./... keep all five. A -coverpkg list without mocks and cmd keeps pricing, gen and legacy. grep -v mocks and gen keeps pricing, legacy and cmd. go test ./pricing/... keeps only pricing. A DO NOT EDIT header on gen keeps all five.](https://supercov.com/assets/go-test-coverage-exclude-grid-1360.b02bf8199c.webp) *Which packages each setup left in the profile, measured with Go 1.27.4.* 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 ```sh 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](https://supercov.com/compare/go-test-coverage.md) for the tools side by side and [Go covermode](https://supercov.com/compare/go-covermode.md) for `set`, `count` and `atomic`.