doulin2025
2019-09-05 13:00 阅读 308
已采纳

如何在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 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.

    点赞 评论 复制链接分享

相关推荐