1. Blog
  2. Golang
  3. kubernetes
  4. Rust
  5. 关于作者

GolangCi-Lint

golangci-lint 是一个 golangci 开源的静态检查工具,是一个 linter 的聚合器。 它平均比 gometalinter 快 5 倍。 有良好的格式化的输出和能够简单快速的集成使用。并且支持 go module。

官方文档在这里

额外的: golangci 是一个盈利性公司,golangci-lint 只是其下的开源组件,因其集成了许多 linter,由于各种 license 原因,不得不开源这一部分。 golangci 还提供云服务,可以对开源项目免费进行 lint 检查并生成可视化的报告。参见golangci 官网

安装

参考官方文档 install

配置

GolangCi-Lint 能够通过命令行参数完成所有配置,也能够通过编写一个配置文件完成配置。 我们推荐使用配置文件配置的方式进行配置。

GolangCI-Lint 从当前工作目录的以下路径中查找配置文件:

以下是默认的配置文件:

# 该文件包含所有可用的配置选项及其默认值。

# 分析运行的选项
run:
  # 默认并发是可用的CPU数量,非必要无需设置
  concurrency: 4

  # 分析超时,例如30s,5m,默认为1m
  timeout: 1m

  # 发现至少一个问题时退出代码,默认为1
  issues-exit-code: 1

  # 是否包含测试文件,默认为true
  tests: true

  # 构建标记列表,所有短linter都使用它。默认为空列表。(目前未使用到)
  build-tags:
    - mytag

  # 跳过哪些目录,可以使用regexp
  skip-dirs:
    - src/external_libs
    - autogenerated_by_my_lib

  # 默认开启. 启用后以下目录被跳过:vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
  skip-dirs-use-default: true

  # 跳过哪些文件:将对它们进行分析,但不会报告来自它们的问题。
  # 默认值为空列表,但无需包含所有自动生成的文件,我们可以放心地识别自动生成的文件。如果不是,请告诉我们。
  skip-files:
    - ".*\\.my\\.go$"
    - lib/bad.go

  # 默认情况下未设置。如果设置,我们将其传递给“ go list -mod = {option}”。
  # 从“ go帮助模块”中:如果使用-mod = readonly调用,则上述隐式自动更新go.mod不允许使用go命令。而是在需要对go.mod进行任何更改时失败。此设置对于检查go.mod不需要更新(例如在持续集成和测试系统中)是最有用的。如果使用-mod = vendor调用,则go命令将假定vendor目录包含正确的依赖关系副本,并忽略go.mod中的依赖关系描述。
  modules-download-mode: readonly|release|vendor

# 输出配置选项
output:
  # colored-line-number|line-number|json|tab|checkstyle|code-climate, 默认是 "colored-line-number"
  format: colored-line-number

  # 打印有问题的代码行,默认为true
  print-issued-lines: true

  # 在问题文本的末尾打印linter名称,默认为true
  print-linter-name: true

  # 使问题按行输出唯一,默认为true
  uniq-by-line: true

# 特定linter的所有可用设置,你可以在这里设置特定linter的参数。
# 因其中的内容过多,且不是重要部分,所以放置到文末进行说明。
linters-settings: {}

# 这里设置选择需要启用的 linter 和需要禁用的 linter 。
linters:
  enable:
    - megacheck
    - govet
  disable:
    - maligned
    - prealloc
  disable-all: false
  presets:
    - bugs
    - unused
  fast: false

issues:
  # 要排除的问题文本的正则表达式列表,默认情况下为空列表。
  # 但是独立于此选项,我们使用默认的排除模式,可以通过`exclude-use-default:false`禁用它。要列出默认模式中排除的所有内容,请执行`golangci-lint run --help`。
  exclude:
    - abcdef

  # 按路径,按线性,按文本和按源码进行排除的配置
  exclude-rules:
    # 从测试文件上排除一些linter。
    - 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 "

  # Independently from option `exclude` we use default exclude patterns,
  # it can be disabled by this option. To list all
  # excluded by default patterns execute `golangci-lint run --help`.
  # Default value for this option is true.
  exclude-use-default: false

  # Maximum issues count per one linter. Set to 0 to disable. Default is 50.
  max-issues-per-linter: 0

  # Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
  max-same-issues: 0

  # Show only new issues: if there are unstaged changes or untracked files,
  # only those changes are analyzed, else only changes in HEAD~ are analyzed.
  # It's a super-useful option for integration of golangci-lint into existing
  # large codebase. It's not practical to fix all existing issues at the moment
  # of integration: much better don't allow issues in new code.
  # Default is false.
  new: false

  # Show only new issues created after git revision `REV`
  new-from-rev: REV

  # Show only new issues created in git patch with set file path.
  new-from-patch: path/to/patch/file

下面是上文中 .linters 节的完整支持配置:

linters:
  # 使用快速模式,官方对于该选项的解释为: 因为第一次运行会缓存类型信息。所有后续运行将很快。通常,此选项在本地计算机上的开发过程中使用,并且已经执行了编译。
  # 也就是 --fast 会第一次生成缓存,后续执行会很快,推荐在本地开发环境启用,但是在自动化构建时设置为 false
  fast: false
  # 里面是所有支持的 linter , 各个linter的作用可以在 `golangci-lint linters` 输出中找到,下文也有附加。
  enable:
    - bodyclose
    - depguard
    - dogsled
    - dupl
    - funlen
    - gochecknoglobals
    - gochecknoinits
    - gocognit
    - goconst
    - gocritic
    - gocyclo
    - godox
    - gofmt
    - goimports
    - golint
    - gomnd
    - gosec
    - interfacer
    - lll
    - maligned
    - misspell
    - nakedret
    - prealloc
    - scopelint
    - stylecheck
    - unconvert
    - unparam
    - whitespace
    - wsl
  # 为了简化配置,官方设置了"预设"linter组,每个组由一系列的linter组成。可以直接启用某个预设组。默认只启用了 "bugs" "unused" 两个组。
  presets:
    # 以下是每个组包含的 linter ,官方文档没有该项说明,该列表可以使用 `golangci-lint linters`查看获得.
    #bugs: bodyclose, errcheck, gosec, govet, scopelint, staticcheck, typecheck
    #complexity: gocognit, gocyclo, nakedret
    #format: gofmt, goimports
    #performance: bodyclose, maligned, prealloc
    #style: depguard, dogsled, dupl, funlen, gochecknoglobals, gochecknoinits, goconst, gocritic, godox, golint, gomnd, gosimple, interfacer, lll, misspell, stylecheck, unconvert, whitespace, wsl
    #unused: deadcode, ineffassign, structcheck, unparam, unused, varcheck
    - bugs
    - complexity
    - format
    - performance
    - style
    - unused

附带 golangci-lint linters 的输出以及各个 linter 的作用:

% golangci-lint version
golangci-lint has version v1.23.2 built from (unknown, mod sum: "h1:WeUpTxmRFK8+MRCJj2Vh0zSdIZvIcrFufQjflW0mL8Q=") on (unknown)
% golangci-lint linters
Enabled by your configuration linters:
bodyclose: 检查HTTP响应主体是否成功关闭[fast: true, auto-fix: false]
deadcode: 查找未使用的代码 [fast: true, auto-fix: false]
depguard: Go linter检查软件包导入是否在可接受的软件包列表中(这个选项可以防止随意引用第三方包,可接受的软件包列表可以在linters-setting配置) [fast: true, auto-fix: false]
dogsled: 检查分配的标识符过多(防止编写返回值数量过多的代码) (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
dupl: 代码重复检测工具 [fast: true, auto-fix: false]
errcheck: Errcheck是用于检查go程序中未经检查的错误的程序。在某些情况下,这些未经检查的错误可能是关键错误。(强制对每一个错误进行处理) [fast: true, auto-fix: false]
funlen: 长函数检测工具(防止编写行数过多的函数) [fast: true, auto-fix: false]
gochecknoglobals: 检查Go代码中是否没有全局变量 [fast: true, auto-fix: false]
gochecknoinits: 检查Go代码中是否没有init函数(某些情况下(例如gomodule的同包不同版本)使用init会出现难以发现的bug,所有非必要不推荐使用init函数。) [fast: true, auto-fix: false]
gocognit: 计算并检查功能的认知复杂性(代码可读性) [fast: true, auto-fix: false]
goconst: 查找可以被常量替换的重复字符串(这些重复的字符串就应该被抽离为常量)[fast: true, auto-fix: false]
gocritic: 最自以为是的Go源代码linter [fast: true, auto-fix: false]
gocyclo: 计算并检查功能的圈复杂度 [fast: true, auto-fix: false]
godox: 用于检测FIXME,TODO和其他注释关键字的工具 [fast: true, auto-fix: false]
gofmt: Gofmt检查代码是否经过gofmt格式化。默认情况下,此工具与-s选项一起运行以检查代码是否简化。 [fast: true, auto-fix: true]
goimports: Goimports可以执行gofmt所做的一切。此外,它还会检查未使用的导入(该选项可以和 gofmt 二选一,也可以同时启用,不会冲突) [fast: true, auto-fix: true]
golint: Golint与gofmt不同。 Gofmt重新格式化Go源代码,而golint打印出代码风格错误(类似go要求使用后置的"{"一样,不允许五花八门的代码风格) [fast: true, auto-fix: false]
gomnd: 用于检测魔法值。 [fast: true, auto-fix: false]
goprintffuncname: 检查类似printf的函数是否以`f`结尾 (也是代码可读性一部分)[fast: true, auto-fix: false]
gosec (gas): 检查源代码是否存在安全问题 [fast: true, auto-fix: false]
gosimple (megacheck):Linter for Go源代码,专门用于简化代码 [fast: true, auto-fix: false]
govet (vet, vetshadow): 兽医检查Go源代码并报告可疑结构,例如Printf调用,其参数与格式字符串不一致,被内部语句覆盖的值等 [fast: true, auto-fix: false]
ineffassign: 检测对现有变量已分配但未使用(unused) [fast: true, auto-fix: false]
interfacer: Linter建议使用较窄的接口类型 [fast: true, auto-fix: false]
lll: 报告长行(单行语句过长,可读性要求) [fast: true, auto-fix: false]
maligned: 检测 go struct 的工具,如果对它们字段进行排序,它们会占用更少的内存 [fast: true, auto-fix: false]
misspell: 查找注释中通常拼写错误的英语单词 [fast: true, auto-fix: true]
nakedret: 在大于指定函数长度的函数中查找裸返回[fast: true, auto-fix: false]
prealloc: 查找可能预先分配的切片声明 [fast: true, auto-fix: false]
rowserrcheck: 检查是否成功检查了行的错误 [fast: true, auto-fix: false]
scopelint: Scopelint检查go程序中未固定的变量[fast: true, auto-fix: false]
staticcheck (megacheck): Staticcheck是类似 go vet,可以进行大量的静态分析检查 [fast: true, auto-fix: false]
structcheck: 查找未使用的结构字段 [fast: true, auto-fix: false]
stylecheck:Stylecheck可以代替 go lint [fast: true, auto-fix: false]
typecheck: 像Go编译器的前处理一样,对Go代码进行解析和类型检查 [fast: true, auto-fix: false]
unconvert: 删除不必要的类型转换 [fast: true, auto-fix: false]
unparam: 报告未使用的功能参数 [fast: true, auto-fix: false]
unused (megacheck): 检查Go代码中未使用的常量,变量,函数和类型 [fast: false, auto-fix: false]
varcheck: 查找未使用的全局变量和常量 [fast: true, auto-fix: false]
whitespace: 用于检测前导和后置空格的工具 [fast: true, auto-fix: true]
wsl: 空白Linter-强制您使用空行!(它会推荐你在函数内部使用大量空格,会提高可读性,会统一风格。但是在实际使用中,很多情况会因为这个检查不通过而无法通过检查,不是很推荐使用) [fast: true, auto-fix: false]

附加一个我正在使用的配置:

linters:
  disable:
    - wsl
  presets:
    - bugs
    - complexity
    - format
    - performance
    - style
    - unused

运行检查

gilangci-lint 可以很方便的集成到各个自动化工具中,执行检查仅需要运行命令(因为所有配置都可以使用文件,并且纳入 git 进行管理,随着项目走)

golangci-lint run

或者使用 docker

docker run --rm \
   -v ${PWD}:/app \
   -w /app \
   golangci/golangci-lint:v1.21.0 \
   golangci-lint run

开发时检查

详细参考官方文档: editor-integration

针对于 Goland:

linter 配置

以下是配置文件中 .linters-settings 部分中的内容,必要时可以参考来进行配置,因一般不需要额外配置。

linters-settings:
  dogsled:
    # checks assignments with too many blank identifiers; default is 2
    max-blank-identifiers: 2
  dupl:
    # tokens count to trigger issue, 150 by default
    threshold: 100
  errcheck:
    # report about not checking of errors in type assertions: `a := b.(MyStruct)`;
    # default is false: such cases aren't reported by default.
    check-type-assertions: false

    # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
    # default is false: such cases aren't reported by default.
    check-blank: false

    # [deprecated] comma-separated list of pairs of the form pkg:regex
    # the regex is used to ignore names within pkg. (default "fmt:.*").
    # see https://github.com/kisielk/errcheck#the-deprecated-method for details
    ignore: fmt:.*,io/ioutil:^Read.*

    # path to a file containing a list of functions to exclude from checking
    # see https://github.com/kisielk/errcheck#excluding-functions for details
    exclude: /path/to/file.txt
  funlen:
    lines: 60
    statements: 40
  gocognit:
    # minimal code complexity to report, 30 by default (but we recommend 10-20)
    min-complexity: 10
  goconst:
    # minimal length of string constant, 3 by default
    min-len: 3
    # minimal occurrences count to trigger, 3 by default
    min-occurrences: 3
  gocritic:
    # Which checks should be enabled; can't be combined with 'disabled-checks';
    # See https://go-critic.github.io/overview#checks-overview
    # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`
    # By default list of stable checks is used.
    enabled-checks:
      - rangeValCopy

    # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
    disabled-checks:
      - regexpMust

    # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
    # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
    enabled-tags:
      - performance

    settings: # settings passed to gocritic
      captLocal: # must be valid enabled check name
        paramsOnly: true
      rangeValCopy:
        sizeThreshold: 32
  gocyclo:
    # minimal code complexity to report, 30 by default (but we recommend 10-20)
    min-complexity: 10
  godox:
    # report any comments starting with keywords, this is useful for TODO or FIXME comments that
    # might be left in the code accidentally and should be resolved before merging
    keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting
      - NOTE
      - OPTIMIZE # marks code that should be optimized before merging
      - HACK # marks hack-arounds that should be removed before merging
  gofmt:
    # simplify code: gofmt with `-s` option, true by default
    simplify: true
  goimports:
    # put imports beginning with prefix after 3rd-party packages;
    # it's a comma-separated list of prefixes
    local-prefixes: github.com/org/project
  golint:
    # minimal confidence for issues, default is 0.8
    min-confidence: 0.8
  gomnd:
    settings:
      mnd:
        # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
        checks: argument,case,condition,operation,return,assign
  govet:
    # report about shadowed variables
    check-shadowing: true

    # settings per analyzer
    settings:
      printf: # analyzer name, run `go tool vet help` to see all analyzers
        funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf

    # enable or disable analyzers by name
    enable:
      - atomicalign
    enable-all: false
    disable:
      - shadow
    disable-all: false
  depguard:
    list-type: blacklist
    include-go-root: false
    packages:
      - github.com/sirupsen/logrus
    packages-with-error-message:
      # specify an error message to output when a blacklisted package is used
      - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
  lll:
    # max line length, lines longer will be reported. Default is 120.
    # '\t' is counted as 1 character by default, and can be changed with the tab-width option
    line-length: 120
    # tab width in spaces. Default to 1.
    tab-width: 1
  maligned:
    # print struct with more effective memory layout or not, false by default
    suggest-new: true
  misspell:
    # Correct spellings using locale preferences for US or UK.
    # Default is to use a neutral variety of English.
    # Setting locale to US will correct the British spelling of 'colour' to 'color'.
    locale: US
    ignore-words:
      - someword
  nakedret:
    # make an issue if func has more lines of code than this setting and it has naked returns; default is 30
    max-func-lines: 30
  prealloc:
    # XXX: we don't recommend using this linter before doing performance profiling.
    # For most programs usage of prealloc will be a premature optimization.

    # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
    # True by default.
    simple: true
    range-loops: true # Report preallocation suggestions on range loops, true by default
    for-loops: false # Report preallocation suggestions on for loops, false by default
  rowserrcheck:
    packages:
      - github.com/jmoiron/sqlx
  unparam:
    # Inspect exported functions, default is false. Set to true if no external program/library imports your code.
    # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find external interfaces. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false
  unused:
    # treat code as a program (not a library) and report unused exported identifiers; default is false.
    # XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find funcs usages. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false
  whitespace:
    multi-if: false # Enforces newlines (or comments) after every multi-line if statement
    multi-func: false # Enforces newlines (or comments) after every multi-line function signature
  wsl:
    # If true append is only allowed to be cuddled if appending value is
    # matching variables, fields or types on line above. Default is true.
    strict-append: true
    # Allow calls and assignments to be cuddled as long as the lines have any
    # matching variables, fields or types. Default is true.
    allow-assign-and-call: true
    # Allow multiline assignments to be cuddled. Default is true.
    allow-multiline-assign: true
    # Allow declarations (var) to be cuddled.
    allow-cuddle-declarations: false
    # Allow trailing comments in ending of blocks
    allow-trailing-comment: false
    # Force newlines in end of case at this limit (0 = never).
    force-case-trailing-whitespace: 0

  # The custom section can be used to define linter plugins to be loaded at runtime. See README doc
  #  for more info.
  custom:
    # Each custom linter should have a unique name.
    example:
      # The path to the plugin *.so. Can be absolute or local. Required for each custom linter
      path: /path/to/example.so
      # The description of the linter. Optional, just for documentation purposes.
      description: This is an example usage of a plugin linter.
      # Intended to point to the repo location of the linter. Optional, just for documentation purposes.
      original-url: github.com/golangci/example-linter