Go 安裝配置 golint

【導讀】本文介紹了 golint 代碼檢查工具。

一. 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,下載地址:https://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 檢驗規則

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
  1. don't use an underscore in package name
    don't use MixedCaps in package name; xxXxx should be xxxxx
    包命名統一小寫不使用駝峯和下劃線

  2. comment on exported type Repo should be of the form "Repo ..." (with optional leading article)
    註釋第一個單詞要求是註釋程序主體的名稱,註釋可選不是必須的

  3. type name will be used as user.UserModel by other packages, and that stutters; consider calling this Model
    外部可見程序實體不建議再加包名前綴

  4. if block ends with a return statement, so drop this else and outdent its block
    if 語句包含 return 時,後續代碼不能包含在 else 裏面

  5. should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)
    errors.New(fmt.Sprintf(…)) 建議寫成 fmt.Errorf(…)

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

  7. error var SampleError should have name of the form ErrSample
    錯誤變量命名需以 Err/err 開頭

  8. should replace num += 1 with num++
    should replace num -= 1 with num--
    a+=1 應該改成 a++,a-=1 應該改成 a–

轉自:

zhoubotong.site/post/3.html

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/oAI-oJHtmqUQlUIGQCC01w