Static analysis of Go service code
In previous chapters, we covered many ways of testing and monitoring Go services to ensure high reliability; however, we still haven’t used a powerful technique, called static analysis. Static analysis is a process of examining code to identify potential issues in it, including (but not limited to) the following:
- Improper uses of
defer
statements - Invalid error handling (for example, invalid use of the
errors.As
function) - Mistakes using HTTP responses
- Invalid
Printf
formatting - Incorrect structured logging calls
- Unreachable code blocks
The power of static analysis lies in its ability to find potential problems in Go code even before executing it or running unit and integration tests. Static code analysis checks all code structures and function calls and identifies issues that would often be hard to detect otherwise (for example, not using the defer
statement correctly, causing memory leaks sometime...