Code Explainers

Go code explainers

go
package handlers
 
import (
	"encoding/json"

Hardening JSON decoding in Go HTTP handlers

json error-handling http
Intermediate 8 steps
go
package handlers
 
import (
	"net/http"

Validating query params with Gin binding tags

validation struct-tags pagination
Intermediate 8 steps
go
package api
 
import (
	"encoding/json"

Building reusable JSON helpers in Go HTTP handlers

http json error-handling
Intermediate 9 steps
go
package server
 
import (
	"net/http"

Serving cached static assets in Gin

caching static-files middleware
Intermediate 7 steps
go
package middleware
 
import (
	"net/http"

Per-IP rate limiting middleware in Gin

rate-limiting token-bucket concurrency
Intermediate 7 steps
go
package middleware
 
import (
	"net/http"

Building a CORS middleware in Gin

cors middleware closures
Intermediate 9 steps
go
package middleware
 
import (
	"context"

How a request-timeout middleware works in Gin

middleware context goroutines
Advanced 6 steps
go
package main
 
import (
	"context"

Graceful shutdown for a Gin server in Go

graceful-shutdown signals goroutines
Intermediate 6 steps
go
package token
 
import (
	"crypto/rand"

Generating secure random tokens in Go

cryptography randomness encoding
Intermediate 5 steps
go
package middleware
 
import (
	"bytes"

Capturing response bodies in Gin middleware

middleware response-capture struct-embedding
Intermediate 7 steps
go
func GetArticle(c *gin.Context) {
	id := c.Param("id")
 
	article, err := articleRepo.FindByID(c.Request.Context(), id)

Conditional GET with ETags in Gin

http-caching etag conditional-requests
Intermediate 7 steps
go
package middleware
 
import (
	"net/http"

How a www-to-apex redirect middleware works in Gin

middleware http-redirect closures
Intermediate 5 steps
go
package handlers
 
import (
	"errors"

Turning Gin binding errors into field messages

input validation struct tags error handling
Intermediate 9 steps
go
type Employee struct {
	Department string
	LastName   string
	FirstName  string

Multi-key sorting in Go

sorting comparators tie-breaking
Intermediate 6 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
go
package main
 
import (
	"html/template"

Auto-escaping HTML with Go templates

templating xss auto-escaping
Intermediate 6 steps
go
package middleware
 
import (
	"strconv"

Instrumenting a Gin API with Prometheus

middleware observability metrics
Intermediate 6 steps
go
type SensorReading struct {
	DeviceID    string    `json:"device_id" binding:"required"`
	Temperature float64   `json:"temperature"`
	RecordedAt  time.Time `json:"recorded_at" binding:"required"`

Streaming NDJSON ingestion in Gin

streaming batching json-decoding
Advanced 10 steps