Skip to content

samber/cc-skills-golang / skills/golang-samber-do

golang-samber-do

Verified

Dependency injection in Golang using samber/do — service containers, lifecycle management, scopes, health checks, graceful shutdown, and module organization. Apply when using or adopting samber/do, when the codebase imports github.com/samber/do or github.com/samber/do/v2, or when refactoring manual constructor injection into a DI container.

FileKindSize
SKILL.mdskill md9.1 KB
evals/evals.jsonconfig11.6 KB
references/advanced.mdmarkdown5.7 KB
references/testing.mdmarkdown1.4 KB

references/testing.md

View on GitHub
# Testing with samber/do

## Container Cloning

Clone containers for isolated tests:

```go
func TestUserService(t *testing.T) {
    // Create test container by cloning main container
    testInjector := mainInjector.Clone()

    // Override with mocks
    mockDB := &MockDatabase{}
    do.OverrideValue(testInjector, mockDB)

    // Test with mocked dependencies
    service := do.MustInvoke[UserService](testInjector)
    // ... test code
}
```

## Reusable Test Helpers

```go
func SetupTestContainer(t *testing.T) do.Injector {
    injector := do.New()

    do.Provide(injector, func(i do.Injector) (Database, error) {
        return &MockDatabase{}, nil
    })

    return injector
}
```

## Quick Reference

### Testing & Overrides

| Function                         | Purpose                         |
| -------------------------------- | ------------------------------- |
| `injector.Clone()`               | Clone container for testing     |
| `injector.CloneWithOpts()`       | Clone with custom options       |
| `do.Override[T]()`               | Replace service (use in tests)  |
| `do.OverrideNamed[T]()`          | Replace named service           |
| `do.OverrideValue[T]()`          | Replace value service           |
| `do.OverrideNamedValue[T]()`     | Replace named value             |
| `do.OverrideTransient[T]()`      | Replace transient factory       |
| `do.OverrideNamedTransient[T]()` | Replace named transient factory |
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 323kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 323kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 323kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 323kUpdated Claude CodeCodexVerifiedInstall