Go代码检查的推荐工具及使用详解
本篇文章向大家介绍《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代码检查的推荐工具及使用详解》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于golang的知识点!

- 上一篇
- Go Redis客户端使用的两种对比

- 下一篇
- Golang如何编写内存高效及CPU调优的Go结构体
-
- 愉快的发带
- 真优秀,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢作者分享技术文章!
- 2023-01-22 05:21:22
-
- 悲凉的酒窝
- 这篇博文太及时了,太细致了,赞 👍👍,码起来,关注楼主了!希望楼主能多写Golang相关的文章。
- 2023-01-08 04:47:26
-
- Golang · Go教程 | 5小时前 |
- Golang命令模式:闭包与接口应用详解
- 378浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang非阻塞通信:select与epoll详解
- 185浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang字符串操作技巧全解析
- 310浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang网络超时与重试设置详解
- 368浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang接口缓存优化技巧分享
- 476浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang搭建NATS高性能消息队列教程
- 109浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang快速搭建HTTP服务器方法
- 427浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang连接MySQL数据库教程详解
- 334浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golangmap优化:哈希扩容与分片解析
- 143浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang适合做RESTfulAPI,Echo框架路由技巧
- 211浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 509次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 边界AI平台
- 探索AI边界平台,领先的智能AI对话、写作与画图生成工具。高效便捷,满足多样化需求。立即体验!
- 21次使用
-
- 免费AI认证证书
- 科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
- 48次使用
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 170次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 249次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 190次使用
-
- Go语言包管理工具Godep的用法
- 2022-12-31 302浏览
-
- Go语言自定义linter静态检查工具
- 2022-12-27 203浏览
-
- MySQL数据库的多种连接方式以及实用工具
- 2023-02-25 389浏览
-
- 调研Redis高可用两种方案
- 2023-02-16 247浏览
-
- 一文秒懂Go 编写命令行工具的代码
- 2022-12-29 208浏览