VSCode安装GO语言依赖工具

由于vscode对go语言的支持还是hin不错滴,所以我日常学习go都用vscode,但这货有个毛病,各种lint、补全、nav、调试都依赖go语言的其他扩展工具,如果安装补全,会给出类似提示:

The "gocode" command is not available. Use "go get -v github.com/mdempsky/gocode" to install.

但如果按照vscode的提示点击“安装”后,经过漫长等待,会迎来进一步提示:

gocode:
Error: Command failed: /usr/local/go/bin/go get -u -v github.com/mdempsky/gocode
github.com/mdempsky/gocode (download)
Fetching https://golang.org/x/tools/go/gcexportdata?go-get=1
https fetch failed: Get https://golang.org/x/tools/go/gcexportdata?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
package golang.org/x/tools/go/gcexportdata: unrecognized import path "golang.org/x/tools/go/gcexportdata" (https fetch: Get https://golang.org/x/tools/go/gcexportdata?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

What’s the fuuuuuuu!!!

必须说明我是在科学上网的前提下执行的安装命令,所以猜测不是网络问题,具体原因没深究,我参考了官方指导以及一篇博文解决的该问题,博客写得有点绕,我挑简单的总结一下。

解决方案

从官方文档中可以明确,VSCode依赖的几款工具完成不同功能:

名称描述链接
gocode代码自动补全https://github.com/mdempsky/gocode
go-outline在当前文件中查找https://github.com/ramya-rao-a/go-outline
go-symbols在项目路径下查找https://github.com/acroca/go-symbols
gopkgs自动补全未导入包https://github.com/uudashr/gopkgs
guru查询所有引用https://golang.org/x/tools/cmd/guru
gorename重命名符号https://golang.org/x/tools/cmd/gorename
goreturns格式化代码https://github.com/sqs/goreturns
godef跳转到声明https://github.com/rogpeppe/godef
godoc鼠标悬浮时文档提示https://golang.org/x/tools/cmd/godoc
golint就是linthttps://golang.org/x/lint/golint
dlv调试功能https://github.com/derekparker/delve/tree/master/cmd/dlv
gomodifytags修改结构体标签https://github.com/fatih/gomodifytags
goplay运行当前go文件https://github.com/haya14busa/goplay/
impl新建接口https://github.com/josharian/impl
gotype-live类型诊断https://github.com/tylerb/gotype-live
gotests单元测试https://github.com/cweill/gotests/
go-langserver语言服务https://github.com/sourcegraph/go-langserver
filstruct结构体成员默认值https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct

以上的工具可以有选择地安装,但为了开发过程中不要出什么岔子,我一般选择全部安装,很不幸的是安装过程中80%的工具会出现timeout的提示。

安装步骤

为了统一每个人的开发环境,下文中GOPATH表示自己电脑go的安装路径,例如我的电脑默认安装路径为~/go,如果没有的话建议先执行命令export GOPATH=~/go,Windows用户…👋

  1. 创建目录$GOPATH/src/golang.org/x,并切换到该目录
mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
  1. 克隆golang.org工具源码
    如果不克隆的话,go get -u -v golang.org/xxx肯定是timeout的,所以只能先把它们下载到本地src/golang.org/x/tools
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint.git
  1. 下载github源码
    按照go get -u -v命令,从GitHub上下载代码后还会fetch,我们很可能会在fetch https://golang.org/xxx的时候挂掉,原因你懂的。所以去掉-u选项,禁止从网络更新现有代码。
# 先从github下载依赖工具的源码,fetch提示timeout不要管
go get -v github.com/ramya-rao-a/go-outline
go get -v github.com/acroca/go-symbols
go get -v github.com/mdempsky/gocode
go get -v github.com/rogpeppe/godef
go get -v github.com/zmb3/gogetdoc
go get -v github.com/fatih/gomodifytags
go get -v sourcegraph.com/sqs/goreturns
go get -v github.com/cweill/gotests/...
go get -v github.com/josharian/impl
go get -v github.com/haya14busa/goplay/cmd/goplay
go get -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -v github.com/alecthomas/gometalinter
  1. 安装工具
go install github.com/ramya-rao-a/go-outline
go install github.com/acroca/go-symbols
go install github.com/mdempsky/gocode
go install github.com/rogpeppe/godef
go install github.com/zmb3/gogetdoc
go install github.com/fatih/gomodifytags
go install sourcegraph.com/sqs/goreturns
go install github.com/cweill/gotests/...
go install github.com/josharian/impl
go install github.com/haya14busa/goplay/cmd/goplay
go install github.com/uudashr/gopkgs/cmd/gopkgs
go install github.com/davidrjenni/reftools/cmd/fillstruct
go install github.com/alecthomas/gometalinter
$GOPATH/bin/gometalinter --install
go install golang.org/x/tools/cmd/godoc
go install golang.org/x/lint/golint
go install golang.org/x/tools/cmd/gorename
go install golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/guru
  1. 集成到系统环境中
    由于我是在用户目录下临时安装的,真正的GO环境是/usr/local/go,所以最后一步无比注意,把~/go/bin下面生成的所有执行文件拷贝到系统环境中
sudo cp -af $GOPATH/bin/* /usr/local/go/bin/

打完收工!

    原文作者:码农飞龙
    原文地址: https://www.jianshu.com/p/f952042af8ff
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞