Golang Uber Fx
by @samber
Golang application framework using uber-go/fx — fx.New, fx.Provide, fx.Invoke, fx.Module, fx.Lifecycle hooks, fx.Annotate (name/group/As), fx.Decorate, fx.Su...
1. Keep main() thin — providers, modules, and a single Run(). Push real work into modules so each can be tested in isolation.
2. Use lifecycle hooks instead of init() or goroutines launched from constructors — Start/Stop ordering depends on graph topology, but init() goroutines do not, which leads to races and leaks.
3. OnStart must return promptly — long work goes in a goroutine inside the hook. A blocking OnStart hangs the rest of the boot.
4. Respect ctx.Done() in hooks — a hook that ignores cancellation is reported as a timeout failure but its goroutine continues, leaking resources.
5. Group by module, not by layer — a module owns the providers, lifecycle, and decorators for one concern (HTTP, DB, metrics).
6. Use fx.Annotate for tags rather than wrapping a constructor in an fx.Out struct — keeps the constructor reusable outside fx.
7. Replace fx.Provide with fx.Supply for pre-built values (config, command-line flags). Shorter, signals intent.
8. Validate the graph in CI by booting under fx.New(...).Err() — catches missing providers and cycles before deploy.
clawhub install golang-uber-fx