Urgent.News

What's breaking now, across thousands of outlets.

Tech

Dependency Injection in Go, After Years of Symfony

The container I had, the container I don't have, and what I do instead. ๐Ÿ‘‹ I'm Anton - a software engineer working mostly in PHP/Symfony and Go, currently carving a live PHP monolith into Go services. This one is about the single piece of Symfony I missed most on the way over, and about what I found when I stopped missing it and looked at what I'd built in its place. Notes:โ€ฆ

Dependency injection in Go has been lacking compared to frameworks like Symfony in PHP. The author, Anton, worked on migrating a PHP monolith to Go services and missed the convenience of Symfony's dependency injection. This article examines the benefits and drawbacks of dependency injection in Go.

Symfony's container provided several key features:

- Autowiring: constructor parameters with interface types were resolved automatically without configuration.

- Services declared via configuration: a directory of classes could be added to the container with a single YAML file.

- Decoration: existing services could be wrapped in decorators to add cross-cutting behavior without modifying consumers.

- Lazy services: expensive dependencies could be delayed until the first method call.

- Per-environment graphs: different container configurations could be used for testing and production.

- Compiled container: the entire dependency graph could be resolved ahead of time and stored as generated PHP code.

The author argues that the benefits of dependency injection include:

- Adding a dependency only requires one constructor parameter, no configuration needed.

- The container verifies the entire graph before serving traffic.

However, the drawbacks include:

- The container's internal structure is not visible in the code, requiring special tools to inspect.

- Debugging issues like missing dependencies or ambiguous type hints requires running the container directly.

In contrast, Go does not have built-in dependency injection mechanisms. The language encourages passing dependencies as parameters at call sites, which must be explicitly written by developers. Anton argues that the same benefits of dependency injection in Go would require writing custom mechanisms, such as lazy construction, decoration, and per-environment substitution. These implementations would likely be inconsistent across services and difficult to discover or debug.

The author concludes that while dependency injection can improve code organization and testability, Go developers must manually implement these features, which can be error-prone and inconsistent. The absence of a built-in container in Go means developers must choose their own solutions, potentially leading to less reliable dependency management compared to frameworks like Symfony.

Written by urgent.news from Dev.to's reporting โ€” not their text. Machine-written โ€” may contain errors; check the original before relying on it.

Read the original at dev.to โ†’

More in Tech

More from Thursday 17 September โ†’