Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCircularDependency = errors.New("circular dependency")
ErrCircularDependency declares a circular dependency error.
var ErrDependencyNotResolved = errors.New("dependency not resolved")
ErrDependencyNotResolved declares service not resolved error.
var ErrEntrypointReturnedError = errors.New("entrypoint returned error")
ErrEntrypointReturnedError declares entrypoint returned error.
var ErrFactoryReturnedError = errors.New("factory returned error")
ErrFactoryReturnedError declares factory returned error.
var ErrFactoryTypeDuplicated = errors.New("factory type duplicated")
ErrFactoryTypeDuplicated declares service duplicated error.
var ErrNoEntrypointsProvided = errors.New("no entrypoints provided")
ErrNoEntrypointsProvided declares no entrypoints provided error.
Functions ¶
func Run ¶
Run runs a container with a set of configured factories.
Run registers the provided options, validates the registry, invokes entrypoints synchronously, and then tears down all spawned factories in reverse order. It returns when all entrypoints have returned and teardown has completed.
func WithAnnotation ¶ added in v2.2.0
func WithAnnotation(value any) annotationOpt
WithAnnotation returns an option that attaches a value to a Factory or Entrypoint.
Types ¶
type Entrypoint ¶ added in v2.1.0
type Entrypoint struct {
// contains filtered or unexported fields
}
Entrypoint is a container option that registers an entrypoint function.
func NewEntrypoint ¶
func NewEntrypoint(function any, opts ...EntrypointOption) *Entrypoint
NewEntrypoint creates a new factory which will be called by the container.
Example:
gontainer.NewEntrypoint(func(db *Database) error { ... })
gontainer.NewEntrypoint(func(db *Database) { ... })
func (*Entrypoint) Annotations ¶ added in v2.2.0
func (e *Entrypoint) Annotations() []any
Annotations returns a copy of the associated annotations.
func (*Entrypoint) Name ¶ added in v2.2.0
func (e *Entrypoint) Name() string
Name returns the human-readable name of the entrypoint.
func (*Entrypoint) Source ¶ added in v2.2.0
func (e *Entrypoint) Source() string
Source returns the source package path of the entrypoint.
type EntrypointOption ¶ added in v2.2.0
type EntrypointOption interface {
// contains filtered or unexported methods
}
EntrypointOption is an option for configuring an Entrypoint.
type Factory ¶ added in v2.1.0
type Factory struct {
// contains filtered or unexported fields
}
Factory is a container option that registers a service factory or singleton.
func NewFactory ¶
func NewFactory(function any, opts ...FactoryOption) *Factory
NewFactory creates a new service load using the provided load function.
The load function must be a function. It may accept dependencies as input parameters and return exactly one service instances, optionally followed by an error as the second return value.
Example:
gontainer.NewFactory(func(db *Database) *Handler { ... })
gontainer.NewFactory(func(db *Database) (*Handler, error) { ... })
gontainer.NewFactory(func(db *Database) (*Handler, func() error) { ... })
gontainer.NewFactory(func(db *Database) (*Handler, func() error, error) { ... })
func NewService ¶
func NewService[T any](service T, opts ...FactoryOption) *Factory
NewService creates a new service load that always returns the given singleton value.
This is a convenience helper for registering preconstructed service instances as factories. The returned load produces the same instance on every invocation.
This is useful for registering constants, mocks, or externally constructed values.
Example:
logger := NewLogger() gontainer.NewService(logger)
func (*Factory) Annotations ¶ added in v2.2.0
Annotations returns a copy of the associated annotations.
type FactoryOption ¶ added in v2.2.0
type FactoryOption interface {
// contains filtered or unexported methods
}
FactoryOption is an option for configuring a Factory or a Service.
type Invoker ¶
type Invoker struct {
// contains filtered or unexported fields
}
Invoker invokes functions with automatic dependency resolution.
The Invoke method accepts a function `fn`, resolves its input parameters using the invoker's dependency resolver, and then calls the function with the resolved arguments.
If the container has not been started yet, dependency resolution happens in lazy mode — only the required arguments and their transitive dependencies are instantiated on demand.
The Invoke method returns:
- []any - all values returned by the function (including any errors)
- error - only if dependency resolution fails or fn is not a function
All return values from the invoked function are collected in the []any slice, including any error values. The caller is responsible for checking and handling these values as appropriate.
type Multiple ¶
type Multiple[T any] []T
Multiple defines a dependency on zero or more services of the same type.
This generic wrapper is used in service factory function parameters to declare a dependency on all services assignable to type T registered in the container.
The container will collect and inject all matching services into the slice. For interface types, multiple matches are allowed. For concrete (non-interface) types, at most one match is possible.
Example:
func MyFactory(providers gontainer.Multiple[AuthProvider]) {
for _, p := range providers {
...
}
}
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is the interface for container options.
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional defines a dependency on a service that may or may not be registered.
This generic wrapper is used in service factory function parameters to declare that the service of type T is optional. If the container does not contain a matching service, the zero value of T will be injected.
Use the Get() method to access the wrapped value inside the factory.
Example:
func MyFactory(logger gontainer.Optional[Logger]) {
if log := logger.Get(); log != nil {
log.Info("Logger available")
}
}
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves service dependencies.
The Resolve method accepts a pointer to a variable (`varPtr`) and attempts to populate it with an instance of the requested type. The type is determined via reflection based on the element type of `varPtr`.
If the container has not been started yet, Resolve operates in lazy mode — it instantiates only the requested type and its transitive dependencies on demand.
An error is returned if the service of the requested type is not found or cannot be resolved.
