Tools to Improve Your Golang Code Quality
In this article, we intend to put forward a few code quality tools you should consider to help you write simple, yet fast go code in no time.
Join the DZone community and get the full member experience.
Join For FreeGolang is probably one of the simplest programming languages to read and understand, but not all code written in Go is simple. As it goes, anyone can write complex things by composing together simple stuff, in ways that make no sense. Writing Go can become a complex task as you figure out how not to write code and learn ways to simplify things. In this article, we intend to put forward a few code quality tools you should consider, to help you write simple, yet fast go code in no time!
Gofmt
This is a Go code formatting utility built into the Go language toolchain, which helps you write well-formatted code in the standard Golang programming style. Prescribed and built by the best of Go programmers, it is as simple as running a single line command in the terminal within your project directory to perform automatic code style reviews and help keep things consistent and easy to read.
How to install: Comes inbuilt with the Go toolchain.
How to run: go fmt -sw ./...
Govet
A vet
, like fmt
, ships with the standard Go distribution. It checks for the correctness of source code by matching the code against a set of pre-defined rules. Ideally, issues raised by vet
should be fixed on priority, and it should be run as often as possible.
How to install: Comes inbuilt with the Go toolchain
How to run: go vet ./...
Goimport
This is a tool to help you add missing imports, as well as remove imports that aren't used within your Golang program. This tool performs automatic code remediation, helping you write code without all the unnecessary hassles of figuring out where you are missing imports.
How to install: go get -u[golang.org/x/tools/cmd/goimports](<http://golang.org/x/tools/cmd/goimports>)
How to run: goimport
DeepSource
This is static analysis as a service offering that parses your Golang project repositories to provide comprehensive code reviews. DeepSource provides you with interesting reports about how to fine-tune your code according to the industry standards, helping you write performant services. The DeepSource Autofix utility helps you perform automatic code fixes on your repositories, sending PRs with a few clicks. You get all of this from within the comfort of a user-friendly GUI.
Staticcheck
Staticcheck is a state of the art linter for Go that performs automatic code reviews that finds bugs and performance issues in your code, all the while, offering simplifications that enforce the Go programming conventions. You can set your own preferences for code styles and the like by writing your own configurations, and setting expectations for the code remediation stage.
How to install: go get -u honnef.co/go/tools/cmd/staticcheck
How to run: staticcheck file.go
Go-critic
Go-critic is a highly opinionated golang linter that is basically a collection of code quality checks to detect style and performance issues, while also flagging common programming errors for code remediation. It is also MIT licensed and has some great documentation on their project website for you to get started.
How to install: go get -v -u[github.com/go-critic/go-critic/cmd/gocritic](<http://github.com/go-critic/go-critic/cmd/gocritic>)
How to run: gocritic check pkg/file.go
What Should I Use?
If you want a hassle-free, hosted experience with support and fewer false positives, go for a SaaS-based product like DeepSource. If you'd rather use an open-source tool, Staticcheck is a great choice. In either case, always make sure you run go vet
and let its checks pass before you commit to any code.
Opinions expressed by DZone contributors are their own.
Comments