Up your Go Game with golangci-lint

November 5, 2019

Another linter?

If you’re working on Go projects, running golangci-lint is a no brainer. It has helped me catch real problems on many different projects.

There are a lot of Go linters out there:

Go linters supported by ALE

(source: ALE supported tools)

but, in the tradition of gometalinter, golangci-lint runs a bunch of other linters for you. Golangci-lint is convenience itself, you can skip:

each linter separately.

How to install it?

Installing golangci-lint can be as easy as:

> brew install golangci/tap/golangci-lint

(see here for alternative installations)

How to run it?

Go to the base directory of your project and run:

> golangci-lint run

and marvel at everything you’re doing wrong 😃

I personally like to take it to the next level and run:

> golangci-lint run --enable-all

which runs ALL the linters. Which linters are run by default?

Golangci-lint default linters

(also here)

It hurts!

The best time to plant a tree...

When is the best time to run a linter?

back when you started your project…

The second best time is now.

Douglas Crockford said it best:

JSLint will hurt your feelings.

which can be generalized to most linters. But, over time, using a linter makes you internalize the rules. As you learn the rules, you learn to avoid breaking them. Eventually, your project is clean and there’s nothing to feel bad about.

However, in the meantime…

Taking it one step at a time

If you’re getting too many warnings, there are ways to cope. You can put a config file (called .golangci.yml) at the base of your project. Here’s mine, for example:

    - generated
    - wsl

(all config options)


That highlights a good-news/bad-news aspect of linters: they are moving targets. When you update them, you get newer and better rules, but new “problems” might be flagged in your previously clean project.

Use common sense to decide if/when you want to update your linter, and whether the new rules make sense for your project (or go too far, for your liking, into crazy). Linters are meant to cheaply catch problems, not just make you feel bad.

Discuss on Twitter