GolangCI-Lint: 比gometalinter快5倍的Golang静态检查工具

GolangCI-Lint是一款比gometalinter快5倍的Go语言静态检查工具,广泛应用于Google、Facebook等公司。它支持本地安装、VSCode配置,并可通过配置文件定制检查规则。用户可以指定检查的文件和目录,且无需配置文件即可工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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配置

  1. 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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值