【Go系列学习】Golint检测代码规范

        在使用VSCode学习Go语言的过程中,使用了Golint用于检测代码规范,在习惯了C语言的命名方法,在编写代码过程中出现各种小问题,譬如:package包不能使用下划线‘_’、函数需要注释、变量也不能使用下划线‘_’命名、通用名词需要大写等。

1. 基本概念

        1) Golint 是一个源码检测工具用于检测代码规范

        2) Golint 不同于gofmt, Gofmt用于代码格式化

2. 代码检测

        Golint会对代码做如下几个方面的检测:

        1) package注释 必须按照 “Package xxx 开头”

        2) package命名 不能有大写字母、下划线等特殊字符

        3) struct、interface等注释 必须按照指定格式开头

        4) struct、interface等命名

        5) 变量注释、命名

        6) 函数注释、命名

        7) 各种语法规范校验等

扫描二维码关注公众号,回复: 17296338 查看本文章

3. 常见问题

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

猜你喜欢

转载自blog.csdn.net/qq_33206497/article/details/122571860