开发多模块的Go工作区存在问题

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

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/&lt;repo&gt;@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/&lt;repo&gt;/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 ---------&gt;---------------&gt;---------------------&gt;----|
      |--go.mod (github.com/demorepo/directory1/module1)              |
      |--go.sum                                                       |
   |--module2                                                         |
   |--module3                                                         |
|--directory2                                                         |
  |-- (no go.mod at this level)                                       |
  |--newmodule ------&lt;------------&lt;------------------&lt;----------------|

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/&lt;repo&gt;@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/&lt;repo&gt;/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/&lt;repo&gt;/directory1/module1

demorepo/directory2/newmodule/go.mod:

module github.com/&lt;repo&gt;/directory2/newmodule

replace github.com/&lt;repo&gt;/directory1/module1 =&gt; ../../directory1/module1

Now you can import github.com/&lt;repo&gt;/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

huangapple
  • 本文由 发表于 2021年12月20日 15:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70418534.html
匿名

发表评论

匿名网友

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

确定