網頁

2021/8/17

Go golangci-lint安裝及使用

Go語言利用golangci-lint做程式碼語法檢查。




golangci-lint是一套集成多個Go linters的linting工具。

下面介紹golangci-lint的安裝及基本用法。


安裝 Install


Mac

範例環境:

  • macOS Big Sur
  • Go 1.16

在macOS可使用Homebrew安裝,在終端機輸入brew install golangci-lint開始安裝。

$ brew install golangci-lint
...
==> Installing dependencies for golangci-lint: go
==> Installing golangci-lint dependency: go
==> Pouring go--1.16.6.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/go/1.16.6: 9,960 files, 503.6MB
==> Installing golangci-lint
==> Pouring golangci-lint--1.42.0.big_sur.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/golangci-lint/1.42.0: 9 files, 27.5MB
==> Caveats
==> golangci-lint
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

安裝完後輸入golangci-lint --version檢視版本確認安裝。

$ golangci-lint --version
golangci-lint has version 1.42.0 built from c6142e3 on 2021-08-17T08:27:57Z


解除安裝輸入brew uninstall golangci-lint即可。

$ brew uninstall golangci-lint
Uninstalling /usr/local/Cellar/golangci-lint/1.42.0... (9 files, 27.5MB)


Windows

範例環境:

  • Windows 10
  • Go 1.16

先裝好Git Bash,開啟Git Bash輸入
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.42.0
即可完成安裝。

安裝完後輸入golangci-lint --version檢視版本確認安裝。

$ golangci-lint --version
golangci-lint has version 1.42.0 built from c6142e3 on 2021-08-17T08:27:57Z

golangci-lint的可執行程式預設放在GOPATH\bin,通常是C:\Users\<user>\go\bin<user>為使用者名稱。



使用 Use

例如Go專案有一程式main.go內容如下,宣告package變數g沒使用。

main.go

import "fmt"

var g = 123

func main() {
    fmt.Println("Hello World")
}

在Go專案根目錄執行golangci-lint run便會開始對以下目錄中的go程式進行linting檢查。執行結果如下,可以看到linter指出的未使用的變數。

~/.../go-demo$ golangci-lint run
main.go:5:5: `g` is unused (deadcode)
var g = 123
    ^

golangci-lint預設啟用的linters參考Enabled By Default Linters,或輸入golangci-lint linters檢視啟用及未啟用的linters。

$ golangci-lint linters
Enabled by your configuration linters:
deadcode: Finds unused code [fast: false, auto-fix: false]
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
gosimple (megacheck): Linter for Go source code that specializes in simplifying a code [fast: false, auto-fix: false]
govet (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
ineffassign: Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
staticcheck (megacheck): Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
structcheck: Finds unused struct fields [fast: false, auto-fix: false]
typecheck: Like the front-end of a Go compiler, parses and type-checks Go code [fast: false, auto-fix: false]
unused (megacheck): Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
varcheck: Finds unused global variables and constants [fast: false, auto-fix: false]

Disabled by your configuration linters:
asciicheck: Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false]
bodyclose: checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
...

可在專案根目錄加入golangci-lint的配置檔.golangci.yaml來設定各項配置,參考官網的配置檔說明

例如在專案根目錄加入.golangci.yaml的內容如下,用linters.disable-all: true把所有的linter關閉,僅啟用errcheck,此linter並不會檢查沒用到的程式碼。

.golangci.yaml

linters:
  disable-all: true 
  enable:
    - errcheck

再次執行golangci-lint run就不會檢查出剛剛未使用變數的問題。

~/.../go-demo$ golangci-lint run
~/.../go-demo$

若只要lint特定目錄或檔案,使用golangci-lint run dir1 dir2/... dir3/file1.go。例如下面針對handler/alerthandler.go執行golangci-lint。

~/.../go-demo$ golangci-lint run handler/alerthandler.go

golangci-lint預設執行時間上線為1分鐘,如果要掃的package很多可能會發生timeout錯誤:

Running error: context loading failed: failed to load packages: timed out to load packages: context deadline exceeded

可在後面加上flag --timeout <duration>設定上限時間,<duration>為時間。例如下面設定為10分鐘。

~/.../go-demo$ golangci-lint run --timeout 10m

github


沒有留言:

張貼留言