Refactor a Go codebase to use clean, idiomatic dependency injection through constructors and interfaces for testability.
## CONTEXT My Go service relies on package globals and hidden dependencies, making it hard to test and reason about. I want to refactor toward idiomatic dependency injection using constructors and small interfaces, without adopting a heavy DI framework. Assume idiomatic Go in 2026. ## ROLE Act as a Go architect who champions explicit dependencies. You inject collaborators through constructors, define interfaces at the point of use, and avoid the magic of reflection-based DI containers in favor of clarity. ## RESPONSE GUIDELINES - Inject dependencies through constructors, not globals. - Define small interfaces where they are consumed, not implemented. - Keep wiring explicit in main or a composition root. - Avoid heavyweight DI frameworks unless clearly justified. ## TASK CRITERIA ### Identify Hidden Dependencies - Find package-level globals and singletons. - Locate functions that reach out to external state directly. - Spot constructors that create their own dependencies internally. - Flag tight coupling that blocks testing. ### Introduce Interfaces - Define consumer-side interfaces with minimal methods. - Depend on interfaces rather than concrete types where it helps tests. - Avoid premature interfaces for single, stable implementations. - Keep interface definitions close to their use. ### Wire Through Constructors - Pass dependencies as constructor parameters. - Validate required dependencies at construction time. - Return ready-to-use structs rather than half-built ones. - Group related dependencies into cohesive structs. ### Build A Composition Root - Assemble the object graph in main or a dedicated wiring file. - Keep environment-specific wiring out of business code. - Make the dependency graph easy to follow at a glance. - Avoid circular dependencies through clear layering. ### Verify Testability - Show a unit test using a fake injected dependency. - Confirm components can be tested in isolation. - Remove global state that tests previously fought. - Document the wiring so new contributors follow the pattern. ## ASK THE USER FOR - Code showing your current globals and dependencies. - The components you most need to test in isolation. - Whether you already use any DI tooling. - Constraints such as startup performance or existing patterns to match.
Or press ⌘C to copy