DEPRECATED: Use https://github.com/golangci/golangci-lint
DEPRECATED: Use https://github.com/golangci/golangci-lint
gometalinter is DEPRECATED and the project will be archived on 2019-04-07. See #590 for discussion.
Switch to golangci-lint.
The number of tools for statically checking Go source for errors and warnings is impressive.
This is a tool that concurrently runs a whole bunch of those linters and normalises their output to a standard format:
<file>:<line>:[<column>]: <message> (<linter>)eg.
stutter.go:9::warning: unused global variable unusedGlobal (varcheck)
stutter.go:12:6:warning: exported type MyStruct should have comment or be unexported (golint)It is intended for use with editor/IDE integration.
To install the latest stable release:
curl -L https://git.io/vp6lP | shAlternatively you can install a specific version from the releases list.
brew tap alecthomas/homebrew-tap
brew install gometalinterDisabled by default (enable with --enable=<linter>):
--line-length=N).Additional linters can be added through the command line with --linter=NAME:COMMAND:PATTERN (see below).
gometalinter now supports a JSON configuration file called .gometalinter.json that can
be placed at the root of your project. The configuration file will be automatically loaded
from the working directory or any parent directory and can be overridden by passing
--config=<file> or ignored with --no-config. The format of this file is determined by
the Config struct in config.go.
The configuration file mostly corresponds to command-line flags, with the following exceptions:
--help displays the list of default linters with the exact names
you must use.Here is an example configuration file:
{
"Enable": ["deadcode", "unconvert"]
}If a .gometalinter.json file is loaded, individual options can still be overridden by
passing command-line flags. All flags are parsed in order, meaning configuration passed
with the --config flag will override any command-line flags passed before and be
overridden by flags passed after.
Format keyThe default Format key places the different fields of an Issue into a template. this
corresponds to the --format option command-line flag.
Default Format:
Format: "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})"{{.Path.Relative}} - equivalent to {{.Path}} which outputs a relative path to the file{{.Path.Abs}} - outputs an absolute path to the fileLinters can be added and customized from the config file using the Linters field.
Linters supports the following fields:
Command - the path to the linter binary and any default argumentsPattern - a regular expression used to parse the linter outputIsFast - if the linter should be run when the --fast flag is usedPartitionStrategy - how paths args should be passed to the linter command:directories - call the linter once with a list of all the directoriesfiles - call the linter once with a list of all the filespackages - call the linter once with a list of all the package pathsfiles-by-package - call the linter once per package with a list of the
files in the package.single-directory - call the linter once per directoryThe config for default linters can be overridden by using the name of the linter.
Additional linters can be configured via the command line using the format
NAME:COMMAND:PATTERN.
Example:
$ gometalinter --linter='vet:go tool vet -printfuncs=Infof,Debugf,Warningf,Errorf:PATH:LINE:MESSAGE' .gometalinter supports suppression of linter messages via comment directives. The form of the directive is:
// nolint[: <linter>[, <linter>, ...]]Suppression works in the following way:
Line-level suppression
A comment directive suppresses any linter messages on that line.
eg. In this example any messages for a := 10 will be suppressed and errcheck
messages for defer r.Close() will also be suppressed.
a := 10 // nolint
a = 2
defer r.Close() // nolint: errcheckStatement-level suppression
A comment directive at the same indentation level as a statement it immediately precedes will also suppress any linter messages in that entire statement.
eg. In this example all messages for SomeFunc() will be suppressed.
// nolint
func SomeFunc() {
}Implementation details: gometalinter now performs parsing of Go source code, to extract linter directives and associate them with line ranges. To avoid unnecessary processing, parsing is on-demand: the first time a linter emits a message for a file, that file is parsed for directives.
Install gometalinter (see above).
Run it:
…Gometalinter also supports the commonly seen <path>/... recursive path
format. Note that this can be very slow, and you may need to increase the linter --deadline to allow linters to complete.
gometalinter sets two bits of the exit status to indicate different issues:
| Bit | Meaning |
|---|---|
| 0 | A linter generated an issue. |
| 1 | An underlying error occurred; eg. a linter failed to execute. In this situation a warning will also be displayed. |
eg. linter only = 1, underlying only = 2, linter + underlying = 3
gometalinter in CI?There are two main problems running in a CI:
gometalinter --install --update to errorgometalinter adds a new linter.I have solved 1 by vendoring the linters.
For 2, the best option is to disable all linters, then explicitly enable the ones you want:
gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow ...gometalinter work with Go 1.5 vendoring?gometalinter has a --vendor flag that just sets GO15VENDOREXPERIMENT=1, however the
underlying tools must support it. Ensure that all of the linters are up to date and built with Go 1.5
(gometalinter --install --force) then run `gometalinter --ven
No open issues yet, or sync has not completed.