Go安装、配置和vsCode配置Go
go的安装与配置
-
go下载
go语言官方下载地址:https://golang.org/dl/
- go安装
安装过程省略,默认安装路径C:\Go即可。
- go环境变量配置
1. 配置GOROOT: C:\Go\ -------go的环境地址
2. 配置GOPATH: E:\go\src\Go(我自己配置的地址) --------go的工作目录的地址
3. 在环境变量Path中添加C:\Go\bin路径
- 查看是否安装成功
go env (查看GOPATH和GOROOT的路径是否配置成功)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
vsCode的安装与配置
- vsCode下载
https://code.visualstudio.com/
安装过程省略。
- vsCode下载安装Go插件
如图所示,输入关键字Go后,点击install即可
- vsCode配置Go插件
如图所示,点击设置按钮
输入gopath如图所示,点击"Edit in setting.json"
进行如图配置 其中go语言环境安装目录和工作目录的路径根据自己的实际情况填写即可
至此配置完毕 重启即可
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
问题1:
我们打开vsCode后会提示我们好多库安装失败,错误如下所示:
Installing github.com/mdempsky/gocode FAILED
Installing github.com/uudashr/gopkgs/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/go-delve/delve/cmd/dlv SUCCEEDED
Installing github.com/stamblerre/gocode FAILED
...
失败原因:
golang.org 在国内由于一些原因无法直接访问,而go get
在获取gocode
、go-def
、golint
等插件依赖工具的源码时,需要从 golang.org 上拉取部分代码至GOPATH
,自然就导致了最后这些依赖于 golang.org 代码的依赖工具安装失败。
解决方法:
在GOPATH目录下,建立src\golang.org\x目录,通过终端进入x目录,执行:
git clone https://github.com/golang/tools.git tools
git clone https://github.com/golang/lint.git lint
git clone https://github.com/golang/sync.git sync
git完成后,重新install还是会报以下错误:
go: github.com/yuin/[email protected]: Get "https://proxy.golang.org/github.com/yuin/goldmark/@v/v1.1.32.mod": dial tcp 34.64.4.113: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.
...
问题原因:
由于墙的原因,无法直接更新。
解决方案(亲测有效):
通过设置 GOPRIVATE 环境变量来控制哪些私有仓库和依赖(公司内部仓库)不通过 proxy 来拉取,直接走本地。设置如下:
go env -w GOPROXY=https://goproxy.io,direct # 设置不走 proxy 的私有仓库,多个用逗号相隔
go env -w GOPRIVATE=*.corp.example.com
配置完毕后,重新安装,成功!
部分参考: