golangci-lint for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

golangci-lint: Linter Aggregator for Go

\n\n

1. What It Is and Why You Need It

\n\n

golangci-lint is a powerful linter aggregator for the Go language that combines dozens of popular static code analysis tools into a single, fast, and configurable interface. Instead of installing and running each linter separately (e.g., vet, staticcheck, errcheck, gocyclo, and dozens of others), the developer gets one command that runs them all in parallel, caches results, and produces a unified report. This saves hours of CI/CD setup and local development time.

\n\n

Why is it needed? In large Go projects (microservices, CLI utilities, web servers), maintaining code quality manually is impossible. Errors like unhandled errors, unused variables, incorrect formatting, or potential deadlocks can lead to bugs in production. golangci-lint automatically finds such issues during code review or pre-commit. It also helps maintain a consistent code style within the team, which is critical for readability and maintainability.

\n\n

Key advantages over running linters individually: speed (linters run in parallel, results are cached), flexibility (you can enable/disable any linter, configure its rules via YAML config), integration (works with IDEs, CI/CD systems, supports go vet out of the box), and unified output format (JSON, SARIF, colored text — for any tool).

\n\n

2. Installation

\n\n

Installing golangci-lint can be done in several ways. The recommended method is via go install (requires Go 1.18+):

\n\n
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
\n\n

After installation, the binary will be available in $GOPATH/bin (usually ~/go/bin). Verify the installation:

\n\n
golangci-lint --version
\n\n

Alternative methods (for CI/CD or if Go is not available):

\n
    \n
  • Linux/macOS (curl): curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
  • \n
  • macOS (Homebrew): brew install golangci-lint
  • \n
  • Docker: docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.55.2 golangci-lint run -v
  • \n
\n\n

3. Quick Start — Minimal Working Example

\n\n

Create a simple Go module and a file with an intentional error:

\n\n
mkdir myproject && cd myproject\ngo mod init myproject
\n\n

Create a file main.go:

\n\n
package main\n\nimport "fmt"\n\nfunc main() {\n    x := 10\n    fmt.Println("Hello")\n    // Error: unused variable x\n}
\n\n

Run the linter:

\n\n
golangci-lint run
\n\n

The output will be something like:

\n\n
main.go:6:2: `x` is unused (deadcode)\n    x := 10\n    ^
\n\n

The linter found an unused variable. Fix the code (remove the line x := 10) and run it again — there will be no errors.

\n\n

For a stricter check, use the -v (verbose) flag and --fast (only fast linters):

\n\n
golangci-lint run -v --fast
\n\n

4. Main Methods/Functions/Commands

\n\n

Unlike a library, golangci-lint is a CLI tool. Its "methods" are commands and flags. Let's look at 8 key commands and options.

\n\n

4.1. run — Main linter execution

\n

Signature: golangc

Recommendations