我的文件夹结构看起来像这样...(假设我的 git 存储库名称是 demorepo)
demorepo
|--directory1
|-- (no go.mod at this level)
|--module1
|--package1 --------->--------------->--------------------->----|
|--go.mod (github.com/demorepo/directory1/module1) |
|--go.sum |
|--module2 |
|--module3 |
|--directory2 |
|-- (no go.mod at this level) |
|--newmodule ------<------------<------------------<----------------|
现在,我想在我的“newmodule”中使用“package1”中定义的函数
当我击中
去“new_module”获取
github.com/<repo>@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/<repo>/directory1/module1/package1
最佳答案
有一个proposal for a Go Workspace File对于 Go 1.18 来说,这应该会简化这个任务。
同时,您可以使用 replace
directive在你的go.mod
文件来引用位于本地文件系统上的模块。
<强> demorepo/directory1/module1/go.mod
:
module github.com/<repo>/directory1/module1
<强> demorepo/directory2/newmodule/go.mod
:
module github.com/<repo>/directory2/newmodule
replace github.com/<repo>/directory1/module1 => ../../directory1/module1
现在您可以import github.com/<repo>/directory1/module1/package1
在newmodule
通常它会引用本地 module1
.
您可能不想要 replace
go.mod
中的指令文件本身,而是制作它的副本,例如go.local.mod
并在构建项目时使用它:go build -modfile go.local.mod .
(也可以将 go.local.mod
添加到 .gitignore
)。
关于go - 开发多模块 Go 工作区的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70418534/