others-How to solve 'verifying module and dial tcp i/o timeout' issue when using go get in golang applications?

1. Purpose

In this post, I would demo how to solve the below issue when using go get . to install dependencies in golang application.

$  go get .
go: finding module for package gopkg.in/yaml.v3
go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
go: finding module for package gopkg.in/yaml.v3
person.go:5:2: gopkg.in/[email protected]: verifying module: gopkg.in/[email protected]: Get "https://sum.golang.org/lookup/gopkg.in/[email protected]": dial tcp 142.250.199.81:443: i/o timeout

The packages imported in source code:

import (
   "fmt"
   "gopkg.in/yaml.v3"
   "io/ioutil"
   "os"
)

But the package gopkg.in/yaml.v3 can not be resolved:

image-20210720084829628

Why did this happen?

2. Environment

  • go
    • go version go1.14.2 darwin/amd64
  • goland
    • 2020.3

3. The solution and solution

3.2 The solution

Let’d debug the problem.

First, let’s check the proxy settings in the environment:

$ go env|grep goproxy   
$

You can see that we have NOT set any proxy in our environment.

Then check the goproxy settings in goland:

image-20210720090308919

I think the key point is that we should set a proxy for our go develop environment.

image-20210720090636192

Lastly, we do the test again:

$ go get .   
go: finding module for package gopkg.in/yaml.v3
go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
go: found gopkg.in/yaml.v3 in gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

github.com/goinaction/study/hello51

And now the packages are all parsed correctly.

image-20210720092209495

It works!

3.3 The theory

According to faith’s article:

By default the go command downloads modules from VCS’s directly, e.g. github.com. The GOPROXY environment variable allows further control over the download source. The environment variable configures the go command to use a Go module proxy.

To start using a Go module proxy, we need to set the GOPROXY environment variable to a compatible Go module proxy. There are multiple ways:

1.) If GOPROXY is unset, empty or set to direct then go get will use a direct connection to the VCS (e.g github.com):

GOPROXY=""
GOPROXY=direct

It can be also set to off, which means no network use is allowed.

GOPROXY=off

2.) You can start using a public Go proxy. One of your options is to use the Go proxy from the Go team (which is run by Google). More information can be found here: https://proxy.golang.org/

To start using it, all you have is to set the environment variable:

GOPROXY=https://proxy.golang.org

Other public proxies are:

GOPROXY=https://goproxy.io
GOPROXY=https://goproxy.cn # proxy.golang.org is blocked in China, this proxy is not

3.) You can run several open source implementations and host it yourself. Some of these are:

  • goproxy: https://github.com/goproxy/goproxy

You need to maintain these yourself. It’s up to you if you want to serve it over the public internet or on your internal network.

4.) You can pass a file:/// URL. Because a Go module proxy is a web server that responds to GET requests (with no query parameters), a folder in any filesystem can be also used to serve as a Go module proxy.

4. Summary

In this post, I tried to demo how to verifying module and dial tcp i/o timeout issue when using go get in golang applications , the key point is to set a goproxy for our developement environment.