Building Go Static Analysis Tools
I built static analysis tools for Go in 3 days as a part of summer internship at Mercari. Although these tools still need some refinements, they might be useful in most cases.
What I’ve built
Here’s what I’ve built.
1. checkunderscore
Go functions can return any number of results. In some cases, however, there may exist a function result which is always
handled with _
, which you can consider removing.
This tool can check this kind of return value on the package level.
2. norawinit
On some occasions, you may want to initialize a struct only in a particular function; for example, executing validation after every initialization. This tool helps you and your team develop under this limitation. By specifying an initialization function in the comment above struct declarations, it checks whether there is any initialization outside the function. It can be performed on package level, and also covers structs initialized in other packages.
What I’ve learned
I was completely unfamiliar with building static analysis tools, but I think I’ve learned way more than I initially thought I would. As I was (and still am) a beginner in Go, reading through specs of Go and ast package, which is almost necessary to build static analysis tools for Go, helped me deepen my understandings of the grammatical structure of Go. Using static analysis tools was pretty helpful for Go development, so I will keep developing these kinds of tools when necessary. Thanks to Mercari for providing me this great opportunity!