golangci-lint 使用

安装

这里我使用V1.53.2版本,是比较新的,因为我要使用泛型

go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.2

查看是否安装成功

golangci-lint version

image.png

缓存

众所周知,在go test测试中也有缓存,golangci-lint 也有缓存

查看缓存状态
 golangci-lint cache status

image.png

清除缓存
golangci-lint cache status

image.png

指定配置文件

运行时指定

golangci-lint run -config path

image.png
使用默认配置,会在运行运行golangci-lint 的目录开始向上查找(.goalngci.yaml | .golangci-lint.json | .golangci.xml)

常用配置项目
skip-dirs 忽略检查目录

.goalngci-lint.yam 如下

run:
    skip-dirs:
      - internal # 忽略internal目录

image.png
image.png

skip-files 忽略检查文件
run:
  skip-files:
    - serv.go   # 忽略后半部分为 serv.go 的文件 (如: aserv.go, bserve.go)
	  - internal/cmd/cmd_serv.go # 指定具体忽略的文件
linters-settings errcheck 配置
linters-settings:
  errcheck:
    # 从接口类型尝试转回某个具体的struct时,使用第二个参数进行断言
    # 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: true

    # 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

check-type-assertions: true, 从接口类型尝试转回某个具体的struct时,需要使用第二个参数进行断言
check-blank: false, 允许使用 _ 接收error,从而不判断error

使用

基本用法

golangci-lint run 对当前目录下所有文件进行检测
image.png

指定文件或目录

golangci-lint run <dirname | filename> 对指定目录或文件进行检查
image.png

指定配置

golangci-lint run -c <config-path> 使用指定的配置文件进行检查
image.png

指定或关闭检查项

golangci-lint -E <linter> 让linter有效
image.png
golangci-lint -D <linter> 让linter无效
image.png

在代码中指定跳过检查
跳过一个文件中的检查

在 package 上增加 //nolint
增加前:

package main

import (
	"fmt"
	"os"
)

func testOne(a string) {
	_, err := os.Open("abc.txt")
}

func main() {
	fmt.Println("ok")
}

image.png
增加后:

// nolint
package main

import (
	"fmt"
	"os"
)

var b int

func testOne(a string) {
	_, err := os.Open("abc.txt")
}

func main() {
	fmt.Println("ok")
}

image.png

忽略一行的检查

在一行的后面增加 //nolint
增加前:

package main

import (
	"fmt"
)

var b int

func main() {
	fmt.Println("ok")
}

image.png
增加后:

package main

import (
	"fmt"
)

var b int // nolint

func main() {
	fmt.Println("ok")
}

image.png

忽略一个函数的检查

在函数的上面增加 //nolint
增加前:

package main

import (
	"errors"
)

func check() error {
	return errors.New("err")
}

//nolint
func One() {
	check()
}

func main() {
	One()
}

image.png
增加后:

package main

import (
	"errors"
)

func check() error {
	return errors.New("err")
}

//nolint
func One() {
	check()
}

func main() {
	One()
}

image.png

跳过指定聚合器

//nolint:聚合器1,聚合器2......
跳过 staticcheck 前:

package main

import (
	"errors"
)

func check() error {
	return errors.New("err")
}

//nolint:errcheck
func One() {
	check()
}

func main() {
	One()
}

image.png
跳过后:

package main

import (
	"errors"
)

func check() error {
	return errors.New("err")
}

//nolint:errcheck,staticcheck
func One() {
	check()
}

func main() {
	One()
}

image.png

跳过检查需要给出具体的原因
package main

import (
	"errors"
)

func check() error {
	return errors.New("err")
}

//nolint:errcheck,staticcheck
//因为xxx原因,所以此处跳过检查
func One() {
	check()
}

func main() {
	One()
}

### 如何在 VSCode 中配置使用 golangci-lint 进行 Go 代码静态分析 #### 安装 golangci-lint 工具 为了能够在 VSCode 中利用 `golangci-lint` 对 Go 代码执行静态分析,首先需要安装此工具。通过运行如下命令完成安装: ```bash go get -u github.com/golangci/golangci-lint/cmd/golangci-lint[^1] ``` #### 设置 VSCode 扩展 对于更便捷地集成 `golangci-lint` 到开发环境中,在 Visual Studio Code 上建议安装官方支持的语言服务器插件——`golangci-lint-langserver`。 访问并克隆该项目仓库可获取最新版本的扩展包: - 项目地址: [https://gitcode.com/gh_mirrors/go/golangci-lint-langserver](https://gitcode.com/gh_mirrors/go/golangci-lint-langserver)[^2] 按照文档说明来构建和部署该语言服务端到本地环境当中去。 #### 配置 VSCode 用户设置 为了让编辑器识别新安装好的 linter 并自动触发检查流程,需调整 VSCode 的用户偏好设定文件(`settings.json`)。添加下面几项配置条目以便启用 `golangci-lint` 功能: ```json { "editor.codeActionsOnSave": { "source.organizeImports": true, "source.fixAll.golangci_lint_ls": true }, "gopls.analyses.unusedparams": true, "[go]": { "editor.formatOnSave": false, // 如果已启用了其他格式化程序,则应禁用默认格式化处理以免冲突 "editor.codeActionsOnSave": [ "source.organizeImports", "source.fixAll" ] } } ``` 以上 JSON 片段中的 `"source.fixAll.golangci_lint_ls"` 参数确保每次保存文件时都会调用 `golangci-lint` 来扫描潜在问题;而 `[go]` 下面的部分则是针对特定编程语言(即Go)所做的额外定制化选项定义。 #### 创建 `.golangci.yml` 文件 最后一步是在项目的根目录下创建名为`.golangci.yml` 的 YAML 格式的配置文件用于指定哪些 linting 规则应该被激活以及它们各自的参数值。这里给出一个简单的例子作为起点: ```yaml linters: enable-all: true issues-exclude-use-default: true run: deadline: '5m' ``` 上述配置会开启所有的内置 Linters,并给予五分钟左右的时间限制让其充分工作完毕后再返回结果给开发者查看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值