Procyon|Docs
ComponentsResolving Components

Resolve by Name

Resolve a named component as a target type.

Use component.Resolve when the component name matters.

mail.go
func SendAuditMail(
	ctx context.Context,
	container component.Container,
) error {
	mailer, err := component.Resolve[Mailer](
		ctx,
		container,
		"auditMailer",
	)
	if err != nil {
		return err
	}

	return mailer.Send("admin@example.com", "audit event")
}

Named resolution is useful when multiple components implement the same interface and boundary code needs a specific one.

Check names

Use component.CanResolve when the name is optional.

optional.go
if component.CanResolve(container, "auditMailer") {
	// Resolve and use the optional component.
}

Prefer constructor qualifiers when the named dependency is required by a normal component.

On this page