国内由于GFW和网速比较慢的原因,部分网站无法直接访问。但是golang学习者从部分技术网站下载代码进行学习又是必不可少的。

1 问题描述
go getgo installgo modgo get golang.org/x/net
2 问题解决
go moduleGOPROXYGOPROXYGOPROXYGOPRIVATE
3 设置方法
  1. Linux, MacOS
  • 当前terminal生效
# 启用Go module功能
export GO111MODULE=on
# 配置GOPROXY环境变量 export GOPROXY={proxy_url}
export GOPROXY=https://goproxy.io,direct
# Set environment variable allow bypassing the proxy for specified repos (optional if Go version >=1.13)
export GOPRIVATE=git.mycompany.com,github.com/my/private

或者:

$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile
~/.bashrc~/.bash_profile~/.bash_profile~/.bash_login~/.profile/etc/profile/etc/bashrc
  1. Windows
    PowerShell中运行以下命令:
# 启用go module功能
$env:GO111MODULE="on"
# 配置GOPROXY环境变量$env:GOPROXY="{proxy_url}"
$env:GOPROXY = "https://goproxy.io,direct"
# Set environment variable allow bypassing the proxy for specified repos (optional if Go version >=1.13)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"
4 可用的代理
  • Linux or macOS
    Bash
# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io,direct
# Set environment variable allow bypassing the proxy for specified repos (optional if Go version >=1.13)
export GOPRIVATE=git.mycompany.com,github.com/my/private
  • Windows
    PowerShell
# Set the GOPROXY environment variable
$env:GOPROXY = "https://goproxy.io,direct"
# Set environment variable allow bypassing the proxy for specified repos (optional if Go version >=1.13)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"
4 参考