injector

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any]() T

Get returns an instance of the type specified by the type parameter T. If an instance has already been created, it is returned from the cache. Otherwise, a new instance is created using the registered constructor function. The constructor function is called with its dependencies resolved using reflection.

func Init

func Init()

Init initializes the dependencies and constructors maps. Init must be called before any other function in this package.

func Inject

func Inject[T any]() T

Inject creates a new instance of the specified struct type with its fields injected with their respective dependencies. It returns the created instance.

func Provide

func Provide[T any](constructor interface{}) error

Provide registers a constructor function that returns a value of type T. The constructor function is called immediately and the result is stored as a dependency. If T is an interface, the constructor function must return a value that implements T. If T is not an interface, the constructor function must return a value of type T. The constructor function must return at least one value. The constructor function must be a function. Provide is a shorthand for ProvideLazy followed by Get[T].

func ProvideLazy

func ProvideLazy[T any](constructor interface{}) error

ProvideLazy registers a constructor function that returns a value of type T. The constructor function is not called until Get[T]() is called. If T is an interface, the constructor function must return a value that implements T. If T is not an interface, the constructor function must return a value of type T. The constructor function must return at least one value. The constructor function must be a function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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