Code coverage for Golang in 3 steps
- Enable the RKTracer tool and rebuild the application.
- Test the instrumented application and save the coverage data.
- 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 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.
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.
/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.
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 -on path-toproject/module/ go -v rktracer -off path-toproject/module/ go -v
goat the end means RKTracer needs to instrument only Go source files.-vat 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.
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.
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.
After generating code coverage, make sure to turn off (un-instrument) the RKTracer tool for the application that we instrumented to create the reports.
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:
# 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
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.
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"
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.
ignore *.go instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.goignores all Go source files from instrumentation.instrumentinstruments source files from the given folders.neverignores 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.
ignore *.go instrument *file-X.go *file-Y.go *file-Z.go function-ignore * function-instrument fun_X() fun_Y() fun_Z()
ignore *.goignores all Go programming source files.instrument *file-X.go *file-Y.go *file-Z.goinstruments 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).
Related code coverage guides
Working across more than one language or toolchain? These guides use the same RKTracer instrument, test, and rkresults workflow: