(嗨,大家好!歡迎關(guān)注我的公眾號(hào)“茶歇驛站”,微信號(hào)“tech_tea”,請(qǐng)大家多多支持,歡迎大家分享,如若轉(zhuǎn)載請(qǐng)注明出處~~~)
前面有分享 Golang 必備開發(fā)工具,并且重點(diǎn)介紹了vim和IntelliJ IDEA(WebStorm),文中提到了golint(go 代碼檢查工具)。
那我就先來做一個(gè)統(tǒng)計(jì),看看大家平常怎么做好代碼規(guī)范的呢?
-
哪些方法來做代碼規(guī)范呢?
- 從來沒有考慮過 2. CodeReview 3. 代碼檢查工具 4. 其他
在Golang語言中你使用 golint 或其他類似的代碼檢查工具嗎?
接下來,我就把我的一些實(shí)踐分享給大家,我將展開來講一講它的使用,重點(diǎn)是在IntelliJ IDEA上的配置使用。
golint 代碼檢查,官方提供了vim和emacs的配置使用說明,上一篇文章已經(jīng)介紹。
安裝golint
go get -u github.com/golang/lint/golint
實(shí)戰(zhàn)講解
vim golint demo
在配置好GOPATH之后,golint命令就可以在Terminal中運(yùn)行了,例子如下:
$GOPATH/server/demo/demo.go
package main
import "fmt"
var tt string = "fdsalf"
const (
DEMO_TT = 1
)
func main() {
fmt.Println(tt)
fmt.Println(DEMO_TT)
}
$GOPATH/server/demo> golint .
$GOPATH/server/demo> golint demo.go
demo.go:5:8: should omit type string from declaration of var tt; it will be inferred from the right-hand side
demo.go:8:2: don't use ALL_CAPS in Go names; use CamelCase
demo.go:8:2: exported const DEMO_TT should have comment (or a comment on this block) or be unexported
以上是在Terminal中的效果,怎么樣在IntelliJ IDEA中使用golint呢?
IntelliJ IDEA golint demo
IDE工具和Golang開發(fā)環(huán)境的配置,可以參考上一篇文章,這里不再敖述。
golang-idea-plugin 已經(jīng)有issue介紹了golint 在插件上的討論,golang-idea-plugin issue、golang-idea-plugin issue,內(nèi)部還有一個(gè)+1引起作者的不滿。
下面我就把操作配置列一下:
1. 點(diǎn)擊Edit Configuration, 添加一個(gè) `Go Application`,設(shè)置Before launch: External tool
2. 點(diǎn)擊+,選擇`Run External tool`,再點(diǎn)擊+,create tool
3. 設(shè)置好Name: golint, 選擇運(yùn)行的Progam: $GOPATH/bin/golint,設(shè)置Parameters: ., 設(shè)置Working directory: $FileDir$, 勾選上顯示信息在console
然后重新運(yùn)行demo.go,就可以看到Run結(jié)果窗口中,會(huì)多一個(gè)golint, 切換過去同樣可以看到golint的檢查結(jié)果。