Documentation
ΒΆ
Index ΒΆ
- Variables
- func HealthCheck[T any](i *Container) error
- func HealthCheckNamed(i *Container, name string) error
- func Invoke[T any](i *Container) (T, error)
- func InvokeNamed[T any](i *Container, name string) (T, error)
- func MustInvoke[T any](i *Container) T
- func MustInvokeNamed[T any](i *Container, name string) T
- func MustShutdown[T any](i *Container)
- func MustShutdownNamed(i *Container, name string)
- func Override[T any](i *Container, provider Provider[T])
- func OverrideNamed[T any](i *Container, name string, provider Provider[T])
- func OverrideNamedValue[T any](i *Container, name string, value T)
- func OverrideValue[T any](i *Container, value T)
- func Provide[T any](i *Container, provider Provider[T])
- func ProvideNamed[T any](i *Container, name string, provider Provider[T])
- func ProvideNamedValue[T any](i *Container, name string, value T)
- func ProvideValue[T any](i *Container, value T)
- func Shutdown[T any](i *Container) error
- func ShutdownNamed(i *Container, name string) error
- type Container
- func (i *Container) Clone() *Container
- func (i *Container) CloneWithOpts(opts *ContainerOpts) *Container
- func (i *Container) HealthCheck() map[string]error
- func (container *Container) Inject(servicePtr interface{}) error
- func (i *Container) ListInvokedServices() []string
- func (i *Container) ListProvidedServices() []string
- func (i *Container) Shutdown() error
- func (i *Container) ShutdownOnSIGTERM() error
- func (i *Container) ShutdownOnSignals(signals ...os.Signal) error
- type ContainerOpts
- type Healthcheckable
- type Provider
- type Service
- type Shutdownable
Examples ΒΆ
- Container.Clone
- Container.HealthCheck
- Container.ListInvokedServices (Invoked)
- Container.ListInvokedServices (NotInvoked)
- Container.ListProvidedServices
- Container.Shutdown
- Invoke
- InvokeNamed
- MustInvoke
- MustInvokeNamed
- New
- NewWithOpts
- Override
- OverrideNamed
- OverrideNamedValue
- Provide
- ProvideNamed
- ProvideNamedValue
- ProvideValue
Constants ΒΆ
This section is empty.
Variables ΒΆ
View Source
var DefaultContainer = New()
Functions ΒΆ
func HealthCheck ΒΆ
func HealthCheckNamed ΒΆ
func Invoke ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
Provide(container, func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := Invoke[*test](container)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func InvokeNamed ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "my_service", func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := InvokeNamed[*test](container, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func MustInvoke ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
Provide(container, func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value := MustInvoke[*test](container)
fmt.Println(value)
Output: &{foobar}
func MustInvokeNamed ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "my_service", func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value := MustInvokeNamed[*test](container, "my_service")
fmt.Println(value)
Output: &{foobar}
func MustShutdown ΒΆ
func MustShutdownNamed ΒΆ
func Override ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
Provide(container, func(i *Container) (*test, error) {
return &test{foobar: "foobar1"}, nil
})
Override(container, func(i *Container) (*test, error) {
return &test{foobar: "foobar2"}, nil
})
value, err := Invoke[*test](container)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideNamed ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "my_service", func(i *Container) (*test, error) {
return &test{foobar: "foobar1"}, nil
})
OverrideNamed(container, "my_service", func(i *Container) (*test, error) {
return &test{foobar: "foobar2"}, nil
})
value, err := InvokeNamed[*test](container, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideNamedValue ΒΆ
Example ΒΆ
Container := New()
type test struct {
foobar string
}
ProvideNamedValue(Container, "my_service", &test{foobar: "foobar1"})
OverrideNamedValue(Container, "my_service", &test{foobar: "foobar2"})
value, err := InvokeNamed[*test](Container, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideValue ΒΆ
func Provide ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
Provide(container, func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := Invoke[*test](container)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideNamed ΒΆ
Example ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "my_service", func(i *Container) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := InvokeNamed[*test](container, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideNamedValue ΒΆ
Example ΒΆ
Container := New()
type test struct {
foobar string
}
ProvideNamedValue(Container, "my_service", &test{foobar: "foobar"})
value, err := InvokeNamed[*test](Container, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideValue ΒΆ
Example ΒΆ
Container := New()
type test struct {
foobar string
}
ProvideValue(Container, &test{foobar: "foobar"})
value, err := Invoke[*test](Container)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ShutdownNamed ΒΆ
Types ΒΆ
type Container ΒΆ
type Container struct {
// contains filtered or unexported fields
}
func New ΒΆ
func New() *Container
Example ΒΆ
Container := New() ProvideNamedValue(Container, "PG_URI", "postgres://user:pass@host:5432/db") uri, err := InvokeNamed[string](Container, "PG_URI") fmt.Println(uri) fmt.Println(err)
Output: postgres://user:pass@host:5432/db <nil>
func NewWithOpts ΒΆ
func NewWithOpts(opts *ContainerOpts) *Container
Example ΒΆ
container := NewWithOpts(&ContainerOpts{
HookAfterShutdown: func(container *Container, serviceName string) {
fmt.Printf("service shutdown: %s\n", serviceName)
},
})
ProvideNamed(container, "PG_URI", func(i *Container) (string, error) {
return "postgres://user:pass@host:5432/db", nil
})
MustInvokeNamed[string](container, "PG_URI")
_ = container.Shutdown()
Output: service shutdown: PG_URI
func (*Container) Clone ΒΆ
Clone clones injector with provided services but not with invoked instances.
Example ΒΆ
container := New() ProvideNamedValue(container, "PG_URI", "postgres://user:pass@host:5432/db") Container2 := container.Clone() services := Container2.ListProvidedServices() fmt.Println(services)
Output: [PG_URI]
func (*Container) CloneWithOpts ΒΆ
func (i *Container) CloneWithOpts(opts *ContainerOpts) *Container
CloneWithOpts clones injector with provided services but not with invoked instances, with options.
func (*Container) HealthCheck ΒΆ
Example ΒΆ
container := New() Provide(container, dbServiceProvider) health := container.HealthCheck() fmt.Println(health)
Output: map[*di.dbService:<nil>]
func (*Container) ListInvokedServices ΒΆ
Example (Invoked) ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "SERVICE_NAME", func(i *Container) (test, error) {
return test{foobar: "foobar"}, nil
})
_, _ = InvokeNamed[test](container, "SERVICE_NAME")
services := container.ListInvokedServices()
fmt.Println(services)
Output: [SERVICE_NAME]
Example (NotInvoked) ΒΆ
container := New()
type test struct {
foobar string
}
ProvideNamed(container, "SERVICE_NAME", func(i *Container) (test, error) {
return test{foobar: "foobar"}, nil
})
services := container.ListInvokedServices()
fmt.Println(services)
Output: []
func (*Container) ListProvidedServices ΒΆ
Example ΒΆ
container := New() ProvideNamedValue(container, "PG_URI", "postgres://user:pass@host:5432/db") services := container.ListProvidedServices() fmt.Println(services)
Output: [PG_URI]
func (*Container) Shutdown ΒΆ
Example ΒΆ
container := New() Provide(container, dbServiceProvider) err := container.Shutdown() fmt.Println(err)
Output: <nil>
func (*Container) ShutdownOnSIGTERM ΒΆ
ShutdownOnSIGTERM listens for sigterm signal in order to graceful stop service. It will block until receiving a sigterm signal.
func (*Container) ShutdownOnSignals ΒΆ
ShutdownOnSignals listens for signals defined in signals parameter in order to graceful stop service. It will block until receiving any of these signal. If no signal is provided in signals parameter, syscall.SIGTERM will be added as default signal.
type ContainerOpts ΒΆ
type Healthcheckable ΒΆ
type Healthcheckable interface {
HealthCheck() error
}
type Shutdownable ΒΆ
type Shutdownable interface {
Shutdown() error
}
Source Files
ΒΆ
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
healthcheckable
command
|
|
|
interface
command
|
|
|
shutdownable
command
|
|
|
simple
command
|
Click to show internal directories.
Click to hide internal directories.