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.
Let’s go ahead and install it now:The two steps above have compiled and installed a binary
gocovprogam in your
$GOPATH/bin. I would recommend adding this to your
$PATH: Now we’re ready to start using
gocovin 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
lesscommand. 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.
gocovis 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
less, etc. [Edited – 2013-01-09]: Amended the article to reflect the feedback I got on reddit.com/r/golang