Skip to content

Getting started

gocontainer holds your app’s services, each under a key that fixes its type. It has two types:

  • Key[T] names a service of type T. Its methods put the service in a container and get it out as a T.
  • Container holds the services. The zero value is empty and ready to use.

gocontainer needs Go 1.26 or newer.

Terminal window
go get github.com/vardius/gocontainer/v2

This program has three services: a logger, a store of users, and a greeter built from the other two. It declares a key for each, registers them in order, and gets the greeter back to use it:

examples/wiring/main.go
// Command wiring registers an app's services at startup, each built from the ones registered before it.
//
// go run ./examples/wiring
package main
import (
"fmt"
"io"
"log/slog"
"os"
"github.com/vardius/gocontainer/v2"
)
// Users looks up a user's name by id.
type Users interface {
Name(id int) (string, bool)
}
// memoryUsers is a Users kept in a map.
type memoryUsers map[int]string
func (m memoryUsers) Name(id int) (string, bool) {
name, ok := m[id]
return name, ok
}
// Greeter greets users by name, and logs the ids it doesn't know.
type Greeter struct {
users Users
log *slog.Logger
}
func (g *Greeter) Greet(id int) string {
name, ok := g.users.Name(id)
if !ok {
g.log.Warn("unknown user", "id", id)
return "Hello, stranger."
}
return "Hello, " + name + "."
}
// Each key names a service and fixes its type.
var (
logKey = gocontainer.NewKey[*slog.Logger]("log")
usersKey = gocontainer.NewKey[Users]("users")
greeterKey = gocontainer.NewKey[*Greeter]("greeter")
)
// register adds the app's services to c, building each from those before it.
func register(c *gocontainer.Container, logs io.Writer) {
logKey.Register(c, slog.New(slog.NewTextHandler(logs, nil)))
// A memoryUsers goes in under the Users key: no conversion needed.
usersKey.Register(c, memoryUsers{1: "Ada", 2: "Grace"})
greeterKey.Register(c, &Greeter{
users: usersKey.MustGet(c),
log: logKey.MustGet(c),
})
}
func main() {
var c gocontainer.Container
register(&c, os.Stderr)
greeter := greeterKey.MustGet(&c)
for _, id := range []int{1, 2, 3} {
fmt.Println(greeter.Greet(id))
}
}
  1. From a clone of the repository:

    Terminal window
    go run ./examples/wiring
  2. It greets the two users it knows and logs a warning for the one it doesn’t:

    Hello, Ada.
    Hello, Grace.
    time=… level=WARN msg="unknown user" id=3
    Hello, stranger.
  • Each key, made with NewKey, named a service and fixed its type: usersKey is a Key[Users].
  • usersKey.Register took a memoryUsers, which implements Users, with no conversion. A value that isn’t a Users wouldn’t have compiled.
  • The greeter was built from services registered before it, with MustGet, which returns the key’s type: no type assertions anywhere.
  • main declared the container. There is no global one, and nothing ran in init().