简体   繁体   English

Go 多路径模块

[英]Go module with multiple path

I have a go module that is mirrored to several locations.我有一个镜像到多个位置的 go 模块。 One is in gitlab and the other bitbucket. When trying to base it off of the new gitlab location I get:一个在 gitlab 中,另一个在 bitbucket 中。当我试图将其基于新的 gitlab 位置时,我得到:

go: gitlab.com/company/core@v1.0.21: parsing go.mod: module declares its path as: bitbucket.org/company/core but was required as: gitlab.com/company/core go:gitlab.com/company/core@v1.0.21:解析 go.mod:模块声明其路径为:bitbucket.org/company/core 但要求为:gitlab.com/company/core

I know why this is happening, but how can I define my go.mod to be either or?我知道为什么会这样,但是我如何将我的 go.mod 定义为 or ?

Short answer: You can't .简短的回答:你不能

But all may not be lost...但一切都可能不会丢失......

If you have code in both the location referenced by the module path and in some other location that for some reason you prefer to download from, you can configure a git alias to redirect the reference from the stated module path to the desired download source:如果您在模块路径引用的位置和出于某种原因喜欢从中下载的其他位置都有代码,您可以配置git alias以将引用从指定的模块路径重定向到所需的下载源:

git config --global --add url."https://<download path>".insteadOf "https://<module path>"

The alias doesn't have to be a complete replacement.别名不必完全替换。 You can use an alias to replace a prefix on any module path.您可以使用别名替换任何模块路径上的前缀 So if you have a number of modules hosted on server X that you have identified (in their module path) on server Y , you only need to alias the server host prefix and path to cover all of these modules (as long as the repo names are the same).因此,如果您在服务器 X上托管了许多模块,并且您已在服务器 Y上识别(在它们的模块路径中),则只需为服务器主机前缀和路径添加别名即可覆盖所有这些模块(只要 repo 名称是相同的)。

For example, we use this to create 'friendly' module paths for modules hosted in our on-prem AzureDevOps Server git repos, for example:例如,我们使用它为我们本地 AzureDevOps 服务器 git 存储库中托管的模块创建“友好”模块路径,例如:

git config --global --add url."https://tfs.myorg.com/myorgcollection/_git".insteadOf "https://tfs.myorg.com"

Our modules are then able to identify themselves as tfs.myorg.com/somemodule.git but when go get attempts to fetch this git will look for it at https://tfs.myorg.com/myorgcollection/_git/somemodule.git然后我们的模块能够将自己标识为tfs.myorg.com/somemodule.git但是当go get尝试获取此 git 时将在https://tfs.myorg.com/myorgcollection/_git/somemodule.git中查找它

( the .git suffix is a necessary Azure DevOps peculiarity ) .git后缀是必要的 Azure DevOps 特性

To be honest, I haven't tried this with entirely different servers, only for "rewriting" paths on the same server, but in principle it should work.老实说,我还没有在完全不同的服务器上尝试过这个,只是为了在同一台服务器上“重写”路径,但原则上它应该可以工作。 So in your case:所以在你的情况下:

git config --global --add url."https://gitlab.com/company/".insteadOf "https://bitbucket.org/company/"

Because this works at the git level of things, as far as go is concerned it is still getting from bitbucket.org even though git is going to gitlab.com under the hood.因为这在git级别工作,就go而言,它仍然是从bitbucket.org获取的,即使git将转到gitlab.com

ie when you go get you need to reference the declared module path:即当你go get你需要引用声明的模块路径:

go get bitbucket.org/company/core

The only downside of this is that you need to configure the alias on any/all workstations (and build machines) that need to go get from any location other than that declared in the module path.唯一的缺点是您需要在需要从模块路径中声明的位置以外的任何位置go get的任何/所有工作站(和构建机器)上配置别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM