container

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 2 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.

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

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