Problem description
When we use go get to obtain packages, connection refused will often occur.
go get -u github.com/onsi/gomega
Copy the code
After waiting, we get the following error:
go get: module github.com/onsi/gomega: Get "https://proxy.golang.org/github.com/onsi/gomega/@v/list": dial tcp 172.217.27.145:443: connect: connection refused
Copy the code
The solution
Set the GOPROXY environment variable
The default environment variables can be checked with the go env directive:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build525051245=/tmp/go-build -gno-record-gcc-switches"
Copy the code
You can see a GOPROXY environment variable, which is normally used by Go Get to access Github to complete the package download. How to use it? Fortunately, goproxy. IO, an open source project, provides a free proxy for us. Its official website provides configuration methods for different platforms.
-
Set environment variables by using export
export GOPROXY=https://goproxy.io,direct Copy the code
However, those familiar with Linux know that the environment variable set through export will become invalid when we log in to the terminal again. In order to solve the problem once and for all, it is recommended to set the environment variable in a way that takes effect for a long time
-
Set environment variables with go env
go env -w GOPROXY=https://goproxy.io,direct Copy the code
In this way, environment variables can be written to the go configuration file for longevity
Setting the System Agent
When it comes to network problems, it is natural to think of four big words – set proxy. In the previous method, we solved the network access problem by configuring proxy for go itself.
We can also solve this problem by setting up the system agent.
export http_proxy=http://proxyAddress:port
export https_proxy=http://proxyAddress:port
Copy the code
The final result
Configure the proxy to solve the problem that Go Get cannot download dependencies for the project.
# go get -u github.com/onsi/gomegaGo: downloading github.com/onsi/gomega v1.13.0 go: downloading golang.org/x/net v0.0.0-20210428140749-89ef3D95e781 go: In/YAMl. V2 v2.4.0 GO: downloading golang.org/x/net v0.0.0-20210614182718-04defd469f4e go: Downloading golang.org/x/text v0.3.6Copy the code