Modules let you group multiple registrations and apply them to an injector in a single call. They are a thin, optional
abstraction built on top of the existing API; you can keep using the regular Register* functions directly, or compose
them via modules for cleaner setup code.
The With* helpers adapt the existing registration API into module-ready registration functions. Only error-returning
constructor adapters are provided.
packagemainimport("log""github.com/wrapped-owls/goremy-di/remy")funcmain(){inj:=remy.NewInjector(remy.Config{CanOverride:false,// Default})module:=remy.NewModule(remy.WithInstance("first"),remy.WithInstance("second"),// This will cause an error)iferr:=remy.RegisterModule(inj,module);err!=nil{// Error is returned, first registration remains appliedlog.Fatal(err)}}
Interoperability
Modules do not replace the standard API. They simply let you stage registrations and apply them later. You can mix
direct registrations and modules freely:
packagemainimport("github.com/wrapped-owls/goremy-di/remy")funcmain(){inj:=remy.NewInjector()// Direct registrationremy.RegisterInstance(inj,42)// Module-based registrationssvcMod:=remy.NewModule(remy.WithFactory(remy.LazySingleton(func(_remy.DependencyRetriever)(string,error){return"data",nil})),)_=remy.RegisterModule(inj,svcMod)// Both are available_=remy.MustGet[int](inj)// 42_=remy.MustGet[string](inj)// "data"}
Best Practices
Organizing Modules
Group by feature: user.Module, billing.Module, http.Module
Provide small, reusable modules: e.g., logging, config, database
Compose them in your application’s main wiring function
This allows you to create custom module implementations beyond the NewModule helper:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
packagemainimport"github.com/wrapped-owls/goremy-di/remy"typeCustomModulestruct{// Your custom fields}func(m*CustomModule)Register(injectorremy.Injector){// Your custom registration logicremy.RegisterInstance(injector,"custom-value")}funcmain(){// UsagecustomMod:=&CustomModule{}_=remy.RegisterModule(inj,customMod)}