← ClaudeAtlas

golang-dependency-injectionlisted

Design, simplify, or review Go dependency wiring: constructors, composition roots, interface boundaries, lifecycle ownership, test substitution, and DI tool tradeoffs. Use for manual injection, container evaluation, or an existing generated/runtime dependency graph.
reagin/agent-skills · ★ 0 · AI & Automation · score 63
Install: claude install-skill reagin/agent-skills
# Dependency Injection in Go Make dependencies and lifecycle ownership visible. Prefer the simplest wiring that fits the existing application. ## Inspect the graph first Read constructors, startup code, shutdown paths, tests, and any existing container or code-generation configuration. Sketch: - long-lived resources such as database pools, clients, queues, and servers; - which component creates and closes each resource; - optional or environment-specific implementations; - cycles, hidden globals, and dependencies retrieved at runtime; - test seams that are actually used. Do not rewrite working construction code merely to impose a library or a uniform constructor shape. ## Default approach Manual constructor injection is usually the clearest baseline: - constructors accept required collaborators explicitly; - configuration is parsed and validated before construction; - startup code composes concrete implementations; - cleanup ownership follows construction ownership; - callers receive useful construction errors rather than partially initialized objects. Use interfaces where the consumer needs behavioral abstraction, multiple implementations exist, or a meaningful test boundary benefits. Do not create an interface for every struct, and do not move a consumer-specific interface into an implementation package merely for centralization. Avoid package initialization for fallible setup, mutable package globals, and service locators. Passing a container into business compon