Lint Your Go Software With Golint

Writing well formatted and documented software should be the lowest common denominator. Moving beyond that, software should be written to follow best practices, use common conventions, and shouldn't try to be tricky unless it's one of those rare times to be tricky.

To catch where we fail at these situations and suggest improvements there are lint programs. To fill this space in Go we have Golint.

To understand what it does, here are a couple examples.

Last Else In A Function

Imagine a function like:

func Foo(bar int) int {
    if bar > 0 {
        return 123
    } else {
        return 456
    }
}

Do you see an easy way to improve this? Golint would suggest:

example.go:20:9: if block ends with a return statement, so drop this else and outdent its block

Here it points out that in the file example.go at line 20 there is an if/else block ending with a return and suggests an improvement. It would look like:

func Foo(bar int) int {
    if bar > 0 {
        return 123
    }

    return 456
}

Now we have the same functionality with fewer instructions.

Documentation Improvements

The way Go handles documentation is remarkable. Go isn't just a programming language. It provides a tool chain to get things done and that includes handling documentation.

But, just because Go has the tools to do documentation doesn't mean a project will have good documentation or even catch the places documentation is missing or could use improvement. Golint can help with this.

For example, it looks for package and function comments in the right place and following the pattern set in Effective Go. When it finds a deviation it lets you know with suggestions that look like:

baz.go:1:1: package comment should be of the form "Package baz ..."
baz.go:7:1: comment on exported function LogMe should be of the form "LogMe ..."
baz.go:19:1: comment on exported function AddTo should be of the form "AddTo ..."

Want To See More Rules?

If you want to see more of what it looks for in a project dig into the source. It follows it's own guidance on documentation so you should be able to read what's it's doing and get a look at the code to see how it does it.