di

package
v1.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package di provides a lightweight dependency injection container for managing component lifecycle.

This package supports:

  • Singleton and transient service lifecycles
  • Interface-based registration and resolution
  • Thread-safe concurrent access
  • Factory functions and instance registration
  • Automatic cleanup of closable services

Example usage:

container := di.New()
container.Register((*MyInterface)(nil), func(c *di.Container) (any, error) {
    return NewMyService(), nil
}, true) // true = singleton

service, err := container.Resolve((*MyInterface)(nil))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDefaultRegisteredTyped

func IsDefaultRegisteredTyped[T any]() bool

IsDefaultRegisteredTyped checks if a service is registered with the default container with type safety

func IsRegistered

func IsRegistered(iface any) bool

IsRegistered checks if a service is registered with the default container

func IsRegisteredTyped

func IsRegisteredTyped[T any](c *Container) bool

IsRegisteredTyped checks if a service is registered with type safety

func MustResolve

func MustResolve(iface any) any

MustResolve resolves a service from the default container and panics if it fails

func MustResolveDefaultTyped

func MustResolveDefaultTyped[T any]() T

MustResolveDefaultTyped resolves a service from the default container with type safety and panics if it fails

func MustResolveTyped

func MustResolveTyped[T any](c *Container) T

MustResolveTyped resolves a service from the container with type safety and panics if it fails

func ResetForTesting

func ResetForTesting()

ResetForTesting clears the global default container for testing. This must only be called from test code to ensure clean state between tests. Production code must never call this function.

func Resolve

func Resolve(iface any) (any, error)

Resolve resolves a service from the default container

func ResolveDefaultTyped

func ResolveDefaultTyped[T any]() (T, error)

ResolveDefaultTyped resolves a service from the default container with type safety

func ResolveTyped

func ResolveTyped[T any](c *Container) (T, error)

ResolveTyped resolves a service from the container with type safety

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container represents a dependency injection container. It manages service registration, lifecycle, and resolution with thread-safe access.

func Clear

func Clear() *Container

Clear clears all registered services from the default container

func Default

func Default() *Container

Default returns the global default container

func New

func New() *Container

New creates a new dependency injection container. The container starts empty and requires explicit service registration before use.

func NewWithOptions

func NewWithOptions(opts ...Option) *Container

NewWithOptions creates a new container with options

func Register

func Register(iface any, factory func(*Container) (any, error), isSingleton bool) *Container

Register registers a service with the default container

func RegisterDefaultServices

func RegisterDefaultServices(services ...func(*Container) *Container) *Container

RegisterDefaultServices registers multiple services at once with the default container

func RegisterDefaultSingletonInstance

func RegisterDefaultSingletonInstance[T any](instance T) *Container

RegisterDefaultSingletonInstance registers a singleton instance with the default container

func RegisterInstance

func RegisterInstance(iface any, instance any) *Container

RegisterInstance registers an instance with the default container

func RegisterSingleton

func RegisterSingleton(iface any, factory func(*Container) (any, error)) *Container

RegisterSingleton registers a singleton service with the default container

func RegisterSingletonInstance

func RegisterSingletonInstance[T any](c *Container, instance T) *Container

RegisterSingletonInstance registers a singleton instance

func RegisterTransient

func RegisterTransient(iface any, factory func(*Container) (any, error)) *Container

RegisterTransient registers a transient service with the default container

func (*Container) Clear

func (c *Container) Clear() *Container

Clear clears all registered services

func (*Container) Close added in v1.1.3

func (c *Container) Close() error

Close closes all initialized singleton services that implement io.Closer. It returns a multi-error if any service fails to close.

func (*Container) IsRegistered

func (c *Container) IsRegistered(iface any) bool

IsRegistered checks if a service is registered

func (*Container) MustResolve

func (c *Container) MustResolve(iface any) any

MustResolve resolves a service from the container and panics if it fails

func (*Container) Register

func (c *Container) Register(iface any, factory func(*Container) (any, error), isSingleton bool) *Container

Register registers a service with the container.

Parameters:

  • iface: pointer to interface type (e.g., (*MyInterface)(nil))
  • factory: function that creates the service instance
  • isSingleton: if true, only one instance is created and reused; if false, new instance each time

Returns the container for method chaining.

func (*Container) RegisterInstance

func (c *Container) RegisterInstance(iface any, instance any) *Container

RegisterInstance registers an existing instance with the container. The instance is treated as a singleton and returned as-is on resolution.

Parameters:

  • iface: pointer to interface type
  • instance: the concrete instance to register

Returns the container for method chaining.

func (*Container) RegisterServices

func (c *Container) RegisterServices(services ...func(*Container) *Container) *Container

RegisterServices registers multiple services at once

func (*Container) RegisterSingleton

func (c *Container) RegisterSingleton(iface any, factory func(*Container) (any, error)) *Container

RegisterSingleton registers a singleton service with the container. The service factory will be called only once, and the same instance is returned on every resolution.

func (*Container) RegisterTransient

func (c *Container) RegisterTransient(iface any, factory func(*Container) (any, error)) *Container

RegisterTransient registers a transient service with the container. A new instance is created every time the service is resolved.

func (*Container) Resolve

func (c *Container) Resolve(iface any) (any, error)

Resolve resolves a service from the container. It returns the service instance or an error if the service is not registered or initialization fails. For singletons, the instance is created only once and cached for subsequent calls.

type Option

type Option func(*Container)

Option represents a container option

func WithInstance

func WithInstance(iface interface{}, instance interface{}) Option

WithInstance registers an existing instance

func WithSingleton

func WithSingleton(iface interface{}, factory func(*Container) (interface{}, error)) Option

WithSingleton registers a service as a singleton

func WithTransient

func WithTransient(iface interface{}, factory func(*Container) (interface{}, error)) Option

WithTransient registers a service as transient

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL