我们知道从 Go 1.11 版本开始,官方支持了 go module 包依赖管理工具。
其实不仅如此, Go 1.11 还新增了 GOPROXY 环境变量。如果设置了该变量,下载源代码时将会通过这个环境变量设置的代理地址。
更可喜的是,goproxy.io 提供了一个开源的解决方案允许开发者一键构建自己的 GOPROXY 代理服务。同时,也提供了公用的代理服务 https://goproxy.io,而七牛云也出了个国内代理 goproxy.cn 方便国内用户更快的访问。
最终的使用效果如下:
使用前:
$ go get -u -v golang.org/x/tools/go/packages
package golang.org/x/tools/go/packages: unrecognized import path "golang.org/x/tools/go/packages" (https fetch: Get https://golang.org/x/tools/go/packages?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.)
使用后:
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
warning: go env -w GOPROXY=... does not override conflicting OS environment variable
$ go get -u -v github.com/securego/gosec/cmd/gosec
go: finding github.com/securego/gosec latest
go: downloading github.com/securego/gosec v0.0.0-20191217083152-cb4f343eaff1
go: extracting github.com/securego/gosec v0.0.0-20191217083152-cb4f343eaff1
go: downloading gopkg.in/yaml.v2 v2.2.7
go: extracting gopkg.in/yaml.v2 v2.2.7
go: finding gopkg.in/yaml.v2 v2.2.7
go: finding golang.org/x/tools latest
go: finding github.com/nbutton23/zxcvbn-go latest
github.com/nbutton23/zxcvbn-go/match
github.com/nbutton23/zxcvbn-go/utils/math
gopkg.in/yaml.v2
github.com/securego/gosec
github.com/nbutton23/zxcvbn-go/data
github.com/nbutton23/zxcvbn-go/frequency
github.com/nbutton23/zxcvbn-go/adjacency
github.com/nbutton23/zxcvbn-go/entropy
github.com/securego/gosec/output
github.com/nbutton23/zxcvbn-go/scoring
github.com/nbutton23/zxcvbn-go/matching
github.com/nbutton23/zxcvbn-go
github.com/securego/gosec/rules
github.com/securego/gosec/cmd/gosec