Skip to main content

Getting started

Discover samber/do in less than 5 minutes.

What you'll need

Compatible with Go 1.18 or later.

This library has no dependencies except the Go std lib.

Import package:

go get -u github.com/samber/do/v2

Create a DI container

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

Play: https://go.dev/play/p/g549GqBbj-n

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

injector := do.New()

Service registration and invocation

Services can be declared as a singleton or a factory. In this example, we create two services, Car and Engine, with a simple dependency relationship.

Declare constructors and invoke singleton

// Provider
func NewCar(i do.Injector) (*Car, error) {
return &Car{
// import dependency
Engine: do.MustInvoke[*Engine](i),
Wheels: [4]*Wheel{
do.MustInvokeNamed[*Wheel](i, "front-left"),
do.MustInvokeNamed[*Wheel](i, "front-right"),
do.MustInvokeNamed[*Wheel](i, "back-left"),
do.MustInvokeNamed[*Wheel](i, "back-right"),
},
}, nil
}

type Car struct {
Engine *Engine
Wheels [4]*Wheel
}

func (c *Car) Start() {
c.Engine.Started = true
println("vroooom")
}

Register services using individual declaration

func main() {
// Create DI container and inject services
injector := do.New()

do.Provide(injector, NewCar)
do.Provide(injector, NewEngine)
do.ProvideNamed(injector, "front-left", NewWheel)
do.ProvideNamed(injector, "front-right", NewWheel)
do.ProvideNamed(injector, "back-left", NewWheel)
do.ProvideNamed(injector, "back-right", NewWheel)
do.ProvideValue(injector, &Config{
Port: 4242,
})

// Invoking Car will instantiate the singleton and its Engine+Wheel dependencies
car, err := do.Invoke[*Car](injector)
if err != nil {
log.Fatal(err.Error())
}

car.Start() // that's all folks 🤗

// Handle ctrl-c and shutdown services
injector.ShutdownOnSignals(syscall.SIGTERM, os.Interrupt)
}

Play: https://go.dev/play/p/cp5wNpo-5wn

Register services using package declaration

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

package car

// Export every services of a package and make them available in a single big provider
var Package = do.Package(
do.Lazy(NewCar),
do.Lazy(NewEngine),
do.LazyNamed("front-left", NewWheel),
do.LazyNamed("front-right", NewWheel),
do.LazyNamed("back-left", NewWheel),
do.LazyNamed("back-right", NewWheel),
)

Play: https://go.dev/play/p/kmf8aOVyj96