Skip to main content

Container

Default options

The simplest way to start is to use the default parameters:

import "github.com/samber/do/v2"

injector := do.New()

Global container

For a quick start, you may use the default global container. This is highly discouraged in production.

import "github.com/samber/do/v2"

do.Provide(nil, ...)
do.Invoke(nil, ...)

// equal to:

do.Provide(do.DefaultRootScope, ...)
do.Invoke(do.DefaultRootScope, ...)

Register services on container initialization

The services can be assembled into a package, and then, imported all at once into a new container.

// pkg/stores/package.go

var Package = do.Package(
do.Lazy(NewPostgresqlConnectionService),
do.Lazy(NewUserRepository),
do.Lazy(NewArticleRepository),
do.EagerNamed("repository.logger", slog.New(slog.NewTextHandler(os.Stdout, nil))),
)
// cmd/main.go

import "example/pkg/stores"

injector := do.New(stores.Package)

Custom options

import "github.com/samber/do/v2"

injector := do.NewWithOps(&do.InjectorOpts{
HookBeforeRegistration []func(scope *do.Scope, serviceName string),
HookAfterRegistration []func(scope *do.Scope, serviceName string),
HookBeforInvocation []func(scope *do.Scope, serviceName string),
HookAfterInvocation []func(scope *do.Scope, serviceName string, err error),
HookBeforeShutdown []func(scope *do.Scope, serviceName string),
HookAfterShutdown []func(scope *do.Scope, serviceName string, err error),

Logf func(format string, args ...any) {
// ...
},

HealthCheckParallelism: 100,
HealthCheckGlobalTimeout: 1 * time.Second,
HealthCheckTimeout: 100 * time.Millisecond,
})