vscode-go 远程开发添加golangci-lint支持 tag:vscode;remote ssh;golangci-lint vscode对远程开发的支持可谓一骑绝尘。关于golangci-lint的支持方法,网上已经很多。但没有找到远程开发的配置,故摸索了一番。 环境:本地vscode+Remote ssh插件,远程 centos ## 远程安装golangci-lint ``` curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2 golangci-lint ``` 再安装一些依赖: ``` go install mvdan.cc/gofumpt@latest # gofumpt 避免出现File is not `gofumpt`-ed的错误 ``` ## 远程新建配置文件 更多配置方法可以参考这篇文章:Golang 聊聊最经典的 linter—— golangci-lint 怎么用(https://juejin.cn/post/7130188153792495630) 首先,golangci-lint插件会搜索本项目的`.golangci.yml`,如果没有,就会搜索默认位置的这个配置。 如:`~/.golangci.yml`,这里我没有打算为每个项目新建一个配置文件。而是准备共用一个配置。 ``` touch ~/.golangci.yml vim ~/.golangci.yml ``` 然后,把下面的内容粘贴进去: ``` linters: disable-all: true # 关闭其他linter enable: # 下面是开启的linter列表,之后的英文注释介绍了相应linter的功能 # 进制使用非ASCII字符 - asciicheck # 降低代码复杂度 - cyclop - gocognit - gocyclo # 高可拓展性的go源码linter - gocritic # 禁止保留未使用的代码块 #--------------------------------------- #- deadcode # The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter # 新版本的linter已经不建议使用deadcode了,不使用的代码不会被报错了 #--------------------------------------- - ineffassign # 减少代码拷贝 - dupl # 禁止两个time.Duration类型相乘 - durationcheck # 所有err都要处理 - errcheck # 在Go 1.13之后使用errors.Wrap可能导致的问题 - errorlint # 检查switch的全面性,以免遗漏场景 - exhaustive # 禁止使用特定的标识符 - forbidigo # 禁止出现长函数 - funlen # 控制golang的包引用的顺序 - gci # 禁止使用全局变量 --需要使用 //nolint:gochecknoglobals // 说明原因 - gochecknoglobals # 禁止使用init函数 - gochecknoinits # 如果有相同的string变量请使用consts替换 - goconst # 禁止出现长语句 - lll # 如果知道slice大小,定义时需分配空间 - prealloc # 检查prometheus meteics的指标名是否规范 - promlinter # 强制一致性的impotr别名 - importas # 类型断言时需检查是否成功 - forcetypeassert # 检查err的定义规范--types类型的定义是以Error结尾的,常量的定义是Err打头的 - errname # 禁止errors使用'=='和'!='等表达式--与nil和io.EOF比较除外 - err113 # 官方代码格式化 - gofmt - gofumpt - goimports # 禁止使用魔法数字 - mnd # 检查依赖的黑白名单 - gomodguard # 检查类似printf的函数是否以f结尾 - goprintffuncname # 安全检查 - gosec # 官方错误检查 - govet # 检查拼写错误 - misspell # 如果函数过长,禁用裸返回 - nakedret # 禁止深度嵌套的if语句 - nestif # 如果使用nolint指令需要给出理由-- //nolint:gochecknoglobals // 原因 - nolintlint # 禁止使用Go关键字命名 - predeclared # 去掉没有必要的type转换 - unconvert # 强制使用空行 - wsl # 检查文件头部和尾部的换行 - whitespace # 替换golint的 - revive # 测试代码使用*_test的包目录 - testpackage # 启用并行测试 - paralleltest # 检查帮助函数里面有没有调用t.Helper()函数 - thelper linters-settings: errcheck: check-type-assertions: true # 检查类型断言 check-blank: true # 检查使用 _ 来处理错误 errorlint: errorf: true # 检查fmt.Errorf错误是否用%w exhaustive: check-generated: false # 不检查生成的文件 default-signifies-exhaustive: false # 不检查是否有default funlen: lines: 65 # 一个函数总行数限制 statements: 40 # 检查函数中语句的数量 gci: sections: - standard # 标准库 - default # 默认按字典顺序排序 - prefix(cos/lobbyplatform) # 特殊前缀的包 gomodguard: # 检查依赖的黑白名单 allowed: # List of allowed modules. # Default: [] modules: - gopkg.in/yaml.v2 # List of allowed module domains. # Default: [] domains: - golang.org blocked: # List of blocked modules. # Default: [] modules: # Blocked module. - github.com/uudashr/go-module: # Recommended modules that should be used instead. (Optional) recommendations: - golang.org/x/mod # Reason why the recommended module should be used. (Optional) reason: "`mod` is the official go.mod parser library." # List of blocked module version constraints. # Default: [] versions: # Blocked module with version constraint. - github.com/mitchellh/go-homedir: # Version constraint, see https://github.com/Masterminds/semver#basic-comparisons. version: "< 1.1.0" # Reason why the version constraint exists. (Optional) reason: "testing if blocked version constraint works." # Set to true to raise lint issues for packages that are loaded from a local path via replace directive. # Default: false local_replace_directives: false ``` 这个配置文件可谓火力全开。如果觉得告警太多,按自己的想法精简即可。 ## vscode配置 进入到remote的环境配置中: 打开配置json: 添加下面的配置: ``` { "go.lintTool": "golangci-lint", "go.lintFlags": [ "--fast" ], "go.formatTool": "gofumpt", "gopls": { "formatting.gofumpt": true } } ``` 一般来说,`--fast`已经可以满足要求。如果想要在配置中指定`.golangci.yml`的位置,可以使用`-c`的配置来直接指定。 ```` "go.lintFlags": [ "-c", "/root/.golangci.yml" ] ```` ## 手工lint项目 使用快捷键`ctrl + shift + P`(mac 使用 `cmd+shift+P`)打开命令窗口。 搜索并运行`Lint Workspace`。稍等片刻,就能发现warnings中有了很多新增的提示。  比如下面的提示是没有对返回的error进行检查,来自于golanglint-ci 到此,整个工具就配置完毕了。通过这个CI工具,可以学习到很多的编程知识和细节,大家玩得愉快。 来自 大脸猪 写于 2024-05-20 14:32 -- 更新于2024-08-28 17:46 -- 0 条评论