Catalogue / go-service-module-blueprint
go-service-module-blueprint
Older skill: source not recordedSignedGenerates Go business services in a modular architecture: DTO structs, a port interface, a traced and logged implementation, and Fx dependency wiring. Use when the user says "create a new service module", "add a service for X", "wire this service with Fx", or asks where a service, its interface, or…--- name: go-service-module-blueprint description: >- Generates Go business services in a modular architecture: DTO structs, a port interface, a traced and logged implementation, and Fx dependency wiring. Use when the user says "create a new service module", "add a service for X", "wire this service with Fx", or asks where a service, its interface, or its DTO belongs. Not for HTTP handlers, database migrations, or repositories themselves. ---
Go Service Blueprint
Each business service is three small files plus one Fx line: a DTO, a port interface, and an implementation. Create or modify files only with the user's confirmation, and resolve the project's real module path and shared packages rather than copying placeholders.
File layout
| File | Path | Contents, in order | | --- | --- | --- | | DTO (if needed) | internal/modules/<module>/dto/<name>_dto.go | Input/output structs | | Port | internal/modules/<module>/ports/<name>_service.go | Fully documented XxxService interface | | Implementation | internal/modules/<module>/service/<name>_service.go | Struct → compile-time assertion → constructor → methods | | Wiring | internal/modules/<module>/fx.go | fx.Provide(fx.Annotate(...)) entry |
The three pieces
DTO — plain input/output structs:
package dto
type IssueLicenseInput struct {
ProductID string
SeatCount int
}Port — interface only, with a thorough comment (comments live here, not on the implementation):
package ports
import (
"context"
"<module>/internal/modules/licensing/dto"
)
// IssueLicenseService grants and persists a product license after a
// completed purchase. Use cases call it during checkout finalization.
type IssueLicenseService interface {
Execute(ctx context.Context, input dto.IssueLicenseInput) error
}Implementation — struct, assertion, constructor, methods:
package service
import (
"context"
"<module>/pkg/logger"
"<module>/pkg/otel/trace"
"<module>/internal/modules/licensing/dto"
"<module>/internal/modules/licensing/ports"
)
type IssueLicenseService struct {
logger logger.Logger
licenses ports.LicenseRepository
signingKey []byte
}
var _ ports.IssueLicenseService = (*IssueLicenseService)(nil)
func NewIssueLicenseService(
logger logger.Logger,
licenses ports.LicenseRepository,
signingKey []byte,
) *IssueLicenseService {
return &IssueLicenseService{logger: logger, licenses: licenses, signingKey: signingKey}
}
func (s *IssueLicenseService) Execute(ctx context.Context, input dto.IssueLicenseInput) error {
ctx, span := trace.Span(ctx, "IssueLicenseService.Execute")
defer span.End()
if err := s.licenses.Save(ctx, input.ProductID, input.SeatCount); err != nil {
s.logger.Error("IssueLicenseService.Execute failed", logger.Error(err))
return err
}
return nil
}Fx wiring:
fx.Provide( fx.Annotate( service.NewIssueLicenseService, fx.As(new(ports.IssueLicenseService)), ), ),
Service variants
- Single action — one
Execute(ctx, input)method with a dedicated
input DTO, as above.
- Related operations — descriptive method names instead of
Execute,
for example a webhook-signing service exposing GenerateFromPayload, CompareSignature, GenerateRandomBytes.
- Pure utility — no logger, config or tracing when the service does no
I/O: type XorService struct{} with func NewXorService() *XorService. Caution: the moment a "stateless" service can return an error, give it a logger field and log before returning.
Naming and constructor rules
- Port interface and implementation struct share the name
XxxService;
the packages (ports vs service) disambiguate.
- Constructor
NewXxxServicereturns*XxxService. The single exception
is fallible startup work (parsing a key, creating a directory, opening a connection), which returns (*XxxService, error).
- The logger parameter and field are always spelled
logger— never
l or log.
// correct func NewIssueLicenseService(logger logger.Logger) *IssueLicenseService // wrong func NewIssueLicenseService(l logger.Logger) *IssueLicenseService
Tracing and error logging
- Every method doing I/O takes
context.Contextfirst, opens
trace.Span(ctx, "StructName.MethodName"), and defers span.End(), passing the returned ctx onward.
- Pure utilities (hashing, template compilation) skip tracing.
- Log every returned error immediately, with the canonical form:
if err != nil {
s.logger.Error("IssueLicenseService.Execute failed", logger.Error(err))
return err
}- Never use the
WithErrorform (s.logger.WithError(err).Error(...));
it is banned in this convention. Redact secrets from messages and fields.
Helper and comment discipline
- No standalone functions next to a service struct. All helper logic —
even one-line string utilities — becomes a private method on the receiver.
// wrong: package-level helper beside struct methods
func normalize(input string) string { ... }
// correct: private method
func (s *SlugService) normalize(input string) string { ... }- No duplicated helpers across files. If two services need the same
utility, each defines a private method with the same name on its own struct; propose a shared service only when duplication grows.
- Comments: none on implementation structs, constructors or methods;
detailed purpose/usage comments on port interfaces.
- Dependencies: inject only interfaces —
ports.XxxService,
ports.XxxRepository, ports.XxxCache, logger.Logger — never concrete types.
Workflow and validation
- Create the DTO file if input/output structs are needed.
- Create the port interface in
ports/. - Create the implementation in
service/. - Add the Fx
fx.Provideentry to the module'sfx.go. - Run
make lint, thenmake nilaway(confirm these targets exist
locally before executing them).
- Test with fakes for repository and cache ports; never contact real
external systems from a unit test.
Never
- Depend on a concrete implementation where a port interface exists.
- Use
WithErrorlogging, or return an error from a service without logging it first. - Leave helper functions at package level or duplicate the same helper under two names in one package.
- Skip the compile-time assertion or return
(*XxxService, error)without a genuinely fallible startup step. - Put comments on implementation methods instead of the port interface.
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 6.5 kB | 47ab5d3d0369 |
| examples.json | 1.7 kB | efc2fad516b9 |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 1.0.1 Current | Not recorded | 6 Oct 2026 | |
| 1.0.0 | Not recorded | 3 Oct 2026 |
Locking a skill to one version needs Pro.