Golint的簡易使用方法

根據做者的說法:git

Golint is a linter for Go source code.
Golint differs from gofmt. Gofmt reformats Go source code, whereas
golint prints out style mistakes.

Golint differs from govet. Govet is concerned with correctness, whereas
golint is concerned with coding style. Golint is in use at Google, and it
seeks to match the accepted style of the open source Go project.

一句話就是Golint用於檢查go代碼中不夠規範的地方。github

1、編譯及生成可執行程序

一、下載golang 的 lint,下載地址:https://github.com/golang/lintgolang

二、解壓文件到$GOPATH/src/github.com/golang/lintui

三、到目錄$GOPATH/src/github.com/golang/lint/golint中運行go build ./this

四、在當前目錄有golint的可執行程序code

固然,最簡單的方式是:orm

go get github.com/golang/lint
go install github.com/golang/lint

2、執行方式:

golint 文件名或者目錄

檢查結果以下:get

import-dot.go:6:8: should not use dot imports
else.go:11:9: if block ends with a return statement, so drop this else and outdent its block
sort.go:11:1: exported method T.Len should have comment or be unexported
sort.go:20:1: exported method U.Other should have comment or be unexported

從上面輸出能夠看到,golint對go代碼給出的建議。string

golint 會檢查的內容:it

變量名規範
變量的聲明,像var str string = "test",會有警告,應該var str = "test"
大小寫問題,大寫導出包的要有註釋
x += 1 應該 x++
等等……

使用注意事項

若是使用命令下載安裝不通的狀況下,建議使用第一種方法

在使用golint命令檢查時,須要把golint.exe文件放在你要檢查的名錄下,這樣在使用命令時才能找到該執行程序。

相關文章
相關標籤/搜索