当给出一个未知的代码托管域名时,’go get’ 如何工作?

huangapple go评论87阅读模式
英文:

How does 'go get' work when given a not known code hosting domain site?

问题

让我们举个例子。下面的命令将执行以下操作:

go get robpike.io/ivy

这将获取位于$GOPATH/src下的存储库的内容。太好了!

现在,它是如何工作的呢?

首先,robpike.io/ivy会回复一个HTTP重定向:

HTTP/1.1 302 Found

<a href="http://godoc.org/robpike.io/ivy">Found</a>

通过阅读go help importpath,我了解到:

如果导入路径不是已知的代码托管站点,也没有版本控制限定符,go工具将尝试通过https/http获取导入,并在文档的HTML中查找标签。

然而,使用以下命令在重定向页面的内容中查找元标签:

curl -D --raw https://godoc.org/robpike.io/ivy | grep go-import

没有返回任何结果。

进一步阅读:

repo-root是包含方案但不包含.vcs限定符的版本控制系统的根目录。

例如,

import "example.org/pkg/foo"

将导致以下请求:

https://example.org/pkg/foo?go-get=1(首选)

http://example.org/pkg/foo?go-get=1(回退,仅在使用-insecure选项时)

同样地:

curl -D --raw https://robpike.io/ivy?go-get=1

没有返回任何结果。

所以问题是:我如何像Rob Pike先生一样,使用自己的站点和go get命令执行相同的操作?

英文:

Let's take an example. The command below will do:

go get robpike.io/ivy

This will get me the content of the repository under $GOPATH/src. Great!

Now, how does it work?

First, robpike.io/ivy replies with a HTTP-redirect:

HTTP/1.1 302 Found

&lt;a href=&quot;http://godoc.org/robpike.io/ivy&quot;&gt;Found&lt;/a&gt;

From reading at go help importpath, I learn that:

> If the import path is not a known code hosting site and also lacks a
> version control qualifier, the go tool attempts to fetch the import
> over https/http and looks for a <meta> tag in the document's HTML

However, looking for a metatag inside the content of the redirected page using:

curl -D --raw https://godoc.org/robpike.io/ivy | grep go-import

returns nothing.

Reading further:

> The repo-root is the root of the version control system containing a
> scheme and not containing a .vcs qualifier.
>
> For example,
>
> import "example.org/pkg/foo"
>
> will result in the following requests:
>
> https://example.org/pkg/foo?go-get=1 (preferred)
>
> http://example.org/pkg/foo?go-get=1 (fallback, only with -insecure)

Again:

curl -D --raw https://robpike.io/ivy?go-get=1

returns nothing.

So the question is: how can I do the same thing as Mr. Rob Pike and use my own site with the go get command?

答案1

得分: 2

命令curl -D --raw 'https://robpike.io/ivy?go-get=1'返回一个包含以下标签的HTML文档:

<meta name="go-import" content="robpike.io/ivy git https://github.com/robpike/ivy.git">

go get命令使用这个meta标签来将虚拟导入路径解析为实际的git仓库。你也可以使用相同的方法。

英文:

The command curl -D --raw &#39;https://robpike.io/ivy?go-get=1&#39; returns an HTML document containing the tag

&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/ivy git https://github.com/robpike/ivy.git&quot;&gt;

The go get command uses this meta tag to resolve the vanity import path to an actual git repository. You can do the same.

答案2

得分: 2

你输入的最后一个命令确实返回了数据。当我在终端中运行curl -D --raw https://robpike.io/ivy\?go-get\=1时,我得到了以下数据:

<!-- language: lang-html -->

&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/toy git https://github.com/robpike/toy.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/translate git https://github.com/robpike/translate.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/freq git https://github.com/robpike/freq.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/hira git https://github.com/robpike/hira.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/kana git https://github.com/robpike/kana.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/kata git https://github.com/robpike/kata.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/nihongo git https://github.com/robpike/nihongo.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/typo git https://github.com/robpike/typo.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/filter git https://github.com/robpike/filter.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/unicode git https://github.com/robpike/unicode.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/doc git https://github.com/robpike/doc.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/scrub git https://github.com/robpike/scrub.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/strings git https://github.com/robpike/strings.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/ivy git https://github.com/robpike/ivy.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/now git https://github.com/robpike/now.git&quot;&gt;

这使得go get命令能够将虚拟路径解析为git仓库。

英文:

That last command you entered does return data. When I run curl -D --raw https://robpike.io/ivy\?go-get\=1 in my terminal, I get the following data back:

<!-- language: lang-html -->

&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/toy git https://github.com/robpike/toy.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/translate git https://github.com/robpike/translate.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/freq git https://github.com/robpike/freq.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/hira git https://github.com/robpike/hira.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/kana git https://github.com/robpike/kana.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/kata git https://github.com/robpike/kata.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/nihongo git https://github.com/robpike/nihongo.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/typo git https://github.com/robpike/typo.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/filter git https://github.com/robpike/filter.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/unicode git https://github.com/robpike/unicode.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/doc git https://github.com/robpike/doc.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/scrub git https://github.com/robpike/scrub.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/strings git https://github.com/robpike/strings.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/ivy git https://github.com/robpike/ivy.git&quot;&gt;&lt;meta name=&quot;go-import&quot; content=&quot;robpike.io/cmd/now git https://github.com/robpike/now.git&quot;&gt;

Ths allows the go get command to resolve the vanity path to git repositories.

huangapple
  • 本文由 发表于 2016年3月23日 01:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/36161565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定