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 配置
快捷鍵配置
golint 使用
- console下: golint file
- console下: golint directory
- pressKey: 選擇檔案直接按配置的快捷鍵
golint 規範
- don't use ALL_CAPS in Go names; use CamelCase
不能使用下劃線命名法,使用駝峰命名法
- exported function Xxx should have comment or be unexported
外部可見程式結構體、變數、函式都需要註釋
- var statJsonByte should be statJSONByte
var taskId should be taskID
通用名詞要求大寫
iD/Id -> ID
Http -> HTTP
Json -> JSON
Url -> URL
Ip -> IP
Sql -> SQL
- don't use an underscore in package name
don't use MixedCaps in package name; xxXxx should be xxxxx
包命名統一小寫不使用駝峰和下劃線
- comment on exported type Repo should be of the form "Repo ..." (with optional leading article)
註釋第一個單詞要求是註釋程式主體的名稱,註釋可選不是必須的
- type name will be used as user.UserModel by other packages, and that stutters; consider calling this Model
外部可見程式實體不建議再加包名字首
- if block ends with a return statement, so drop this else and outdent its block
if語句包含return時,後續程式碼不能包含在else裡面
- should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...) errors.New(fmt.Sprintf(…)) 建議寫成 fmt.Errorf(…)
- receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"
receiver名稱不能為this或self
- error var SampleError should have name of the form ErrSample
錯誤變數命名需以 Err/err 開頭
- should replace num += 1 with num++
should replace num -= 1 with num--
a+=1應該改成a++,a-=1應該改成a–