VSCode下安装配置Go环境


由于国内墙的原因,安装配置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语言中文网。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值