Find Related products on Amazon

Shop on Amazon

Dependency injection frameworks add confusion

Published on: 2025-06-22 15:41:07

When working with Go in an industrial context, I feel like dependency injection (DI) often gets a bad rep because of DI frameworks. But DI as a technique is quite useful. It just tends to get explained with too many OO jargons and triggers PTSD among those who came to Go to escape GoF theology. Dependency Injection is a 25-dollar term for a 5-cent concept. — James Shore DI basically means passing values into a constructor instead of creating them inside it. That’s really it. Observe: type server struct { db DB } // NewServer constructs a server instance func NewServer () * server { db := DB {} // The dependency is created here return & server { db : db } } Here, NewServer creates its own DB . Instead, to inject the dependency, build DB elsewhere and pass it in as a constructor parameter: func NewServer ( db DB ) * server { return & server { db : db } } Now the constructor no longer decides how a database is built; it simply receives one. In Go, DI is often done using interfaces. ... Read full article.