英文:
How to get another branch instead of default branch with go get
问题
我有两个代码仓库,假设它们是repo_a和repo_b。我在repo_b中导入了repo_a。
当我运行go get命令时,它会获取repo_a的主分支。有没有办法使用go get或其他命令从repo_b获取develop分支?
我不想在每个特定的包(在这种情况下是repo_a)上执行git pull命令。
英文:
I have 2 repositories. Let say them repo_a and repo_b. I imported repo_a in repo_b
When I ran go get, it will get repo_a master branch.
Is there any way to get develop branch using go get or another command from repo_b?
I do not want to git pull on each specific package (in this case repo_a)
答案1
得分: 118
从Go 1.11开始,使用Go模块可以实现这一点。在为Go模块安装依赖项时,可以指定一个模块查询,其中可以包含分支或标签名称:
$ go get <path-to-repo>@<branch>
英文:
Starting with Go 1.11, this is possible when using Go modules. When installing a dependency for a Go module, you can specify a module query which may contain a branch or tag name:
$ go get <path-to-repo>@<branch>
答案2
得分: 20
稳定的HEAD哲学
纯粹使用go get
是不可能的。
Go采取了最简约和务实的方法来管理包。Go包没有多个版本的概念。
但是这并不像一开始看起来那么糟糕,因为这背后存在着一种哲学。
作为包的作者,你必须遵循稳定的HEAD哲学。你的默认分支必须始终是稳定的、发布的版本。你必须在功能分支上进行工作,只有在准备发布时才合并。
这种方法是由于go get
的限制而被强制执行的,它应该像Python的缩进一样被视为一种语言设计所强制的哲学。
开发方法
如果你想要fork一个项目或尝试新功能,你可以克隆仓库,然后切换到所需的分支并执行go build
。这种方式不应该用于生产环境。
git clone <仓库名称>
cd <仓库名称>
git checkout <分支名称>
go build
你也可以使用第三方的包管理工具。但是大多数工具支持标签和修订版本,而不是分支(因为假设你不需要安装功能分支)。
gpm:
> 你可以使用<导入路径> <版本>
的格式指定包,其中版本可以是修订号(git/bazaar/mercurial/svn的修订哈希值)或标签。
英文:
Stable HEAD philosophy
It is not possible with pure go get
.
> Go takes the most minimal and pragmatic approach of any package manager. There is no such thing as multiple versions of a Go package.
But this is not as bad as it seems at the first view because there exists a philosophy behind this behavior.
> As a package author, you must adhere to the stable HEAD philosophy. Your default branch must always be the stable, released version of your package. You must do work in feature branches and only merge when ready to release.
This approach is forced by go get
limitations and it should be treated like Python indentations - it is kind of philosophy forced by language design.
Development approaches
If you want to fork something or try new features you can clone repo then switch to a desired branch and do go build
. This way shouldn't go to production.
git clone <repo name>
cd <repo name>
git checkout <branch name>
go build
Also you can use third party package management tools. But most of them support tags and revisions, not branches (since it is implied that you don't need to install feature branch).
gpm:
> You can specify packages with the <import path> <version> format, where version can be a revision number (a git/bazaar/mercurial/svn revision hash) or a tag.
答案3
得分: 16
你可以使用gopkg.in
,它会重定向到GitHub。
支持两种URL模式:
gopkg.in/pkg.v3 → github.com/go-pkg/pkg(分支/标签为v3、v3.N或v3.N.M)
gopkg.in/user/pkg.v3 → github.com/user/pkg (分支/标签为v3、v3.N或v3.N.M)
go get gopkg.in/pkg.v3
的意思是go get github.com/go-pkg/pkg
,但是分支或标签是v3.*
。
更多详细信息,请参见这里。
英文:
you can use gopkg.in
, it will redirect to github.
There are two URL patterns supported:
gopkg.in/pkg.v3 → github.com/go-pkg/pkg (branch/tag v3, v3.N, or v3.N.M)
gopkg.in/user/pkg.v3 → github.com/user/pkg (branch/tag v3, v3.N, or v3.N.M)
go get gopkg.in/pkg.v3
means go get github.com/go-pkg/pkg
, but is branch or tag v3.*
.
for more details, see here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论