Go 修改ENV代理GoProxy解决运行官方样例代码无法获得module的问题

按照官方向导的样例代码,运行rsc.io/quote的部分 no required module provides package rsc.io/quote; to add it:,出现了我发找到所需模块mudule的报错 ,从mod tidy输出也是报无法连接 A connection attempt failed because the connected party did not properly respond after a period of time。可以通过修改代理GoProxy来解决。

报错样例

go mod tidy

报错为A connection attempt failed because the connected party did not properly respond after a period of time

E:\go\hello>go mod tidy
go: finding module for package rsc.io/quote
example/hello imports
        rsc.io/quote: module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 142.251.43.17: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 run .

报错信息为no required module provides package rsc.io/quote; to add it:

E:\go\hello>go run .
hello.go:3:8: no required module provides package rsc.io/quote; to add it:
        go get rsc.io/quote

go get rsc.io/quote

报错信息为 A connection attempt failed because the connected party did not properly respond after a period of time,

E:\go\hello>go get rsc.io/quote
go: module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 142.251.43.17: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.

解决

通过env 修改代理服务器GoProxy

E:\go\hello>go env -w GOPROXY=https://goproxy.cn

获得module

E:\go\hello>go mod tidy
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c

运行

E:\go\hello>go run .
Hello world!
Don't communicate by sharing memory, share memory by communicating.

E:\go\hello>