文章目录

由于国内墙的原因,安装配置Go环境一般情况下是不能直接成功的,需要翻墙或者使用代理。

一、下载并安装Go安装包

如果可以上官网,可以直接从官网下载。如果上不了官网,可以从下面的地址下载: https://studygolang.com/dl

以Windows为例,下载 go1.14.4.windows-amd64.msi并安装。

二、VSCode安装Go相关插件

1. 安装Go插件

2. 安装其它插件

在VSCode中Ctrl+Shift+P,打开命令面板,输入:

1go:install

在弹出的提示中选择Go:Install/Update Tools,然后选中前面的复选框,再点确定

此时会安装,一般会出现:

 1Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs FAILED
 2Installing github.com/ramya-rao-a/go-outline FAILED
 3Installing github.com/acroca/go-symbols FAILED
 4Installing golang.org/x/tools/cmd/guru FAILED
 5Installing golang.org/x/tools/cmd/gorename FAILED
 6Installing github.com/cweill/gotests/... FAILED
 7Installing github.com/fatih/gomodifytags FAILED
 8Installing github.com/josharian/impl FAILED
 9Installing github.com/davidrjenni/reftools/cmd/fillstruct FAILED
10Installing github.com/haya14busa/goplay/cmd/goplay FAILED
11Installing github.com/godoctor/godoctor FAILED
12Installing github.com/go-delve/delve/cmd/dlv FAILED
13Installing github.com/stamblerre/gocode FAILED
14Installing github.com/rogpeppe/godef FAILED
15Installing golang.org/x/tools/cmd/goimports FAILED
16Installing golang.org/x/lint/golint FAILED

那是因为墙的原因。 解决方案: 在VSCode中Ctrl+`,打开终端,在终端中执行:

1go env -w GOPROXY=https://goproxy.cn,direct

此时再安装就成功了。

虽然这样设置了,但我们在使用go get命令安装其它包时也有可能出现连接失败的情况,比如在使用如下命令安装protoc-gen-go时:

1go get github.com/golang/protobuf/protoc-gen-go

可能会出现:

1unrecognized 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.
2unrecognized 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.
3unrecognized 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即可:

1go env -w GO111MODULE="on"

在此感谢七牛云以及Go语言中文网。