DEV Community

Cover image for golangci-lint: a powerful and complete Go linter
Guiyomh
Guiyomh

Posted on

golangci-lint: a powerful and complete Go linter

golangci-lint is a Go linter tool that helps detect and fix style errors, convention errors, and potential vulnerabilities. It is based on a set of predefined rules, but you can also create your own rules.

Installation and configuration

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
Enter fullscreen mode Exit fullscreen mode

Use

To run golangci-lint on your project, use the following command:

golangci-lint run
Enter fullscreen mode Exit fullscreen mode

golangci-lint will display the list of errors and warnings found. You can also use the --format option to display the results in a custom format.

Here are some examples of issues that golangci-lint can detect:

  • Style errors: golangci-lint can detect the most common Go style errors, such as using uninitialized variables, missing comments, and bad indentation.
  • Convention errors: golangci-lint can detect violations of Go coding conventions, such as using non-compliant variable names or missing unit tests.
  • Vulnerabilities: golangci-lint can detect known vulnerabilities in Go code, such as the use of outdated encryption primitives or lack of input validation.

Configuration

golangci-lint is a very configurable tool. It supports several formats YAML, JSON, TOML. You can customize linter rules to meet your specific needs.

Enable or disable linters

linters:
  enable-all: true
  disable:
    - deadcode
    - exhaustivestruct
    - golint
    - ifshort
    - interfacer
    - maligned
Enter fullscreen mode Exit fullscreen mode

This configuration activates all linters except those listed.

Apply specific rules

For example, you can add custom rules to detect issues specific to your project.

rules:
      # Name of a rule.
      main:
        deny:
          - pkg: "github.com/sirupsen/logrus"
            desc: not allowed
          - pkg: "github.com/pkg/errors"
            desc: Should be replaced by standard lib errors package
Enter fullscreen mode Exit fullscreen mode

This configuration prohibits the use of certain packages (this is an example)

Linter included

Golangci-lint includes over 30 linters, which cover a wide range of code quality issues. These linters are divided into several categories:

  • Style linters check code for compliance with Go style conventions.
    • A variable declared without initialization.
    • A function declared with an inappropriate name.
    • A function that does not return a value.
  • Security linters detect potential security issues in code.
    • A function that uses an inappropriate data type for a parameter.
    • A function that does not properly isolate errors.
    • A function that uses an uninitialized variable.
  • Performance linters detect potential performance issues in code.
    • A loop that could be replaced by an arithmetic expression.
    • A function that could be simplified to improve performance.
    • A function that uses an inefficient Go structure.
  • Quality linters check the overall quality of code, including consistency, readability, and maintainability.
    • A variable that is used only once.
    • A function that is too long.
    • A function that is not well documented.

To get a list of supported linters and enabled/disabled linters, you can do the following command:

golangci-lint help linters
Enter fullscreen mode Exit fullscreen mode

Integration with IDEs

Golangci-lint offers integrations with major Go IDEs, allowing you to run the linter directly from your IDE.

To install the IDE extension for Visual Studio Code, open the VS Code Marketplace and search for "golangci-lint". Click on the extension to install it and add it to your configuration:

{
    "go.lintTool": "golangci-lint",
    "go.lintOnSave": "workspace",
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

You can adjust golangci-lint's behavior to suit your needs by configuring it. You can make sure your code is of the highest caliber by taking the effort to configure golangci-lint.

The following advice will help you configure golangci-lint:

  • Start with a simple setup and add options as you need them.
  • Learn more about the various configuration choices by reading the golangci-lint manual.
  • Take inspiration from the golangci-lint configuration samples.

I hope this piece clarifies the golangci-lint settings for you.

Top comments (0)