Code Explainers

Gin code explainers

go
package handlers
 
import (
	"net/http"

Struct-level validation for partial updates in Gin

validation struct-tags partial-update
Intermediate 7 steps
go
package handlers
 
import (
	"net/http"

Content negotiation in Gin handlers

content-negotiation struct-tags rest-api
Intermediate 4 steps
go
package handlers
 
type BookURI struct {
	Collection string `uri:"collection" binding:"required,alphanum"`

Validating URI params in a Gin handler

input validation struct binding error handling
Intermediate 7 steps
go
package middleware
 
import (
	"crypto/subtle"

Building an API-key middleware in Gin

middleware authentication dependency-injection
Intermediate 5 steps
go
package api
 
func RegisterRoutes(r *gin.Engine, deps *Dependencies) {
	r.GET("/health", func(c *gin.Context) {

Layering route groups and middleware in Gin

routing middleware authentication
Intermediate 8 steps
go
package middleware
 
import (
	"compress/gzip"

How gzip response compression works in Gin

middleware compression http
Intermediate 8 steps
go
package middleware
 
import (
	"context"

Correlation ID tracing middleware in Gin

middleware request tracing structured logging
Intermediate 7 steps
go
package middleware
 
import (
	"bytes"

Verifying webhook HMAC signatures in Gin

hmac middleware webhooks
Intermediate 7 steps
go
package handlers
 
import (
	"fmt"

Handling secure file uploads in Gin

file-upload validation security
Intermediate 7 steps
go
package server
 
import (
	"net/http"

Serving a single-page app with Gin

static-files spa routing
Intermediate 7 steps
go
package handlers
 
import (
	"fmt"

Handling multipart profile uploads in Gin

form binding file upload validation
Intermediate 7 steps
go
package middleware
 
import (
	"time"

Building a request logger middleware in Gin

middleware structured-logging closures
Intermediate 6 steps
go
func UploadFiles(c *gin.Context) {
	form, err := c.MultipartForm()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid multipart form"})

Handling multi-file uploads in Gin

file-upload multipart validation
Intermediate 8 steps
go
package auth
 
import (
	"net/http"

Building a JWT auth middleware in Gin

middleware jwt authentication
Intermediate 7 steps
go
func RecoveryHandler(logger *zap.Logger) gin.HandlerFunc {
	return gin.CustomRecovery(func(c *gin.Context, recovered any) {
		var brokenPipe bool
		if ne, ok := recovered.(*net.OpError); ok {

A panic-recovery middleware in Gin

middleware panic-recovery structured-logging
Intermediate 6 steps
go
package main
 
import (
	"net/http"

How route groups nest in Gin

routing middleware api versioning
Intermediate 6 steps
go
func (h *TransactionHandler) ExportCSV(c *gin.Context) {
	ctx := c.Request.Context()
	filters := parseTransactionFilters(c)
 

Streaming a CSV export in Gin

streaming csv-export database-cursor
Intermediate 8 steps
go
package handlers
 
type SignupRequest struct {
	Email           string `json:"email" binding:"required,email"`

Validated signup handler in Gin

request validation error handling http status codes
Intermediate 7 steps