Cory LaNou
Sun, 17 Mar 2019

Don’t Commit Improperly Formatted Go (golang) Code


This article will outline how in just a few easy steps using git hooks how to ensure you never commit improperly formatted 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

Hype Quick Start Guide


This article covers the basics of quickly writing a technical article using Hype.

Learn more

Writing Technical Articles using Hype


Creating technical articles can be painful when they include code samples and output from running programs. Hype makes this easy to not only create those articles, but ensure that all included content for the code, etc stays up to date. In this article, we will show how to set up Hype locally, create hooks for live reloading and compiling of your documents, as well as show how to dynamically include code and output directly to your documents.

Learn more

Go (golang) Slog Package


In Go (golang) release 1.21, the slog package will be added to the standard library. It includes many useful features such as structured logging as well as level logging. In this article, we will talk about the history of logging in Go, the challenges faced, and how the new slog package will help address those challenges.

Learn more

Subscribe to our newsletter