GitHub 仓库:https://github.com/golangci/golangci-lint
本地安装
Windows, MacOS and Linux
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.16.0
MacOS
brew install golangci/tap/golangci-lint brew upgrade golangci/tap/golangci-lint
快速使用
golangci-lint run
等价于
golangci-lint run ./...
可以指定要检查的文件和文件夹
golangci-lint run dir1 dir2/... dir3/file1.go
文件夹默认是不会递归检查的,需要 dir2/... 才会递归检查
vscode配置
-
Go for Visual Studio Code. 建议配置:
"go.lintTool":"golangci-lint", "go.lintFlags": [ "--fast" ]
其他编辑器或者IDE可以GitHub文档查看
配置文件
GolangCI-Lint 完全可以在没有配置文件的情况下工作,可以通过命令行指定各个配置项
GolangCI-Lint 会从当前目录搜索一下命名的配置文件:
.golangci.yml
.golangci.toml
.golangci.json
下面是一个模版配置:
# This file contains all available configuration options # with their default values. # options for analysis running run: # default concurrency is a available CPU number concurrency: 4 # timeout for analysis, e.g. 30s, 5m, default is 1m deadline: 1m # exit code when at least one issue was found, default is 1 issues-exit-code: 1 # include test files or not, default is true tests: true # list of build tags, all linters use it. Default is empty list. build-tags: - mytag # which dirs to skip: they won't be analyzed; # can use regexp here: generated.*, regexp is applied on full path; # default value is empty list, but next dirs are always skipped independently # from this option's value: # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ skip-dirs: - src/external_libs - autogenerated_by_my_lib # which files to skip: they will be analyzed, but issues from them # won't be reported. Default value is empty list, but there is # no need to include all autogenerated files, we confidently recognize # autogenerated files. If it's not please let us know. skip-files: - ".*\\.my\\.go$" - lib/bad.go # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": # If invoked with -mod=readonly, the go command is disallowed from the implicit # automatic updating of go.mod desc