Search⌘ K
AI Features

Module Code Organization

Explore how to organize module code effectively in event-driven Golang applications. Understand the use of internal packages for controlling dependencies, follow the guideline of accepting interfaces and returning structs, and implement interface checks to ensure robust and loosely coupled modules.

Each of the modules that makes up our application exposes a protocol buffer API and a small module file that contains the composition root for the module code. The modules also have their internal packages to keep unintentional imports from being made between the modules.

Internal package import rules
Internal package import rules

The above figure illustrates how the multiple internal packages help us manage our relationships and control the dependencies between the modules:

  • /root/internal: This package can be imported by /root and any package found in the directory tree under it.

  • /root/pkg-b/internal: This package can only be imported by /root/pkg-b and any package found in the directory tree under it. Both /root and /root/pkg-a will not be permitted ...