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 aT. - Container holds the services. The zero value is empty and ready to use.
Install
Section titled “Install”gocontainer needs Go 1.26 or newer.
go get github.com/vardius/gocontainer/v2Your first container
Section titled “Your first container”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:
// Command wiring registers an app's services at startup, each built from the ones registered before it.//// go run ./examples/wiringpackage 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)) }}Run it
Section titled “Run it”-
From a clone of the repository:
Terminal window go run ./examples/wiring -
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=3Hello, stranger.
What just happened
Section titled “What just happened”- Each key, made with
NewKey, named a service and fixed its type:usersKeyis aKey[Users]. usersKey.Registertook amemoryUsers, which implementsUsers, with no conversion. A value that isn’t aUserswouldn’t have compiled.- The greeter was built from services registered before it, with
MustGet, which returns the key’s type: no type assertions anywhere. maindeclared the container. There is no global one, and nothing ran ininit().
Next steps
Section titled “Next steps”KeysDeclaring keys, interface keys, and why every key is unique.
ContainerRegister, Get, MustGet, Has and Deregister, and what they cost.
TestingSwap a service for a fake in a test's own container.
BenchmarksWhat each call costs, and how v2 compares with v1.