Go Dependency Injection with Wire

Several months ago I wrote a blog post about dependency injection in go. In the time since that post was written, Google has released a fantastic new dependency injection container for go called wire. I much prefer wire to other containers in the go ecosystem. This post will explain why. A (Very) Brief Primer on Dependency Injection Dependency injection (DI) is a style of writing code such that the dependencies of a particular object (or, in go, a struct) are provided at the time the object is initialized. ...

April 7, 2019 · Drew Olson

Lazy Providers in Dig

If you haven’t yet read my previous post on depedency injection in Go, please do so first. This post describes a simple technique to make dig’s DI container even more powerful. All values provided by dig’s container are singletons. The provider is called the first time the value is required and the result is cached. All subsequent providers that depend on this value used the cached result. This behavior is usually very desireable but there are times when we want a fresh value to be produced by the container. Often this is useful for data that is scoped by a lifecycle that is shorter than the lifecycle of the application itself (say, the lifecycle of an HTTP request inside of an HTTP server). ...

May 25, 2018 · Drew Olson

Dependency Injection in Go

Update - you should probably read my more recent post about dependency injection with wire. I recently built a small project in Go. I’ve been working with Java for the past few years and was immediately struck by the lack of momentum behind Dependency Injection (DI) in the Go ecosystem. I decided to try building my project using Uber’s dig library and was very impressed. I found that DI helped solve a lot of problems I had encountered in my previous Go applications – overuse of the init function, abuse of globals and complicated application setup. ...

May 15, 2018 · Drew Olson