原文链接:VS Code 中的代码自动补全和自动导入包
文章目录
-
- 其他
- 参考资料
VSCode 必须安装以下插件:
首先你必须安装 Golang 插件,然后再给 Go 安装工具包。
在 VS Code 中,使用快捷键:command+shift+P,然后键入:go:install/update tools,将所有 16 个插件都勾选上,然后点击 OK 即开始安装。
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1010 1111 1212 1313 1414 1515 1616 1717 1818 1919 2020 2121 2222 2323 2424 2525 2626 2727 2828 2929 3030 3131 3232 3333 3434 3535 3636 37 38 39Installing 16 tools at /Users/maiyang/develop/goworkspace//bin 40 gocode 41 gopkgs 42 go-outline 43 go-symbols 44 guru 45 gorename 46 dlv 47 godef 48 godoc 49 goreturns 50 golint 51 gotests 52 gomodifytags 53 impl 54 fillstruct 55 goplay 56 57Installing github.com/mdempsky/gocode SUCCEEDED 58Installing github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED 59Installing github.com/ramya-rao-a/go-outline SUCCEEDED 60Installing github.com/acroca/go-symbols SUCCEEDED 61Installing golang.org/x/tools/cmd/guru SUCCEEDED 62Installing golang.org/x/tools/cmd/gorename SUCCEEDED 63Installing github.com/derekparker/delve/cmd/dlv SUCCEEDED 64Installing github.com/rogpeppe/godef SUCCEEDED 65Installing golang.org/x/tools/cmd/godoc SUCCEEDED 66Installing github.com/sqs/goreturns SUCCEEDED 67Installing github.com/golang/lint/golint SUCCEEDED 68Installing github.com/cweill/gotests/... SUCCEEDED 69Installing github.com/fatih/gomodifytags SUCCEEDED 70Installing github.com/josharian/impl SUCCEEDED 71Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED 72Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED 73 74All 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。
11 2 3 4 "go.docsTool": "gogetdoc",
其他
- 当我们在使用 import 功能的时候,如果无法通过 lint 检查,则不会执行自动 import。
- 如果你需要自动 import 的前提是你必须把要导入的包的函数写完整。
附带我的 settings.json
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1010 1111 12 13 14{ 15 "go.goroot": "", 16 "go.gopath": "", 17 "go.inferGopath": true, 18 "go.autocompleteUnimportedPackages": true, 19 "go.gocodePackageLookupMode": "go", 20 "go.gotoSymbol.includeImports": true, 21 "go.useCodeSnippetsOnFunctionSuggest": true, 22 "go.useCodeSnippetsOnFunctionSuggestWithoutType": true, 23 "go.docsTool": "gogetdoc", 24}