欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VS Code中golang环境配置

程序员文章站 2024-01-10 09:38:04
...

1.VSCode 建议安装以下插件:

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

在 VS Code 中,使用快捷键:command+shift+P,然后键入:go:install/update tools,将所有 16 个插件都勾选上,然后点击 OK 即开始安装。

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 :).

2.代码自动补全 

File->Preferences -> Setting ,然后输入 go,然后选择 setting.json,填入你想要修改的配置。或者通过右上角图标VS Code中golang环境配置,进入设置 setting.json页面,添加如下字段信息。实现代码自动补全需要设置字段为go.useCodeSnippetsOnFunctionSuggest ,go.inferGopath。

"go.autocompleteUnimportedPackages": true,         //自动完成未导入的包
“go.useCodeSnippetsOnFunctionSuggest”: true,       //自动提示代码,代码自动补全   
"go.docsTool": "gogetdoc",                         //支持跳转到文件名含.路径等功能
"go.inferGopath": true,                            //开启推断gopath的功能
"go.gocodePackageLookupMode": "go",                //代码包查询模式
"go.gotoSymbol.includeImports": true

可参考setting.json文件格式。

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