Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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/...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: