ComponentsLifecycle
Initialization
Prepare a component after constructor dependencies are available.
Implement component.Initializer when a component needs setup after dependency
injection.
type CacheClient interface {
Ping(context.Context) error
}
type Cache struct {
client CacheClient
ready bool
}
func NewCache(client CacheClient) *Cache {
return &Cache{client: client}
}
func (c *Cache) Init(ctx context.Context) error {
if err := c.client.Ping(ctx); err != nil {
return err
}
c.ready = true
return nil
}Use initialization for:
- validating external clients
- warming caches
- preparing runtime state
- checking required configuration
Return an error when initialization fails. Startup should fail clearly instead of leaving the component half-ready.
