Code Explainers

Go code explainers

go
package retry
 
import (
	"errors"

How a retry budget caps retries in Go

rate-limiting sliding-window concurrency
Intermediate 7 steps
go
package middleware
 
import (
	"crypto/subtle"

Timing-safe API key auth in Gin

middleware authentication constant-time-compare
Intermediate 8 steps
go
package handlers
 
import (
	"context"

A custom Gin validator backed by Redis

validation redis struct-tags
Intermediate 8 steps
go
package trie
 
import "sort"
 

Building a frequency-ranked autocomplete trie in Go

trie autocomplete recursion
Intermediate 9 steps
go
package useragent
 
import (
	"regexp"

Parsing user-agent strings in Go

regex string-parsing ordering
Intermediate 7 steps
go
func ServeThumbnail(c *gin.Context) {
	raw := c.Param("token")
	sig := c.Query("sig")
 

A signed thumbnail endpoint in Gin

signed-urls image-processing http-handler
Intermediate 9 steps
go
package middleware
 
import (
	"hash/fnv"

A percentage feature-rollout middleware in Gin

feature-flags middleware hashing
Intermediate 6 steps
go
package payment
 
import (
	"regexp"

Masking card numbers in Go

regex string-processing redaction
Intermediate 6 steps
go
package slug
 
import (
	"regexp"

Turning titles into URL slugs in Go

unicode-normalization regexp string-processing
Intermediate 8 steps
go
package middleware
 
import (
	"github.com/gin-gonic/gin"

Injecting a scoped GORM query via Gin middleware

middleware dependency-injection request-context
Intermediate 9 steps
go
package handlers
 
import (
	"net/http"

Locale-aware pricing in a Gin handler

internationalization content-negotiation currency-formatting
Intermediate 9 steps
go
type Warning struct {
	Field   string `json:"field"`
	Code    string `json:"code"`
	Message string `json:"message"`

Partial-success responses in Gin

error-handling input-validation http-status
Intermediate 8 steps
go
package dedup
 
import (
	"crypto/sha256"

Finding duplicate files by content hash in Go

hashing filesystem deduplication
Intermediate 8 steps
go
package storage
 
import (
	"errors"

Preventing path traversal in Go

path-traversal input-validation security
Intermediate 9 steps
go
package logsample
 
import (
	"sync"

A per-key rate limiter for log sampling

rate-limiting concurrency time-windows
Intermediate 7 steps
go
func (h *ReportHandler) ExportTransactions(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
 
	from, to, err := parseDateRange(r.URL.Query())

Streaming a CSV export in Go

streaming http csv
Intermediate 8 steps
go
package config
 
import (
	"bufio"

Parsing INI files in Go

parsing state-machine error-handling
Intermediate 7 steps
go
package logtail
 
import (
	"bufio"

How a tail -f follower works in Go

io context channels
Intermediate 10 steps