Table of Contents
How to write a linter
Use go/analysis
and take a look at this tutorial:
it shows how to write go/analysis
linter from scratch and integrate it into golangci-lint
.
How to add a public linter to golangci-lint
You need to implement a new linter using go/analysis
API.
We don't accept non go/analysis
linters.
After that:
- Implement functional tests for the linter:
- Add one file into directory
pkg/golinters/{yourlintername}/testdata/
. - Run the test to ensure that test fails.
- Run:go run ./cmd/golangci-lint/ run --no-config --disable-all --enable={yourlintername} ./pkg/golinters/{yourlintername}/testdata/{yourlintername}.go
- Add one file into directory
- Add a new file
pkg/golinters/{yourlintername}/{yourlintername}.go
. Look at other linters in this directory. Implement linter integration and check that test passes. - Add the new struct for the linter (which you've implemented in
pkg/golinters/{yourlintername}/{yourlintername}.go
) to the list of all supported linters inpkg/lint/lintersdb/builder_linter.go
to the methodLinterBuilder.Build
.- Add
WithSince("next_version")
, wherenext_version
must be replaced by the next minor version. (ex: v1.2.0 if the current version is v1.1.0)
- Add
- Find out what options do you need to configure for the linter.
For example,
nakedret
has only 1 option:max-func-lines
. Choose default values to not being annoying for users of golangci-lint. Add configuration options to:- .golangci.reference.yml - the example of a configuration file. You can also add them to .golangci.yml if you think that this project needs not default values.
- config struct -
don't forget about
mapstructure
tag for proper configuration files parsing by pflag.
- Take a look at the example of pull requests with new linter support.
How to add a private linter to golangci-lint
Some people and organizations may choose to have custom-made linters run as a part of golangci-lint
.
Typically, these linters can't be open-sourced or too specific.
Such linters can be added through 2 plugin systems:
Edit this page on GitHub