VS Code 中的代碼自動補全和自動導入包

原文鏈接:VS Code 中的代碼自動補全和自動導入包

文章目錄

VSCode 必須安裝以下插件:

首先你必須安裝 Golang 插件,然後再給 Go 安裝工具包。

在 VS Code 中,使用快捷鍵:command+shift+P,然後鍵入:go:install/update tools,將所有 16 個插件都勾選上,然後點擊 OK 即開始安裝。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Installing 16 tools at /Users/maiyang/develop/goworkspace//bin
  gocode
  gopkgs
  go-outline
  go-symbols
  guru
  gorename
  dlv
  godef
  godoc
  goreturns
  golint
  gotests
  gomodifytags
  impl
  fillstruct
  goplay

Installing github.com/mdempsky/gocode SUCCEEDED
Installing github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED
Installing github.com/ramya-rao-a/go-outline SUCCEEDED
Installing github.com/acroca/go-symbols SUCCEEDED
Installing golang.org/x/tools/cmd/guru SUCCEEDED
Installing golang.org/x/tools/cmd/gorename SUCCEEDED
Installing github.com/derekparker/delve/cmd/dlv SUCCEEDED
Installing github.com/rogpeppe/godef SUCCEEDED
Installing golang.org/x/tools/cmd/godoc SUCCEEDED
Installing github.com/sqs/goreturns SUCCEEDED
Installing github.com/golang/lint/golint SUCCEEDED
Installing github.com/cweill/gotests/... SUCCEEDED
Installing github.com/fatih/gomodifytags SUCCEEDED
Installing github.com/josharian/impl SUCCEEDED
Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED

All tools successfully installed. You're ready to Go :).

修改默認配置的方法:

在 Preferences -> Setting 然後輸入 go,然後選擇 setting.json,填入你想要修改的配置

  • 自動完成未導入的包。
1
  "go.autocompleteUnimportedPackages": true,
  • VSCode 的一些插件需要配置代理,才能夠正常安裝。
1
  "http.proxy": "192.168.0.100:1087",
  • 如果你遇到使用標準包可以出現代碼提示,但是使用自己的包或者第三方庫無法出現代碼提示,你可以查看一下你的配置項。
1
  "go.inferGopath": true,
  • 如果引用的包使用了 ( . “aa.com/text”) 那這個text包下的函數也無法跳轉進去,這是爲什麼?

修改 "go.docsTool"gogetdoc,默認是 godoc

1
  "go.docsTool": "gogetdoc",

其他

  1. 當我們在使用 import 功能的時候,如果無法通過 lint 檢查,則不會執行自動 import。
  2. 如果你需要自動 import 的前提是你必須把要導入的包的函數寫完整。

附帶我的 settings.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "go.goroot": "",
  "go.gopath": "",
  "go.inferGopath": true,
  "go.autocompleteUnimportedPackages": true,
  "go.gocodePackageLookupMode": "go",
  "go.gotoSymbol.includeImports": true,
  "go.useCodeSnippetsOnFunctionSuggest": true,
  "go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
  "go.docsTool": "gogetdoc",
}

參考資料

  1. GOPATH in the VS Code Go extension
  2. VSCode Golang 開發配置之代碼提示
  3. Use gogetdoc instead of godef and godoc #622

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章