
To check code coverage in GoLang for your Go app, you'll want to use a tool like Go Test. Go Test is a built-in tool that comes with the Go installation, making it easy to use.
Go Test can be run with the -cover flag to generate a coverage report. This report will show you which parts of your code are covered by your tests and which parts are not.
The -cover flag is used in conjunction with the -coverprofile flag to specify a file where the coverage report should be written. For example, running go test -cover -coverprofile=coverage.out will generate a coverage report in a file named coverage.out.
To see the coverage report, you can use the go tool cover command, which can be run on the coverage.out file generated by Go Test.
Recommended read: Golang Test Parallel
Understanding Coverage Threshold
Code coverage is a useful tool for tracking the effectiveness of your tests. If your tests are less than a specified percentage, they will fail even if they are successful.
A coverage threshold is a percentage that determines whether your tests are considered successful or not. If your package is below that threshold, your tests will fail.
This threshold can be set to notify you when your coverage goes below the acceptable level, helping you catch any missed branches in your code.
In the context of contributing to a team's project, a coverage threshold shows that you expect at least some basic unit tests for the code written by others.
Preparing for Profiling
Before you start checking coverage in Go, you need to prepare your code for profiling. This involves installing the necessary packages and setting up your test environment.
To install the necessary packages, run the command `go get -u github.com/axw/gocov/gocov`. This command installs the gocov package, which is used to generate coverage reports.
Next, you need to set up your test environment by running the command `go test -cover`. This command runs all the tests in your project and generates a coverage report.
Related reading: Golang Test Framework
Building a Binary for Profiling

To build an application for collecting coverage profiles, pass the -cover flag when invoking go build on your application binary target.
You can do this by running a command like go build -cover in your terminal. This will create a binary that can be used for coverage profiling.
The resulting binary can then be run using an environment variable setting to capture coverage profiles.
You can set the environment variable by following the instructions in the next section.
Suggestion: Golang Build Command
Using Go Build with GO111MODULE=Off
You can use go build -cover in GO111MODULE=off mode.
Only the package specifically named as the target on the command line will be instrumented for profiling when building a program in GO111MODULE=off mode.
Use the -coverpkg flag to include additional packages in the profile.
Running Profiling Tests
To run your application with coverage profiling, you'll need to set an environment variable. This variable is used to capture coverage profiles, which is a crucial step in checking coverage in Go.
The environment variable you need to set is not specified in the article section facts, but it's mentioned in the previous section as a prerequisite for running the application. Set it accordingly to enable coverage profiling.
Once you've set the environment variable, you can run your application as you normally would, and it will collect coverage profiles in the process.
Take a look at this: Golang Application
Running a Binary
To run a coverage-instrumented binary, you'll need to set the GOCOVERDIR environment variable to specify a directory for the profile data files.
The binary will write out profile data files to this directory at the end of its execution. If the GOCOVERDIR environment variable is not set, the binary will still execute correctly, but will issue a warning.
Multi-Run Tests
Multi-Run Tests can involve multiple program runs, which can produce a new data file with each run.
Integration tests often require multiple runs, especially when the program is built with the "-cover" option.

Each run will generate both a counter data file and a meta-data file, but only the counter data file changes from run to run.
This means that meta-data files only need to be written once, as they contain items that are invariant from run to run, such as source file names and function names.
The first run of an example program produced two files, a counter and a meta-data file, while the second run generated only a counter data file.
Suggestion: Golang Check If File Exists
Working with Profile Data
You can produce human-readable reports from coverage data files using the "go tool covdata" command.
To generate a text-format coverage profile for a given coverage test run, simply run "go test -coverprofile=abc.txt". This will write a text-format coverage profile to the specified file.
You can also generate a text-format profile after the fact by running "go tool covdata textfmt" on the files emitted into the GOCOVERDIR directory. This allows you to interpret and visualize the data just as you would with "go test -coverprofile".
Creating Profile Reports

Creating Profile Reports is a crucial step in working with profile data. You can use the `go tool covdata` command to produce human-readable reports from coverage data files.
To run `go tool covdata`, you need to have coverage data files first. These files are generated when you run your tests with the `-cover` flag.
You can use the `percent` command to produce a report on the percentage of statements covered. For example, `go tool covdata percent` will give you a report on percentage of statements covered.
Here are some key things to note about generating profile reports:
- Use `go tool covdata` to produce human-readable reports from coverage data files.
- Use the `percent` command to produce a report on the percentage of statements covered.
- Run `go tool covdata percent` to generate a report on percentage of statements covered.
By following these steps, you can get a clear understanding of how well your tests are exercising your code.
Visualizing and Merging Profiles
Go provides a built-in tool for visualizing coverage profiles, allowing you to see which parts of the code are not covered by tests.
You can generate a cover.html page with a visualized coverage profile by running a specific command. The command will create a page that shows you which parts of the code are not covered by tests.
To convert profiles to a text format, you can run go test -coverprofile=abc.txt. This will write a text-format coverage profile for a given coverage test run.
You can also generate a text-format profile after the fact by running go tool covdata textfmt on the files emitted into the GOCOVERDIR directory.
Visualize
Visualizing your code coverage is a great way to see which parts of your code need more attention. You can use the built-in "go tool covdata" to produce human-readable reports from coverage data files.
To get a visual representation, you can use the "go tool" with a specific command. This will generate a cover.html page with a visualized coverage profile.
The command to generate the cover.html page is simply a matter of running the right tool with the right parameters. You can use this visualized profile to identify areas of your code that need more testing.
Merging Raw Profiles with Go Tool Merge
If you've run your application multiple times with the -cover flag, you'll end up with a lot of data files in your output directory.
Each execution of a " -cover" built application will write out one or more data files to the directory specified in the GOCOVERDIR environment variable.
You can use the go tool covdata merge command to compact the data and/or combine data sets from different integration test runs.
The go tool covdata merge operation also accepts a -pkg flag that can be used to select out a specific package or set of packages, if that is desired.
This merge capability is also useful to combine results from different types of test runs, including runs generated by other test harnesses.
Selecting Packages
You can select specific packages for coverage instrumentation using the -coverpkg flag.
The flag accepts a list of import paths, not package names, so make sure to identify your packages by their import paths.
You can use the -coverpkg flag to extend coverage instrumentation to other packages beyond the main module, which is selected by default.
For example, you can use the flag to include packages that are dependencies listed in go.mod or part of the Go standard library.
The -coverpkg flag takes the same form as the Go command's -coverpkg flag, so you can use the same syntax to select packages for coverage instrumentation.
You can use the -pkg flag with "go tool covdata" commands to perform package selection as part of the operation, with the argument taking the same form as the -coverpkg flag.
In some cases, you may want to exclude certain packages from coverage instrumentation, even if they are part of the main module.
Data Collection and Conversion
You can collect coverage data using the integration test by running a wrapper script that invokes the previous script with the "-cover" flag, which gives you a coverage-instrumented binary.
This approach allows you to see how well your integration tests exercise your application's source code, and you can make changes to enhance the test harness and see the effects in the coverage report.
The wrapper script also sets the GOCOVERDIR environment variable to a directory where coverage data files will be written, and runs "go tool covdata percent" to produce a report on percentage of statements covered.
Here are the key things to note about collecting coverage data:
- Pass the “-cover” flag when running the integration test script
- Set the GOCOVERDIR environment variable to a directory for coverage data files
- Run “go tool covdata percent” to produce a coverage report
After collecting coverage data, you can convert profiles to text format by running "go test -coverprofile=abc.txt" or "go tool covdata textfmt" on the files emitted into the GOCOVERDIR directory.
Collecting Data with Integration Test
You can use integration test to collect coverage data by running a wrapper script that invokes the previous script, but builds the tool for coverage and then post-processes the resulting profiles.
The wrapper script passes in the “-cover” flag when running integration_test.sh, which gives us a coverage-instrumented “mdtool.exe” binary.

The wrapper script also sets the GOCOVERDIR environment variable to a directory into which coverage data files will be written.
When the test is complete, it runs “go tool covdata percent” to produce a report on percentage of statements covered.
Here are some key things to note about the wrapper:
- it passes in the “-cover” flag
- it sets the GOCOVERDIR environment variable
- it runs “go tool covdata percent”
Running the coverage testing wrapper again after making changes to the test harness will show the effects of the change in the coverage report.
Usage and Suites
In GoLand, a coverage suite is the collection of coverage data for a specific run. This data is stored in a specific directory, which varies depending on your operating system.
The results for one or more coverage suites can be displayed at once in GoLand. If multiple suites are selected, the IDE shows merged results, considering a line covered if it was executed in at least one of the selected suites.
Usage
You can use go-test-coverage in two ways: locally as part of your development process and as a step in your GitHub Workflow. It's recommended to utilize both options for Go projects.

To get started locally, you can use go-test-coverage as part of your development process. This will help you keep track of your test coverage as you write and refine your code.
Here are the two main ways to use go-test-coverage:
- Locally as part of your development process.
- As a step in your GitHub Workflow.
Using both options will give you a more comprehensive view of your test coverage and help you identify areas where you may need to focus your testing efforts.
Suites
GoLand allows you to display the results of one or more coverage suites at once. This means you can see the merged results from multiple suites, where a line is considered covered if it was executed in at least one of the selected suites.
A coverage suite is the collection of coverage data for a specific run. GoLand stores this data in a specific directory, which varies depending on your operating system.
On Windows, the coverage data is stored in %LOCALAPPDATA%\JetBrains\GoLand2025.2\coverage. On Linux, it's stored in ~/.cache/JetBrains/GoLand2025.2/coverage, and on macOS, it's stored in ~/Library/Caches/JetBrains/GoLand2025.2/coverage.
Featured Images: pexels.com


