英文:
How does "go get" get to know what version control system to use with remote repos?
问题
我是新手学习golang。我了解到go get
作为一个包管理器,可以与各种版本控制系统的远程仓库进行通信。
那么,给定一个包的导入路径,go get
是如何决定使用哪个版本控制系统的呢?我经常在请求中看到下面的查询字符串:
?go-get=1
所以这里似乎有一些协议/约定?
英文:
I am new to golang. I learned that go get
serves as a package manager and contact remote repos with various version control systems.
So, given a package import path, how does go get
decide which VCS to use? I always see below query string in the reqeust:
?go-get=1
So seems some protocol/convention here?
答案1
得分: 6
Go的一个好处是,所有东西都是用Go编写的,包括几乎所有的标准库和工具。
在cmd/go/vcs.go
中,我们可以看到是如何实现的,在cmd/go/alldocs.go
中有相关文档。
具体来说,看一下repoRootForImportPath
函数:
// repoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func repoRootForImportPath(importPath string, security securityMode) (*repoRoot, error) {
这个函数的作用是:
-
vcsPaths
保存了一个静态的“已知主机”列表。对于一些主机(比如GitHub),这很容易,我们可以直接设置版本控制系统(VCS)。对于其他主机(比如BitBucket),使用一个回调函数来检查URL,以确定可以使用哪个代码仓库。 -
如果第一步失败,它会尝试查看路径中的“VCS扩展”,比如
example.com/foo.git
或example.com/foo.git/dir
。 -
最后,
go get
会查找go-import
的元标签,格式如下:<meta name="go-import" content="example.com/path git https://github.com/Example/repo.git">
还可以参考parseMetaGoImports()
函数。Vanity Imports with Hugo是一个关于如何使用这个功能的好介绍。
没有真正的“自动检测”机制。所以如果你的代码仓库位于https://example.com/stuff
,那么go get example.com/stuff
将不会起作用。你需要使用扩展或元标签。
添加go-get=1
参数是为了让网站构建者知道这是一个来自go get
的请求,这在某些情况下可能很有用。
英文:
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
orexample.com/foo.git/dir
. -
And finally
go get
will look for ago-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论