doulin2025 2019-09-05 13:00
浏览 562
已采纳

如何在Go中访问嵌套模块(子模块)?

Go version: 1.12.9

Here is the structure of a simple demo project:

enter image description here

So we have a module domain, which contains two modules: activity and person.

I would like to use domain with all nested modules in the main file => modules.go.

Ok I know how to import domain in the main go.mod:

module modules

go 1.12

replace modules/domain v0.0.0 => ./domain

require modules/domain v0.0.0

So after that can use code from domain/domain.go, but I cannot access !!! code from activity and person modules.

Yes, I could manually import nested modules, for example:
(main go.mod):

module modules

go 1.12

replace modules/domain v0.0.0 => ./domain

replace modules/domain/activity v0.0.0 => ./domain/activity

require (
    modules/domain v0.0.0
    modules/domain/activity v0.0.0
)

but I don't want to manually import all submodules.

Question:

How to configure modules to import domain with all submodules ???

Thanks in advance,
Hubert

  • 写回答

1条回答 默认 最新

  • dongqiang1226 2019-09-05 13:11
    关注

    but I don't want to manually import all submodules.

    There is no "I want" in Go. And there is no notion of "sub"-module, all modules are equal. As are all(*) packages. If you want to use a package you must import it. Dead simple.

    Best advice: Stop making every package its own module; most likely this is overkill or plain wrong. Stop making tiny packages. Stop trying to mimick a source code layout you might be used from other languages. Read "How to Write Go Code" and stick to it word for word.

    (*) internal and vendored packages are an exception which does not apply to your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录