英文:
Issue with developing a multi-module Go workspace
问题
我的文件夹结构大致如下...(假设我的git仓库名为demorepo)
demorepo
|--directory1
|--(此级别没有go.mod文件)
|--module1
|--package1 --------->--------------->--------------------->----|
|--go.mod (github.com/demorepo/directory1/module1) |
|--go.sum |
|--module2 |
|--module3 |
|--directory2 |
|--(此级别没有go.mod文件) |
|--newmodule ------<------------<------------------<----------------|
现在,我想在我的"newmodule"中使用"package1"中定义的函数。
当我运行以下命令:
go get <repo_address>/directort1/module1/package1 at "new_module"
它显示....
github.com/<repo>@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/<repo>/directory1/module1/package1
英文:
My folder structure looks something like this... (say my git repo name is 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 ------<------------<------------------<----------------|
Now, I want to use a function defined in "package1" in my "newmodule"
When I hit
go get <repo_address>/directort1/module1/package1 at "new_module"
it says ....
github.com/<repo>@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/<repo>/directory1/module1/package1
答案1
得分: 3
Go 1.18有一个关于Go工作区文件的提案,这应该能简化这个任务。
与此同时,你可以在你的go.mod
文件中使用replace
指令来引用位于本地文件系统上的模块。
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
现在你可以在newmodule
中正常地import github.com/<repo>/directory1/module1/package1
,它将引用本地的module1
。
你可能不想在go.mod
文件本身中使用replace
指令,而是复制一份,比如go.local.mod
,并在构建项目时使用它:go build -modfile go.local.mod .
(也可以将go.local.mod
添加到.gitignore
中)。
英文:
There is a proposal for a Go Workspace File for Go 1.18 which should simplify this task.
Meanwhile, you can use the replace
directive in your go.mod
file to refer to a module located on a local filesystem.
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
Now you can import github.com/<repo>/directory1/module1/package1
in newmodule
normally and it'll refer to the local module1
.
You might not want the replace
directive in the go.mod
file itself, and instead make a copy of it e.g. go.local.mod
and use it when building your project: go build -modfile go.local.mod .
(can also add go.local.mod
to .gitignore
).
答案2
得分: 0
方法:
如果你想在newModule
中使用module1
,那么你应该为module1
创建一个新的repo
,将你的逻辑放在那里,并将其推送到github
。请确保你使用适当的库版本。
将其作为一个库进行import
,它就会工作。
还可以参考官方文档中关于模块依赖和根级依赖的内容。
官方文档:https://go.dev/blog/using-go-modules
英文:
Approach :
If you want to use module1
inside newModule
then you should make one new repo
for module1
put your logic there push it in github
. please make sure you should use appropriate version
for the library.
import
it as a library
and it will work.
Also refer the official docs of module dependency and also check root level dependency.
Official docs : https://go.dev/blog/using-go-modules
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论