What is golangci-lint?
Golangci-lint is a powerful linter aggregator for Go. It's designed to improve code quality, find bugs, and enforce coding standards. It runs multiple Go linters concurrently, saving time and computing resources. It's highly configurable, allowing developers to enable or disable specific linters and tailor the linting process to their project's needs.
Golangci-lint is the all-in-one/one-stop-shop linting solution for go, and it’s even better when run with Trunk! Let's dive into installing and running golangci-lint.
Installing golangci-lint
With Trunk Check, you can automatically install and configure golangci-lint along with any relevant linters in a few straightforward steps. Here's how:
First, if you haven't already installed Trunk CLI, you can do so with the command below:
1curl https://get.trunk.io -fsSL | bash
Next, you can initialize Trunk from the root of your git repository:
1trunk init
This command will scan your repository and create a .trunk/trunk.yaml
file that enables all linters, formatters, and security analyzers, recommended by Trunk Check. This includes golangci-lint if applicable to your project.
To see all available linters Trunk Check installed, simply run:
1trunk check list
If you findgolangci-lint is not automatically enabled, you can do so by running:
1trunk check enable golangci-lint
Alternatively, to disable golangci-lint run the command below. To disable other tooling applied by Trunk Check, simply replace golangci-lint
with the respective tool you're looking to disable.
1trunk check disable golangci-lint
For more details on Trunk Check setup, see here.
Configuring golangci-lint
Most linters provide some mechanism to tweak their configuration, e.g. .eslintrc
or Cargo.toml
. Trunk is aware of all the ways individual tools are configured and supports them. This means linters you've already configured will continue to work exactly the same, just now supercharged by Trunk Check.
Like many linters with Trunk, golangci-lint works out of the box so there's no need to set up a custom configuration. If you're interested in more customization, golangci-lint supports .golangci.json
, .golangci.toml
, .golangci.yaml
, .golangci.yml
as optional configs You can learn more about golangci-lint and its config in our docs.
If you're interested in other tooling outside of golangci-lint, check out our open-source repository to see how we define and support 90+ linters.
Running golangci-lint
To check your code with golangci-lint, run the command below. This command executes golangci-lint along with any other linters Trunk Check has enabled on files you've modified. Since Trunk is git-aware, it knows what you've changed, and by adding batched execution and caching, you end up with a much faster and smoother way to run golangci-lint and other tools.
1trunk check
If you prefer to check files you've modified with only, run the following:
1trunk check --filter=golangci-lint
Although we'd recommend against it depending on the size of your repository, you can check all files with golangci-lint by running the command below.
1trunk check --all --filter=golangci-lint
In most scenarios, you'll want to execute against modified files. Since Trunk is git-aware, it knows what you've changed, and by adding batched execution and caching, you end up with a much faster and smoother way to run golangci-lint and other tools.
Updating Trunk Check & golangci-lint
To upgrade the Trunk CLI along with all plugins and linters in your trunk.yaml
simply run:
1trunk upgrade
We highly recommend running on the latest validated versions of tools as updates will frequently include important security fixes and additional valuable checks. Trunk only auto-suggests linter upgrades to versions that we have tested and support, so you may see a slight lag time when a new linter version is released.
Upgrade will also recommend new tools that have become applicable since the last time your repository was scanned. This can be a result of using new technologies in your repository or Trunk itself adding support for more tools. If you don't like a particular recommendation, you can always run trunk check disable <linter>
to teach trunk not to recommend it.
Recommended Linters to Pair with golangci-Lint
While golangci-lint covers a wide range of linting needs, pairing it with these tools can further enhance your Go project's code quality:
gofmt: The standard Go formatter, ensuring your code adheres to Go’s formatting guidelines.
gofumpt: A stricter version of gofmt, enforcing more consistent code style rules.
golines: Helps with wrapping long lines of code, making your code more readable.
goimports: Formats your code and manages import statements by automatically adding and removing them as needed.
nancy: Scans your Go dependencies for known vulnerabilities, helping to keep your project secure.