前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vscode-go 远程开发添加golangci-lint支持

vscode-go 远程开发添加golangci-lint支持

作者头像
超级大猪
发布2024-05-21 15:45:46
1310
发布2024-05-21 15:45:46
举报
文章被收录于专栏:大猪的笔记大猪的笔记

tag: vscode;remote ssh;golangci-lint

vscode对远程开发的支持可谓一骑绝尘。关于golangci-lint的支持方法,网上已经很多。但没有找到远程开发的配置,故摸索了一番。

环境:本地vscode+Remote ssh插件,远程 centos

远程安装golangci-lint

代码语言:javascript
复制
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2

golangci-lint

再安装一些依赖:

代码语言:javascript
复制
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,这里我没有打算为每个项目新建一个配置文件。而是准备共用一个配置。

代码语言:javascript
复制
touch ~/.golangci.yml
vim ~/.golangci.yml

然后,把下面的内容粘贴进去:

代码语言:javascript
复制
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
    # 禁止将for-range value的指针暴露给外部
    - exportloopref
    # 禁止使用特定的标识符
    - forbidigo
    # 禁止出现长函数
    - funlen
    # 控制golang的包引用的顺序
    - gci
    # 禁止使用全局变量  --需要使用  //nolint:gochecknoglobals // 说明原因
    - gochecknoglobals
    # 禁止使用init函数
    - gochecknoinits
    # 如果有相同的string变量请使用consts替换
    - goconst
    # 检查if语句是否有简单的语法
    - ifshort
    # 禁止出现长语句
    - lll
    # 如果知道slice大小,定义时需分配空间
    - prealloc
    # 检查prometheus meteics的指标名是否规范
    - promlinter
    # 强制一致性的impotr别名
    - importas
    # 类型断言时需检查是否成功
    - forcetypeassert
    # 检查err的定义规范--types类型的定义是以Error结尾的,常量的定义是Err打头的
    - errname
    # 禁止errors使用'=='和'!='等表达式--与nil和io.EOF比较除外
    - goerr113
    # 官方代码格式化
    - gofmt
    - gofumpt
    - goimports
    # 禁止使用魔法数字
    - gomnd
    # 检查依赖的黑白名单
    - 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的环境配置中:

<img src="https://share.superpig.win/img_share/20240520/image-20240520142552570_1716186353006.webp" alt="image-20240520142552570" style="zoom:50%;" />

打开配置json:

<img src="https://share.superpig.win/img_share/20240520/image-20240520142632698_1716186392836.webp" alt="image-20240520142632698" style="zoom:33%;" />

添加下面的配置:

代码语言:javascript
复制
{
    &quot;go.lintTool&quot;: &quot;golangci-lint&quot;,
    &quot;go.lintFlags&quot;: [
        &quot;--fast&quot;
    ]
}

一般来说,--fast已经可以满足要求。如果想要在配置中指定.golangci.yml的位置,可以使用-c的配置来直接指定。

代码语言:javascript
复制
    &quot;go.lintFlags&quot;: [
        &quot;-c&quot;,
        &quot;/root/.golangci.yml&quot;
    ]

手工lint项目

使用快捷键ctrl + shift + P(mac 使用 cmd+shift+P)打开命令窗口。

<img src="https://share.superpig.win/img_share/20240520/image-20240520142935926_1716186576054.webp" alt="image-20240520142935926" style="zoom:33%;" />

搜索并运行Lint Workspace。稍等片刻,就能发现warnings中有了很多新增的提示。

比如下面的提示是没有对返回的error进行检查,来自于golanglint-ci

<img src="https://share.superpig.win/img_share/20240520/image-20240520143120611_1716186680741.webp" alt="image-20240520143120611" style="zoom:33%;" />

到此,整个工具就配置完毕了。通过这个CI工具,可以学习到很多的编程知识和细节,大家玩得愉快。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 远程安装golangci-lint
  • 远程新建配置文件
  • vscode配置
  • 手工lint项目
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档