之前在家里的电脑上是整理过一遍了, 遗憾的是没有记录, 最近想把公司的win电脑也搭下环境, 没想到又是从头到尾摸索了一遍, 索性记录下, 供以后参考吧.
1. GO环境安装
2. VScode环境搭建
2.1 vscode插件安装
2.2 vscode go tools安装
为了使用起来更顺手, 需要下载一部分工具, 但是联网直接安装是要从Google网站下载数据的, 因为众所周知的原因, 不能科学上网的小伙伴们只能得到一连串的安装失败提示, 但好在这些数据在GitHub是有托管的, 只能麻烦点手动安装了.
一般在开始编辑GO文件时, vscode会自动提示安装插件, 如果没有, 也可以手动触发:
win下使用Ctrl+Shift+P
组合键召唤出命令控制台:
然后输入Go:Install/Update Tools
,并回车:
首先选择左上角的复选框, 选中所有工具, 然后点击右上角的OK
.
但选了安装之后, 不能科学上网的童鞋只能在控制台OUTPUT
里收到下面这一堆错误提示:
Tools environment: GOPATH=F:\go
Installing 17 tools at F:\go\bin in module mode.
gocode
gopkgs
go-outline
go-symbols
guru
gorename
gotests
gomodifytags
impl
fillstruct
goplay
godoctor
dlv
gocode-gomod
godef
goreturns
golint
Installing github.com/mdempsky/gocode FAILED
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs 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 golang.org/x/tools/cmd/gorename FAILED
Installing github.com/cweill/gotests/... FAILED
Installing github.com/fatih/gomodifytags FAILED
Installing github.com/josharian/impl FAILED
Installing github.com/davidrjenni/reftools/cmd/fillstruct FAILED
Installing github.com/haya14busa/goplay/cmd/goplay FAILED
Installing github.com/godoctor/godoctor FAILED
Installing github.com/go-delve/delve/cmd/dlv FAILED
Installing github.com/stamblerre/gocode FAILED
Installing github.com/rogpeppe/godef FAILED
Installing github.com/sqs/goreturns FAILED
Installing golang.org/x/lint/golint FAILED
17 tools failed to install.
gocode: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/mdempsky/gocode
go get github.com/mdempsky/gocode: module github.com/mdempsky/gocode: Get "https://proxy.golang.org/github.com/mdempsky/gocode/@v/list": dial tcp 34.64.4.17: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.
undefined
gopkgs: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/uudashr/gopkgs/v2/cmd/gopkgs
go get github.com/uudashr/gopkgs/v2/cmd/gopkgs: module github.com/uudashr/gopkgs/v2/cmd/gopkgs: Get "https://proxy.golang.org/github.com/uudashr/gopkgs/v2/cmd/gopkgs/@v/list": dial tcp 34.64.4.17: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.
undefined
go-outline: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/ramya-rao-a/go-outline
go get github.com/ramya-rao-a/go-outline: module github.com/ramya-rao-a/go-outline: Get "https://proxy.golang.org/github.com/ramya-rao-a/go-outline/@v/list": dial tcp 34.64.4.17: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.
undefined
go-symbols: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/acroca/go-symbols
go get github.com/acroca/go-symbols: module github.com/acroca/go-symbols: Get "https://proxy.golang.org/github.com/acroca/go-symbols/@v/list": dial tcp 34.64.4.17: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.
undefined
guru: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v golang.org/x/tools/cmd/guru
go get golang.org/x/tools/cmd/guru: module golang.org/x/tools/cmd/guru: Get "https://proxy.golang.org/golang.org/x/tools/cmd/guru/@v/list": dial tcp 34.64.4.81: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.
undefined
gorename: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v golang.org/x/tools/cmd/gorename
go get golang.org/x/tools/cmd/gorename: module golang.org/x/tools/cmd/gorename: Get "https://proxy.golang.org/golang.org/x/tools/cmd/gorename/@v/list": dial tcp 34.64.4.81: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.
undefined
gotests: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/cweill/gotests/...
go get github.com/cweill/gotests/...: module github.com/cweill/gotests: Get "https://proxy.golang.org/github.com/cweill/gotests/@v/list": dial tcp 34.64.4.81: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.
undefined
gomodifytags: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/fatih/gomodifytags
go get github.com/fatih/gomodifytags: module github.com/fatih/gomodifytags: Get "https://proxy.golang.org/github.com/fatih/gomodifytags/@v/list": dial tcp 34.64.4.81: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.
undefined
impl: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/josharian/impl
go get github.com/josharian/impl: module github.com/josharian/impl: Get "https://proxy.golang.org/github.com/josharian/impl/@v/list": dial tcp 34.64.4.81: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.
undefined
fillstruct: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/davidrjenni/reftools/cmd/fillstruct
go get github.com/davidrjenni/reftools/cmd/fillstruct: module github.com/davidrjenni/reftools/cmd/fillstruct: Get "https://proxy.golang.org/github.com/davidrjenni/reftools/cmd/fillstruct/@v/list": dial tcp 34.64.4.81: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.
undefined
goplay: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/haya14busa/goplay/cmd/goplay
go get github.com/haya14busa/goplay/cmd/goplay: module github.com/haya14busa/goplay/cmd/goplay: Get "https://proxy.golang.org/github.com/haya14busa/goplay/cmd/goplay/@v/list": dial tcp 34.64.4.81: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.
undefined
godoctor: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/godoctor/godoctor
go get github.com/godoctor/godoctor: module github.com/godoctor/godoctor: Get "https://proxy.golang.org/github.com/godoctor/godoctor/@v/list": dial tcp 34.64.4.81: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.
undefined
dlv: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/go-delve/delve/cmd/dlv
go get github.com/go-delve/delve/cmd/dlv: module github.com/go-delve/delve/cmd/dlv: Get "https://proxy.golang.org/github.com/go-delve/delve/cmd/dlv/@v/list": dial tcp 34.64.4.81: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.
undefined
gocode-gomod: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v -d github.com/stamblerre/gocode
go get github.com/stamblerre/gocode: module github.com/stamblerre/gocode: Get "https://proxy.golang.org/github.com/stamblerre/gocode/@v/list": dial tcp 34.64.4.81: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.
undefined
godef: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/rogpeppe/godef
go get github.com/rogpeppe/godef: module github.com/rogpeppe/godef: Get "https://proxy.golang.org/github.com/rogpeppe/godef/@v/list": dial tcp 34.64.4.81: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.
undefined
goreturns: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v github.com/sqs/goreturns
go get github.com/sqs/goreturns: module github.com/sqs/goreturns: Get "https://proxy.golang.org/github.com/sqs/goreturns/@v/list": dial tcp 34.64.4.81: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.
undefined
golint: failed to install [object Object]: Error: Command failed: E:\Go\bin\go.exe get -v golang.org/x/lint/golint
go get golang.org/x/lint/golint: module golang.org/x/lint/golint: Get "https://proxy.golang.org/golang.org/x/lint/golint/@v/list": dial tcp 34.64.4.81: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.
undefined
我们接下来要做的就是修复这一堆错误.
---------------划重点----------------------
中国人苦go代理久矣, 哪里有压迫哪里就有反抗, 活人总不能被尿憋死, 早就有前辈们帮忙解决好这个问题了, 轻轻松松改下go的代理就好了:
Go 1.13 及以上(推荐)
打开你的终端并执行
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
完成。
macOS 或 Linux
打开你的终端并执行
$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.cn
或者
$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile
完成。
好了, 如果你设置完了, 剩下的就都不用看了, 直接开始使用就好了!!!
---------------划重点----------------------
首先我们进入到%GOPATH%\src路径, GOPATH可以通过以下方法拿到:
方法1: win桌面右键选择我的电脑
->属性
->高级系统设置
->高级
->环境变量(N)...
:
方法2:
使用win+R
组合键, 输入CMD
打开命令行, 输入go env
就可以看到一系列配置项, 里面的GOPATH就是我们要找的路径:
接下来在%GOPATH%/src下面建立两个文件夹路径:
src
|__golang.org
| |__x
|__github.com
然后我们把需要的包拉下来.
首先再开个CMD窗口, 进入%GOPATH%\src
, 用go get
获取工具包:
go get -u -v github.com/ramya-rao-a/go-outline
go get -u -v github.com/acroca/go-symbols
go get -u -v github.com/mdempsky/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v golang.org/x/tools/cmd/godoc
go get -u -v github.com/zmb3/gogetdoc
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/fatih/gomodifytags
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/goimports
go get -u -v github.com/cweill/gotests/...
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v github.com/josharian/impl
go get -u -v github.com/haya14busa/goplay/cmd/goplay
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -u -v github.com/alecthomas/gometalinter
上面这些命令, 有一部分可能会执行失败, 遇到的话可以尝试直接通过git把仓库克隆下来.
举个例子,
如果go get -u -v golang.org/x/tools/cmd/godoc
执行失败, 可以使用下面的命令克隆:
git clone git@github.com:golang/tools.git
但是注意clone到的仓库要放到对应的目录下.
全部下载到后就可以通过go install
安装了, 举个例子:
go install golang.org/x/tools/cmd/guru
这个包依赖较多, 可能会碰到类似下面的错误提示:
golang.org\x\tools\internal\imports\mod.go:17:2: cannot find package "golang.org/x/mod/module" in any of:
E:\Go\src\golang.org\x\mod\module (from $GOROOT)
F:\go\src\golang.org\x\mod\module (from $GOPATH)
golang.org\x\tools\internal\gocommand\vendor.go:16:2: cannot find package "golang.org/x/mod/semver" in any of:
E:\Go\src\golang.org\x\mod\semver (from $GOROOT)
F:\go\src\golang.org\x\mod\semver (from $GOPATH)
golang.org\x\tools\go\packages\golist.go:27:2: cannot find package "golang.org/x/xerrors" in any of:
E:\Go\src\golang.org\x\xerrors (from $GOROOT)
F:\go\src\golang.org\x\xerrors (from $GOPATH)
根据提示把这3个依赖包安装上就好了, 也是通过go get
放到错误提示里的对应目录里面去, 然后再执行go install
.
of:
E:\Go\src\golang.org\x\xerrors (from $GOROOT)
F:\go\src\golang.org\x\xerrors (from $GOPATH)
根据提示把这3个依赖包安装上就好了, 也是通过```go get```放到错误提示里的对应目录里面去, 然后再执行```go install```.