One of the nice things about Go is that everything is in, well, Go :-) Including almost all of the standard library and tools.
In cmd/go/vcs.go
we can see how this is done, and in cmd/go/alldocs.go
it is documented.
Specifically, look at repoRootForImportPath
:
// repoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func repoRootForImportPath(importPath string, security securityMode) (*repoRoot, error) {
What this does is:
vcsPaths
holds a static lists of "known hosts". For some (e.g. GitHub) this is easy and we can just set the VCS. For others (e.g. BitBucket) a callback function is used which examines the URL to see which repo can be used.
If that fails, it tries to look at a "VCS extension" in the path, such as example.com/foo.git
or example.com/foo.git/dir
.
-
And finally go get
will look for a go-import
meta tag, which looks like:
<meta name="go-import" content="example.com/path git https://github.com/Example/repo.git">
Also see parseMetaGoImports()
. Vanity Imports with Hugo is a nice introduction on how to use this.
There is no real "auto-detection" mechanism. So if you repo lives at https://example.com/stuff
then go get example.com/stuff
will not work. You need the extension or meta tags.
The go-get=1
parameter is added so that it's easy for website builders to see that this is a request from go get
, which may be useful in some cases.