A New, Simpler Way to Do Dependency Injection in Go

Elliot Chance
7 min readJun 7, 2019

Dependency injection (DI) is a great thing. Even if you haven’t heard of the term, it’s likely that you have already used it.

This article assumes zero existing knowledge of DI. However, a basic understanding of Go. I will work from fundaments, challenges, solutions and eventually lead to how to build a complete service container.

Anatomy of This Article

If you are already familiar with DI, you can skip to Introducing 🐺 Dingo. I will talk about a new project for generating a service container from YAML.

  1. Overview and Terminology: Some basic language and concepts that will be referred to.
  2. Simple Example: Diving right in with a simple example that leads to a common problem. We will refactor it to use dependency injection so that unit tests can be created.
  3. With Great Complexity Comes Great Responsibly: Explains the cost and associated problems with DI as the codebase grows.
  4. Building the Services With Functions: The simplest method of dealing with the aforementioned problems in the previous section.
  5. Singletons: An important optimization and DI concept. Explains how it affects your code.
  6. Introducing: 🐺 dingo: Putting it all together with dingo

--

--

Elliot Chance

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com