英文:
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
<a href="http://godoc.org/robpike.io/ivy">Found</a>
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 'https://robpike.io/ivy?go-get=1'
returns an HTML document containing the tag
<meta name="go-import" content="robpike.io/ivy git https://github.com/robpike/ivy.git">
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 -->
<meta name="go-import" content="robpike.io/toy git https://github.com/robpike/toy.git"><meta name="go-import" content="robpike.io/cmd/translate git https://github.com/robpike/translate.git"><meta name="go-import" content="robpike.io/cmd/freq git https://github.com/robpike/freq.git"><meta name="go-import" content="robpike.io/cmd/hira git https://github.com/robpike/hira.git"><meta name="go-import" content="robpike.io/cmd/kana git https://github.com/robpike/kana.git"><meta name="go-import" content="robpike.io/cmd/kata git https://github.com/robpike/kata.git"><meta name="go-import" content="robpike.io/nihongo git https://github.com/robpike/nihongo.git"><meta name="go-import" content="robpike.io/cmd/typo git https://github.com/robpike/typo.git"><meta name="go-import" content="robpike.io/filter git https://github.com/robpike/filter.git"><meta name="go-import" content="robpike.io/cmd/unicode git https://github.com/robpike/unicode.git"><meta name="go-import" content="robpike.io/cmd/doc git https://github.com/robpike/doc.git"><meta name="go-import" content="robpike.io/cmd/scrub git https://github.com/robpike/scrub.git"><meta name="go-import" content="robpike.io/cmd/strings git https://github.com/robpike/strings.git"><meta name="go-import" content="robpike.io/ivy git https://github.com/robpike/ivy.git"><meta name="go-import" content="robpike.io/cmd/now git https://github.com/robpike/now.git">
这使得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 -->
<meta name="go-import" content="robpike.io/toy git https://github.com/robpike/toy.git"><meta name="go-import" content="robpike.io/cmd/translate git https://github.com/robpike/translate.git"><meta name="go-import" content="robpike.io/cmd/freq git https://github.com/robpike/freq.git"><meta name="go-import" content="robpike.io/cmd/hira git https://github.com/robpike/hira.git"><meta name="go-import" content="robpike.io/cmd/kana git https://github.com/robpike/kana.git"><meta name="go-import" content="robpike.io/cmd/kata git https://github.com/robpike/kata.git"><meta name="go-import" content="robpike.io/nihongo git https://github.com/robpike/nihongo.git"><meta name="go-import" content="robpike.io/cmd/typo git https://github.com/robpike/typo.git"><meta name="go-import" content="robpike.io/filter git https://github.com/robpike/filter.git"><meta name="go-import" content="robpike.io/cmd/unicode git https://github.com/robpike/unicode.git"><meta name="go-import" content="robpike.io/cmd/doc git https://github.com/robpike/doc.git"><meta name="go-import" content="robpike.io/cmd/scrub git https://github.com/robpike/scrub.git"><meta name="go-import" content="robpike.io/cmd/strings git https://github.com/robpike/strings.git"><meta name="go-import" content="robpike.io/ivy git https://github.com/robpike/ivy.git"><meta name="go-import" content="robpike.io/cmd/now git https://github.com/robpike/now.git">
Ths allows the go get command to resolve the vanity path to git repositories.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论