Table of Contents
Private linters can be added throughGo's plugin system.
For a private linter (which acts as a plugin) to work properly,the plugin as well as the golangci-lint binaryneeds to be built for the same environment.
CGO_ENABLED
is another requirement.
This means thatgolangci-lint
needs to be built for whatever machine you intend to run it on(cloning the golangci-lint repository and running aCGO_ENABLED=1 make build
should do the trick for your machine).
Create a Plugin
Your linter must provide one or moregolang.org/x/tools/go/analysis.Analyzer
structs.
Your project should also usego.mod
.
All versions of libraries that overlapgolangci-lint
(including replaced libraries) MUST be set to the same version asgolangci-lint
.You can see the versions by runninggo version -m golangci-lint
.
You'll also need to create a Go file likeplugin/example.go
.
This file MUST be in the packagemain
, and MUST define an exposed function calledNew
with the following signature:
funcNew(conf any)([]*analysis.Analyzer,error){// ...}
Seeplugin/example.go for more info.
To build the plugin, from the root project directory, run:
go build -buildmode=plugin plugin/example.go
This will create a plugin*.so
file that can be copied into your project or another well known location for usage ingolangci-lint
.
Configure a Plugin
If you already have a linter plugin available, you can follow these steps to define its usage in a projects.golangci.yml
file.
An example linter can be found athere.
If you're looking for instructions on how to configure your own custom linter, they can be found further down.
If the project you want to lint does not have one already, copy the.golangci.yml to the root directory.
Adjust the YAML to appropriate
linters.settings.custom
entries as so:.golangci.ymlversion:"2"linters:settings:custom:example:path: /example.sodescription: The description of the linteroriginal-url: github.com/golangci/example-lintersettings:# Settings are optional.one: Footwo:-name: Barthree:name: Bar
That is all the configuration that is required to run a custom linter in your project.
Custom linters are enabled by default, but abide by the same rules as other linters.
If the disable all option is specified either on command line or in.golangci.yml
fileslinters.default: none
, custom linters will be disabled;they can be re-enabled by adding them to thelinters.enable
list,or providing the enabled option on the command line,golangci-lint run -Eexample
.
The configuration inside thelinters.settings
field of linter have some limitations (there are NOT related to the plugin system itself):we use Viper to handle the configuration but Viper put all the keys in lowercase, and.
cannot be used inside a key.