3 ms·
> Go's context can be used as a DI container. the WithValue/Value API allows you to treat the context as essentially a map[string]any. [0] so if you want to m
by evil-olive 2y ago
> Go's context can be used as a DI container.
the WithValue/Value API allows you to treat the context as essentially a map[string]any. [0]
so if you want to make the definition so expansive as to be meaningless, then yes, map[string]any can be used as a "DI container". and so can a context object, because it exposes a map[string]any-ish API.
0: though if you read the implementation [1] looking up a value in a context is essentially a linked-list traversal and not a hashtable lookup
1: https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/context/context.go;l=766 https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/...