Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Go Dependency Injection in 150 lines (github.com/jswidler)
2 points by swid on Feb 27, 2022 | hide | past | favorite | 3 comments


There 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


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


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




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

Search: