工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆

go get 获得 golang.org 的项目

 go get 用来动态获取远程代码包的,目前支持的有BitBucket、GitHub、Google Code和Launchpad。这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行go install。下载源码包的go工具会自动根据不同的域名调用不同的源码工具,对应关系如下:

BitBucket (Mercurial Git) 
GitHub (Git) 
Google Code Project Hosting (Git, Mercurial, Subversion) 
Launchpad (Bazaar)

go get 的参数说明:

-d 只下载不安装 
-f 只有在你包含了-u参数的时候才有效,不让-u去验证import中的每一个都已经获取了,这对于本地fork的包特别有用 
-fix 在获取源码之后先运行fix,然后再去做其他的事情 
-t 同时也下载需要为运行测试所需要的包 
-u 强制使用网络去更新包和它的依赖包 
-v 显示执行的命令 
注意,这里的 –v 参数对我们分析问题很有帮助。

参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.3.md

 

国内由于墙,我们会收到 unrecognized import path 的错误,这时候我们如何通过命令行来执行 go get 呢? 
这时我们会获得类似如下错误:

go get -u -v golang.org/x/oauth2 
Fetching https://golang.org/x/oauth2?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2": https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
package golang.org/x/oauth2: unrecognized import path "golang.org/x/oauth2" 
localhost:~ ghj1976$

如果目录下有以前的版本,则是如下情况:

go get -u -v golang.org/x/oauth2 
Fetching https://golang.org/x/oauth2?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2": https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/oauth2 (download) 
Fetching https://golang.org/x/net/context?go-get=1 
https fetch failed. 
import "golang.org/x/net/context": https fetch: Get https://golang.org/x/net/context?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/net (download) 
Fetching https://golang.org/x/oauth2/internal?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2/internal": https fetch: Get https://golang.org/x/oauth2/internal?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/net/context 
golang.org/x/oauth2/internal 
golang.org/x/oauth2 
localhost:~ ghj1976$

 

这时候我们需要设置代理。代理工具我推荐用 lantern https://github.com/getlantern/lantern

需要注意的是,它的代理地址是: http://127.0.0.1:8787   而不是 http://127.0.0.1:16823/ ,后一个是它的配置网站地址。

 

以mac为例, 在命令行 Terminal 中设置网络代理,一般方法如下:

root@ed27c545f7af:~# cat ~/proxy.conf  export http_proxy=http://172.17.42.1:8118 export https_proxy=$http_proxy export ftp_proxy=$http_proxy export rsync_proxy=$http_proxy export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

参考:https://github.com/tools/godep/issues/154

win下 用 set 代理 export ,参考 https://groups.google.com/forum/#!topic/lantern-users-zh/FiywFrEHSHE 

删除环境变量用

删除:unset 变量名    参考 http://blog.csdn.net/debug_cpp/article/details/2679991 

当前系统上下文的环境设置可以用 env 命令查看。

image

https://code.google.com/p/go/issues/detail?id=2919

这步代理设置后,我们可以用 wget 命令去试验效果。参考: https://github.com/getlantern/lantern/issues/3341 

 

 

另外,go get 使用的 git 、mercurial、svn 设置代理的方法请参考:

https://github.com/golang/go/wiki/GoGetProxyConfig 

以我们最常用的 git 为例,

在终端设置: 
git config --global http.proxy http://127.0.0.1:1080 
git config --global https.proxy https://127.0.0.1:1080

默认不设置代理: 
git config --global --unset http.proxy 
git config --global --unset https.proxy

查看已经设置的值:

git config http.proxy

 

参考: http://blog.csdn.net/dengbin9009/article/details/38058153 

 

配置完成后,以下载 golang.org/x/net 为例,执行的返回值如下:

go get -u -v  golang.org/x/net 
Fetching https://golang.org/x/net?go-get=1 
Parsing meta tags from https://golang.org/x/net?go-get=1 (status code 200) 
get "golang.org/x/net": found meta tag main.metaImport{Prefix:"golang.org/x/net", VCS:"git", RepoRoot:"https://go.googlesource.com/net"} athttps://golang.org/x/net?go-get=1 
golang.org/x/net (download) 
package golang.org/x/net: no buildable Go source files in /Users/ghj1976/project/mygocode/src/golang.org/x/net 
localhost:text ghj1976$

 

我们可以看到其实是到 https://go.googlesource.com/text/ 这样的地址去下载源码的。中间涉及到跳转和git下载,所以 要注意, 网络请求的 http_proxy  和 git 的 代理 都需要设置才可以。

-----------------

命令行设置代理,确保可以下载的命令如下:

export http_proxy=http://127.0.0.1:8787 

git config --global http.proxy http://127.0.0.1:8787 
git config --global https.proxy https://127.0.0.1:8787

 

--------

go get -u golang.org/x/mobile

取消设置

unset http_proxy 
git config --global --unset http.proxy 
git config --global --unset https.proxy

------------

查看设置的变量

echo $http_proxy
git config http.proxy
git config https.proxy

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):