Cory LaNou
Sun, 17 Mar 2019

Don’t Commit Improperly Formatted Go (golang) Code

I work on a lot of Go (golang) projects, and I still see simple things going wrong. One of them is improperly formatted Go code. I also include in the category of improperly formatted to mean any code that didn’t use go imports to sort the import statements.

On the surface, many people might think this is me being a little over-critical of the code quality. In part, that is true. However, the real issue is the fact that it creates unnecessary code-churn in your commits and code diffs. For instance, if I edit a file and make a one line change, but the code wasn’t formatted properly when I got to it, it may result in 10 lines of code changing that have nothing to do with my actual change. This leads to confusion in PR reviews, changes the blame on a line of code that shouldn’t have changed, etc.

All of this is very avoidable if you are using a technology like git and employ a hook .

You’ll find a directory in your git project at the root in .git/hooks . In there will already be some .sample hooks for you to look at. We are interested in the pre-commit hook.

This is what my pre-commit hook looks like for all of my Go projects:

#!/usr/bin/env bash

fmtcount=`git ls-files | grep '.go$' | xargs gofmt -l 2>&1 | wc -l`
if [ $fmtcount -gt 0 ]; then
    echo "Some files aren't formatted, please run 'go fmt ./...' to format your source code before committing"
    exit 1

vetcount=`go vet ./... 2>&1  | wc -l`
if [ $vetcount -gt 0 ]; then
    echo "Some files aren't passing vet heuristics, please run 'go vet ./...' to see the errors it flags and correct your source code before committing"
    exit 1

Note: I didn’t come up with this pre-commit file. I learned this trick working with the great developers at Influx Data .

I have this in a gist as well. To install this gist in a pre-commit hook for your project, run these commands (be sure to be in the root of your project first):

# from the root of your project:
curl -o .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

And that’s it! Now, the next time you try to commit code that doesn’t pass go fmt or go vet you will get an error like this:

$ commit 'test'
Some files aren't formatted, please run 'go fmt ./...' to format your source code before committing

Feel free to contact me with suggestions and feedback on this article on twitter .

More Articles

Exploring "io/fs" to Improve Test Performance and Testability

The most anticipated feature of Go 1.16 is the addition to the Go tooling, and standard library, that allow for embedding static content into binaries. While it is tempting to start playing with this new toy right away, it is important to understand how it works first. In this article we are going to take a look at the new io/fs package introduced in Go 1.16 to support embedding.

Learn more

Where and When to use Iota in Go

Iota is a useful concept for creating incrementing constants in Go. However, there are several areas where iota may not be appropriate to use. This article will cover several different ways in which you can use iota, and tips on where to be cautious with it's use.

Learn more

Leveraging the Go Type System

If you haven't worked in a typed language before, it may not be obvious at first the power that it brings. This article will show you how to leverage the type system to make your code easier to use and more reusable.

Learn more

Subscribe to our newsletter