4 ms·
Go Dependency Injection in 150 lines
- swid 5y agoSaw the uber/fx this morning and decided to give another go at sharing this small project. The thing is, dependency injection doesn't need to be all that complicated. I think, if nothing else, it makes testing easier and helps you be sure you can run all your tests in parallel. I'm not here to defend DI if you don't like it, but otherwise happy to answer questions about it. Check out the test file for some kind of example on how to use it.
- roy2220 5y agoThere is a defect while using struct tags to name dependencies - struct tags are static as hard-coded, sometimes we may need to choose a dependency by a dynamic name according to external configuration, but struct tags can't be changed. Some days ago I had some thoughts on dependency injection and made a tiny library as well, just for reference: https://github.com/go-tk/di https://github.com/go-tk/di
- swid 5y agoReading from config isn’t a problem with the way I am doing it… the name is only the key for the thing to inject, you need some code to fill that key to something else dynamically already.