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 ¶
Types ¶
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 defines an option for the service container.
func NewEntrypoint ¶
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 NewFactory ¶
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 ¶
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)
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.
