Go代码检查的推荐工具及使用详解
作者:Luoyger 发布时间:2024-05-09 15:00:11
1. Golint
(1)安装golint
git clone https://github.com/golang/lint.git cd lint/golintgo 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 unexportedmiddlewares.go:41:1: error should be the last type when returning multiple itemsmiddlewares.go:41:1: exported method xxxInstrumentingMiddleware.Creativexxx should have comment or be unexportedmiddlewares.go:88:1: error should be the last type when returning multiple itemsmiddlewares.go:88:1: exported method xxxInstrumentingMiddleware.CreativexxxRTB should have comment or be unexportedmiddlewares.go:135:1: error should be the last type when returning multiple itemsmiddlewares.go:135:1: exported method xxxInstrumentingMiddleware.UpdateConf should have comment or be unexportedmiddlewares.go:141:1: error should be the last type when returning multiple itemsmiddlewares.go:141:1: exported method xxxInstrumentingMiddleware.GetConf should have comment or be unexportedmiddlewares.go:147:1: exported function NewWrappedxxxInstrumentingMiddleware should have comment or be unexportedmiddlewares.go:218:6: exported type xxxInstrumentingMiddleware should have comment or be unexportedmiddlewares.go:225:1: exported method xxxInstrumentingMiddleware.PredictGRPC should have comment or be unexportedmiddlewares.go:250:1: exported function NewxxxInstrumentingMiddleware should have comment or be unexportedmiddlewares.go:281:6: exported type xxxxxxInstrumentingMiddleware should have comment or be unexportedmiddlewares.go:288:1: exported method xxxxxxInstrumentingMiddleware.PredictGRPC should have comment or be unexportedmiddlewares.go:288:116: method parameter appId should be appIDmiddlewares.go:306:1: exported function NewxxxxxxInstrumentingMiddleware should have comment or be unexportedxxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."xxx_callee.go:1:1: package comment should not have leading spacexxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."xxx_callee.go:1:1: package comment should not have leading spacexxx_callee.go:1:1: package comment should be of the form "Package xxxservice ..."xxx_callee.go:1:1: package comment should not have leading spacexxx_callee.go:38:96: method parameter appId should be appIDservice.go:124:5: exported const xxxLevelLowerLimit should have comment (or a comment on this block) or be unexportedservice.go:134:6: exported type xxxTdbankConfig should have comment or be unexportedservice.go:138:6: exported type xxxConfig should have comment or be unexportedservice.go:152:6: exported type xxxResponse should have comment or be unexportedservice.go:162:6: exported type xxxMetaDataCache should have comment or be unexportedservice.go:515:1: error should be the last type when returning multiple itemsservice.go:592:9: range var appId should be appIDservice.go:604:16: range var appIdStr should be appIDStrservice.go:609:17: var appId should be appIDservice.go:688:13: range var appId should be appIDservice.go:801:13: range var appId should be appIDservice.go:806:13: var behaviorGameIdList should be behaviorGameIDList
IDE集成工具参考:https://www.aspxhome.com/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.1golangci-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.goINFO [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.248885msINFO [runner/filename_unadjuster] Pre-built 0 adjustments in 7.130535msINFO [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µsINFO [runner/max_same_issues] 1/4 issues with text "undeclared name: `ModelInstrumentingMiddleware`" were hidden, use --max-same-issuesINFO [runner/max_same_issues] 1/4 issues with text "undeclared name: `NewModelInstrumentingMiddleware`" were hidden, use --max-same-issuesINFO [runner] Issues before processing: 2608, after processing: 9INFO [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/11INFO [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: 391nsINFO [runner] linters took 434.383892ms with stages: goanalysis_metalinter: 330.666778msservice/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.6KiBINFO Memory: 10 samples, avg is 70.1MB, max is 128.1MBINFO Execution took 893.716ms
3. Go-reporter
github地址:https://github.com/qax-os/goreporter
1)安装codestyle
# golang版本需要 >=1.6yum -y install graphvizgo 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.
来源:https://cloud.tencent.com/developer/article/2056397


猜你喜欢
- 前言上一篇介绍了服务端流式RPC,客户端发送请求到服务器,拿到一个流去读取返回的消息序列。 客户端读取返回的流的数据。本篇将介绍客户端流式R
- 前言因近期进行时间序列分析时遇到了数据预处理中的缺失值处理问题,其中日期缺失和填充在网上没有找到较好较全资料,耗费了我一晚上工作时间,所以下
- 在网络设备管理中,传输配置文件、镜像文件等是经常需要进行的操作。Netmiko是一个Python库,可用于与各种网络设备进行交互,提供了一些
- 目录1. 将索引从 groupby 操作转换为列2. 使用现有的 DataFrame 设置索引3. 一些操作后重置索引4.排序后重置索引5.
- 本文主要介绍了用OpenCV进行年龄和性别检测的实现示例,分享给大家,具体如下:在本文中,我将带您完成用 Python 进行机器学习的年龄和
- 本文详细分析了smarty缓存的用法。分享给大家供大家参考。具体分析如下:一开始以为smarty只是用来做一些掩饰php代码功能,但是后来才
- 本文实例讲述了python单向链表的基本实现与使用方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8 -*-#! p
- 目录事件类型事件处理方法处理键盘事件处理鼠标事件事件(Event)是 Pygame 的重要模块之一,它是构建整个游戏程序的核心,比如鼠标点击
- 配置环境:redhat6.5server1:redis(172.25.254.1)server2:php(172.25.254.2)serv
- 从大的发展来看,网站就是一块试验田,一块在错误中成长、在错误中变强变大的试验田。这决定了互联网产品的成长路线,一定是一个反复修正和迭代的曲线
- 先新建一组散点充当坐标轴为了比较直观地展示旋转过程,这里通过散点来新建三个坐标轴,通过对这三个坐标轴的转动,来直观地展现转动矩阵对坐标变换的
- 近几日遇到采集某网页的时候大部分网页OK,少部分网页出现乱码的问题,调试了几日,终于发现了是含有一些非法字符造成的..特此记录1. 在正常情
- 目录1.任务要求2.简单设计3.模块实现4.总结由于一些小原因,被迫开始了tkinter一次实战演练。在此做一些记录,总结以及给自己留一些轮
- python开启debug模式的代码如下所示:import requests session = requests.session()imp
- 前面简单介绍了Python基本运算,这里再来简单讲述一下Python字符串相关操作1. 字符串表示方法>>> "
- 一切皆是对象在 Python 一切皆是对象,包括所有类型的常量与变量,整型,布尔型,甚至函数。 参见stackoverflow上的一个问题
- random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串。•random.seed(x)改变随机数生成器的种子seed。一般
- 概述今天我们要来做一个进阶的花分类问题. 不同于之前做过的鸢尾花, 这次我们会分析 102 中不同的花. 是不是很上头呀.预处理导包常规操作
- tornado 默认有一个模板引擎但是功能简单(其实我能用到的都差不多)使用起来颇为麻烦, 而jinja2语法与django模板相似所以决定
- 1 端口映射举个例子来说明一下端口映射的作用。有A、B、C三台计算机,A、B互通,B、C互通,但是A、C不通,这个时候在C上开了一个Web服