原文鏈接: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
,填入你想要修改的配置
- 自動完成未導入的包。
|
|
- VSCode 的一些插件需要配置代理,才能夠正常安裝。
|
|
- 如果你遇到使用標准包可以出現代碼提示,但是使用自己的包或者第三方庫無法出現代碼提示,你可以查看一下你的配置項。
|
|
- 如果引用的包使用了 ( . “aa.com/text”) 那這個text包下的函數也無法跳轉進去,這是為什么?
修改 "go.docsTool"
為 gogetdoc
,默認是 godoc
。
|
|
其他
- 當我們在使用 import 功能的時候,如果無法通過 lint 檢查,則不會執行自動 import。
- 如果你需要自動 import 的前提是你必須把要導入的包的函數寫完整。
附帶我的 settings.json
|
|
參考資料
- GOPATH in the VS Code Go extension
- VSCode Golang 開發配置之代碼提示
- Use gogetdoc instead of godef and godoc #622