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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度