Go 安装配置golint

原文链接://zhoubotong.site/post/3.html
一. Golint介绍

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

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

  3. Golint会对代码做以下几个方面检查

  4. package注释 必须按照 “Package xxx 开头”

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

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

  7. struct、interface等命名

  8. 变量注释、命名

  9. 函数注释、命名

  10. 各种语法规范校验等

二. Golint安装

    首先在我们下载的位置,通过右键git bash here 打开git控制台

    下载golang 的 lint,下载地址://github.com/golang/lint

mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/lint.git  
git clone https://github.com/golang/tools.git

    到目录$GOPATH/src/golang.org/x/lint/golint中运行

go install

    安装成功后我们会在C:\用户\77293\go\bin 目录下面看到我们的golint.exe执行程序,这个目录是我们安装go包的目录路径。

三、配置golint

     1、打开goland Idea
     2、选择项目栏File 下拉选中 Setting,打开设置控制面板

 

设置参数说明:

Program    $GOPATH\src\bin\golint.exe (直接填写glint.exe所在路径即可)
Arguments $FilePath$
Working directory    $ProjectFileDir$
    3、选中keymap > External Tools > External Tools > golint进行快捷键配置

四、golint使用

     选择我们需要检测的go文件
     按住我们之前设置的快捷键,就可以进行检测了,比如说结果如下:

五. Golint检验规则

  1. golint检测代码有2种方式
    1: golint file

  2. 2: golint directory

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–

Tags: