簡介
golanci-lint 集成了多種靜態語法插件,有許多 linter,可以同時執行,是golang靜態檢查的集大成者。
可以官網直接下載二進制,或者用go安裝
# Go 1.16+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
# Go version < 1.16
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
注意 需要設置go代理 ,設置環境變量 GOPROXY="https://goproxy.cn"
也使用docker,注意需要設置代理的環境變量,如下
docker run --rm -v $(pwd):/app -w /app -e GOPROXY="https://goproxy.cn" golangci/golangci-lint:latest-alpine golangci-lint run -v
基本用法
golangci-lint 使用配置來運行,可以使用 -c 指定配置文件
如果沒有指定配置文件的位置,會引用下面的配置文件。
● .golangci.yml
● .golangci.yaml
● .golangci.toml
● .golangci.json
運行 golangci-lint run 就會對當前文件夾下的文件進行靜態檢測。
run 其它參數
--fix 自動修復對應的 linter 報告
--disable-all 關閉所有linter
常見配置
golangci-lint配置部分如下:
# Options for analysis running.
run:
# See the dedicated "run" documentation section.
option: value
# output configuration options
output:
# See the dedicated "output" documentation section.
option: value
# All available settings of specific linters.
linters-settings:
# See the dedicated "linters-settings" documentation section.
option: value
linters:
# See the dedicated "linters" documentation section.
option: value
issues:
# See the dedicated "issues" documentation section.
option: value
severity:
# See the dedicated "severity" documentation section.
option: value
分為以下六大部分,按需配置,並不是都需要
- run 運行配置,比如超時,忽略文件等
- output 輸出配置,比如格式
- linters-settings 檢測器配置,對具體的檢測器細化配置
- linters 開啟關閉檢測器
- issues 檢測器輸出報告配置,比如忽略某些檢測器的輸出
- severity 檢測器敏感度配置
run 配置
忽略目錄
run:
skip-dirs:
- src/external_libs
- autogenerated_by_my_lib
忽略文件
run:
skip-files:
- ".*\\.my\\.go$"
- lib/bad.go
output 配置
配置示例如下:
# output configuration options
output:
# Default: colored-line-number
format: json
# Print lines of code with issue.
# Default: true
print-issued-lines: false
# Print linter name in the end of issue text.
# Default: true
print-linter-name: false
# Make issues output unique by line.
# Default: true
uniq-by-line: false
# Add a prefix to the output file references.
# Default is no prefix.
path-prefix: ""
# Sort results by: filepath, line and column.
sort-results: false
linters-settings 配置
可以對每一個 linter 進行微調,具體每個 linter 配置見官網,點擊 linter 右側的齒輪⚙圖標。
比如 stylecheck 的配置:
linters-settings:
stylecheck:
# Select the Go version to target.
# Default: 1.13
go: "1.15"
# https://staticcheck.io/docs/options#checks
checks: ["all", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022"]
# https://staticcheck.io/docs/options#dot_import_whitelist
dot-import-whitelist:
- fmt
# https://staticcheck.io/docs/options#initialisms
initialisms: ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS"]
# https://staticcheck.io/docs/options#http_status_code_whitelist
http-status-code-whitelist: ["200", "400", "404", "500"]
linters 配置
開啟某些 linter 如下:
linters:
disable-all: true #關閉所有
enable: # 開啟下列linter
- megacheck
- govet
當然也可以反過來開啟所有,關閉某些linter,但是開啟所有不建議。各個 linter 得詳細配置和默認開啟得 linter 詳見官網
linters:
enable-all: true
disable:
- maligned
- prealloc
其它設置
preset 縮小按類別執行得 linter,每個 linter 都有預設集,下面得示例將執行帶有“bugs”和“unused”預設的 linter。
fast 啟用快速執行
linters:
presets:
- bugs
- unused
fast: false
issue 配置
忽略 排除包含字符得報告
issues:
exclude:
- abcdef
忽略規則 排除符合規則得報告
issues:
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
# Exclude known linters from partially hard-vendored code,
# which is impossible to exclude via "nolint" comments.
- path: internal/hmac/
text: "weak cryptographic primitive"
linters:
- gosec
# Exclude some staticcheck messages
- linters:
- staticcheck
text: "SA9003:"
# Exclude lll issues for long lines with go:generate
- linters:
- lll
source: "^//go:generate "
另外可以在代碼中使用 //nolint 注釋,讓 linter 忽略
var bad_name int //nolint
指定某個 linter 忽略,詳見官網
var bad_name int //nolint:golint,unused
severity 配置
對報告敏感度進行調節,示例如下:
severity:
# Default value is an empty string.
default-severity: error
case-sensitive: true
# Default: []
rules:
- linters:
- dupl
severity: info
可以把默認級別設置為 info ,這樣 info 級別的報告,對應於 sonarqube 的代碼異味
