golangci-lint Setup with Custom Rules
Beginner8 minTrending
Install and configure golangci-lint with a curated set of linters and custom rules for consistent code quality.
Prerequisites
- -Go 1.21+ installed
Steps
1
Install golangci-lint
Install the latest version of golangci-lint using the official install script.
$ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
On macOS you can also use: brew install golangci-lint
2
Run with default settings
Run golangci-lint against your project to see baseline issues.
$ golangci-lint run ./...
3
Create a configuration file
Write a .golangci.yml with curated linters and settings for your project.
$ cat > .golangci.yml << 'EOF'
run:
timeout: 5m
linters:
enable:
- errcheck
- govet
- staticcheck
- unused
- gosimple
- ineffassign
- misspell
- gofmt
- revive
- gocritic
linters-settings:
revive:
rules:
- name: exported
severity: warning
gocritic:
enabled-tags:
- diagnostic
- style
issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
EOF
4
Run with the custom configuration
Execute the linter using your new config file.
$ golangci-lint run --config .golangci.yml ./...
5
Fix auto-fixable issues
Apply automatic fixes for issues that golangci-lint can resolve.
$ golangci-lint run --fix ./...
Review auto-fixed changes before committing. Not all fixes are perfect.
Full Script
FAQ
Discussion
Loading comments...