Typed keys
A Key[T] names a service and fixes its type. Register takes a T and Get returns one: no
interface{}, no type assertions.
Register services under typed keys and get them back as their own types, with no type assertions or reflection. Reads never wait for a lock.
go get github.com/vardius/gocontainer/v2 // A key names a service and fixes its type.// Declare it once and share it.var ( DB = gocontainer.NewKey[*sql.DB]("db") Users = gocontainer.NewKey[UserStore]("users"))var c gocontainer.Container
DB.Register(&c, openDB())// A *SQLUsers goes in as a UserStore.Users.Register(&c, NewSQLUsers(DB.MustGet(&c)))
// And comes out as one: no type assertion.users := Users.MustGet(&c)func TestRoutes(t *testing.T) { // The same key, a fake, and a container // of the test's own. var c gocontainer.Container Users.Register(&c, fakeUsers{"1": "Ada"})
srv := httptest.NewServer(routes(&c)) defer srv.Close() // GET /users/1 now answers "Ada".}Features
Typed keys
A Key[T] names a service and fixes its type. Register takes a T and Get returns one: no
interface{}, no type assertions.
Checked when you build
A value of the wrong type under a key doesn’t compile. A concrete type goes in under an interface key as it is.
Reads never wait
Get, MustGet and Has take no lock and allocate nothing, so goroutines reading at once don’t slow
each other down.
No globals
The zero Container is ready to use, and nothing runs in init(). Declare a container where you wire
your services.
Easy to fake
A key works with any container. A test registers a fake under the same key in a container of its own.
Small API
One type to hold services, one to name them, five methods. No dependencies beyond the standard library.
Performance
v2 reads from a map it never changes once published, so a read is an atomic load and a lookup, with no lock. The calls that replace v1’s reflection-based Invoke are about twenty times faster. Writes copy the map, so they cost more than v1’s.
| Scenario | v1 | v2 |
|---|---|---|
| Get | 5.8 ns0 allocs | 3.6 ns0 allocs |
| Get, not registered | 4.8 ns0 allocs | 3.3 ns0 allocs |
| MustGet | 6.4 ns0 allocs | 3.5 ns0 allocs |
| Has | 6.2 ns0 allocs | 3.4 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
| Scenario | v1 | v2 |
|---|---|---|
| Invoke in v1, Get in v2 | 80.9 ns0 allocs | 3.6 ns0 allocs |
| MustInvoke in v1, MustGet in v2 | 71.0 ns0 allocs | 3.5 ns0 allocs |
| Three services: MustInvokeMany in v1, three MustGets in v2 | 162.7 ns5 allocs | 10.2 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
| Scenario | v1 | v2 |
|---|---|---|
| Get from 16 goroutines at once | 128.1 ns0 allocs | 0.27 ns0 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
| Scenario | v1 | v2 |
|---|---|---|
| Register, replacing a service | 9.5 ns0 allocs | 224.6 ns5 allocs |
Apple M4 Max, Go 1.27.0, median of 5 runs.
Upgrading
String ids become typed keys, and Invoke gives way to Get and MustGet, which return the type you want. The global container is gone: you declare your own.
gocontainer.Register("db", openDB())
gocontainer.MustInvoke("db", func(db *sql.DB) { // use db },)var DB = gocontainer.NewKey[*sql.DB]("db")
var c gocontainer.ContainerDB.Register(&c, openDB())
db := DB.MustGet(&c) // use dbInstall the module, declare a key for each service, and register them in a container. The guide walks through it.