【Go学习】VS Code 中的代码自动补全和自动导入包([go learning] automatic code completion and automatic import package in vs Code)

原文链接:VS Code 中的代码自动补全和自动导入包

文章目录

  • 其他
    参考资料

  • 其他
  • 参考资料

VSCode 必须安装以下插件:

首先你必须安装 Golang 插件,然后再给 Go 安装工具包。

在 VS Code 中,使用快捷键:,然后键入:,将所有 16 个插件都勾选上,然后点击 OK 即开始安装。

command+shift+P
go:install/update tools
 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 :).
 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,然后选择 ,填入你想要修改的配置

在 Preferences -> Setting 然后输入 go,然后选择 ,填入你想要修改的配置

setting.json
  • 自动完成未导入的包。
1
  "go.autocompleteUnimportedPackages": true,
1
  "go.autocompleteUnimportedPackages": true,
  • VSCode 的一些插件需要配置代理,才能够正常安装。
1
  "http.proxy": "192.168.0.100:1087",
1
  "http.proxy": "192.168.0.100:1087",
  • 如果你遇到使用标准包可以出现代码提示,但是使用自己的包或者第三方库无法出现代码提示,你可以查看一下你的配置项。
1
  "go.inferGopath": true,
1
  "go.inferGopath": true,
  • 如果引用的包使用了 ( . “aa.com/text”) 那这个text包下的函数也无法跳转进去,这是为什么?

修改  为 ,默认是 。

"go.docsTool"
gogetdoc
godoc
1
  "go.docsTool": "gogetdoc",
1
  "go.docsTool": "gogetdoc",

其他

  • 当我们在使用 import 功能的时候,如果无法通过 lint 检查,则不会执行自动 import。
  • 如果你需要自动 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
 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",
}

参考资料

  • GOPATH in the VS Code Go extension
  • VSCode Golang 开发配置之代码提示
  • Use gogetdoc instead of godef and godoc #622
————————

Original link: automatic code completion and automatic import package in vs Code

Article catalog

  • other
    reference material
  • other
  • reference material

Vscode must install the following plug-ins:

First, you must install the golang plug-in, and then install the toolkit for go.

In vs code, use the shortcut key:, then type:, check all 16 plug-ins, and then click OK to start the installation.

command+shift+P
go:install/update tools
 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 :).
 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 :).

How to modify the default configuration:

In preferences – & gt; Setting, then enter go, then select, and fill in the configuration you want to modify

In preferences – & gt; Setting, then enter go, then select, and fill in the configuration you want to modify

setting.json
  • Automatically complete packages that are not imported.
1
  "go.autocompleteUnimportedPackages": true,
1
  "go.autocompleteUnimportedPackages": true,
  • Some plug-ins of vscode need to configure agents before they can be installed normally.
1
  "http.proxy": "192.168.0.100:1087",
1
  "http.proxy": "192.168.0.100:1087",
  • If you encounter code prompts when using standard packages, but code prompts cannot appear when using your own packages or third-party libraries, you can check your configuration items.
1
  "go.inferGopath": true,
1
  "go.inferGopath": true,
  • If the referenced package uses (. “AA. COM / text”), the functions under the text package cannot jump in. Why?

Modify {to, and the default is.

"go.docsTool"
gogetdoc
godoc
1
  "go.docsTool": "gogetdoc",
1
  "go.docsTool": "gogetdoc",

other

  • When we use the import function, if we cannot pass the lint check, we will not execute automatic import.
  • If you need automatic import, the premise is that you must write the functions of the package to be imported completely.

With mine

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
 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",
}

reference material

  • GOPATH in the VS Code Go extension
  • VSCode Golang 开发配置之代码提示
  • Use gogetdoc instead of godef and godoc #622