ComponentsDependency Injection
Qualifiers
Select a named component for a constructor argument.
Use qualifiers when multiple components can satisfy the same constructor parameter type.
func init() {
component.Register(NewPrimaryMailer, component.WithName("primaryMailer"))
component.Register(NewAuditMailer, component.WithName("auditMailer"))
component.Register(
NewNotificationService,
component.WithQualifierFor[Mailer]("primaryMailer"),
)
}component.WithQualifierFor[T] applies a component name to constructor
arguments of type T.
Repeated parameter types
When the same type appears more than once, qualify by index.
component.Register(
NewRelayService,
component.WithQualifierAt(0, "primaryMailer"),
component.WithQualifierAt(1, "auditMailer"),
)Qualifier guidance
- Qualify only when type resolution is ambiguous.
- Prefer
WithQualifierFor[T]when the constructor has one parameter of that type. - Use
WithQualifierAtwhen the same parameter type appears multiple times. - Keep component names stable because qualifiers depend on them.
