Modules
Modules are the primary organizational unit in Ginject. Learn how to create static and dynamic modules, configure global providers, and compose module graphs.
Modules
A Module is a class-like construct (a Go struct) that groups related controllers, providers, and sub-modules. The module graph describes how your application is wired together.
Static Modules
A static module is a func() *core.Module variable — it is a singleton created exactly once per application lifecycle.
Reference it from any parent module:
Dynamic Modules
Dynamic modules are instantiated with configuration arguments. They are defined as structs with a New(...) factory method.
Import a dynamic module by calling its factory:
Global Modules
Setting IsGlobal: true on a module makes all its exported providers available to every other module without explicit imports.
The built-in ConfigModule and CacheModule are both designed to be used as global modules.
Module Lifecycle Hook
A module can run initialization logic when the application is wired. Use OnInit for tasks like establishing database connections:
Module-Level Middleware
Bind middleware that only applies to routes within the current module (and its sub-modules):
ModuleBuilder API
| Method | Description |
|---|---|
Imports(modules ...any) | Import static or dynamic sub-modules |
Controllers(cs ...Controller) | Register controllers for this module |
Providers(ps ...Provider) | Register providers for this module |
Build() | Returns the configured *core.Module |