di

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container is a simple dependency injection container. It supports registering constructors or values and resolving them by type. Constructor functions are called once and cached as singletons.

func NewContainer

func NewContainer() *Container

NewContainer creates a new DI container.

func (*Container) Inject

func (c *Container) Inject(target any) error

Inject fills struct fields tagged with `inject:""` using the container. target must be a pointer to a struct.

Lazy first-time resolution of a func-provider writes the shared singletons/resolved maps, so the whole method takes the full write lock (c.mu.Lock), mirroring Resolve. Using an RLock here would let two concurrent cold-start injections of the same type write the same map simultaneously — an unrecoverable "concurrent map writes" crash.

func (*Container) Provide

func (c *Container) Provide(constructor any) error

Provide registers a constructor or value. The constructor can be:

  • A function returning one value (used as singleton factory)
  • A direct value (stored as singleton)

func (*Container) Resolve

func (c *Container) Resolve(target any) error

Resolve retrieves a value by type. The target must be a pointer. It uses the registered constructor to create the value (singleton pattern).

Jump to

Keyboard shortcuts

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