Can sub folder have its own module

Hi forum,

I read this in How to Write Go Code

The module contains the packages in the directory containing its go.mod file as well as subdirectories of that directory, up to the next subdirectory containing another go.mod file (if any).

Can I have separate modules in both parent and child folders? For example: a/go.mod, a/b/go.mod.

Can I use exported names from child module in parent module?


| |____go.mod
| |____a.go
| |____b
| | |____go.mod
| | |____b.go 

It also says there “A Go repository typically contains only one module, located at the root of the repository.” And it says here, “if we created a subdirectory world , we would not need to (nor want to) run go mod init there.”

Make your life easier and don’t do that. If your code should be packaged as two modules, house them in separate directory trees. If you have related packages, keep them in one module. Don’t confuse packages and modules. Packages are for organization and namespacing, modules are for distribution and versioning.

1 Like

Thanks Jeff.

I understand it now. Please correct me if I was wrong.

  • One folder contains one module. There can be only one go.mod
  • One folder contains one package. All source files inside declare package with the same name
  • No sub-module in sub-folder
  • There can be sub-package in sub-folder

Looks correct

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.