Don’t Commit Improperly Formatted Go (golang) Code – Cory LaNou – Medium

By Cory LaNou

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
fi

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
fi

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 https://gist.githubusercontent.com/corylanou/3639c901965922d5507ce4acf539de4a/raw/e535b525c408b5b145fed5d17c854c2a6dc90216/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.