golangci-lint: a powerful and complete Go linter
Tags: go, productivity, codequality, cicd
Published: 2023-10-31 10:48:59

**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

```bash
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

## Use

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

```bash
golangci-lint run
```

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

```yaml
linters:
  enable-all: true
  disable:
    - deadcode
    - exhaustivestruct
    - golint
    - ifshort
    - interfacer
    - maligned
```

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. 

```yaml
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
```

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:

```bash
golangci-lint help linters
```

## 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:

```json
{
    "go.lintTool": "golangci-lint",
    "go.lintOnSave": "workspace",
}
```

## 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.