vscode 配置 go 开发环境

vscode 版本: 1.34.0 (system setup)

起因

由于墙的原因,更新 vscode go 开发环境各种FAILED,网上看了很多材料没解决。
ctrl+shift+x,搜索 Go 安装。
ctrl+shift+p,搜索 go:install/update tools,全选,执行。
vscode 配置 go 开发环境_第1张图片

go install 17 tools at D:\gopath\bin
  gocode
  gopkgs
  go-outline
  go-symbols
  guru
  gorename
  dlv
  gocode-gomod
  gogetdoc
  goreturns
  golint
  gotests
  gomodifytags
  impl
  fillstruct
  goplay
  godoctor

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

10 tools failed to install.

解决方案

研究后发现,主要是 golang.org/x/tools, golang.org/x/lint 和 golang.org/x/net 这三个包在 google 官网(被墙)。但是github上有,可以手动安装。

  • 安装 go 扩展包。
  • 配置 vscode。

安装 go 扩展包

linux下

cd $GOPATH
git clone [email protected]:golang/tools.git ./src/golang.org/x/tools
git clone [email protected]:golang/lint.git ./src/golang.org/x/lint
git clone [email protected]:golang/net.git ./src/golang.org/x/net

经测试在 windows 版本还要再装 github.com/golang/lint 包

cd $env:GOPATH
git clone [email protected]:golang/tools.git ./src/golang.org/x/tools
git clone [email protected]:golang/lint.git ./src/golang.org/x/lint
git clone [email protected]:golang/net.git ./src/golang.org/x/net

git clone [email protected]:golang/lint.git ./src/github.com/golang/lint

在 shell 里安装
第一遍

go get -u -v github.com/mdempsky/gocode  
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs  
go get -u -v github.com/ramya-rao-a/go-outline  
go get -u -v github.com/acroca/go-symbols  
go get -u -v golang.org/x/tools/cmd/guru  
go get -u -v golang.org/x/tools/cmd/gorename  
go get -u -v github.com/go-delve/delve/cmd/dlv  
go get -u -v github.com/stamblerre/gocode  
go get -u -v github.com/zmb3/gogetdoc  
go get -u -v github.com/sqs/goreturns  
go get -u -v golang.org/x/lint/golint  
go get -u -v github.com/cweill/gotests/...  
go get -u -v github.com/fatih/gomodifytags  
go get -u -v github.com/josharian/impl  
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct  
go get -u -v github.com/haya14busa/goplay/cmd/goplay  
go get -u -v github.com/godoctor/godoctor

第二遍


go install github.com/mdempsky/gocode  
go install github.com/uudashr/gopkgs/cmd/gopkgs  
go install github.com/ramya-rao-a/go-outline  
go install github.com/acroca/go-symbols  
go install golang.org/x/tools/cmd/guru  
go install golang.org/x/tools/cmd/gorename  
go install github.com/go-delve/delve/cmd/dlv  
go install github.com/stamblerre/gocode  
go install github.com/zmb3/gogetdoc  
go install github.com/sqs/goreturns  
go install golang.org/x/lint/golint  
go install github.com/cweill/gotests/...  
go install github.com/fatih/gomodifytags  
go install github.com/josharian/impl  
go install github.com/davidrjenni/reftools/cmd/fillstruct  
go install github.com/haya14busa/goplay/cmd/goplay  
go install github.com/godoctor/godoctor 

在 vscode 里确认一遍

Installing 17 tools at D:\gopath\bin
  gocode
  gopkgs
  go-outline
  go-symbols
  guru
  gorename
  dlv
  gocode-gomod
  gogetdoc
  goreturns
  golint
  gotests
  gomodifytags
  impl
  fillstruct
  goplay
  godoctor

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/go-delve/delve/cmd/dlv SUCCEEDED
Installing github.com/stamblerre/gocode SUCCEEDED
Installing github.com/zmb3/gogetdoc SUCCEEDED
Installing github.com/sqs/goreturns SUCCEEDED
Installing golang.org/x/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
Installing github.com/godoctor/godoctor SUCCEEDED

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

配置 vscode

打开方式

  • 文件/首选项/设置/{}(在右上角,打开设置 settings.json)
  • ctrl+,->{}(在右上角,打开设置 settings.json)
  • ctrl+shift+p->输入settings.json

设置的内容

//go
"go.gopath": "",
"go.inferGopath": true,
"go.autocompleteUnimportedPackages": true,
"go.gocodePackageLookupMode": "go",
"go.gotoSymbol.includeImports": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.docsTool": "gogetdoc",

如果win10下安装了wsl,可以设置shell为wsl。

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe",
"terminal.external.windowsExec": "C:\\WINDOWS\\System32\\bash.exe",

参考

Windows Subsystem for Linux 简称WSL

你可能感兴趣的:(开发环境,win,go,wsl,vscode)