container

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFrozen          = errors.New("container is frozen and cannot be mutated")
	ErrBindingNotFound = errors.New("binding not found")
	ErrInvalidFactory  = errors.New("invalid factory function")
)

Functions

func Make

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

Make resolves a dependency from the container type-safely.

Types

type Container

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

Container is the IoC container.

func New

func New() *Container

New creates a new Container.

func (*Container) AfterHook

func (c *Container) AfterHook(fn func())

AfterHook registers a callback to run after the container is frozen.

func (*Container) Alias

func (c *Container) Alias(alias string, iface any) error

Alias registers an alias for a type.

func (*Container) Bind

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

Bind registers a binding with the container.

func (*Container) Call

func (c *Container) Call(fn any) ([]any, error)

Call invokes a function with dependencies resolved from the container.

func (*Container) Flush

func (c *Container) Flush()

Flush removes all bindings and instances from the container.

func (*Container) Freeze

func (c *Container) Freeze()

Freeze locks the container from further mutations.

func (*Container) Has

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

Has checks if a binding exists for the given type.

func (*Container) Instance

func (c *Container) Instance(iface any, instance any) error

Instance registers an existing instance as a singleton.

func (*Container) MakeAlias

func (c *Container) MakeAlias(alias string) (any, error)

MakeAlias resolves a type by alias name.

func (*Container) Resolve

func (c *Container) Resolve(typ reflect.Type) (any, error)

Resolve resolves a dependency by its reflection type.

func (*Container) Singleton

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

Singleton registers a singleton binding with the container.

func (*Container) Tagged

func (c *Container) Tagged(iface any, tag string, factory any) error

Tagged registers a binding with a tag for later retrieval by tag.

func (*Container) TaggedOf

func (c *Container) TaggedOf(tag string) ([]any, error)

TaggedOf retrieves all bindings registered with a specific tag.

func (*Container) Unbind

func (c *Container) Unbind(iface any)

Unbind removes a binding from the container.

func (*Container) When

func (c *Container) When(concrete any) *ContextualBindingBuilder

When begins defining a contextual binding.

type ContextualBindingBuilder

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

ContextualBindingBuilder helps build a contextual binding.

func (*ContextualBindingBuilder) Give

func (b *ContextualBindingBuilder) Give(factory any)

Give specifies the factory to provide when the context is resolved.

func (*ContextualBindingBuilder) Needs

Needs specifies the interface needed by the concrete class.

Jump to

Keyboard shortcuts

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