LinuxCommandLibrary

golangci-lint

Lint and analyze Go source code

TLDR

Run linters in the current folder

$ golangci-lint run
copy

List enabled and disabled linters (Note: Disabled linters are shown last, do not mistake them for enabled ones)
$ golangci-lint linters
copy

Enable a specific linter for this run
$ golangci-lint run [[-E|--enable]] [linter]
copy

SYNOPSIS

golangci-lint command [flags]

Common commands:
run: Runs linters on the specified paths.
fix: Automatically fixes issues reported by linters.
linters: Lists all supported linters.
version: Prints the golangci-lint version.

Example: golangci-lint run ./...
Example with flags: golangci-lint run --config .golangci.yml --timeout 5m

PARAMETERS

--config
    Specify a custom configuration file path.

--enable
    Enable a specific linter. Can be used multiple times.

--disable
    Disable a specific linter. Can be used multiple times.

--skip-dirs
    Skip directories matching the specified glob pattern.

--out-format
    Specify the output format (e.g., github-actions, json, tab, line).

--issues-exit-code
    Exit code to use when issues are found (default 1).

--fix
    Automatically fix issues (if supported by linters and the fix command is used).

--verbose
    Enable verbose output, showing more details about execution.

--timeout
    Maximum duration for linters execution (e.g., 5m, 30s).

--concurrency
    Number of linters to run in parallel.

--path-prefix
    Path prefix to remove from output file paths for cleaner reports.

--print-resources-usage
    Print CPU and memory resources usage of linters at the end of execution.

DESCRIPTION

golangci-lint is a powerful command-line interface tool designed to orchestrate and run multiple Go linters efficiently. It aggregates the results from various static analysis tools, such as go vet, errcheck, staticcheck, and many others, into a single, comprehensive report. The primary goal of golangci-lint is to significantly speed up the linting process by running linters in parallel and caching their results. It provides a unified configuration file (.golangci.yml) to enable, disable, and configure individual linters, making it easier for developers to maintain code quality and consistency across projects. This tool is widely adopted in Go development workflows, especially in CI/CD pipelines, to enforce coding standards and catch potential issues early in the development cycle.

CAVEATS

While designed for speed, golangci-lint can still be resource-intensive on very large codebases, potentially consuming significant CPU and memory. Fine-tuning its extensive configuration for specific project needs can sometimes be complex. Users should also be aware that some linters might occasionally produce false positives, requiring careful consideration and potential issue-specific filtering.

SUPPORTED LINTERS

golangci-lint aggregates a vast collection of Go linters, supporting over 100 different static analysis tools. Users can view the full list of supported linters by running the command golangci-lint linters.

CONFIGURATION FILE (<I>.GOLANGCI.YML</I>)

The tool leverages a powerful YAML configuration file, typically named .golangci.yml, to define its behavior. This file allows users to enable or disable specific linters, fine-tune individual linter settings, filter issues based on severity or pattern, and customize output formats. This centralized configuration greatly simplifies project-wide code quality enforcement.

HISTORY

golangci-lint emerged around 2018 as a comprehensive solution to the growing challenge of managing numerous individual Go linters. Before its development, developers often had to manually install and configure dozens of separate linter tools, leading to inefficiencies and inconsistent code quality checks. The project's core objective was to provide a single, fast, and opinionated tool that could run all these linters in parallel with unified configuration and output. Its focus on performance, ease of use, and broad linter coverage quickly led to its widespread adoption, establishing it as a standard for Go code quality checks in both development environments and CI/CD pipelines.

SEE ALSO

go(1), go vet(1), gofmt(1), staticcheck(1)

Copied to clipboard