Component's dependencies separator
At first, register a factory of *sql.DB
as component.
import "github.com/koron-go/materialize"
materialize.MustAdd(func() (*sql.DB, error) {
return sql.Open(os.Getenv("DB_DRIVER"), os.Getenv("DB_DATASOURCE_NAME"))
})
Then, obtain a *sql.DB
instance when you use.
The instance will be created automatically, and managed as singleton.
var db *sql.DB
err := materialize.Materialize(&db)
if err != nil {
return err
}
// TODO: let's work with "db".
All materiazlied instances which implement Close() error
or Close()
method,
will be closed when you call materialize.CloseAll()
.
materialize.CloseAll()