3 ms·
> as if Golang's context has anything at all to do with DI Yes, Go's context can be used as a DI container.
by paulddraper 2y ago
> as if Golang's context has anything at all to do with DI
Yes, Go's context can be used as a DI container.
- tomcam 2y agoI thought it was mostly used to terminate an asynchronous event?
- paulddraper 2y agoYeah it can inject a signal for that.
- 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/...