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

目录
  • 1. Golint
  • 2. Golangci-lint
  • 3. Go-reporter

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代码检查工具的资料请关注我们其它相关文章!

(0)

相关推荐

  • 一文秒懂Go 编写命令行工具的代码

    前言 最近因为项目需要写了一段时间的 Go ,相对于 Java 来说语法简单同时又有着一些 Python 之类的语法糖,让人大呼"真香". 但现阶段相对来说还是 Python 写的多一些,偶尔还得回炉写点 Java :自然对 Go 也谈不上多熟悉. 于是便利用周末时间自己做个小项目来加深一些使用经验.于是我便想到了之前利用 Java 写的一个博客小工具. 那段时间正值微博图床大量图片禁止外链,导致许多个人博客中的图片都不能查看.这个工具可以将文章中的图片备份到本地,还能将图片直接替换到

  • Go 代码生成工具详解

    分享两个常用的代码生成工具: gormgen handlergen gormgen 基于 MySQL 数据表结构进行生成 3 个文件: 生成表的 struct 结构体 生成表的 Markdown 文档 生成表的 CURD 方法 场景 在进行业务需求开发时,创建完数据表后,执行代码生成工具,常用的 CURD 操作全部生成完毕,使用的时候只需要 . 后面的方法即可,这样大大提高了业务开发效率. 示例 表结构: CREATE TABLE `user_demo` (   `id` int(11) uns

  • JavaScript代码压缩工具UglifyJS和Google Closure Compiler的基本用法

    一.UglifyJS UglifyJS是用JavaScript编写的JavaScript压缩工具. 官网:http://lisperator.net/uglifyjs/ 1.通过NPM安装UglifyJS (1)安装Node.js 从Node.js官网https://nodejs.org/en/下载对应平台的安装程序,当前最新版本11.4.0,推荐版本10.14.2. 本人下载的是10.14.2,下载下来是一个node-v10.14.2-x64.msi安装包,按照默认下一步安装. 安装成功后在c

  • Go语言驱动低代码应用引擎工具Yao开发管理系统

    目录 前言 Yao简介 安裝 使用 基本使用 创建数据模型 编写接口 编写界面 总结 前言 之前写过一篇关于阿里的低代码工具LowCodeEngine的文章,发现大家还是挺感兴趣的.最近又发现了一款很有意思的低代码工具Yao,支持使用JSON的形式开发管理系统,不仅可以用来开发后端API,还能用来开发前端界面,简洁而且高效,推荐给大家! SpringBoot实战电商项目mall(50k+star)地址:https://github.com/macrozheng/mall Yao简介 Yao是一款

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

    目录 1. Golint 2. Golangci-lint 3. Go-reporter 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校验规则 不能使用下划线命名法,使用驼峰命名法 外部可见程序结构体.变量.函

  • Go源码字符串规范检查lint工具strchecker使用详解

    目录 1.背景 2.strchecker介绍 3.结论 1.背景 在大型项目开发过程中,经常会遇到打印大量日志,输出信息和在源码中写注释的情况.对于软件开发来说,我们一般都是打印输出英文的日志(主要考虑软件在各种环境下的兼容性,如果打印中文日志可能会出现乱码,另外英文日志更容易搜索,更容易后续做国际化),但是对于我们中国人来说,很容易就把中文全角的中文标点符号一不注意就写到日志中了.不过源码中的注释因为是完全面向开发者的,不会面向客户,所以如果研发团队全是中国人,那么代码注释用中文就更有效率.

  • python2利用wxpython生成投影界面工具的图文详解

    本投影界面工具的功能: 准备好.prj投影文件,将输入文件夹内的WGS84经纬度坐标shp文件,投影为平面文件,成果自动命名为prj_***并新建在输入文件夹同一路径下. 下一步目标: 利用pyinstaller或其他打包库生成exe文件,目前停滞在python2语法.arcpy打包出错相关问题上. 参考文献: <Using Py2exe with Arcpy- It can be done easily!> <如何使用py2exe打包arcpy脚本?> GUI界面示意图 投影文件

  • Golang 官方依赖注入工具wire示例详解

    目录 依赖注入是什么 开源选型 wire providers injectors 类型区分 总结 依赖注入是什么 Dependency Injection is the idea that your components (usually structs in go) should receive their dependencies when being created. 在 Golang 中,构造一个结构体常见的有两种方式: 在结构体初始化过程中,构建它的依赖: 将依赖作为构造器入参,传入进

  • Vue中util的工具函数实例详解

    Vue中util的工具函数,下面通过实例代码给大家介绍的非常详细,具体代码如下所示: // 防抖函数 function debounce (fn, wait) { let t return () => { let context = this let args = arguments if (t) clearTimeout(t) t = setTimeout(() => { fn.apply(context, args) }, wait) } } function flatten (arr)

  • java property配置文件管理工具框架过程详解

    这篇文章主要介绍了java property配置文件管理工具框架过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 property property 是 java 实现的 property 框架. 特点 优雅地进行属性文件的读取和更新 写入属性文件后属性不乱序 灵活定义编码信息 使用 OO 的方式操作 property 文件 支持多级对象引用 快速开始 环境依赖 Maven 3.x Jdk 1.7+ Maven 引入依赖 <depende

  • Android Studio 3.6中新的视图绑定工具ViewBinding 用法详解

    前言 我们在Android开发的过程中总是需要获取XML布局中的ViewId,以便给其赋值进行显示,早期我们只能使用 findViewById 这个API,会导致很多的模版代码出现.2013年左右Android界大神 Jake Wharton开源了Butter Knife框架,通过Bind("viewid")方式方便开发者获取ViewId.近两年由于谷歌对Kotlin的支持,我们开始使用 Android Kotlin extensions. 在文件中导入布局文件直接引用viewId.无

  • python数据分析工具之 matplotlib详解

    不论是数据挖掘还是数学建模,都免不了数据可视化的问题.对于 Python 来说,matplotlib 是最著名的绘图库,它主要用于二维绘图,当然也可以进行简单的三维绘图.它不但提供了一整套和 Matlab 相似但更为丰富的命令,让我们可以非常快捷地用 python 可视化数据. matplotlib基础 # 安装 pip install matplotlib 两种绘图风格: MATLAB风格: 基本函数是 plot,分别取 x,y 的值,然后取到坐标(x,y)后,对不同的连续点进行连线. 面向对

  • 如何使用PyCharm将代码上传到GitHub上(图文详解)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶说明 测试条件:需要有GitHub账号以及在本地安装了Git工具,无论是Linux环境还是Windows都是一样的 如果还没有GitHub账号的同学 请查看该篇博客 https://www.jb51.net/article/135606.htm Windows Git安装:https://www.jb51.net/softjc/711624.html Linux Git安装:https://www.jb51.net/art

  • 使用Mybatis的PageHelper分页工具的教程详解

    1.导入相关的jar包 在pom.xm中加入 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</ver

随机推荐