您的位置 首页 golang

关于 一键解决 go get golang.org/x 包失败

问题描述

当我们使用 go get、go install、go mod 等命令时,会自动下载相应的包或依赖包。但由于众所周知的原因,类似于 golang.org/x/… 的包会出现下载失败的情况。如下所示:

$ go get -u golang.org/x/sys
go get golang.org/x/sys: unrecognized import path "golang.org/x/sys" (https fetch: Get  dial tcp 216.239.37.1:443: i/o timeout)
 

解决方式

那我们该如何解决问题呢?毕竟还要制造 bug 的嘛~

手动下载

我们常见的 golang.org/x/… 包,一般在 git Hub 上都有官方的镜像仓库对应。比如 golang.org/x/text 对应 github.com/golang/text。所以,我们可以手动下载或 clone 对应的 GitHub 仓库到指定的目录下。

 mkdir  $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone git@github.com:golang/text.git
rm -rf text/.git
 

当如果需要指定版本的时候,该方法就无解了,因为 GitHub 上的镜像仓库多数都没有 tag。并且,手动嘛,程序员怎么能干呢,尤其是依赖的依赖,太多了。

设置代理

如果你 有代理 ,那么可以设置对应的 环境变量

export http_proxy=
export https_proxy=
 

或者,直接用 all_proxy:

export all_proxy=
 

go mod replace

从 Go 1.11 版本开始,新增支持了 go modules 用于解决包依赖管理问题。该工具提供了 replace,就是为了解决包的别名问题,也能替我们解决 golang.org/x 无法下载的的问题。

go module 被集成到原生的 go mod 命令中,但是如果你的代码库在 $GOPATH 中,module 功能是默认不会开启的,想要开启也非常简单,通过一个环境变量即可开启 export GO111MODULE=on。

以下为参考示例:

module example.com/hello
require (
 golang.org/x/text v0.3.0
)
replace (
 golang.org/x/text => github.com/golang/text v0.3.0
)
 

类似的还有 glide、gopm 等这类第三方包管理工具,都不同方式的解决方案提供给我们。

GOPROXY 环境变量

终于到了本文的终极大杀器 —— GOPROXY

我们知道从 Go 1.11 版本开始,官方支持了 go module 包依赖管理工具。

其实还新增了 GOPROXY 环境变量。如果设置了该变量,下载源代码时将会通过这个环境变量设置的代理地址,而不再是以前的直接从代码库下载。这无疑对我等无法科学上网的开发良民来说是最大的福音。

更可喜的是,goproxy.io 这个 开源项目 帮我们实现好了我们想要的。该项目允许开发者一键构建自己的 GOPROXY 代理服务。同时,也提供了公用的代理服务 ,我们只需设置该环境变量即可正常下载被墙的源码包了:

export GOPROXY=
 

不过, 需要依赖于 go module 功能 。可通过 export GO111MODULE=on 开启 MODULE。

如果项目不在 GOPATH 中,则无法使用 go get …,但可以使用 go mod … 相关命令。

也可以通过置空这个环境变量来关闭,export GOPROXY=。

对于 Windows 用户,可以在 PowerShell 中设置:

$env:GOPROXY = ""
 

最后,我们当然推荐使用 GOPROXY 这个环境变量的解决方式,前提是 Go version >= 1.11

参考资料

  • goproxy.io for Go modules()
  • goproxy.io()

文章来源:智云一二三科技

文章标题:关于 一键解决 go get golang.org/x 包失败

文章地址:https://www.zhihuclub.com/98012.shtml

关于作者: 智云科技

热门文章

评论已关闭

41条评论

  1. Postato da Marco Signore Google il Marted 30 agosto 2022 Our online medicinestore offers Tadalista at a very affordable rate

  2. Always contact your physician or fertility specialist if you feel that something isn t right.

  3. My husband had the fertility issue and sperm was taken via direct removal with a needle not masturbation.

  4. acetate zyrexin review So yes, I too, am so shallow that I wouldn t use a practical, economical and environmentally friendly product because I don t identify with it on a personal level and to a greater or lesser extent, so are you

  5. More recently, one lipid associated biomarker, lipoprotein associated phospholipase A2 Lp PLA2, has gained considerable interest If you have this disorder

  6. 162 Type of trauma 4 Minor 1 celecoxib will increase the level or effect of promethazine by affecting hepatic enzyme CYP2D6 metabolism

  7. Charoenchokthavee W, Areepium N, Panomvana D, Sriuranpong V 2017 Effects of CYP2D6 and CYP3A5 polymorphisms on tamoxifen and its metabolites in Thai breast cancer patients 3 Per Pill 37

  8. o n 4 mice in both groups; P 0 The CDC added Georgia, Connecticut and New Jersey to the list of affected states

  9. Nutrition problems are likely when tumors involve the head, neck, esophagus, stomach, intestines, pancreas, or liver

  10. 2021 Dec 1; 22 12 3921 3925 tagonist of the nuclear estrogen receptor, is also able to antagonize the effect of estradiol on its membrane receptor as shown by our group 50 and by others 51, 52

  11. The estrogen stimulates growth of the uterus but menarche does not occur until the estrogens have stimulated sufficient uterine growth such that their withdrawal causes the first menstruation

  12. As scientists improve methods of prevention, early detection, and treatment, growing numbers of women are living with breast cancer for longer periods of time cimetidine will increase the level or effect of tizanidine by affecting hepatic enzyme CYP1A2 metabolism

  13. One other important factor to remember is that of hepatotoxicity of the compound, where hepatotoxicity is increased in relation to increasing doses

  14. They go by many names and have a variety of purposes mainly to supplement your eye s ability to produce fluids to moisturize and relieve your eyes of irritations

  15. Combining this steroid with Anavar enhances its anabolic effect, whilst increasing lipolysis fat loss

  16. OHSU Casey Eye Institute doctors recently compared the chance of complications from contact lenses with the risks of vision correction surgery

  17. The treatment pathways listed below are simplified and don t represent all treatment possibilities

  18. com Credit Suisse leads the first lien loan joined by RBC, Deutsche Bank, Goldman Sachs and Morgan Stanley Outcomes are often less favorable for traumatic cases involving gram negative organisms

  19. These results confirm that ER positive breast cancer cells are more sensitive 4 to greater than 75 fold than ER negative breast cells to the growth inhibitory effects of tamoxifen and demonstrate that, in all ER positive cells, growth inhibition and cytotoxicity are accompanied by characteristic changes in cell cycle kinetic parameters

  20. They consist of a multiple repeat N terminal domain fused to an active site containing a C terminal substrate peptidoglycan specific domain

  21. Subsequent modifications of the culture system resulted in the maintenance of high levels of testosterone production by purified adult rat Leydig cells for up to 3 days 44, 45

网站地图