go-usecases

Writes and reviews Go clean-architecture usecase layer code with transaction and error-handling conventions.

2|Updated Sep 2, 2022
One-click install
npx skills add https://github.com/silvioubaldino/personal-finance --skill go-usecases-silvioubaldino
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: go-usecases
Source: https://github.com/silvioubaldino/personal-finance/tree/main/.claude/skills/go-usecases
Command: npx skills add https://github.com/silvioubaldino/personal-finance --skill go-usecases-silvioubaldino

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? It enforces consistent style when writing or modifying business logic in a Go clean-architecture usecase layer, preventing common mistakes like importing concrete repositories, misplacing validation inside transactions, or adding sentinel errors without matching HTTP error-handler cases. ## Core Features & Use Cases - Interface-at-point-of-use enforcement: Declares narrow Repository, Gateway, and UseCase dependency interfaces in the same file as the consuming usecase, listing only the methods actually called. - Transaction boundary rules: Wraps multi-step writes in txManager.WithTransaction with gorm, validates input before opening transactions, and guards optional validators with nil checks. - Three-case error handling: Distinguishes error wrapping with fmt.Errorf, domain.WrapInvalidInput-style business rejections, and direct sentinel returns, with sentinel registration in usecase_errors.go plus a matching errors_handler.go case. - Use Case: When adding a new Add method to a Movement usecase that reserves credit and persists a record atomically, follow the canonical template to wire dependencies, open one transaction, and wrap errors in English lowercase action-phrased messages. ## Quick Start Ask the AI to add a new usecase method in internal/usecase following the go-usecases conventions, including its dependency interfaces, transaction handling, and sentinel error registration.

Frequently Asked Questions about go-usecases

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a Go usecase in clean architecture?▼

Create one file per feature in internal/usecase named {feature}_usecase.go with package usecase. Define narrow dependency interfaces in the same file, name the struct after the domain entity without a Usecase suffix, and return it by value from a New{Entity} constructor.

How do I handle transactions with gorm in a usecase layer?▼

Wrap multi-step writes in txManager.WithTransaction with a func(tx *gorm.DB) error closure, assign results to outer-scoped variables, and return err from the closure. Single mutating repository calls do not need an explicit transaction, and validation must run before opening one.

Should usecases import concrete repository structs in Go?▼

No, usecases must declare narrow interfaces like {Feature}Repository or {Feature}Gateway in the same file, listing only the methods actually called. Concrete repository and HTTP client implementations live in the infrastructure layer.

How do I add a new sentinel error in a Go usecase package?▼

Add the sentinel as Err{Description} in the flat var block of usecase_errors.go, then add a matching case domain.Is(err, usecase.ErrXxx) in errors_handler.go's toAPIError. Without the handler case, the error silently falls through to a 500 response.

When should a usecase depend on another usecase instead of a repository?▼

Depend on another usecase when you need its business logic rather than plain CRUD, such as Movement depending on InvoiceUseCase for FindOrCreateInvoiceForMovement. It is acceptable to hold both a repository and a usecase dependency for the same feature.