SkilVaultSign inStart free

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 NewXxxService returns *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.Context first, 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 WithError form (s.logger.WithError(err).Error(...));

it is banned in this convention. Redact secrets from messages and fields.

Helper and comment discipline

  1. 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 { ... }
  1. 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.

  1. Comments: none on implementation structs, constructors or methods;

detailed purpose/usage comments on port interfaces.

  1. Dependencies: inject only interfaces — ports.XxxService,

ports.XxxRepository, ports.XxxCache, logger.Logger — never concrete types.

Workflow and validation

  1. Create the DTO file if input/output structs are needed.
  2. Create the port interface in ports/.
  3. Create the implementation in service/.
  4. Add the Fx fx.Provide entry to the module's fx.go.
  5. Run make lint, then make nilaway (confirm these targets exist

locally before executing them).

  1. 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 WithError logging, 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.