SoFunction
Updated on 2025-03-05

GOPROXY: Solve the problem of go get /x package failure

Problem phenomenon

When we usego getgo installgo modWhen waiting for commands, the corresponding package or dependency package will be automatically downloaded.

But for well-known reasons, similar to/x/...The package will fail to download.

As shown below:

package /x/tools/cmd/goimports/...: unrecognized import path "/x/tools/cmd/goimports" 
(https fetch: Get /x/tools/cmd/goimports?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.)

Problem solving

GOPROXY

We know fromGo 1.11The version has started, and the official support isgo modulePackage dependency management tools.

Actually, it has been addedGOPROXYEnvironment variables.

If this variable is set, the proxy address will be set through this environment variable when downloading the source code, and it will no longer be downloaded directly from the code base as before.

This is undoubtedly the greatest blessing for us, such as the good people who cannot access the Internet scientifically.

What’s even more gratifying is that this open source project helped us realize what we wanted.

This project allows developers to build their own one-clickGOPROXYAgent service.

At the same time, public agency services are also provided., we only need to set this environment variable to download the source code package that is blocked normally:

Go version>=1.11 Set GOPROXY

On Linux or macOS, you need to run the following command:

Bash/ Copy

# Enable Go Modulesexport GO111MODULE=on
# Configure GOPROXY environment variablesexport GOPROXY=

Or, you can write the above command to.bashrcor.bash_profilein the file.

On Windows, you need to run the following command:

PowerShell  Copy

# Enable Go Modules$env:GO111MODULE="on"
# Configure GOPROXY environment variables$env:GOPROXY=""

Now, when you build or run your app, Go will get the dependencies through . For more information, please checkgoproxy warehouse

Go version>=1.13 SettingsGOPROXY

If you are using Go version >=1.13, you can control which private repositories and dependencies (company internal repositories) are not pulled through proxy, and go directly to the local area. The settings are as follows:

Go version >= 1.13 Copy

go env -w GOPROXY=,direct
# Set up a private repository that does not go through proxy, multiples are separated by commasgo env -w GOPRIVATE=*.

Qiniu Cloud

An open source solution is provided that allows developers to build their own GOPROXY proxy services in one click.

At the same time, public proxy services are also provided.Qiniu Cloud has also launched a domestic agent to facilitate faster access by domestic users

$ go env -w GO111MODULE=on
            
$ go env -w GOPROXY=,direct

Alibaba Cloud Go Module Agent

go module public proxy repository, proxy and cache go modules.

You can use this proxy to avoid slow or failed module pulling caused by DNS pollution, and speed up your build

address

/goproxy/

use

1. Use go1.11 or above and enable the go module mechanism

2. Export GOPROXY environment variables

export GOPROXY=/goproxy/

Reference link:

/goproxy/

/zh/

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.