1

golint 可以对 go 源码进行静态编码检查,保证团队代码风格统一及编码规范。

golint 安装

传统又稳妥的安装方式

git clone https://github.com/golang/lint.git
cd lint/golint
go install
# 可以看到 golint 可执行文件
ll $GOBIN

可以看到 $GOBIN 下已经有 golint

golint 配置

goland 为例

external tools 配置
image.png

快捷键配置
image.png

golint 使用

  1. console下: golint file
  2. console下: golint directory
  3. pressKey: 选择文件直接按配置的快捷键

golint 规范

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

big_cat
1.7k 声望130 粉丝

规范至上