Product
Solutions
Resources
Company
Download Trial Book a Demo

Code Coverage for Golang Applications

This guide shows how to generate code coverage for Golang (Go language) applications from the command line using the RKTracer tool, in three simple steps. Add the rkgo runtime to your Go installation, instrument the application with rktracer, run your tests, and produce an HTML coverage report. The same steps also run inside Docker and CI.

Code coverage for Golang in 3 steps

  1. Enable the RKTracer tool and rebuild the application.
  2. Test the instrumented application and save the coverage data.
  3. Generate the code coverage reports.

One-time setup first: add the rkgo runtime library to your Go installation (GOROOT).

Code coverage for a Golang sample application with RKTracer
RKTracer instruments Go sources so you can measure statement, decision, condition and MC/DC coverage.

Code coverage for a Golang sample app

To generate code coverage for Golang applications using the RKTracer tool, you need to add the RKTracer runtime library to the Go installation folder, be it a Windows or a Linux platform, and instrument the application with the RKTracer tool. Then test the instrumented application and generate code coverage.

Adding the RKTracer runtime library to the Go installation

Add the RKTracer runtime library rkgo to the GOROOT, i.e., the Go installation folder. It is a one-time setup.

Windows or Linux. To integrate the RKTracer runtime library source file into the Go installation, follow the instructions below.

Step 1. Open a command prompt and run the command.

find GOROOT
go env GOROOT

If Go is on the path, then you should see the following output. In my case, it is in the path /usr/lib/go-1.26.

go env GOROOT (output)
/usr/lib/go-1.26

Step 2. The command output gives us the path where Go is installed. Now create an rkgo folder in the src folder of the Go installation and copy rkgo.go from the RKTracer installation folder into the rkgo folder.

copy rkgo into GOROOT
sudo cp  $HOME/rktracer/share/rktracer/rkgo.go /usr/lib/go-x.x/src/rkgo/

Replace go-x.x with your actual Go version folder from the go env GOROOT output (for example, go-1.26).

Code coverage for Golang applications example

Identify the project folder that needs to be instrumented and generate coverage data. Please make sure the project is configured and you are able to test it.

Enable the RKTracer tool

Then execute the following command to Turn ON (instrument) or Turn OFF (un-instrument) the RKTracer tool. RKTracer will instrument all Go source files in a folder recursively.

RKTracer turn ON / OFF
rktracer -on  path-toproject/module/ go  -v
rktracer -off  path-toproject/module/ go  -v
  • go at the end means RKTracer needs to instrument only Go source files.
  • -v at the end means RKTracer will show a verbose instrumentation log.

The above command will instrument all the Go source files in the project folder recursively. Rebuild the application with the go clean and go build commands.

rebuild the application
go clean
go build

Run tests on the instrumented application

Testing can be unit testing, or functional or integration testing of the application.

Once the testing is completed, you should see the file rk-coverage.txt. Now copy this file to the project folder, or the path where you executed the command rktracer -on path-to-project/module/ go -v to instrument the project folder using the RKTracer tool.

You should also see the rktracer folder with the RKTracer tool intermediate files. The rktracer folder is generated at the time of instrumentation. You need to copy the rk-coverage.txt file parallel to the rktracer folder in the project.

Generate code coverage reports

Once you have the rk-coverage.txt file, run the rkresults command to generate the HTML reports. Ensure that you run the rkresults command in the application root folder, or where you have the rktracer folder generated at the time of build/instrumentation.

generate the report
rkresults

The rkresults command will search for the coverage data file rk-coverage.txt and map it with the JSON files (generated during instrumentation) in the rktracer folder, then generate the HTML reports. You can manually open the HTML report using index.html.

Turn off the tool afterwards

After generating code coverage, make sure to turn off (un-instrument) the RKTracer tool for the application that we instrumented to create the reports.

un-instrument the project
rktracer -off  path-toproject/module/ go  -v

Code coverage for Golang setup in Docker

You can generate code coverage for Golang applications inside Docker or any CI container. Install RKTracer into the image, activate the trial licence, then copy the rkgo runtime into both the Go installation (GOROOT) and your module's vendor tree. Finally, instrument the sources with rktracer -on before you build and test. The setup commands below correspond to the container build script:

container build script
# fail fast and echo each command
set -ex

# install RKTracer into the image and put it on PATH
sh rktracer-installer-linux.run --accept-license --quiet --target /opt/rktracer
export PATH="/opt/rktracer/bin:$PATH"
which rktracer

# activate the trial licence and verify
cp trial-license.lic /opt/rktracer
rktracer -version

# add the rkgo runtime to GOROOT and to the module vendor tree
mkdir /usr/local/go/rkgo
cp /opt/rktracer/share/rktracer/rkgo.go /usr/local/go/rkgo/
mkdir -p /app/vendor/rkgo
cp /opt/rktracer/share/rktracer/rkgo.go /app/vendor/rkgo/

# instrument the Go sources under ./src
rktracer -on src go -v

set +ex
Container build script: install RKTracer, add the rkgo runtime to GOROOT and the module vendor tree, then instrument the Go sources.

Dropped into a Dockerfile, the same steps look like this. After the image is built, run go build, execute your tests, and run rkresults to produce the HTML coverage report exactly as on the command line.

Dockerfile
FROM golang:1.21

WORKDIR /app
COPY . .

# RKTracer installer + trial licence are copied in alongside the sources
RUN set -ex \
 && sh rktracer-installer-linux.run --accept-license --quiet --target /opt/rktracer \
 && export PATH="/opt/rktracer/bin:$PATH" \
 && cp trial-license.lic /opt/rktracer \
 && rktracer -version \
 && mkdir -p /usr/local/go/rkgo /app/vendor/rkgo \
 && cp /opt/rktracer/share/rktracer/rkgo.go /usr/local/go/rkgo/ \
 && cp /opt/rktracer/share/rktracer/rkgo.go /app/vendor/rkgo/ \
 && rktracer -on src go -v \
 && go build ./...

ENV PATH="/opt/rktracer/bin:$PATH"
Equivalent Dockerfile that instruments and builds the Go application in one image layer.

Enable coverage for selected files

Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and want to ignore coverage for the folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder, go to the end of the file, and add the following information.

rktracer.config
ignore *.go
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
  • ignore *.go ignores all Go source files from instrumentation.
  • instrument instruments source files from the given folders.
  • never ignores the selected folders.

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder, go to the end of the file, and set the following variables as shown below.

rktracer.config
ignore *.go
instrument *file-X.go *file-Y.go *file-Z.go
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()
  • ignore *.go ignores all Go programming source files.
  • instrument *file-X.go *file-Y.go *file-Z.go instruments only these three source files.
  • function-ignore * then ignores all functions in the above three files.
  • function-instrument fun_X() fun_Y() fun_Z() does not ignore these three functions from these three files during instrumentation.

Golang code coverage FAQ

What is code coverage for Golang?

Code coverage for Golang (Go) measures which Go statements, branches, and functions your tests actually execute. RKTracer instruments your Go source, runs your existing tests, and reports statement, decision, and function coverage as an HTML report.

How do I generate code coverage for a Go application with RKTracer?

Add the rkgo runtime to GOROOT once, run rktracer -on path-to-project/module/ go -v to instrument, rebuild with go clean and go build, run your tests to produce rk-coverage.txt, then run rkresults to generate the HTML coverage report.

Can I run Golang code coverage in Docker or CI?

Yes. Install RKTracer in the image, copy rkgo.go into GOROOT and into your module's vendor folder, instrument the sources with rktracer -on, then build and test as usual. See the Docker setup section above.

Does RKTracer support the Go language on Windows and Linux?

Yes. RKTracer measures code coverage for the Go language on both Windows and Linux. The rkgo runtime setup is a one-time step in your Go installation (GOROOT).

Working across more than one language or toolchain? These guides use the same RKTracer instrument, test, and rkresults workflow:

Stuck on your Go build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.