在使用go get命令下载依赖包,比如guru时,可能会出现如下错误:

unrecognized import path "golang.org/x/tools/cmd/guru": https fetch: Get "https://golang.org/x/tools/cmd/guru?go-get=1": dial tcp 216.239.37.1:443: i/o timeout

由于众所周知的原因,我们无法直接访问golang.org。

1.手动下载

常见的golang.org/x/...包,一般在GitHub上 https://github.com/golang 都有对应的官方的镜像仓库,但有时可能无法选择指定的版本。

2.设置代理

如果有代理的话,可以设置如下环境变量:

export http_proxy=http://<proxyAddress>:<port>
export https_proxy=http://<proxyAddress>:<port>
export all_proxy=http://<proxyAddress>:<port>

3.go mod replace

Go1.11新增了go modules用于解决包依赖管理问题。可以通过它提供的别名功能来解决这个问题。

module com.example/hello
require (
    golang.org/x/net
replace (
    golang.org/x/net => github.com/golang/net

$GOPATH中module功能默认是关闭的,需要通过设置环境变量开启。 

export GO111MODULE=on

4.GOPROXY

Go1.11新增的环境变量,如果设置了此环境变量,那么在下载依赖时,会从环境变量设置的代理地址去下载。开源项目goproxyio可以帮助开发者一键构建自己的代理服务。并且提供了一个公用的代理服务https://goproxy.io。设置方法如下:

Go1.12及以下:

Bash (Linux or macOS)

# 启用 Go Modules 功能
export GO111MODULE=on
# 配置 GOPROXY 环境变量
export GOPROXY=https://goproxy.io

PowerShell (Windows)

# 启用 Go Modules 功能
$env:GO111MODULE="on"
# 配置 GOPROXY 环境变量
$env:GOPROXY="https://goproxy.io"

Go1.13及以上

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
# 设置不走 proxy 的私有仓库,多个用逗号相隔(可选)
go env -w GOPRIVATE=*.corp.example.com
# 设置不走 proxy 的私有组织(可选)
go env -w GOPRIVATE=example.com/org_nam
unrecognized import path "golang.org/x/sys/unix": https fetch: Get "https://golang.org/x/sys/unix?go .... i/o timeout) 这个异常的原因是因为某些特殊原因, 我们无法下载墙外的依赖, 所以我们需要去代理服务器进行下载 1. 设置GOPROXY环境变量 修改/etc/profile文件 export GOPROXY=https://goproxy.io/zh/ [root@es1 ~]# docker pull scratch Using default tag: latest Error response from daemon: ‘scratch’ is a reserved name 此报错为正常, scratch 为一个空镜像,是所有镜像的基础 报错2 如下: [root@es1 ~]# docker pull busybox Using default tag: latest Trying to pull repository docker.io/library/busybox … Get https://registry- # Syncthing syncthing_transfer # graph the total in/out bits syncthing_cpu # graph the cpu percentage used syncthing_goroutine # graph the number of go routines used syncthing_mem # graph the amount of memory allocated and obtained from the system syncthing_uptime # graph the uptime 配置 GOPROXY 环境变量 export GOPROXY=https://proxy.golang.com.cn,direct 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选) export GOPRIVATE=git.mycompany.com,github.com/my/private 使用 go get -u golang.org/x/tour/pic 遇到报错: package golang.org/x/tour/pic: unrecognized import path "golang.org/x/tour/pic": https fetch: Get "https://golang.org/x/tour/pic?go-get=1": dial tcp 216.239.37.1:443: i/o timeout 网络问题,golang.org需要科学上网才能访问 go mod 是 golang 1.12 推出的包管理工具,具有包依赖管理、版本管理、打包等功能,类似于 Java 生态中的 Maven (不得不说 Maven 真好用)。在使用的过程中遇到了 go mod download 时的 dial tcp 34.64.4.17:443: i/o timeout 问题,这里记录下解决的过程。 dial tcp: lookup production.cloudflare.docker.com on 114.114.114.114:53: read udp 192.168.1.117:25690->114.114.114.114:53: i/o timeout 真的是莫名其妙啊!各大站点逛遍也没有找到一个明确的解释!很寒心啊! 正常使用go get获取安装包时候因为一些原因,导致经常出现超时的问题,特别,go在后面的版本(go.1.13)增加了一个功能,即可以使用代理获取package。 将go升级到1.13以上版本。点击下载Go Linux、Mac # 配置 GOPROXY 环境变量 export GOPROXY=https://goproxy.io,direct # 设置你的 bash 环境变量 echo "export GOPROXY=https://goproxy.io,direct" &