在 VS code 撰寫 go 程式的過程中,要檢查是否符合 coding style,以及提醒哪些變數或參數有定義卻沒使用,設定好 go linter 就沒有煩惱。
1. 環境  Environment
- macOS 12.6.2
- Visual Studio Code v1.74.3
- go v1.19
2. 開始設定  Configuration
2.1. 安裝 Extension
在 VS code 安裝 Extension: Go

按下 cmd + shift + p 開啟 Command Palette,並且輸入 go install 後選取 Go: Install/Update Tools

選取 staticcheck 及 gopls 後,按下 OK 即開始安裝。

2.3. 設定參數
注意:完成下列的設定,一定要關閉 VS code 再重新開啟,才不會有怪怪的問題出現。
按下 cmd + shift + p 開啟 Command Palette,並且輸入 user 後選取 Preferences: Open User Settings (JSON)

先在 settings.json 檔案內貼上基本設定,
| 12
 3
 4
 5
 6
 7
 8
 
 | "[go]": {
 "editor.formatOnSave": true,
 
 
 
 
 },
 
 | 
"editor.snippetSuggestions": "none", 這樣就不會出現提示了。 –可是我需要–
接著貼上關於 gopls formatting 的設定,
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | "gopls": {
 "analyses": {
 "ST1003": true,
 "ST1016": true,
 "ST1020": true,
 "ST1021": true,
 "ST1022": true,
 "U1000": true,
 },
 "formatting.gofumpt": true,
 "staticcheck": true,
 "ui.completion.usePlaceholders": true,
 
 
 
 "ui.semanticTokens": true,
 "completionDocumentation": true
 },
 "go.useLanguageServer": false,
 
 | 
"go.useLanguageServer": false, 一定要設定成 false 才會有動作,這有點奇怪。
只要開啟 .go 檔案,就會連相同 package 的檔案一同檢查,可以看到 warning 如下圖,

如果不想這麼自動的檢查,可以在 settings.json 將關於 gopls formatting 的設定改成以下,
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | "go.lintFlags": ["-checks", "all"],"gopls": {
 
 
 
 
 
 
 
 
 "formatting.gofumpt": true,
 
 "ui.completion.usePlaceholders": true,
 "ui.diagnostic.analyses": {
 "unusedparams": true
 },
 "ui.semanticTokens": true,
 "completionDocumentation": true
 },
 
 
 | 
此時要檢查 User Settings 的 Go: Lint Tool 及 Go: Lint Flags 是否設定正確。

按下 cmd + shift + p 開啟 Command Palette,並且輸入 lint 後選取 Go: Lint Current Package (也可以選擇 Go: Lint Workspace)就可以看檢查的結果了

補充說明:
在設定裡 "formatting.gofumpt": true, 這行若設定為 true 時,會在存檔時執行 “Group imports” 及精簡程式碼。 以下是未執行 format,
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | import ("errors"
 "fmt"
 "haicds/internal/app/application/ebof"
 "haicds/pkg/res"
 "strconv"
 
 "github.com/tidwall/gjson"
 )
 
 func foo() {
 var webClientLogger = logger.WebClientAccessLog{
 Type:        accesslog.LogTypeValueList.EbofAction,
 Description: "FW upgrade by disk ID",
 }
 webClientLogger.Code = res.SuccessCode
 var cnt = make(map[string]interface{})
 
 var qParams struct {
 ID string `uri:"id" binding:"required"`
 }
 ...
 }
 
 | 
按下 cmd + s 存檔時,會自動進行 format,結果如下,
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | import ("errors"
 "fmt"
 "strconv"
 
 "haicds/internal/app/application/ebof"
 "haicds/pkg/res"
 
 "github.com/tidwall/gjson"
 )
 
 func foo() {
 webClientLogger := logger.WebClientAccessLog{
 Type:        accesslog.LogTypeValueList.EbofAction,
 Description: "FW upgrade by disk ID",
 }
 webClientLogger.Code = res.SuccessCode
 cnt := make(map[string]interface{})
 
 var qParams struct {
 ID string `uri:"id" binding:"required"`
 }
 ...
 }
 
 | 
3. 後記
真心推薦 Goland,不要做任何設定 ( ^.< )
4. 參考資料  References