[ANN] inj – idiomatic dependency injection for Go

158 views
Skip to first unread message

Paul M Fox

unread,
Jan 3, 2016, 8:25:03 PM1/3/16
to golang-nuts
Hello!

I used to love package-level globals. There was a wicked, slightly guilty delight in making a shared object that could be used by all the other packages in my application. Nothing pleases me more than brief, human readable function calls like database.Query() or log.Info(). How neat they are, how terse and laconic. And now beautifully simple it is to simply drop in an import statement and start making function calls right away.

But then reality caught up with me, as it always will, because I started to take testing much more seriously than I ever had before. New obsessions came into my life. BDD. Integration testing. Mocked-out full-stack testing. Suddenly my beloved package-level globals were, for the most part, tarnished artefacts of the past. My only option realistic option was dependency injection. I was daunted, I admit; afraid what of might happen. 

I went intravenous, straight to the heart. There are other approaches to dependency injection in Go (Facebook's offering being the most prominent) but none of them felt quite right. Not Go-y enough, or not quite the rigorously-tested software craftsmanship for which I yearned. So, with the guidance and wisdom of my colleagues, I built my own package. I call it inj. It's got a few months of production testing under its belt, and it's used in a few professional, real-world applications already.

Reply all
Reply to author
Forward
0 new messages