container

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 15 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference CI CodeQL Go Report Card Coverage Status

Container Logo

Container

Container is a lightweight yet powerful IoC (dependency injection) container for Go projects. It's built neat, easy-to-use, and performance-in-mind to be your ultimate requirement.

Features:

  • Singleton and Transient bindings
  • Named dependencies (bindings)
  • Scoped containers (a nested scope tree sharing a single root)
  • Resolve by functions, variables, and structs
  • Must helpers that convert errors to panics
  • Optional lazy loading of bindings
  • Global instance for small applications
  • Concurrency-safe with no race conditions
  • Circular dependency detection, backed by a graph of the bindings
  • Dependency graph visualization as Graphviz DOT, a terminal tree, or a live HTML page
  • Bind-time and resolve-time parameter injection
  • 100% Test coverage!

Documentation

Required Go Versions

It requires Go v1.26 or newer versions.

Installation

To install this package, run the following command in your project directory.

go get github.com/danceable/container

Next, include it in your application:

import "github.com/danceable/container"
Introduction

Container works by binding abstractions (interfaces) to their concrete implementations via resolver functions. You register a binding with Bind(), passing a resolver function that returns the concrete type, along with optional configuration:

  • Singleton (bind.Singleton()): The resolver is called once; the same instance is returned for every subsequent request.
  • Transient (default): The resolver is called on every request, producing a new instance each time.
  • Named (bind.WithName("...")) : Multiple concretes can be registered for the same abstraction under different names.
  • Lazy (bind.Lazy()): Defers the resolver invocation until the binding is first resolved.

Once bindings are registered, you can retrieve concretes through:

  • Resolve(&target) — fills a variable with the bound concrete.
  • Call(fn) — invokes a function whose parameters are automatically resolved from the container.
  • Fill(&struct) — injects dependencies into struct fields tagged with container:"type" or container:"name".

Your code depends on abstractions, not implementations!

Quick Start

The following example demonstrates a simple binding and resolving.

err := container.Bind(func() Config {
    return &JsonConfig{...}
}, bind.Singleton())

var c Config
err = container.Resolve(&c)
Examples
Global Instance

The package provides a default global Container instance — exposed as container.Default — for convenience in small applications. Instead of creating a container with container.New(), you can call container.Bind(), container.Resolve(), container.Call(), container.Fill(), and container.Reset() directly as package-level functions; they all delegate to container.Default.

You can also access container.Default directly if you need to pass the global instance to a function or a Must helper.

// No need to create a container — uses the global instance (container.Default)
container.Bind(func() Database {
    return &MySQL{Host: "localhost"}
}, bind.Singleton())

var db Database
container.Resolve(&db)

container.Call(func(db Database) {
    db.Connect()
})

// Pass the global instance to a Must helper
container.MustBind(container.Default, func() Cache {
    return &RedisCache{}
}, bind.Singleton())

// Reset clears all bindings from the global instance
container.Reset()
Singleton Binding

A singleton binding creates one shared instance. The resolver is called once, and every subsequent resolve returns the same object.

c := container.New()

err := c.Bind(func() Database {
    return &MySQL{Host: "localhost", Port: 3306}
}, bind.Singleton())

var db1, db2 Database

// db1 and db2 point to the same instance
c.Resolve(&db1)
c.Resolve(&db2)
Transient Binding

A transient binding (the default) calls the resolver on every resolve, producing a fresh instance each time.

c := container.New()

err := c.Bind(func() Logger {
    return &FileLogger{Path: "/var/log/app.log"}
})

var l1, l2 Logger

// l1 and l2 are different instances
c.Resolve(&l1)
c.Resolve(&l2)
Named Binding

Named bindings allow registering multiple concretes for the same abstraction under different names.

c := container.New()

c.Bind(func() Database {
    return &MySQL{Host: "primary"}
}, bind.Singleton(), bind.WithName("primary"))

c.Bind(func() Database {
    return &MySQL{Host: "replica"}
}, bind.Singleton(), bind.WithName("replica"))
Lazy Binding

A lazy binding defers resolver invocation until the first time the binding is resolved, rather than at bind time. This is useful when a dependency isn't always needed or is expensive to create.

c := container.New()

err := c.Bind(func() Cache {
    return NewRedisCache("localhost:6379") // not called until first resolve
}, bind.Singleton(), bind.Lazy())
Eager Binding

An eager binding (the default) invokes the resolver immediately at bind time to validate it. For singletons, this also creates and caches the instance right away.

c := container.New()

// The resolver runs immediately — any error is returned by Bind.
err := c.Bind(func() Database {
    return &MySQL{Host: "localhost"}
}, bind.Singleton())
Resolving by Name

Use resolve.WithName() to retrieve a specific named binding during Resolve, Call, or Fill.

c := container.New()

c.Bind(func() Database {
    return &MySQL{Host: "primary"}
}, bind.Singleton(), bind.WithName("primary"))

c.Bind(func() Database {
    return &MySQL{Host: "replica"}
}, bind.Singleton(), bind.WithName("replica"))

var replica Database
c.Resolve(&replica, resolve.WithName("replica"))
Resolving with Runtime Parameters

Use resolve.WithParams() to supply values at resolve time. These are matched by type to the resolver's arguments and take precedence over container bindings.

Important: When using resolve.WithParams(), bind with bind.Lazy() so the resolver is not invoked until the parameters are available at resolve time. Without bind.Lazy(), an eager binding will fail if parameters cannot be resolved at bind time.

c := container.New()

// Bind with Lazy() so the resolver isn't called until resolve time,
// when we have the DSN parameter available
c.Bind(func(dsn string) Database {
    return &MySQL{DSN: dsn}
}, bind.Lazy())

var db Database
// Provide the DSN value at resolve time
c.Resolve(&db, resolve.WithParams("user:pass@tcp(localhost)/mydb"))
// db is now a MySQL instance with the provided DSN
Binding with Pre-set Parameters

Use bind.ResolveDepenenciesByParams() to lock in specific parameter values at bind time. These take precedence over container lookups but can still be overridden by resolve.WithParams().

c := container.New()

c.Bind(func(timeout int) Cache {
    return &RedisCache{Timeout: timeout}
}, bind.Lazy(), bind.ResolveDepenenciesByParams(30))

var cache Cache
c.Resolve(&cache) // resolver receives timeout=30
Binding with Named Dependency Resolution

Use bind.ResolveDependenciesByNamedBindings() to wire a resolver's arguments to specific named bindings instead of the default (unnamed) ones.

c := container.New()

c.Bind(func() Database {
    return &MySQL{Host: "replica"}
}, bind.WithName("replica"), bind.Singleton(), bind.Lazy())

c.Bind(func(db Database) ReportService {
    return &Reporter{DB: db}
}, bind.Lazy(), bind.ResolveDependenciesByNamedBindings("replica"))

var svc ReportService
c.Resolve(&svc) // Reporter receives the "replica" Database
Scoped Containers

Scope() derives a nested child container that shares the same root scope, forming a scope tree. A scope can resolve bindings registered on itself or on any of its ancestors, while its own bindings stay invisible to ancestor and sibling scopes. This is useful for layering request- or task-scoped dependencies on top of long-lived application-wide ones.

root := container.New()

// Application-wide singleton, visible to every scope in the tree.
root.Bind(func() Database {
    return &MySQL{Host: "localhost"}
}, bind.Singleton())

// A nested scope — e.g. per request.
request := root.Scope("request")

// Bindings on the child shadow ancestors and stay local to this scope.
request.Bind(func() Logger {
    return &RequestLogger{ID: "req-123"}
}, bind.Singleton())

var db Database
request.Resolve(&db) // resolved from the root scope

var log Logger
request.Resolve(&log) // resolved from the request scope

A binding's dependencies are resolved from the scope where the binding was registered. A service bound on the root therefore never sees services that live only in a descendant scope. Calling Scope() with a name that already exists on a scope returns the existing child, so the tree never holds duplicate siblings. The package-level container.Scope() derives a scope from the global container.Default.

root := container.New()
a := root.Scope("a")
b := root.Scope("a") // a == b — same child returned

a.Root()   // == root
a.Parent() // == root
a.Name()   // "a"
Parameter Resolution Precedence

When a resolver function has arguments, the container resolves them using multiple sources. If the same argument type is available from more than one source, the following precedence applies (highest to lowest):

  1. Resolve-time params (resolve.WithParams()) — values passed when calling Resolve or Call.
  2. Bind-time params (bind.ResolveDepenenciesByParams()) — values locked in at binding time.
  3. Named bindings (bind.ResolveDependenciesByNamedBindings()) — values pulled from named container entries.
  4. Container lookup — the default unnamed binding for the matching type.

A resolver argument of an interface type falls back to a bound type that implements it when the interface itself is not bound. When several bound types implement it, the one registered first answers — every time, so the same argument never resolves to a different binding from one call to the next.

Resolve() and Fill() do not fall back this way: they match the type exactly, so Resolve(&shape) needs a binding for Shape itself and does not settle for a bound *Circle that implements it. Bind the abstraction when you want it resolved directly.

c := container.New()

// 4. Container lookup (lowest priority)
c.Bind(func() Shape { return &Circle{Area: 99} }, bind.Singleton(), bind.Lazy())

// 3. Named binding
c.Bind(func() Shape { return &Circle{Area: 5} }, bind.WithName("special"), bind.Singleton(), bind.Lazy())

// Resolver with bind-time params (2) and named bindings (3) configured
c.Bind(func(x int, s Shape) Database {
    return &PostgreSQL{X: x, Area: s.GetArea()}
}, bind.Lazy(),
    bind.ResolveDepenenciesByParams(10),              // 2. bind-time param for int
    bind.ResolveDependenciesByNamedBindings("special"), // 3. named binding for Shape
)

var db Database

// Without resolve-time params: int=10 (bind-time), Shape.Area=5 (named binding)
c.Resolve(&db)

// With resolve-time params (highest priority): overrides both int and Shape
c.Resolve(&db, resolve.WithParams(42, &Circle{Area: 77}))
Circular Dependency Detection

The container keeps the registrations of a scope as a directed graph: every binding is a node, and every dependency it cannot satisfy on its own is an edge pointing at the binding that satisfies it. Bind() walks that graph from the binding it is about to register, and refuses it when the walk leads back to where it started — so a cycle is caught at bind time, before anything is ever built. The error tells you the path of the cycle:

c := container.New()

c.Bind(func(d Database) Shape { return &Circle{} }, bind.Lazy())        // fine: no Database is registered yet
err := c.Bind(func(s Shape) Database { return &MySQL{} }, bind.Lazy())  // closes the loop

errors.Is(err, containerErrors.ErrCircularDependency) // true
err.Error() // "container: circular dependency detected: main.Database -> main.Shape -> main.Database"

The graph only holds the dependencies the container actually resolves. Arguments supplied at bind time with bind.ResolveDepenenciesByParams() are not edges, so they break a cycle instead of forming one, and edges follow the same lookup the container performs at resolve time — the named bindings given with bind.ResolveDependenciesByNamedBindings() first, then the name of the binding itself. A refused binding is rolled back: the container is left exactly as it was.

Visualizing the Dependency Graph

Visualize() writes the same graph in the Graphviz DOT format, which is handy for seeing what an application actually wired up:

var buf bytes.Buffer
if err := c.Visualize(&buf); err != nil {
    log.Fatal(err)
}

os.WriteFile("container.dot", buf.Bytes(), 0o600)
// dot -Tsvg container.dot -o container.svg
digraph container {
	rankdir = LR;
	node [shape = box, style = rounded, fontname = "Helvetica"];
	edge [fontname = "Helvetica"];

	subgraph cluster_0 {
		label = "root";

		n0 [label = "main.Database\nsingleton, resolved"];
		n1 [label = "main.Shape\ntransient"];
	}

	subgraph cluster_1 {
		label = "scope \"request\"";

		n2 [label = "main.Logger\nsingleton"];
	}

	n1 -> n0;
	n2 -> n0;
}

Every binding becomes a node labelled with what it provides and how — named or not, singleton or transient, already built or not — and every dependency becomes an edge. Each scope becomes a cluster, and edges cross them: the graph covers the scope it is called on, the ancestors it resolves from, and its named descendants. A dependency no binding satisfies — one passed at resolve time, or a missing one — is drawn dashed.

The drawing itself is left to a visualize.Renderer, DOT being the default. Pass another one with visualize.WithRenderer():

Renderer Draws
visualize.DOT{} The Graphviz DOT source above.
visualize.ASCII{} A text tree per scope, for the terminal. Color: true draws a cycle in red with ANSI escape codes.
visualize.HTML{} A standalone page laying the graph out in the browser with Viz.js, loaded from a CDN. Title names the page.
c.Visualize(os.Stdout, visualize.WithRenderer(visualize.ASCII{Color: true}))
root
|-- main.Database (singleton, resolved)
`-- main.Shape (transient)
    `-> main.Database

scope "request"
`-- main.Logger (singleton)
    `-> main.Database [root]

VisualizeHandler() serves the graph over HTTP, as the HTML page unless told otherwise. It draws the graph anew on every request:

http.Handle("/debug/container", c.VisualizeHandler())
http.Handle("/debug/container.dot", c.VisualizeHandler(visualize.WithRenderer(visualize.DOT{})))

Any type implementing Render(w io.Writer, g visualize.Graph) error and ContentType() string can be passed as a renderer, to draw the graph in a format of your own.

Container Methods
Method Signature Description
New New() *Container Creates a new container instance (a root scope).
Scope Scope(name string) *Container Derives a nested child scope that shares the same root scope. Resolves bindings from itself and its ancestors. Returns the existing child if the name is already taken.
Name Name() string Returns the scope's name (empty for a root scope).
Root Root() *Container Returns the root scope of the tree.
Parent Parent() *Container Returns the enclosing scope, or nil for a root scope.
Bind Bind(resolver any, opts ...bind.BindOption) error Registers a resolver function that maps an abstraction to its concrete implementation.
Resolve Resolve(abstraction any, opts ...resolve.ResolveOption) error Fills a pointer-to-interface (or pointer-to-type) with the matching concrete from the container.
Call Call(function any, opts ...resolve.ResolveOption) error Invokes a function whose parameters are automatically resolved from the container. The function may optionally return an error.
Fill Fill(structure any, opts ...resolve.ResolveOption) error Injects dependencies into struct fields tagged with container:"type" or container:"name".
Visualize Visualize(w io.Writer, opts ...visualize.Option) error Writes the dependency graph of the container to w, in the Graphviz DOT format unless another renderer is given.
VisualizeHandler VisualizeHandler(opts ...visualize.Option) http.Handler Serves the dependency graph of the container over HTTP, as an HTML page unless another renderer is given.
Reset Reset() Removes all bindings and empties the container.

Each method also has a Must variant (MustBind, MustResolve, MustCall, MustFill) that panics on error instead of returning it:

// These two are equivalent:
err := c.Bind(func() Database { return &MySQL{} }, bind.Singleton())
if err != nil { panic(err) }

container.MustBind(c, func() Database { return &MySQL{} }, bind.Singleton())
Bind Options

Options passed to Bind() to configure how a binding behaves.

Option Description
bind.Singleton() Marks the binding as a singleton — the resolver is called once, and the same instance is returned on every resolve.
bind.Lazy() Defers resolver invocation until the binding is first resolved. Without this, the resolver runs eagerly at bind time.
bind.WithName(name) Assigns a name to the binding, allowing multiple concretes for the same abstraction.
bind.ResolveDepenenciesByParams(params...) Provides concrete values at bind time to satisfy the resolver's arguments (matched by type).
bind.ResolveDependenciesByNamedBindings(names...) Specifies named bindings to use when resolving the resolver's arguments. Each argument takes the first of these names that has a binding for its type.
c.Bind(func() Database {
    return &MySQL{Host: "replica"}
}, bind.Singleton(), bind.Lazy(), bind.WithName("replica"))
Resolve Options

Options passed to Resolve(), Call(), or Fill() to customize how a binding is looked up and invoked.

Option Description
resolve.WithName(name) Selects a specific named binding instead of the default (unnamed) one.
resolve.WithParams(params...) Supplies runtime values to satisfy the resolver's arguments (matched by type). These take the highest precedence.
var db Database
c.Resolve(&db, resolve.WithName("replica"), resolve.WithParams("custom-dsn"))
Must Helpers (Panic on Error)

Every container method (Bind, Resolve, Call, Fill) has a corresponding Must variant that panics instead of returning an error. These are package-level functions that accept the container as the first argument. They are useful in application setup code where a failed binding or resolution indicates a programming error that should halt execution immediately.

Function Wraps Description
MustBind(c, resolver, opts...) c.Bind(...) Registers a binding or panics.
MustResolve(c, abstraction, opts...) c.Resolve(...) Resolves a dependency or panics.
MustCall(c, function, opts...) c.Call(...) Calls a function with injected dependencies or panics.
MustFill(c, structure, opts...) c.Fill(...) Fills struct fields or panics.
c := container.New()

// Panics if the binding fails
container.MustBind(c, func() Database {
    return &MySQL{Host: "localhost"}
}, bind.Singleton())

// Panics if the resolution fails
var db Database
container.MustResolve(c, &db)

// Panics if the call fails
container.MustCall(c, func(db Database) {
    db.Connect()
})

// Panics if filling fails
type App struct {
    DB Database `container:"type"`
}
var app App
container.MustFill(c, &app)

Documentation

Overview

Package container is a lightweight yet powerful IoC container for Go projects. It provides an easy-to-use interface and performance-in-mind container to be your ultimate requirement.

Index

Constants

This section is empty.

Variables

View Source
var Default = New()

Default is the default concrete of the Container.

Functions

func Bind

func Bind(receiver any, opts ...bind.BindOption) error

Bind calls the same method of the default concrete.

func Call

func Call(receiver any, opts ...resolve.ResolveOption) error

Call calls the same method of the default concrete.

func Fill

func Fill(receiver any, opts ...resolve.ResolveOption) error

Fill calls the same method of the default concrete.

func MustBind

func MustBind(c *Container, resolver any, opts ...bind.BindOption)

MustBind wraps the `Bind` method and panics on errors instead of returning the errors.

func MustCall

func MustCall(c *Container, function any, opts ...resolve.ResolveOption)

MustCall wraps the `Call` method and panics on errors instead of returning the errors.

func MustFill

func MustFill(c *Container, structure any, opts ...resolve.ResolveOption)

MustFill wraps the `Fill` method and panics on errors instead of returning the errors.

func MustResolve

func MustResolve(c *Container, abstraction any, opts ...resolve.ResolveOption)

MustResolve wraps the `Resolve` method and panics on errors instead of returning the errors.

func Reset

func Reset()

Reset calls the same method of the default concrete.

func Resolve

func Resolve(abstraction any, opts ...resolve.ResolveOption) error

Resolve calls the same method of the default concrete.

func Visualize added in v1.2.0

func Visualize(w io.Writer, opts ...visualize.Option) error

Visualize calls the same method of the default concrete.

func VisualizeHandler added in v1.3.0

func VisualizeHandler(opts ...visualize.Option) http.Handler

VisualizeHandler calls the same method of the default concrete.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container holds the registrar and provides methods to interact with bindings. It is the entry point in the package.

Containers form a scope tree: every Container created with New is a root scope, and Scope derives nested child scopes from it. A scope can resolve bindings registered on itself or any of its ancestors, but bindings are never visible to ancestor or sibling scopes.

func Derive added in v1.1.0

func Derive() *Container

Derive calls the same method of the default concrete.

func New

func New() *Container

New creates a new root scope of the Container.

func Scope added in v1.1.0

func Scope(name string) *Container

Scope calls the same method of the default concrete.

func (*Container) Bind

func (c *Container) Bind(resolver any, opts ...bind.BindOption) error

Bind maps an abstraction to concrete and instantiates if it is a singleton binding.

func (*Container) Call

func (c *Container) Call(function any, opts ...resolve.ResolveOption) error

Call takes a receiver function with one or more arguments of the abstractions (interfaces). It invokes the receiver function and passes the related concretes.

func (*Container) Derive added in v1.1.0

func (c *Container) Derive() *Container

Derive returns an anonymous child scope that is NOT registered on its parent. It resolves bindings from itself and its ancestors exactly like a named Scope, but because the parent keeps no reference to it, the scope — and the bindings it owns — becomes eligible for garbage collection as soon as the caller drops it. This makes Derive the cheapest way to create an ephemeral, per-operation scope: there is nothing to clean up.

func (*Container) Fill

func (c *Container) Fill(structure any, opts ...resolve.ResolveOption) error

Fill takes a struct and resolves the fields with the tag `container:"inject"`

func (*Container) Name added in v1.2.0

func (c *Container) Name() string

Name returns the name of the scope, empty for a root or a derived one.

func (*Container) Parent added in v1.1.0

func (c *Container) Parent() *Container

Parent returns the enclosing scope, or nil if the scope is a root scope.

func (*Container) Reset

func (c *Container) Reset()

Reset deletes all the existing bindings and empties the container.

func (*Container) Resolve

func (c *Container) Resolve(abstraction any, opts ...resolve.ResolveOption) error

Resolve takes an abstraction (reference of an interface type) and fills it with the related concrete.

func (*Container) Root added in v1.1.0

func (c *Container) Root() *Container

Root returns the root scope of the tree the scope belongs to.

func (*Container) Scope added in v1.1.0

func (c *Container) Scope(name string) *Container

Scope returns a nested child scope that shares the same root scope, forming a scope tree. The child can resolve bindings registered on itself or on any ancestor scope, while its own bindings stay invisible to ancestor and sibling scopes. Calling Scope with a name that already exists on this scope returns the existing child, so the tree never holds duplicate siblings.

func (*Container) Visualize added in v1.2.0

func (c *Container) Visualize(w io.Writer, opts ...visualize.Option) error

Visualize writes the dependency graph of the container to w, in the Graphviz DOT format unless another renderer is given:

var buf bytes.Buffer
if err := c.Visualize(&buf); err != nil { ... }
os.WriteFile("container.dot", buf.Bytes(), 0o600)

// dot -Tsvg container.dot -o container.svg

c.Visualize(os.Stdout, visualize.WithRenderer(visualize.ASCII{Color: true}))

It covers the scope it is called on, the ancestors it resolves from and its named descendants: a cluster per scope, a node per binding, an edge per dependency. A dependency no binding satisfies is drawn dashed, a cycle red.

func (*Container) VisualizeHandler added in v1.3.0

func (c *Container) VisualizeHandler(opts ...visualize.Option) http.Handler

VisualizeHandler returns an http.Handler serving the dependency graph of the container, as an HTML page unless another renderer is given:

http.Handle("/debug/container", c.VisualizeHandler())

The graph is drawn anew on every request, so it shows the bindings of the moment.

Directories

Path Synopsis
internal
graph
Package graph represents directed graphs and detects cycles in them.
Package graph represents directed graphs and detects cycles in them.
Package visualize draws the dependency graph of a container.
Package visualize draws the dependency graph of a container.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL