golint的下载以及配置使用

628 阅读2分钟

1 下载Golint及解压

  1. 下载golang的lint,下载地址github.com/golang/lint
  2. 解压文件到$GOPATH/src/github.com/goland/lint下面
  3. 到目录$GOPATH/src/github.com/goland/lint/golint中运行 go build ./
  4. 在目录中就会生成一个golint的可执行文件

2 GoLand配置Golint

  1. 打开goland的setting->tool->External Tools 如图 image.png

2.点击+符号

image.png

名称属性
program刚刚生成的golint可执行文件
Arguments$FilePath$
working directory$ProjectFileDir$

3 使用golint

找到一个包或者文件右键

image.png

实例:比如方法名首字符大写的话,执行结果如图

image.png

4 golint 常见错误

  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
    通用名词要求大写
    iD/Id -> ID
    Http -> HTTP
    Json -> JSON
    Url -> URL
    Ip -> IP
    Sql -> SQL

  4. don't use an underscore in package name
    don't use MixedCaps in package name; xxXxx should be xxxxx
    包命名统一小写不使用驼峰和下划线

  5. comment on exported type Repo should be of the form "Repo ..." (with optional leading article)
    注释第一个单词要求是注释程序主体的名称,注释可选不是必须的

  6. type name will be used as user.UserModel by other packages, and that stutters; consider calling this Model
    外部可见程序实体不建议再加包名前缀

  7. if block ends with a return statement, so drop this else and outdent its block
    if语句包含return时,后续代码不能包含在else里面

  8. should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)
    errors.New(fmt.Sprintf(…)) 建议写成 fmt.Errorf(…)

  9. receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"
    receiver名称不能为this或self

  10. error var SampleError should have name of the form ErrSample
    错误变量命名需以 Err/err 开头

  11. should replace num += 1 with num++
    should replace num -= 1 with num--
    a+=1应该改成a++,a-=1应该改成a–