我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Go代码检查的推荐工具及使用详解

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Go代码检查的推荐工具及使用详解

1. Golint

(1)安装golint

git clone https://github.com/golang/lint.git 
cd lint/golint
go install

(2)使用方式

# 检查单个文件
golint service.go
# 检查当前目录所有.go文件,不会递归查找
golint

(3)golint校验规则

  • 不能使用下划线命名法,使用驼峰命名法
  • 外部可见程序结构体、变量、函数都需要注释
  • 通用名词要求大写 iD/Id -> ID Http -> HTTP Json -> JSON Url -> URL Ip -> IP Sql -> SQL
  • 包命名统一小写不使用驼峰和下划线
  • 注释第一个单词要求是注释程序主体的名称,注释可选不是必须的
  • 外部可见程序实体不建议再加包名前缀
  • if语句包含return时,后续代码不能包含在else里面
  • errors.New(fmt.Sprintf(…)) 建议写成 fmt.Errorf(…)
  • receiver名称不能为this或self
  • receiver名称不能为this或self
  • 错误变量命名需以 Err/err 开头
  • a+=1应该改成a++,a-=1应该改成a--

(4)检查的结果示例如下:

middlewares.go:29:6: exported type xxxInstrumentingMiddleware should have comment or be unexported
middlewares.go:41:1: error should be the last type when returning multiple items
middlewares.go:41:1: exported method xxxInstrumentingMiddleware.Creativexxx should have comment or be unexported
middlewares.go:88:1: error should be the last type when returning multiple items
middlewares.go:88:1: exported method xxxInstrumentingMiddleware.CreativexxxRTB should have comment or be unexported
middlewares.go:135:1: error should be the last type when returning multiple items
middlewares.go:135:1: exported method xxxInstrumentingMiddleware.UpdateConf should have comment or be unexported
middlewares.go:141:1: error should be the last type when returning multiple items
middlewares.go:141:1: exported method xxxInstrumentingMiddleware.GetConf should have comment or be unexported
middlewares.go:147:1: exported function NewWrappedxxxInstrumentingMiddleware should have comment or be unexported
middlewares.go:218:6: exported type xxxInstrumentingMiddleware should have comment or be unexported
middlewares.go:225:1: exported method xxxInstrumentingMiddleware.PredictGRPC should have comment or be unexported
middlewares.go:250:1: exported function NewxxxInstrumentingMiddleware should have comment or be unexported
middlewares.go:281:6: exported type xxxxxxInstrumentingMiddleware should have comment or be unexported
middlewares.go:288:1: exported method xxxxxxInstrumentingMiddleware.PredictGRPC should have comment or be unexported
middlewares.go:288:116: method parameter appId should be appID
middlewares.go:306:1: exported function NewxxxxxxInstrumentingMiddleware should have comment or be unexported
xxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."
xxx_callee.go:1:1: package comment should not have leading space
xxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."
xxx_callee.go:1:1: package comment should not have leading space
xxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."
xxx_callee.go:1:1: package comment should not have leading space
xxx_callee.go:38:96: method parameter appId should be appID
service.go:124:5: exported const xxxLevelLowerLimit should have comment (or a comment on this block) or be unexported
service.go:134:6: exported type xxxTdbankConfig should have comment or be unexported
service.go:138:6: exported type xxxConfig should have comment or be unexported
service.go:152:6: exported type xxxResponse should have comment or be unexported
service.go:162:6: exported type xxxMetaDataCache should have comment or be unexported
service.go:515:1: error should be the last type when returning multiple items
service.go:592:9: range var appId should be appID
service.go:604:16: range var appIdStr should be appIDStr
service.go:609:17: var appId should be appID
service.go:688:13: range var appId should be appID
service.go:801:13: range var appId should be appID
service.go:806:13: var behaviorGameIdList should be behaviorGameIDList

IDE集成工具参考:https://www.jb51.net/article/229273.htm

2. Golangci-lint

(1)安装golangci-lint

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

(2)使用方式

# 检查单个文件
golangci-lint run service.go
# 指定目录
golangci-lint run internal/
# 检查当前目录所有.go文件,会递归查找当前目录及子目录
golangci-lint run

(3)golangci-lint校验规则

golangci-lint运行时会在当前目录查找配置文件

  • golangci.yml
  • .golangci.yaml
  • .golangci.toml
  • .golangci.json

具体配置参考:

https://golangci-lint.run/usage/configuration/

(4)检查的结果示例如下,-v参数可以看到更详细的检查结果

# golangci-lint run -v service/service.go
INFO [config_reader] Config search paths: [./ /data/xxx/code/xxx/internal/xxx/service /data/xxx/code/xxx/internal/xxx /data/xxx/code/xxx/internal /data/xxx/code/xxx /data/xxx/code /data/xxx /data / /root]
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (compiled_files|exports_file|name|types_sizes|deps|files|imports) took 448.248885ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 7.130535ms
INFO [linters context/goanalysis] analyzers took 4.744573ms with top 10 stages: buildir: 629.354µs, fact_deprecated: 514.772µs, inspect: 435.291µs, ctrlflow: 362.378µs, typedness: 353.563µs, printf: 345.2µs, SA5012: 345.028µs, fact_purity: 333.959µs, nilness: 333.457µs, isgenerated: 28.152µs
INFO [runner/max_same_issues] 1/4 issues with text "undeclared name: `ModelInstrumentingMiddleware`" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 1/4 issues with text "undeclared name: `NewModelInstrumentingMiddleware`" were hidden, use --max-same-issues
INFO [runner] Issues before processing: 2608, after processing: 9
INFO [runner] Processors filtering stat (out/in): skip_files: 2608/2608, max_from_linter: 9/9, path_shortener: 9/9, path_prefixer: 9/9, skip_dirs: 2608/2608, identifier_marker: 2608/2608, exclude: 2608/2608, nolint: 2608/2608, diff: 11/11, max_per_file_from_linter: 11/11, severity-rules: 9/9, sort_results: 9/9, filename_unadjuster: 2608/2608, path_prettifier: 2608/2608, autogenerated_exclude: 2608/2608, exclude-rules: 2608/2608, source_code: 9/9, cgo: 2608/2608, uniq_by_line: 11/2608, max_same_issues: 9/11
INFO [runner] processing took 103.631607ms with stages: exclude-rules: 61.093674ms, identifier_marker: 32.814984ms, nolint: 7.481643ms, path_prettifier: 1.299346ms, skip_dirs: 217.899µs, uniq_by_line: 187.231µs, source_code: 145.003µs, filename_unadjuster: 132.84µs, cgo: 117.621µs, autogenerated_exclude: 93.646µs, max_same_issues: 37.831µs, path_shortener: 2.715µs, max_from_linter: 2.375µs, max_per_file_from_linter: 1.845µs, exclude: 621ns, severity-rules: 511ns, sort_results: 490ns, skip_files: 471ns, diff: 470ns, path_prefixer: 391ns
INFO [runner] linters took 434.383892ms with stages: goanalysis_metalinter: 330.666778ms
service/service.go:72:29: undeclared name: `ModelInstrumentingMiddleware` (typecheck)
    xxxService            *ModelInstrumentingMiddleware
                            ^
service/service.go:73:29: undeclared name: `ModelInstrumentingMiddleware` (typecheck)
    xxxCalibrationService *ModelInstrumentingMiddleware
                            ^
service/service.go:74:29: undeclared name: `ModelInstrumentingMiddleware` (typecheck)
    xxxCalibrationService *ModelInstrumentingMiddleware
                            ^
service/service.go:76:29: undeclared name: `xxxModelInstrumentingMiddleware` (typecheck)
    xxxService             *xxxModelInstrumentingMiddleware
                            ^
service/service.go:412:33: undeclared name: `NewModelInstrumentingMiddleware` (typecheck)
        xxxService:            NewModelInstrumentingMiddleware(NewxxxService(logger), "xxx_MODEL"),
                                ^
service/service.go:413:33: undeclared name: `NewModelInstrumentingMiddleware` (typecheck)
        xxxCalibrationService: NewModelInstrumentingMiddleware(NewxxxCalibrationService(logger), "xxx_CALIBRATION_MODEL"),
                                ^
service/service.go:414:33: undeclared name: `NewModelInstrumentingMiddleware` (typecheck)
        xxxService:            NewModelInstrumentingMiddleware(NewxxxService(logger), "xxx_MODEL"),
                                ^
service/service.go:416:33: undeclared name: `NewxxxModelInstrumentingMiddleware` (typecheck)
        xxxService:             NewxxxModelInstrumentingMiddleware(NewxxxService(logger), "xxx_MODEL"),
                                ^
service/service.go:448:17: undeclared name: `NewWrappedxxxInstrumentingMiddleware` (typecheck)
    wapedSvc := NewWrappedxxxInstrumentingMiddleware(svc)
                ^
INFO File cache stats: 1 entries of total size 183.6KiB
INFO Memory: 10 samples, avg is 70.1MB, max is 128.1MB
INFO Execution took 893.716ms

3. Go-reporter

github地址:https://github.com/qax-os/goreporter

1)安装codestyle

# golang版本需要 >=1.6
yum -y install graphviz
go get -u github.com/360EntSecGroup-Skylar/goreporter

(2)使用方式

# 展示指令可用参数
goreporter -p [projectRelativePath] -r [reportPath] -e [exceptPackagesName] -f [json/html/text]  {-t templatePathIfHtml}
-version Version of GoReporter.
-p Must be a valid Golang project path.
-r Save the path to the report.
-e Exceptional packages (multiple separated by commas, for example: "linters/aligncheck,linters/cyclo" ).
-f report format json, html OR text.
-t Template path,if not specified, the default template will be used.
By default, the default template is used to generate reports in html format.

以上就是Go代码检查的推荐工具及使用详解的详细内容,更多关于Go代码检查工具的资料请关注编程网其它相关文章!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Go代码检查的推荐工具及使用详解

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

IDEA中的代码检查工具怎么使用

使用 IDEA 中的代码检查工具可以帮助开发者找出代码中的潜在问题和优化代码质量。以下是在 IDEA 中使用代码检查工具的步骤:打开 IDEA,并打开你要检查的项目。在 IDEA 的菜单栏中选择 “Code” -> “Inspect Co
IDEA中的代码检查工具怎么使用
2024-04-03

Java代码检查工具之PMD的使用方法

小编给大家分享一下Java代码检查工具之PMD的使用方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!介绍PMD是一个静态源代码分析器。它发现了常见的编程缺陷,如
2023-06-14

go性能分析工具pprof的用途及使用详解

刚开始接触go就遇到了一个内存问题,在进行内存分析的时候发现了一下比较好的工具,在此留下记录,下面这篇文章主要给大家介绍了关于go性能分析工具pprof的用途及使用的相关资料,需要的朋友可以参考下
2023-01-06

将自然语言查询转换为SQL代码的AI工具使用详解

这篇文章主要为大家介绍了将自然语言查询转换为SQL代码的AI工具使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
2023-03-20

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录