给定未知的代码托管域网站时,“获取”如何工作?

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 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?

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:

<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.

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.