Quickstart
Register configuration and a database factory, resolve a service, and close the container.
Run the registration and resolution example
Use Go 1.27 or later. Install ioc, save the example as main.go, and run go run .. From the ioc source directory, run go run ./examples/basic. A local type stands in for the database connection.
go get github.com/luminyra/ioc// Command basic registers services in an ioc container, resolves them lazily
// by type with Go 1.27 generic methods, and closes them.
package main
import (
"context"
"fmt"
"github.com/luminyra/ioc"
)
type Config struct {
DSN string
}
type DB struct {
dsn string
}
// Close lets the container close the connection on Close (the io.Closer form).
func (db *DB) Close() error {
fmt.Println("closing database:", db.dsn)
return nil
}
type UserRepo struct {
db *DB
}
func main() {
c := ioc.New()
// Ready values are supplied directly.
c.Supply(&Config{DSN: "postgres://localhost/app"})
// Other services are registered as providers. They are built on first use
// and then reused; the *ioc.Container a provider receives resolves its own
// dependencies.
c.Provide(func(c *ioc.Container) (*DB, error) {
cfg, err := c.Resolve[*Config]()
if err != nil {
return nil, err
}
fmt.Println("connecting to database:", cfg.DSN)
return &DB{dsn: cfg.DSN}, nil
})
c.Provide(func(c *ioc.Container) (*UserRepo, error) {
db, err := c.Resolve[*DB]()
if err != nil {
return nil, err
}
return &UserRepo{db: db}, nil
})
// Resolve is a generic method: the requested type goes in brackets.
repo := c.MustResolve[*UserRepo]()
again := c.MustResolve[*UserRepo]()
fmt.Println("same instance:", repo == again)
fmt.Println("registered:", c.Registered())
fmt.Println("built: ", c.Built())
// Services are closed in reverse build order, through ioc.Closer,
// Shutdown(ctx) or io.Closer.
if err := c.Close(context.Background()); err != nil {
fmt.Println("close failed:", err)
}
}Observe construction and shutdown
Container.Supply registers an existing configuration value. Container.Provide registers the database and repository factories. Container.MustResolve builds dependencies on demand while resolving the repository. A second call returns the same repository. Container.Close releases the constructed connection.
c := ioc.New()
c.Supply(&Config{DSN: "postgres://localhost/app"})
c.Provide(func(c *ioc.Container) (*DB, error) {
cfg, err := c.Resolve[*Config]()
if err != nil {
return nil, err
}
return &DB{dsn: cfg.DSN}, nil
})
db := c.MustResolve[*DB]()
fmt.Println("resolved", db.dsn)
if err := c.Close(context.Background()); err != nil {
fmt.Println(err)
}resolved postgres://localhost/app
closing postgres://localhost/appContinue with service resolution.