go-linting
Scannednpx machina-cli add skill cxuu/golang-skills/go-linting --openclawGo Linting
Source: Uber Go Style Guide
Core Principle
More important than any "blessed" set of linters: lint consistently across a codebase.
Consistent linting helps catch common issues and establishes a high bar for code quality without being unnecessarily prescriptive.
Minimum Recommended Linters
Source: Uber Go Style Guide
These linters catch the most common issues while maintaining a high quality bar:
| Linter | Purpose |
|---|---|
| errcheck | Ensure errors are handled |
| goimports | Format code and manage imports |
| revive | Common style mistakes (modern replacement for golint) |
| govet | Analyze code for common mistakes |
| staticcheck | Various static analysis checks |
Note:
reviveis the modern, faster successor to the now-deprecatedgolint.
Lint Runner: golangci-lint
Source: Uber Go Style Guide
Use golangci-lint as your lint runner:
- Performance: Optimized for large codebases
- Unified config: Configure many linters at once
- Extensible: Add linters as needed for your project
See the example .golangci.yml from uber-go/guide.
Example Configuration
Create .golangci.yml in your project root:
linters:
enable:
- errcheck
- goimports
- revive
- govet
- staticcheck
linters-settings:
goimports:
local-prefixes: github.com/your-org/your-repo
revive:
rules:
- name: blank-imports
- name: context-as-argument
- name: error-return
- name: error-strings
- name: exported
run:
timeout: 5m
Running
# Install
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Run all linters
golangci-lint run
# Run on specific paths
golangci-lint run ./pkg/...
Quick Reference
| Task | Command/Action |
|---|---|
| Install golangci-lint | go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest |
| Run linters | golangci-lint run |
| Run on path | golangci-lint run ./pkg/... |
| Config file | .golangci.yml in project root |
| CI integration | Run golangci-lint run in pipeline |
Linter Selection Guidelines
| When you need... | Use |
|---|---|
| Error handling coverage | errcheck |
| Import formatting | goimports |
| Style consistency | revive |
| Bug detection | govet, staticcheck |
| All of the above | golangci-lint with config |
See Also
- For core style principles:
go-style-core - For testing best practices:
go-testing
Source
git clone https://github.com/cxuu/golang-skills/blob/main/skills/go-linting/SKILL.mdView on GitHub Overview
Sets up Go linting with golangci-lint using a curated set of linters (errcheck, goimports, revive, govet, staticcheck). Emphasizes lint consistency across a codebase to catch common issues early and raise the bar for quality without being prescriptive. Includes a ready .golangci.yml configuration and CI/CD integration guidance.
How This Skill Works
Install golangci-lint, configure linters in a .golangci.yml, and run golangci-lint run. golangci-lint provides a unified runner to apply multiple linters across the codebase, with a centralized configuration that can be used in local development and CI. The example config enables core linters and sets revive rules, which can be adjusted to fit your project needs.
When to Use It
- Setting up linting for a new Go project or CI/CD pipeline
- Integrating linting into CI to fail builds on style or correctness issues
- Maintaining code quality across a large Go codebase or monorepo
- Enforcing error handling, formatting, and style consistency in teams
- Migrating from golint to revive and consolidating lint checks with golangci-lint
Quick Start
- Step 1: Install golangci-lint
- Step 2: Create and customize .golangci.yml (enable linters like errcheck, goimports, revive, govet, staticcheck)
- Step 3: Run golangci-lint run and optionally integrate with CI
Best Practices
- Pin and install a specific golangci-lint version in CI to ensure reproducible results
- Enable the recommended linters: errcheck, goimports, revive, govet, staticcheck
- Place the config file at the project root as .golangci.yml and commit it
- Set a reasonable run timeout (e.g., 5m) to balance thoroughness and speed
- Customize revive rules (blank-imports, context-as-argument, error-return, error-strings, exported) to fit your codebase
Example Use Cases
- Initialize linting for a new Go service with a starter .golangci.yml and CI checks
- Integrate golangci-lint into a CI pipeline to run on every pull request
- Use goimports to automatically format imports and enforce local prefixes
- Apply revive rules to enforce consistent style across the repository
- Run errcheck and staticcheck locally to catch error handling and bug-prone patterns