Go Engineering - Lint
golangci-lint
使用最多
优点
| Advantage | Desc |
|---|---|
| 速度快 | 基于 gometalinter 开发,但比 gometalinter 快 5 倍 原因: 并行检查 + 复用 go build 缓存 + 缓存分析结果 |
| 可配置 | 支持 YAML 格式的配置文件 |
| IDE 集成 | Goland / VS Code |
| linter 聚合器 | 不需要单独安装 |
| 最少误报数 | 调整了所集成 linter 的默认配置,大幅度减少误报 |
| 良好的输出 | 颜色、行号等 |
| 迭代快 | 不断有新的 linter 被集成到 golangci-lint 中 |
选项
配置
同时出现 - 命令行选项 / 配置文件
- bool/string/int - 命令行选项
- slice -
合并
命令行选项
golangci-lint run -h
| Flag | Desc |
|---|---|
| –print-issued-lines | Print lines of code with issue (default true) |
| –print-linter-name | Print linter name in issue line (default true) |
| –timeout duration | Timeout for total work (default 1m0s) |
| –tests | Analyze tests (*_test.go) (default true) |
| -c, –config PATH | Read config from file path PATH |
| –no-config | Don’t read config |
| –skip-dirs strings | Regexps of directories to skip |
| –skip-dirs-use-default | Use or not use default excluded directories: - (^|/)vendor($|/) - (^|/)third_party($|/) - (^|/)testdata($|/) - (^|/)examples($|/) - (^|/)Godeps($|/) - (^|/)builtin($|/) (default true) |
| –skip-files strings | Regexps of files to skip |
| -E, –enable strings | Enable specific linter |
| -D, –disable strings | Disable specific linter |
| –disable-all | Disable all linters |
| –fast | Run only fast linters from enabled linters set (first run won’t be fast) |
| -e, –exclude strings | Exclude issue by regexp |
| –exclude-use-default | Use or not use default excludes: (default true) |
| –exclude-case-sensitive | If set to true exclude and exclude rules regular expressions are case sensitive |
| –max-issues-per-linter int | Maximum issues count per one linter. Set to 0 to disable (default 50) |
| –fix | Fix found issues (if it’s supported by the linter) |
配置文件
.golangci.yaml / .golangci.toml / .golangci.json
1 | run: |
按需启用 linters
1 | linters: |
Linter
| Properties | Desc |
|---|---|
| fast | 可以缓存类型信息,支持快速检查 |
| auto-fix | 支持自动修复发现的问题 |
实践
使用
对当前目录及子目录下的
所有 Go 文件进行静态代码检查
1 | $ golangci-lint run |
对指定的 Go 文件或者指定目录下的 Go 文件进行静态代码检查
不会检查 dir1 下子目录的 Go 文件,需要追加/...
1 | $ golangci-lint run dir1 dir2/... dir3/file1.go |
根据指定配置文件,进行静态代码检查
1 | $ golangci-lint run -c .golangci.yaml ./... |
运行指定的 linter
golangci-lint 可以在不指定任何配置文件的情况下运行,会运行默认启用的 linter
golangci-lint 会从当前目录逐层往上寻找 .golangci.yaml / .golangci.toml / .golangci.json,直到根目录
1 | $ golangci-lint run --no-config --disable-all -E errcheck ./... |
禁止运行指定的 linter
1 | $ golangci-lint run --no-config -D godot,errcheck |
误报
- 在命令行中添加
-e参数,或者在配置文件的issues.exclude部分设置要排除的检查错误- 使用
issues.exclude-rules来配置哪些文件忽略哪些 linter
- 使用
- 通过
run.skip-dirs、run.skip-files和issues.exclude-rules忽略指定目录下的所有(或指定) Go 文件 - 在 Go 源码文件中添加
//nolint注释,来忽略指定的代码行- 在
//nolint后添加原因// xxxx - 使用
//nolint,而非// nolint
- 在
忽略某一行
所有 linter的检查
1 | var bad_name int //nolint |
忽略某一行指定 linter 的检查
1 | var bad_name int //nolint:golint,unused |
忽略某个代码块的检查
1 | //nolint |
忽略某个文件的指定 linter 检查 - 在 package 上添加
//nolint注释
1 | //nolint:unparam |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.














