Components
Load Conditionally
Attach runtime conditions to components.
Load Conditionally
Attach a condition when a component should only be loaded in certain runtime states.
type ProductionOnly struct{}
func (ProductionOnly) Matches(ctx component.ConditionContext) bool {
return ctx.Value("profile") == "production"
}
func init() {
component.Register(NewAuditService).
Conditional(ProductionOnly{})
}Conditions run before the component definition is loaded into the container. A
condition can inspect the context and access the current container through
ctx.Container().
Keep components small
Use components for application services, repositories, clients, runners, configuration processors, and framework extensions. Prefer constructor dependencies over global state, and name components only when the type alone is not enough to identify the dependency.
