- read

Why Your Go Code Sucks and How Linters Can Save You

Phuong Le (@func25) 63

Go Productivity — Go Linters

Photo by Towfiqu barbhuiya on Unsplash

Have you ever wished you had a mentor who could quietly remind you, “YOU FORGOT THE RULE!” every time you make a coding mistake?

Or perhaps you’re trying to establish team guidelines, but you notice that team members frequently break them, either because they’re new or they just don’t feel like adhering to them?

Linters are tools that can serve as that mentor, catching errors in your code and explaining what’s wrong. Let’s dive into a quick example:

fieldalignment: struct of size 32 could be 24 (govet)

Here, MyStruct isn’t as efficient as it could be. Its size could shrink from 32 to 24 bytes with the right configuration.

“Really, I can change the size of a struct?”

If that’s news to you, you might want to check out my other article: “What is Alignment? A Small Change for a Huge Impact.”

This guide aims to walk you through installing linter-aggregator, setting it up, and customize it to your needs

1. What linter should I use?

Golint used to be the standard for checking Go code, but it’s no longer the go-to option. In its absence, new tools like revive, go-critic, govet,…

This has led to a sudden increase in different choices, making it sometimes hard to figure out what’s best for different needs and preferences.

“Huh, so which one now?”

Well, I’d like to point you toward golangci-lint. Despite its name suggesting it’s a single tool, it’s actually more of a linter bundle. Think of it as a…