Go: How to Build Your Own Analyzer
Go tools provides go/analysis, an API for analysis tools. The documentation clearly explains the aim of this package:
A static analysis is a function that inspects a package of Go code and reports a set of diagnostics (typically mistakes in the code), and perhaps produces other results as well, such as suggested refactorings or other facts
This new interface and tool provided by Go will allow developers to improve their daily work with creating a whole workflow.
Analyzer example
For this article, I will show how to write a custom analyzer that analyses the usage of the contexts in our code. As explained in the documentation, the context should be the first parameter, typically named ctx
. The analyzer will make sure the code will respect this recommendation.
The first thing to do when building a custom analyzer is to define the usage of the command:
Then, we can define some flags in order to control the usage of our analyzer: