Coding Standard: Go

By Engineering Team•September 27, 2026•6 min read

Go is the default backend language for domain/CRUD/aggregation microservices and the platform’s only Temporal workflow host — see ADR-00010 for why. This doc is the concrete rulebook a Go service is actually held to; Architecture-00007 covers the same shape across all three backend languages side by side.

Services on this standard

tenant-service, shop-service, search-service, product-catalog, inventory-service, content-processor, user-preference-service, shipment-service, order-service.

Folder layout — feature first, layer second

A new feature adds one file per layer under its own feature name — never a new top-level layer, and never an empty placeholder for a layer the feature doesn’t need.

src/
  internal/
    domain/<feature>.go               ← pure business types, one file per aggregate
    domain/<feature>_validation.go    ← validation — always its own file
    repository/<feature>/             ← entity + mapper + interface + implementation
    service/<feature>/                ← interface + implementation
    transport/api/<feature>/          ← REST controller + api-model + mapper
    transport/grpc/<feature>/         ← gRPC server + DTO mapper, composed from @templates/go-grpc-microservice/
    message_publisher/<event>.go      ← flat file per event type, not a subdirectory
    message_receiver/<event>.go
  test/<feature>/                     ← all test files, never next to source
  go.work                             ← local dev override, not copied into Docker builds

Naming & DI

  • Fx module names follow <feature><Layer> in camelCase — categoryRepository, categoryService — and must be unique within the service’s Fx graph. A collision (two features both registering a module literally named repository) fails at wiring time, not compile time.
  • Aggregator module names are template-specific — check @templates/go-mongo-microservice/CLAUDE.md or @templates/go-postgres-microservice/CLAUDE.md for the exact list before adding a new one.

Error handling

Never use errors.New(). Always use the typed constructors from go-lang-core-lib’s pkg/utils/errors/ — FieldValidation, NotFound, ConfigError, DuplicateError, InternalError.

The domain object (DTO) parameter pattern

A service method never takes multiple positional scalar parameters — pass one domain object, at every layer boundary:

// Wrong
func (s *OrderService) Create(ctx context.Context, tenantID, userID, sku string, qty int) (*Order, error)

// Correct
func (s *OrderService) Create(ctx context.Context, input CreateOrderInput) (*Order, error)

Context propagation — mandatory, every boundary

Always pass context.Context through every application boundary. Inside a goroutine launched from a service or repository, never use context.Background() — it discards the OTel span, tenant_id, user_id, and correlation_id. Use context.WithoutCancel(ctx) instead:

func (s *MyService) PublishAsync(ctx context.Context, payload MyPayload) error {
    bgCtx := context.WithoutCancel(ctx)
    go func() {
        if _, err := s.publisher.Publish(bgCtx, payload); err != nil {
            log.LogErrorWithTrace(bgCtx, "PublishAsync failed", zap.Error(err))
        }
    }()
    return nil
}

Tracing — otel.WithSpan[T] and gowrap

Wrap every service and repository boundary in a span:

result, err := otel.WithSpan(ctx, "CategoryService.Create", func(ctx context.Context) (*Category, error) {
    return r.repo.Insert(ctx, doc)
})

Generate the tracing decorator for any controller/service/repository/receiver/publisher interface via a single go:generate gowrap directive — never hand-write one:

//go:generate gowrap gen -p . -i <InterfaceName> -t ../../../../../go-lang-core-lib/generator/otel_generator.tmpl -o <feature>_tracing_gen.go -l ""

The ../../../../../ depth is fixed at 5 levels back to the monorepo root for every feature package — never shorten it or switch to an absolute path or ~. A method whose shape doesn’t fit (T, error) needs a hand-wrapped local type result struct{...} reapplied after every regeneration (see inventory-service’s tax_tracing_gen.go, shop-service’s userpreference_tracing_gen.go).

Validation — its own file, always

domain/<feature>_validation.go is a separate file from domain/<feature>.go — never inline validation logic inside the domain type’s own file.

Entity/mapper split

repository/<feature>/ splits into entity + mapper + interface + implementation by default. Collapse the mapper into the entity file only for a genuinely 1:1 shape, and only when the matching template’s own CLAUDE.md explicitly allows it for that case.

Testing convention

  • All test files live in src/test/<feature>/ — never next to source files.
  • Package <feature>_test (external test package) — tests call only exported symbols.
  • Unit tests cover domain Validate() methods and pure business logic, zero external dependencies, run with go test ./test/... on a bare machine.
  • t.Cleanup drops the test collection after each test — never share state between tests.
  • Test database is always <service>_test, never the development database.
  • TenantID is injected via auth.SetTenantIDToContext in every repository integration test.
  • Never mock the repository in integration tests — use the real implementation against <service>_test.

External API integrations

A third-party HTTP API (a payment gateway, a courier API) is wrapped behind repository/<provider>/ exactly like a database or gRPC dependency — service/ never holds a raw *http.Client. One repository method per provider endpoint, never one aggregate method that internally sequences several HTTP calls (shipment-service’s repository/shiprocket/ — separate Login, CreateOrder, AssignAWB methods — is the reference).

No over-engineering

No extra abstractions, error handling, or features beyond what the task requires. Don’t validate scenarios that can’t happen — trust internal code and framework guarantees; validate only at system boundaries.

Comments

No comments unless the WHY is genuinely non-obvious — a hidden constraint, a subtle invariant, a workaround for a specific bug. Never describe what the code does, and never reference the current task or fix.