8 Testing and tooling
Testing sometimes gets a bad rap as a chore or an afterthought, as something to be added later after the difficult work of solving problems is done. Often, you will hear programmers talk about testing in terms of “peace of mind,” meaning that tests can help verify that your software continues to work as expected, even as new features are added or parts of the underlying logic are changed. If the tests keep passing, the reasoning goes, your code is still functioning as intended, or at least in all the ways that matter.
Peace of mind is certainly an important benefit of testing, but if all you are concerned with is verifying your code after the fact, then you’re missing out on much of what testing can do for you, especially in a language with a solid set of tooling like Go.
Rather than being defensive, tests can be proactive: you can wield them like a scalpel instead of a shield to identify and target areas for improvement in existing code.
The extended tooling around profiling and testing remains one of Go’s enduring strengths and, when combined with benchmarking, it enables you to identify and target areas for performance improvement. By creating a benchmark and adding a couple of flags, you can get targeted statistics on CPU usage and memory allocation, sometimes down to the individual line.