What is golangci-lint and why is it needed?
\n\ngolangci-lint is not just a library, but a powerful linter aggregator for the Go programming language. It is a single tool that runs many popular static code analyzers (linters) simultaneously, combining their results into one clear report. Essentially, it is a "linter of linters."
\n\nWhy is it needed? In modern Go development, maintaining code quality is a critical task. Disparate linters such as vet, staticcheck, errcheck, gocyclo, and dozens of others, each with their own flags and settings, can turn CI/CD configuration into a nightmare. golangci-lint solves this problem by providing:
- \n
- Single entry point: One command runs all configured linters. \n
- Parallel execution: Linters run in parallel, significantly speeding up the analysis process. \n
- Caching: Results are cached, making repeated runs (e.g., when only one file changes) nearly instantaneous. \n
- Flexible configuration: Easily enable/disable linters, configure their behavior via a YAML file or command-line arguments. \n
- Integration with IDE and CI/CD: Supported by most modern development environments (VS Code, GoLand) and continuous integration systems (GitHub Actions, GitLab CI, Jenkins). \n
Using golangci-lint helps teams automatically detect potential errors, stylistic issues, unused code, performance and security problems early in development, leading to more reliable and maintainable code.
\n\nInstalling golangci-lint
\n\nUnlike regular Go libraries, golangci-lint is installed as a binary executable file, not imported into code. There are several installation methods:
\n\n1. Quick installation (recommended for CI/CD)
\nThe simplest and preferred method is to use the installation script:
\ncurl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1\nNote: Replace v1.59.1 with the current version from GitHub Releases.
2. Installation via go install
\nIf you have Go version 1.21 or newer installed:
\ngo install github.com/golangci/golangci-lint/cmd/golangci-lint@latest\n\n3. Installation via package managers (macOS/Linux)
\nmacOS (Homebrew):
\nbrew install golangci-lint\nLinux (Snap):
\nsudo snap install golangci-lint --classic\n\n4. Verify installation
\nAfter installation, verify that everything works:
\ngolangci-lint --version\nYou should see something like: golangci-lint has version 1.59.1 built from ...
Quick start with a code example
\n\nLet's create a simple Go project and run golangci-lint on it.
\n\nStep 1: Create the project
\nmkdir my-lint-project\ncd my-lint-project\ngo mod init my-lint-project\n\nStep 2: Write "bad" code
\nCreate a file main.go:
package main\n\nimport (\n "fmt"\n "io/ioutil" // deprecated\n "log"\n)\n\nvar unusedGlobalVar = 42 // unused variable\n\nfunc main() {\n x := 10 // unused variable\n fmt.Println("Hello")\n readFile("test.txt")\n}\n\nfunc readFile(path string) {\n data, err := ioutil.ReadFile(path) // error is not handled\n if err != nil {\n log.Fatal