由于国内墙的原因,安装配置Go环境一般情况下是不能直接成功的,需要翻墙或者使用代理。
一、下载并安装Go安装包
如果可以上官网,可以直接从官网下载。如果上不了官网,可以从下面的地址下载:
https://studygolang.com/dl
以Windows为例,下载go1.14.4.windows-amd64.msi并安装。
二、VSCode安装Go相关插件
1. 安装Go插件
2. 安装其它插件
在VSCode中Ctrl+Shift+P,打开命令面板,输入:
go:install
在弹出的提示中选择Go:Install/Update Tools,然后选中前面的复选框,再点确定
此时会安装,一般会出现:
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 golang.org/x/tools/cmd/goimports FAILED
Installing golang.org/x/lint/golint FAILED
那是因为墙的原因。
解决方案:
在VSCode中Ctrl+`,打开终端,在终端中执行:
go env -w GOPROXY=https://goproxy.cn,direct
此时再安装就成功了。
虽然这样设置了,但我们在使用go get命令安装其它包时也有可能出现连接失败的情况,比如在使用如下命令安装protoc-gen-go时:
go get github.com/golang/protobuf/protoc-gen-go
可能会出现:
unrecognized import path "google.golang.org/protobuf/compiler/protogen": https fetch: Get "https://google.golang.org/protobuf/compiler/protogen?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.
unrecognized import path "google.golang.org/protobuf/types/descriptorpb": https fetch: Get "https://google.golang.org/protobuf/types/descriptorpb?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.
unrecognized import path "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo": https fetch: Get "https://google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo?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.
可以使用下面的命令设置GO111MODULE为on即可:
go env -w GO111MODULE="on"
在此感谢七牛云以及Go语言中文网。