GIN TUTORIAL · GO WEB SERVICES · 10 LESSONS
Gin in the wider system
Upstream clients, retries and circuit breakers, compression, ranged downloads, content negotiation, versioning, contract tests, admin endpoints, release builds and rolling deploys.
Upstream calls from Gin: one shared client, the request deadline, a bounded body and your own error codes
Call an upstream HTTP API from a Gin handler through a shared, budgeted http.Client and map every failure to your own 404, 502 or 504 without leaking upstream details.
7 min · Worked lesson
Retrying upstream calls from Gin: only when safe, jittered backoff, a retry budget and a circuit breaker
Wrap upstream calls in a retry policy that respects idempotency, Retry-After and a shared budget, and use a circuit breaker so Gin answers 503 quickly while a dependency is down.
7 min · Worked lesson
Gzip in Gin by hand: negotiate q-values, wrap the writer, add Vary and know what never to compress
Write a gzip middleware for Gin that negotiates Accept-Encoding correctly and skips responses that must not be compressed, and accept gzip request bodies without a decompression-bomb hole.
6 min · Worked lesson
File downloads in Gin: ServeContent for ranges and validators, a safe Content-Disposition, files opened by ID
Serve downloadable files from Gin with correct Range, If-Range, ETag and HEAD behavior, a Content-Disposition built by mime.FormatMediaType and file access confined to an os.Root.
6 min · Worked lesson
Content negotiation in Gin: what NegotiateFormat really matches, 406 in your format and Vary: Accept
Serve JSON, CSV and HTML from one Gin handler with q-value-aware negotiation, a 406 that lists what is available, Vary: Accept for caches and a ?format= override where URLs must name the representation.
6 min · Worked lesson
API versions in Gin: one service under /v1 and /v2, an adapter per version, and a v1 that announces its sunset
Serve two API versions from one service through per-version DTO adapters, mark v1 with Deprecation, Sunset and successor-version links, and test that v1 routes survive until the announced date.
6 min · Worked lesson
Keep the OpenAPI document and the Gin router honest: diff routes, operations and status codes in a test
Write a test that converts engine.Routes() to OpenAPI path templates, reports routes and operations that exist on only one side, and checks recorded status codes against declared responses.
5 min · Worked lesson
Debug endpoints in Gin: pprof and expvar on a separate admin engine and listener, never on the public router
Mount net/http/pprof and expvar on an admin-only Gin engine behind a network and token guard, and prove with httptest that the public engine does not serve them.
5 min · Worked lesson
Shipping a Gin binary: release mode, a /version endpoint from build info, static builds and a non-root image
Build a Gin service for production with release mode, -trimpath, -ldflags -X and CGO_ENABLED=0, report its identity from runtime/debug build info, and package it in a minimal non-root image.
6 min · Worked lesson
Surviving a rolling deploy in Gin: fail readiness, keep serving through a drain delay, then Shutdown
Implement and test a SIGTERM sequence that flips readiness to 503, keeps serving while load balancers stop routing, disables keep-alives, and shuts the http.Server down within a grace period the platform honors.
6 min · Worked lesson