Components
Manage Lifecycle
Initialize and dispose components.
Manage Lifecycle
Implement component.Initializer when a component needs setup after dependency
injection.
type Cache struct {
ready bool
}
func NewCache() *Cache {
return &Cache{}
}
func (c *Cache) Init(ctx context.Context) error {
c.ready = true
return nil
}Implement component.Disposable when a singleton needs cleanup during
application shutdown.
type Database struct {
conn *sql.DB
}
func (d *Database) Dispose() error {
return d.conn.Close()
}Processors can wrap initialization when you need cross-cutting behavior around component startup.
type LoggingInitProcessor struct{}
func (LoggingInitProcessor) ProcessBeforeInit(
ctx context.Context,
name string,
instance any,
) (any, error) {
log.Printf("initializing %s", name)
return instance, nil
}