← Gin: web services in Go

GIN TUTORIAL · GO WEB SERVICES · 11 LESSONS

Gin foundations

Engines, routes, parameters, groups, binding sources, validators, JSON, uploads, templates and 404/405 handling.

  1. Your first Gin service: an engine, two routes and a test request

    Build a Gin engine, register routes with a path parameter and exercise them without opening a port.

    4 min · Worked lesson

  2. Path parameters in Gin: :param takes one segment, *wildcard takes the rest, conflicts panic at boot

    Declare Gin route templates with :param and *wildcard, predict how slashes and escapes resolve, and label metrics with c.FullPath().

    6 min · Worked lesson

  3. Query strings and forms in Gin: tell absent from empty, parse numbers strictly, bound every limit

    Read query and form values with the Gin helper that matches the case, convert them with strconv and reject bad input with a clear 400.

    5 min · Worked lesson

  4. Gin route groups: prefixes and middleware are copied at registration, so test the route table

    Organize a Gin API into nested, versioned groups with scoped middleware and prove with r.Routes() that every route has the protection you intended.

    5 min · Worked lesson

  5. Gin JSON responses: know what each renderer writes and return a DTO, never your storage model

    Choose between c.JSON, PureJSON, IndentedJSON, SecureJSON and AsciiJSON by their exact output, set status codes deliberately and render a response DTO with correct omitempty and omitzero tags.

    5 min · Worked lesson

  6. Gin binding sources: bind path, query, headers and body separately, and cache the body to read it twice

    Bind each request source with its own Gin binder and tag, predict what content-type-driven ShouldBind reads, and decode one body twice with ShouldBindBodyWithJSON.

    5 min · Worked lesson

  7. Custom validators in Gin: register domain rules once and return field errors clients can rely on

    Register a custom tag and a struct-level rule on Gin's validator, validate slice elements with dive and convert validator errors into a stable field-level JSON response.

    5 min · Worked lesson

  8. Writing Gin middleware: what c.Next returns to, what Abort does not do, and typed request values

    Write Gin middleware that runs code after c.Next, stops the chain with Abort and return, shares typed values through c.Set and c.Get, and is ordered so logs see the final outcome.

    5 min · Worked lesson

  9. File uploads in Gin: cap the request, sniff the bytes and never let the client name the file

    Accept a multipart upload in Gin with a hard size cap, decide its type from its content, generate the storage name yourself and test it with a request built by mime/multipart.

    5 min · Worked lesson

  10. Server-rendered HTML in Gin: embed the templates, trust contextual escaping, cache assets on purpose

    Render html/template pages with c.HTML from templates parsed out of an embedded file system, serve embedded static assets with StaticFS and set cache headers per kind of asset.

    5 min · Worked lesson

  11. 404, 405 and redirects in Gin: answer misses in your own format and pick redirect codes by what must survive

    Configure NoRoute, NoMethod and HandleMethodNotAllowed for consistent JSON 404 and 405 responses with a correct Allow header, and choose between 301, 302, 303, 307 and 308 for redirects.

    5 min · Worked lesson