Things to do before committing Go code

This blog post will list some of the basic things you should really do before committing Go code into your repository.

1) Run gofmt/goimports

gofmt is probably the most popular Go tool amongst gophers. The job of gofmt is to format Go packages, your code will be formatted to be consistent across your code base. For example if we have the following if statement:

if x == 42 { fmt.Println("The answer to everything") }

Then running gofmt on this will format the code to:

if x == 42 { 
    fmt.Println("The answer to everything") 
}

goimports is a tool that does exactly what gofmt does, but takes it a step further and adds/removes packages. For example if you had the following code:

package main

import (
    "log"
)

func main() {
    fmt.Println("Hello")
}

As you can see from this code, package ‘log’ is not used anywhere and package ‘fmt’ has not been imported. Running goimports on this code transforms it to:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello")
}

Running either of these tools on your code is a must, its great to see a code base using common idioms and a consistent format/style. And to me, it’s one of main things that makes Go code bases so more approachable than code bases in other languages.

2) Run golint

golint will lint your source code and make suggestions concerning coding style.

see golint on github.

3) Run go vet

Go vet is another important tool, it’s perfectly summed up by it’s documentation:

“Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.”

If we run go vet on the following code:

fmt.Printf("%s", 42)

We would get the following error:

test.go:6: arg 42 for printf verb %s of wrong type: int
exit status 1

Go vet differs from golint, go vet is concerned with correctness and golint is concerned with coding style.

see here for more information on vet.

4) Run build/install/run with -race flag:

There is a fully integrated race detector in the go tool chain. The race detector contains complex race detection and deadlock algorithms, to help you hunt down those hard to find concurrency related bugs.

To enable the race detector in your code, add the -race flag on the command line:

$ go test -race mypkg    // test the package
$ go run -race mysrc.go  // compile and run the program
$ go build -race mycmd   // build the command
$ go install -race mypkg // install the package

Note: Your code will run slower when you enable this flag, as the race detector is busy doing its thing :)

5) Run a dependency management tool

It’s very likely that your project will make use of 3rd party libraries. If you need to capture there versions, run a dependency management tool. Below is a list of some popular ones:

I have no real experience with the above tools but instead use the go 1.5 vendor experiment feature to capture dependencies.

I hope this lists helps you in some way in managing your Go projects. Remember, this is just a basic list, Go has numerous tools that you probably run which are essential to you or your project. The ones listed above are ones which I believe are the basics.