An important part of the process process of learning a new language is to learn the ecosystem that helps you to write better code. In a previous article I covered gdb integration with Go.
Next on my list is to find an equivalent of the fantastic Python coverage tool coverage.py. The Go equivalent is called gocov.
Let’s go ahead and install it now:
The two steps above have compiled and installed a binarygocov
progam in your $GOPATH/bin
. I would recommend adding this to your $PATH
:
Now we’re ready to start using gocov
in conjunction with our test suite. Here’s an example of running the test suite via gocov
, generating a coverage report, and then immediately viewing that report via the less
command.
If you’ve worked with coverage tools before, the report should look familiar, with a breakdown of each function. Here’s a sample:
It is also possible to annotate the source listing of a particular function:
This tells us that line 41 isn’t tested.
gocov
is a convenient tool that helps to guide you in writing tests for your program. Since it outputs to stdout you can use it in conjunction with grep
, sort
, less
, etc.
[Edited – 2013-01-09]: Amended the article to reflect the feedback I got on reddit.com/r/golang