美文网首页
Golint使用笔记

Golint使用笔记

作者: voidFan | 来源:发表于2021-07-26 18:55 被阅读0次

一. Golint介绍

Golint is a linter for Go source code.
Golint 是一个源码检测工具用于检测代码规范
Golint 不同于gofmt, Gofmt用于代码格式化
Golint会对代码做以下几个方面检查

package注释 必须按照 “Package xxx 开头”
package命名 不能有大写字母、下划线等特殊字符
struct、interface等注释 必须按照指定格式开头
struct、interface等命名
变量注释、命名
函数注释、命名
各种语法规范校验等

二. Golint安装

go get -u github.com/golang/lint/golint
ls $GOPATH/bin (可以发现已经有golint可执行文件)

三. Golint使用

golint检测代码有2种方式

golint file
golint directory
golint校验常见的问题如下所示

don't use ALL_CAPS in Go names; use CamelCase
不能使用下划线命名法,使用驼峰命名法
exported function Xxx should have comment or be unexported
外部可见程序结构体、变量、函数都需要注释
var statJsonByte should be statJSONByte
var taskId should be taskID
通用名词要求大写
iD/Id -> ID
Http -> HTTP
Json -> JSON
Url -> URL
Ip -> IP
Sql -> SQL
don't use an underscore in package name
don't use MixedCaps in package name; xxXxx should be xxxxx
包命名统一小写不使用驼峰和下划线
comment on exported type Repo should be of the form "Repo ..." (with optional leading article)
注释第一个单词要求是注释程序主体的名称,注释可选不是必须的
type name will be used as user.UserModel by other packages, and that stutters; consider calling this Model
外部可见程序实体不建议再加包名前缀
if block ends with a return statement, so drop this else and outdent its block
if语句包含return时,后续代码不能包含在else里面
should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)
errors.New(fmt.Sprintf(…)) 建议写成 fmt.Errorf(…)
receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"
receiver名称不能为this或self
error var SampleError should have name of the form ErrSample
错误变量命名需以 Err/err 开头
should replace num += 1 with num++
should replace num -= 1 with num--
a+=1应该改成a++,a-=1应该改成a–

四. Goland配置golint

新增tool: Goland -> Tools -> External Tools 新建一个tool 配置如下
新增快捷键: Goland -> Tools -> Keymap -> External Tools -> External Tools -> golint 右键新增快捷键

使用: 打开Go文件,然后使用快捷键就可以进行代码检测了
有golint的输出日志说明存在代码不规范的地方,需要进行修改

五. gitlab提交限制

为了保证项目代码规范,我们可以在gitlab上做一层约束限制,当代码提交到gitlab的时候先做golint校验,校验不通过则不让提交代码。

我们可以为Go项目创建gitlab CI流程,通过.gitlab-ci.yml配置CI流程会自动使用govet进行代码静态检查、gofmt进行代码格式化检查、golint进行代码规范检查、gotest进行单元测试

例如go-common项目 .gitlab.yml文件如下,相关的脚本可以查看scripts
https://github.com/changwh/go-common

相关文章

  • Golint使用笔记

    一. Golint介绍 Golint is a linter for Go source code.Golint ...

  • GOLINT代码规范检测

    一. Golint介绍 Golint 是一个源码检测工具用于检测代码规范 Golint 不同于gofmt用于代码格...

  • golint代码自测

    golint代码自测 1 安装golint 1.1 golang版本和golangci-lint版本需要对应...

  • 替代golint的工具

    revive

  • Golang goland idea+golint+go fmt

    一、Go的相关插件工具简介 golint 代码的风格检测 go fmt 重新格式化Go源代码 goimports ...

  • IntelliJ IDEA 15配置golint

    (嗨,大家好!欢迎关注我的公众号“茶歇驿站”,微信号“tech_tea”,请大家多多支持,欢迎大家分享,如若转载请...

  • [转]Golint代码规范检测

    [原创地址]https://blog.csdn.net/chenguolinblog/article/detail...

  • Goland安装goimports和golint

    windows: 相关链接:https://github.com/golangci/golangci-lint[h...

  • go check 总结

    一个优秀的工程,必然会对代码进行check,go本身提供了如下check工具,常用的有: vet golint g...

  • 印象笔记

    印象笔记相关学习链接:下载链接王泽熙:印象笔记使用教程精简版如何正确使用印象笔记的标签功能印象笔记印象笔记的初级使用方法

网友评论

      本文标题:Golint使用笔记

      本文链接:https://www.haomeiwen.com/subject/fgmimltx.html