Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go常见问题之代码检查 #211

Closed
WGrape opened this issue Jul 2, 2022 · 5 comments
Closed

Go常见问题之代码检查 #211

WGrape opened this issue Jul 2, 2022 · 5 comments
Labels
Go常见问题系列 分享在工作中常遇到的一些Go语言问题

Comments

@WGrape
Copy link
Owner

WGrape commented Jul 2, 2022

代码检查工具

代码检查工具可以完成代码的静态检查,在go中可以使用golangci-lint来实现。

如何安装

brew install golangci-lint

brew uninstall golangci-lint

golangci-lint run

golangci-lint run -v

或者使用这种方法

curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2

.golangci.yml 配置文件

# Linters configurations: https://golangci-lint.run/usage/configuration/#linters-configuration
run:
  # The default concurrency value is the number of available CPU.
  concurrency: 1

# A list of supported linters: https://golangci-lint.run/usage/linters/
linters:
  disable:
    - dogsled # staticcheck .etc

常见问题

1、protobuf/proto is deprecated

SA1019: "github.com/golang/protobuf/proto" is deprecated: Use the "google.golang.org/protobuf/proto" package instead. (staticcheck)

解决方案 :golang/protobuf#1077

2、context loading failed: no go files to analyze

如果出现这个问题,可以使用go build编译下看看是否能成功(大概率是不能成功的),可能原因是依赖项没有下载,所以执行正常下载编译逻辑即可。

golangci/golangci-lint#825
golangci/golangci-lint#825 (comment)

3、could not load export data: no export data for

这个错误是因为缺少下载的包,即没有下载对应的依赖包。

level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: inspect: failed to load package : could not load export data: no export data for

4、WriteFile not declared by package os (typecheck)

一般出现这个问题的原因是Go版本的问题,尝试更新更高版本。

image

@WGrape WGrape changed the title Go常见问题之golangci-lint Go常见问题之代码检查 Jul 3, 2022
@WGrape WGrape added the Go常见问题系列 分享在工作中常遇到的一些Go语言问题 label Jul 17, 2022
@WGrape WGrape closed this as completed Aug 26, 2022
@Iceqaq
Copy link

Iceqaq commented Dec 8, 2022

你好 我不是一个专业的测试工程师 但是我想用golangci-lint做代码审计检查里面是否存在安全问题 想问下是不是golangci-lint run 执行检查代码,代码里必须要有测试用例 即go test 的相关文件?

@WGrape
Copy link
Owner Author

WGrape commented Dec 8, 2022

你好 我不是一个专业的测试工程师 但是我想用golangci-lint做代码审计检查里面是否存在安全问题 想问下是不是golangci-lint run 执行检查代码,代码里必须要有测试用例 即go test 的相关文件?

@Iceqaq 不需要。因为go testgolangci-lint你可以理解为是两个东西,互不影响。

1、go test :是单元测试,需要程序员自己写单元测试的代码 ,具体可以看这篇 #207

2、golangci-lint :是代码检查工具,准确说是一种代码静态检查工具,什么意思呢,就是说它根本不会运行你的代码,而是完全从词法分析、语法分析等方式,把你的代码静态的分析为一棵语法树,然后分析你的代码中潜在的问题。

@WGrape
Copy link
Owner Author

WGrape commented Dec 8, 2022

你好 我不是一个专业的测试工程师 但是我想用golangci-lint做代码审计检查里面是否存在安全问题 想问下是不是golangci-lint run 执行检查代码,代码里必须要有测试用例 即go test 的相关文件?

@Iceqaq 所以,既然golangci-lint是一个静态检查工具,不会运行你的代码,那么写go test文件还有什么用呢,所以不需要

@Iceqaq
Copy link

Iceqaq commented Dec 9, 2022

那SonarQube是需要的对吗?

@WGrape
Copy link
Owner Author

WGrape commented Dec 9, 2022

那SonarQube是需要的对吗?

@Iceqaq 是的 ,因为它提供了单元测试相关的功能 ,比如单测的代码覆盖率等 。这些都是必须运行代码才能检查出来的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go常见问题系列 分享在工作中常遇到的一些Go语言问题
Projects
None yet
Development

No branches or pull requests

2 participants