golangci-lint简要手册

简介

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

分为以下六大部分,按需配置,并不是都需要

  1. run 运行配置,比如超时,忽略文件等
  2. output 输出配置,比如格式
  3. linters-settings 检测器配置,对具体的检测器细化配置
  4. linters 开启关闭检测器
  5. issues 检测器输出报告配置,比如忽略某些检测器的输出
  6. 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 的代码异味

posted @ 2022-03-17 14:29  半山th  阅读(1376)  评论(0编辑  收藏  举报