When writing tests, it is often important to know how much of the actual code is getting exercised (or covered) by the tests. That number is an indication of the penetration of the test logic against the source code. Whether you agree or not, in many software development practices, test coverage is a critical metric as it is a measure of how well the code is tested.
Fortunately, the Go test tool comes with a built-in coverage tool. Running the Go test command with the
-cover flag instruments the original source code with coverage logic. It then runs the generated test binary, providing a summary of the overall coverage profile of the package, as shown in the following:
$> go test -cover PASS coverage: 87.8% of statements ok github.com/vladimirvivien/learning-go/ch12/vector ...