Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access nested modules (submodules) in Go?

Tags:

go

go-modules

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.

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.

How to configure modules to import domain with all submodules?

like image 693
Hubert Avatar asked Sep 05 '19 13:09

Hubert


People also ask

Where does Go store downloaded modules?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root.

What are modules and submodules?

Modules and submodules house all the information and content within your courses. Modules are the foundational building blocks of your course. They can be organized by date, theme, topic, learning outcome, etc. Submodules are nested within modules and generally include more specific details and information.

How do I update go modules?

You can upgrade or downgrade a dependency module by using Go tools to discover available versions, then add a different version as a dependency. To discover new versions use the go list command as described in Discovering available updates.


2 Answers

Yes, I could manually import nested modules [...] but I don't want to manually import all submodules. How to configure modules to import domain with all submodules ?

You simply cannot do this.

Some background:

  • There is no real notion of "sub"-module, all modules are equal.
  • Same for packages: all(*) packages are equal. If you want to use a package you must import it.
  • The layout in the filesystem does not imply any kind of technical relation between packages (e.g. net/http/cookiejar is as much related to net/http as it is related crypto/md5: not at all).
  • Being part of a module doesn't mean much: Modules are just sets of packages versioned together and doesn't add any additional relation between these packages.

The rule is dead simple: If you want to import a package you have to import it.

The fact that there are no magical wildcard imports might seem annoying (see below) but prevents unintended imports: Adding a package doesn't magically import it (and execute its init functions!).

In real live having to import all packages is not that annoying because "normal" Go code doesn't use tiny packages. This is a common mistake for someone indoctrinated by Java/C#/PHP/Node "project architectures" with lots of folders and tiny classes: Don't do that in Go. It is not helping. It often even leads to circular import and hurts.

Same for modules. A Go module is probably not what you think it is. A module is a set of packages versioned together. I doubt there is a reason to provide different versions for package person and package domain at the same time. (It is allowed to have several modules in one repository/project, but having more than one is a very rare case, yours clearly isn't).

Best advice (in order of relevance):

  • Stop making every package its own module.
  • Stop making tiny packages.
  • Stop trying to mimick a source code layout ("architecture") you might be used from other languages.

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

like image 168
Volker Avatar answered Oct 25 '22 21:10

Volker


go 1.18 has the support for the new multi-workspace feature by go.work files. for more info, check golang workspace proposal

like image 1
mohammad Avatar answered Oct 25 '22 21:10

mohammad