Go API Services: Idiomatic, Tested, Production Go

    by Ignasi

    1

    Build and review idiomatic, production-grade Go services with structured logging, sqlc, and safe concurrency.

    Free

    1 useSecurity scanned

    Works with the AI Agents you already use

    Go API Services: Idiomatic, Tested, Production Go

    Example session with this skill installed

    Build a Go 1.22 REST API for a simple inventory service. I need a GET endpoint for items by ID and a POST endpoint. Use sqlc for the database and slog for logging.

    • Read your context and instructions
    • Compiled the go api services

    I have scaffolded the inventory service using idiomatic Go 1.22 patterns. The implementation includes structured logging, context-aware database operations, and a production-ready project layout.

    Inventory Service Implementation

    Project Layout

    • cmd/api/main.go: Entry point with graceful shutdown and dependency injection.
    • internal/http/handler.go: HTTP handlers using Go 1.22 path routing.
    • internal/store/db.go: Data access layer designed for sqlc integration.

    Implementation Code

    // internal/http/handler.go
    package http
    
    import (
    	"encoding/json"
    	"log/slog"
    	"net/http"
    )
    
    type ItemStore interface {
    	GetItem(ctx context.Context, id string) (Item, error)
    	CreateItem(ctx context.Context, item Item) error
    }
    
    func RegisterRoutes(mux *http.ServeMux, store ItemStore, logger *slog.Logger) {
    	mux.HandleFunc("GET /items/{id}", handleGetItem(store, logger))
    	mux.HandleFunc("POST /items", handleCreateItem(store, logger))
    }
    
    func handleGetItem(s ItemStore, l *slog.Logger) http.HandlerFunc {
    	return func(w http.ResponseWriter, r *http.Request) {
    		id := r.PathValue("id")
    		item, err := s.GetItem(r.Context(), id)
    		if err != nil {
    			l.Error("failed to get item", slog.String("id", id), slog.Any("error", err))
    			http.Error(w, "item not found", http.StatusNotFound)
    			return
    		}
    		json.NewEncoder(w).Encode(item)
    	}
    }
    
    func handleCreateItem(s ItemStore, l *slog.Logger) http.HandlerFunc {
    	return func(w http.ResponseWriter, r *http.Request) {
    		var item Item
    		if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1048576)).Decode(&item); err != nil {
    			http.Error(w, "bad request", http.StatusBadRequest)
    			return
    		}
    		if err := s.CreateItem(r.Context(), item); err != nil {
    			l.Error("creation failed", slog.Any("error", err))
    			http.Error(w, "internal error", http.StatusInternalServerError)
    			return
    		}
    		w.WriteHeader(http.StatusCreated)
    	}
    }
    

    Next steps

    1. Run sqlc generate to create the type-safe models for the internal/store package.
    2. Configure environment variables for PGURL and PORT before starting the server.
    3. Use the provided Dockerfile to build the static, non-root container image.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Standardize your Go microservices with idiomatic patterns that prioritize observability and reliability. This skill addresses the common pitfalls of Go development, such as goroutine leaks, improper error handling, and fragile project structures that become unmaintainable as they scale.

    What it does

    • Architects project layouts using a clean cmd/ and internal/ structure to prevent circular dependencies.
    • Implements robust IO with strict context.Context propagation, timeouts, and graceful shutdown sequences.
    • Enforces error handling through structured wrapping with errors.Is/As and centralized HTTP status mapping.
    • Secures data access using sqlc for type-safe queries and pgxpool for efficient PostgreSQL connection management.
    • Optimizes concurrency via errgroup for bounded parallelism and race-free state management.

    How it works

    1. Scaffold or Review: Provide a requirement for a new service or existing code for a performance and idiom audit.
    2. Logic Implementation: The skill generates business logic in internal/service/ and data access in internal/store/.
    3. Infrastructure Wiring: It configures slog JSON logging, environment validation, and Go 1.22+ routing.
    4. Validation: It produces table-driven tests and Docker multi-stage builds for deployment.

    Frameworks & tools

    Go 1.22+, net/http, chi, echo, gin, pgx, sqlc, log/slog, goose, atlas, testcontainers, and golangci-lint.

    Why this beats prompting it yourself

    Generic prompts often produce "Java-in-Go" patterns or ignore critical production details like MaxBytesReader and ReadHeaderTimeout. This skill embeds specific Go 1.22 routing conventions and strict concurrency safety checks that prevent runtime data races and memory leaks.

    Use cases

    • Building a high-performance REST API with PostgreSQL.
    • Refactoring a monolithic main.go into a clean, testable internal structure.
    • Auditing existing services for goroutine leaks and missing context cancellations.
    • Setting up a CI-ready Go project with linting and multi-stage Dockerfiles.

    Known limitations

    Focuses on PostgreSQL via pgx; other drivers may require manual adjustment. OpenTelemetry integration is optional and basic.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before buying a skill.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    1 use

    Times used to date

    Free to use

    No account required to browse

    Trust & safety

    Security scan

    Security scan passed 11 days ago

    • Free with an Agensi account

    Permissions

    Terminal / Shell
    Listed11 days ago

    What's inside

    Frequently Asked Questions

    This skill generates idiomatic Go code using modern 1.22+ standards, specifically avoiding common pitfalls like circular dependencies and goroutine leaks while implementing production-grade features like structured logging and context-aware timeouts.

    The skill is designed to work with any advanced AI agent capable of file-system operations or code generation, providing the architectural guardrails necessary for building scalable microservices.

    Yes, you can provide existing code for a performance and idiom audit, and the skill will suggest refactors to align your project with clean internal/ service structures and type-safe data access.

    While the skill is optimized for PostgreSQL using pgx and sqlc for maximum type safety, the generated service and logic layers can be adapted to other SQL databases with minor adjustments to the store implementation.

    You receive the full logic for scaffolding production-grade APIs, including configurations for slog, environment validation, table-driven test templates, and CI-ready Docker multi-stage builds.

    New in APIs & Backend

    Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for APIs & Backend.

    Free