question
When using the go get command to download dependency packages, such as guru, the following error may occur:
unrecognized import path "/x/tools/cmd/guru": https fetch: Get "/x/tools/cmd/guru?go-get=1": dial tcp 216.239.37.1:443: i/o timeout
We cannot access directly for well-known reasons.
Solution
1. Manual download
Common /x/... packages, usually on GitHub/golangThere are corresponding official mirror repositories, but sometimes the specified version may not be selected.
2. Set up a proxy
If there is a proxy, you can set the following environment variables:
export http_proxy=http://<proxyAddress>:<port>
export https_proxy=http://<proxyAddress>:<port>
or
export all_proxy=http://<proxyAddress>:<port>
mod replace
Go1.11 has added go modules to solve the problem of package dependency management. This problem can be solved by the alias function it provides.
module /hello
require (
/x/net
)
replace (
/x/net => /golang/net
)
The module function in $GOPATH is turned off by default and needs to be turned on by setting environment variables.
export GO111MODULE=on
If this environment variable is set in Go1.11, when downloading dependencies, it will be downloaded from the proxy address set by the environment variable. Open Source ProjectgoproxyioIt can help developers build their own proxy services with one click. It also provides a public proxy service. The setup method is as follows:
Go1.12 and below:
Bash (Linux or macOS)
# Enable Go Modules
export GO111MODULE=on
# Configure GOPROXY environment variables
export GOPROXY=
PowerShell (Windows)
# Enable Go Modules
$env:GO111MODULE="on"
# Configure GOPROXY environment variables
$env:GOPROXY=""
Go1.13 and above
go env -w GO111MODULE=on
go env -w GOPROXY=,direct
# Set up a private repository that does not go through proxy, multiples are separated by commas (optional)
go env -w GOPRIVATE=*.
# Set up a private organization that does not go through proxy (optional)
go env -w GOPRIVATE=/org_nam