本以为Visual Studio Code 安装Golang插件很容易,确实容易。但是安装Golang插件需要的插件却很麻烦。

譬如调试需要用dlv,visual studio code可以自动下载需要的插件,如下所示:

gocode
go-outline
go-symbols
guru
gorename
dlv
godef
goreturns

但是各种报错,信息如下:

Installing github.com/nsf/gocode FAILED
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 github.com/golang/lint/golint FAILED

......

guru:
Error: Command failed: C:\Go\bin\go.exe get -u -v golang.org/x/tools/cmd/guru
Fetching https://golang.org/x/tools/cmd/guru?go-get=1
https fetch failed: Get https://golang.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
golang.org\x\tools (download)
Fetching https://golang.org/x/tools/cmd/guru/serial?go-get=1
https fetch failed: Get https://golang.org/x/tools/cmd/guru/serial?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

......

于是各种查找安装dlv失败的解决办法,网上说的很简单,都是通过如下命令:

go get -u github.com/derekparker/delve/cmd/dlv

我兴致匆匆,以为马上可以解决,但是又报错

# github.com/derekparker/delve/pkg/config

F:\ProgramTest\Golang\src\github.com\derekparker\delve\pkg\config\config.go:137:

 undefined: io.SeekStart

# github.com/derekparker/delve/pkg/dwarf/reader

F:\ProgramTest\Golang\src\github.com\derekparker\delve\pkg\dwarf\reader\variable

s.go:89: dwarf.Ranges undefined (type *dwarf.Data has no field or method Ranges,

 but does have dwarf.ranges)

看来是缺少相应的文件,其中,F:\ProgramTest\Golang是GOPATH变量值

继续找资料,发现了如下解决方法:

1. 在目录%GOPATH%\src\github.com下,执行:git clone https://github.com/golang/tools.git tools

2. 在下载完成后,在%GOPATH%\src\github.com\golang多了一个tools目录,将tools目录复制到%GOPATH%\src\golang.org\x\tools下,如果没有自行创建

3. 然后继续通过Visual Studio Code安装,提示如下

Installing github.com/nsf/gocode FAILED

Installing github.com/ramya-rao-a/go-outline SUCCEEDED

Installing github.com/acroca/go-symbols SUCCEEDED

Installing golang.org/x/tools/cmd/guru FAILED

Installing golang.org/x/tools/cmd/gorename SUCCEEDED

Installing github.com/derekparker/delve/cmd/dlv FAILED

Installing github.com/rogpeppe/godef FAILED

Installing github.com/sqs/goreturns FAILED


5 tools failed to install.

其实还是可以安装成功几个的,失败原因也大致如下:

gocode:

Error: Command failed: C:\Go\bin\go.exe get -u -v github.com/nsf/gocode

github.com\nsf\gocode (download)

github.com/nsf/gocode

# github.com/nsf/gocode

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:99: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:148: undefined: io.SeekCurrent

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:152: undefined: io.SeekCurrent

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:214: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)

github.com\nsf\gocode (download)

github.com/nsf/gocode

# github.com/nsf/gocode

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:99: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:148: undefined: io.SeekCurrent

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:152: undefined: io.SeekCurrent

F:\ProgramTest\Golang\src\github.com\nsf\gocode\package_ibin.go:214: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)



guru:

Error: Command failed: C:\Go\bin\go.exe get -u -v golang.org/x/tools/cmd/guru

Fetching https://golang.org/x/tools/cmd/guru?go-get=1

https fetch failed: Get https://golang.org/x/tools/cmd/guru?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

golang.org\x\tools (download)

Fetching https://golang.org/x/tools/cmd/guru/serial?go-get=1

https fetch failed: Get https://golang.org/x/tools/cmd/guru/serial?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Fetching https://golang.org/x/tools/go/ast/astutil?go-get=1

https fetch failed: Get https://golang.org/x/tools/go/ast/astutil?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Fetching https://golang.org/x/tools/go/buildutil?go-get=1

https fetch failed: Get https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

 

4. 通过手动安装失败的插件,如gocode

在Visual Studio Code的右下角的TERMINAL栏(当前目录自动在%GOPATH%)执行如下命令

go get -u -v github.com/nsf/gocode

依然报错:

github.com\nsf\gocode (download)

github.com/nsf/gocode

# github.com/nsf/gocode

src\github.com\nsf\gocode\package_ibin.go:99: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)

src\github.com\nsf\gocode\package_ibin.go:148: undefined: io.SeekCurrent

src\github.com\nsf\gocode\package_ibin.go:152: undefined: io.SeekCurrent

src\github.com\nsf\gocode\package_ibin.go:214: r.declReader.Reset undefined (type bytes.Reader has no field or method Reset)

说是缺少一些package,如io,r。于是到github上看issuse,也没有说有这类问题。

 

5. 继续查询,发现可能与go的版本号有关

通过go version查看版本号:go version go1.6 windows/amd64

这个版本号真的好低,赶紧更新为1.10版本,安装后信息如下:

go version go1.10.3 windows/amd64

 

6. 由于重新安装go,visual studio code检测到相关信息,因此主动更新之前安装的插件

 

7. 重新安装dlv等插件,安装结果如下

Installing github.com/mdempsky/gocode SUCCEEDED
Installing golang.org/x/tools/cmd/guru SUCCEEDED
Installing github.com/derekparker/delve/cmd/dlv SUCCEEDED
Installing github.com/rogpeppe/godef SUCCEEDED
Installing github.com/sqs/goreturns SUCCEEDED
Installing golang.org/x/lint/golint FAILED

发现只有golint(检测代码中不规范的地方)安装失败,因为golang.org被墙,有些时候可以访问,有些时候不能。多试几次,并且dlv安装成功,可以调试程序了,golint对调试程序也没什么影响。

 

总结:go版本太低,导致安装golang插件一直失败,更新go版本即可解决。

 

参考资料