di

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

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 resets the global default container and its initialization state. 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

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

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) 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

func (*Container) RegisterInstance

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

RegisterInstance registers an existing instance with the container

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

func (*Container) RegisterTransient

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

RegisterTransient registers a transient service

func (*Container) Resolve

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

Resolve resolves a service from the container

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