← Gin: web services in Go

GIN TUTORIAL · GO WEB SERVICES · 9 LESSONS

Gin and your data

Request contexts, JSON Merge Patch, safe filters, batched includes, response caches, streaming import and export, async jobs and signed webhooks.

  1. *gin.Context is a pooled struct, not your request context: pass c.Request.Context() to stores and goroutines

    Predict what *gin.Context returns as a context.Context under each ContextWithFallback setting and give stores and background work a context that is actually canceled, or deliberately not.

    7 min · Worked lesson

  2. PATCH with JSON Merge Patch in Gin: keep absent, null and value apart, then validate the merged resource

    Implement an RFC 7396 merge-patch endpoint in Gin that distinguishes absent from null, enforces application/merge-patch+json with 415, and validates the merged result before storing it.

    6 min · Worked lesson

  3. Gin list filters to parameterized SQL: parse strictly, bound IN lists, escape LIKE, end with a tiebreaker

    Write a pure function that turns a Gin list request into SQL text and arguments, with allow-listed sorts, a placeholder for every value, escaped LIKE patterns, bounded IN lists and a unique tiebreaker.

    6 min · Worked lesson

  4. ?include= without N+1 in Gin: one batched loader per relation, a bounded errgroup and list-order output

    Serve optional related data on a Gin list endpoint with one batched lookup per relation, bounded concurrent loads and deterministic output, and prove the query count by counting store calls.

    6 min · Worked lesson

  5. Cache public GETs in Gin: key by route template, bypass identity, coalesce misses, invalidate on write

    Build a Gin middleware that caches public GET responses under a route-template key, never stores per-user responses, coalesces concurrent misses with singleflight and stays correct across writes, TTLs and conditional requests.

    7 min · Worked lesson

  6. NDJSON imports in Gin: cap the body, decode one bounded line at a time, report each row by its line number

    Build an NDJSON import endpoint that bounds the body and each line, validates rows one at a time and returns a per-line report whose status matches what was written.

    7 min · Worked lesson

  7. Streaming CSV exports in Gin: keyset pages, two flushes, formula-safe cells and an abort on late failure

    Stream a large CSV from a Gin handler page by page with a safe download filename and formula-safe cells, and make a failure after the first byte visible to the client.

    8 min · Worked lesson

  8. Long work behind 202 Accepted in Gin: a bounded queue, fixed workers, a job resource and an honest 503

    Accept slow work with 202 and a job Location, bound it with a queue and a fixed worker pool, refuse overload with 503 and Retry-After, and keep jobs running after the request ends.

    7 min · Worked lesson

  9. Signed webhooks in Gin: verify the raw bytes, reject stale timestamps, dedupe event IDs, answer fast

    Receive signed webhooks in Gin by authenticating the raw body before binding, rejecting replays, deduplicating deliveries and choosing status codes that make the sender retry only when a retry can help.

    8 min · Worked lesson