container

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustMake

func MustMake[T any](c *Container, name string) T

MustMake resolves a service and casts to the expected type. Panics if the service is not found or the type assertion fails.

func TryMake added in v2.5.0

func TryMake[T any](c *Container, name string) (T, error)

TryMakeTyped resolves a service with type safety, returning an error instead of panicking if the service is not found or the type assertion fails.

Types

type Container

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

Container is the service container for dependency injection.

func New

func New() *Container

New creates a new empty container.

func (*Container) Bind

func (c *Container) Bind(name string, factory Factory)

Bind registers a factory function for a service. Each Make() call invokes the factory and returns a new instance (transient).

func (*Container) Has

func (c *Container) Has(name string) bool

Has checks if a service is registered (binding or instance).

func (*Container) Instance

func (c *Container) Instance(name string, instance interface{})

Instance registers an already-created instance directly.

func (*Container) Make

func (c *Container) Make(name string) interface{}

Make resolves a service by name. Checks instances first, then bindings. Panics if the service is not registered.

func (*Container) SafeSingleton added in v2.7.2

func (c *Container) SafeSingleton(name string, factory Factory)

SafeSingleton registers a singleton that catches panics during creation. If the factory panics and the app is in local/development mode, the panic is logged as a warning and the service is skipped (not registered). In production, the panic propagates normally.

func (*Container) Singleton

func (c *Container) Singleton(name string, factory Factory)

Singleton registers a factory that is only called once. The first Make() call creates the instance; subsequent calls return the cached instance.

func (*Container) TryMake added in v2.5.0

func (c *Container) TryMake(name string) (interface{}, error)

TryMake resolves a service by name, returning an error instead of panicking if the service is not registered.

type Factory

type Factory func(c *Container) interface{}

Factory is a function that creates a service instance.

type Provider

type Provider interface {
	// Register binds services into the container.
	// Called before the application boots. Only register bindings here.
	Register(c *Container)

	// Boot runs after ALL providers have been registered.
	// May resolve other services from the container.
	Boot(c *Container)
}

Provider defines the lifecycle hooks for service registration.

Jump to

Keyboard shortcuts

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