编辑多个未发布的 Go 模块的本地副本

huangapple go评论88阅读模式
英文:

Editing local copies of multiple unpublished Go Modules

问题

我觉得使用未发布的Go模块是非常痛苦的,不知道是不是只有我一个人这样感觉?

从这两个链接中,我找到了replace指令。但是现在我遇到了一个困难。

假设我有三个包:example.com/p1、example.com/p2和example.com/p3。
p1使用p2,p2使用p3。

我在go.mod文件中添加了以下replace指令:

  • 对于p2,将路径替换为p3的路径
  • 对于p1,将路径替换为p2的路径

但是现在似乎我还需要在p1的go.mod文件中为p3添加一个replace指令。
考虑到我有很多未发布的包,这变得相当痛苦。

这是正常的行为还是我做错了什么?

英文:

Is it just me or is working with unpublished go modules insanely painful?

https://go.dev/doc/modules/managing-dependencies#tmp_9

https://groups.google.com/g/golang-nuts/c/9MfGXLmRu8w/m/D2gm_viYBAAJ

From these two links, I figured out the replace directive. But now here's where I'm having a hard time.

Let's say I have packages example.com/p1, example.com/p2 and example.com/p3.
p1 uses p2 and p2 uses p3.

I added the replace directive with:

  • the path to p3 in go.mod for p2
  • the path to p2 in go.mod for p1

But now it seems like I also need to add a replace directive in p1's go.mod for p3.
With the number of unpublished packages I have, this is becoming quite painful.

Is this expected or am I doing something wrong?

答案1

得分: 2

正如其他人所说,将所有内容放在一个未发布的模块中可能会更容易,而不是将其拆分为多个模块。这样可以更容易地保持在“快乐路径”上,对于你的情况来说,听起来这样做会起作用。

话虽如此,如果你升级到 Go 1.18,新的工作区功能可以更轻松地同时编辑多个本地模块:

使用多模块工作区,你可以告诉 Go 命令你同时在多个模块中编写代码,并且可以轻松地构建和运行这些模块中的代码。

例如,从一个包含多个模块目录的父目录开始(每个目录都有自己的 go.mod 文件),你可以创建一个 go.work 文件,并通过以下方式递归地添加模块:

$ go work init
$ go work use -r .

例如,如果你有两个模块,假设模块 foo 导入模块 bar,那么生成的 go.work 文件可能如下所示:

$ cat go.work
go 1.18

use (
        ./foo
        ./bar
)

然后,如果你切换到 foo 目录,像 go build 这样的命令将使用 foobar 的本地副本。

具体的机制是,go 命令会检查是否位于具有 go.work 文件的目录树中,并且默认情况下会查找任何找到的 go.work 文件以帮助解析依赖项的位置。

关于工作区的更多信息,可以在这里找到一个很好的教程:
https://go.dev/doc/tutorial/workspaces

或者,你可以使用旧的技术,在各个 go.mod 文件中添加 replace 指令,但这不如新的 go.work 功能好用。

最后,即使考虑到新的 go.work 功能,还是值得重申,如果你一次只使用一个模块,那么你的生活会更简单,所以不要不必要地将可能是一个单一模块的东西切割开来。

英文:

As others had said, it can be much easier to just place everything in a single unpublished module rather than breaking things apart into multiple modules. This makes it much easier to stay on the "happy path", and for your situation it sounds like that would work.

That said, if you upgrade to Go 1.18, the new workspace feature makes it much easier to work with editing multiple local modules simultaneously:

> With multi-module workspaces, you can tell the Go command that you’re writing code in multiple modules at the same time and easily build and run code in those modules.

For example, from a parent directory that contains multiple module directories underneath (each with their own go.mod files), you can create a go.work file and recursively add the modules underneath by doing:

$ go work init
$ go work use -r .

If for example you have two modules and let's say module foo imports module bar, the resulting go.work file could then look like:

$ cat go.work
go 1.18

use (
        ./foo
        ./bar
)

If you then cd to the foo directory, commands like go build will use the local copies of both foo and bar.

The mechanics are that the go command checks to see if it is inside of a directory tree with a go.work file, and by default it consults any go.work file found to help resolve where a dependency is located.

For more information on workspaces, there is a good tutorial here:
https://go.dev/doc/tutorial/workspaces

Alternatively, you can use the older technique of adding replace directives to your individual go.mod files, but that is not as nice as the new go.work feature.

Finally, even taking into account the new go.work capabilities, it is worth reiterating that your life is simpler if you are only working with one module at a time, so don't needlessly cut up what could be a single module.

答案2

得分: 0

我无法回复一个回答(因为没有足够的声望),但我尝试了go work的方法,但它没有起作用。

问题是循环的,为了能够使用go.work,你需要将仓库发布到你的go.mod中,而你的go.mod不能获取未发布的仓库。

所以,我选择了replace指令的方法,它非常简单。

module github.com/obfuscated/unpublished

go 1.18

replace github.com/obfuscated/unpublished => ./

英文:

I cannot reply to a response (because not enough karma) but I tried the go work pathway and it did not... work

The problem is circular, in order to be able use go.work you are required to publish the repo to be in your go.mod, and your go.mod cannot fetch the unpublished repo.

So, I took the replace directive pathway, and it's fairly easy

module github.com/obfuscated/unpublished

go 1.18

replace github.com/obfuscated/unpublished => ./

huangapple
  • 本文由 发表于 2022年1月9日 22:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/70642433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定