一蜒秤、Go的相關(guān)插件工具簡介
- golint 代碼的風(fēng)格檢測
- go fmt 重新格式化Go源代碼
- goimports 引用包的優(yōu)化
- gometalinter 同時(shí)運(yùn)行Go lint工具并規(guī)范化其輸出
- go vet 代碼的語法檢查
- GoReporter 用于執(zhí)行靜態(tài)分析秉扑,單元測試甥雕,代碼審查和生成代碼質(zhì)量報(bào)告农尖。
二朱躺、插件工具使用
1鲤桥、golint
- 安裝golint
#下載源碼
> go get github.com/golang/lint/
#編譯包并生產(chǎn)可執(zhí)行文件到bin目錄下($GOPATH/bin)
> go install github.com/golang/lint/golint
- 打開goland進(jìn)行設(shè)置卑吭,點(diǎn)擊
Settings
打開設(shè)置 - 選擇
External Tools
并新建+
選擇External Tools并新建+ - 參數(shù)說明:
Name : golint #Tool名稱
Description: golint #Tool描述
Program : $GOPATH\bin\golint.exe #選擇golint可執(zhí)行文件的位置
Atguments: $FilePath$ #魔法變量(執(zhí)行當(dāng)前go文件的路徑)
Working directory: $ProjectFileDir$ #魔法變量(當(dāng)前項(xiàng)目的路徑)
-
設(shè)置快捷鍵Keymap -> External Tools -> External Tools -> golint -> Add Keyboard Shortcut(右鍵)
新增快捷鍵 -
連續(xù)按著鍵盤進(jìn)行設(shè)置
設(shè)置快捷鍵 -
選擇項(xiàng)目中的一個(gè)go文件進(jìn)行【快捷鍵測試】
測試結(jié)果 -
git commit 提交代碼測試
提交代碼測試
注意:這是commit
提交之后才進(jìn)行代碼檢測
2、gofmt+goimports+gometalinter
- 打開goland進(jìn)行設(shè)置丙躏,點(diǎn)擊
Settings
打開設(shè)置 - goland 設(shè)置插件择示,
File Watchers
->+
-> 分別點(diǎn)擊安裝go fmt
、goimports
晒旅、gometalinter
image.png - 修改代碼后按住
Ctrl+S
就可以看到效果了
gofmt 栅盲、 goimports直接就可以看見代碼修改的效果
gometalinter的效果
gometalinter
3、go tool vet
- 使用go tool vet針對單個(gè)文件進(jìn)行代碼檢查
> go tool vet global/init.go
測試結(jié)果
go vet
- 使用go tool vet針對整個(gè)項(xiàng)目進(jìn)行代碼檢查
> go tool vet ../zlsc_image
測試結(jié)果
>go tool vet ../zlsc_image
..\zlsc_image\global\init.go:144: struct field c_ip has json tag but is not exported
..\zlsc_image\global\init.go:145: struct field college_id has json tag but is not exported
..\zlsc_image\global\init.go:146: struct field status has json tag but is not exported
..\zlsc_image\global\init.go:147: struct field camera_status has json tag but is not exported
..\zlsc_image\global\init.go:148: struct field sign_status has json tag but is not exported
..\zlsc_image\utils\file.go:160: Println call has possible formatting directive %s
..\zlsc_image\utils\file.go:172: Println call has possible formatting directive %s
..\zlsc_image\utils\file.go:184: Println call has possible formatting directive %s
..\zlsc_image\utils\image.go:89: unreachable code
..\zlsc_image\utils\image.go:79: image/jpeg.Options composite literal uses unkeyed fields
..\zlsc_image\utils\pic.go:388: unreachable code
..\zlsc_image\utils\uid.go:57: result of fmt.Sprintf call not used
..\zlsc_image\utils\uid.go:61: result of fmt.Sprintf call not used
..\zlsc_image\utils\uid.go:70: result of fmt.Sprintf call not used
..\zlsc_image\utils\uid.go:98: result of fmt.Sprintf call not used
..\zlsc_image\utils\convert\map.go:182: unreachable code
..\zlsc_image\utils\convert\map.go:196: unreachable code
..\zlsc_image\utils\convert\map.go:210: unreachable code
..\zlsc_image\utils\convert\map.go:224: unreachable code
4废恋、GoReporter
執(zhí)行格式:
> $ goreporter -p [projectRelativePath] -r [reportPath] -e [exceptPackagesName] -f [json/html] {-t templatePathIfHtml}
-p 有效的相對路徑
-r 報(bào)告保存的地址
-e 例外的包谈秫,多個(gè)包使用逗號分隔。例如: "linters/aligncheck,linters/cyclo" ).
-f 生成報(bào)告的格式
-t 模版路徑鱼鼓,不設(shè)置會使用默認(rèn)模版
默認(rèn)會生出HTML格式的報(bào)告
運(yùn)行測試:
> goreporter -p ../falcon-plus -r ../falcon-plus -t ./templates/template.html
生成結(jié)果文件:
生成結(jié)果文件
報(bào)告預(yù)覽:
首頁
單元測試
代碼風(fēng)格
代碼優(yōu)化
代碼統(tǒng)計(jì)
可維護(hù)性