Code Explainers

Go code explainers

go
package streaming
 
import (
	"bufio"

Streaming NDJSON logs over HTTP in Go

http-streaming channels select
Advanced 10 steps
go
package api
 
import (
	"crypto/sha256"

ETag conditional requests in Gin

http-caching etag conditional-requests
Intermediate 6 steps
go
func (w *Watcher) resetDebounce(d time.Duration) {
	if !w.timer.Stop() {
		select {
		case <-w.timer.C:

Debouncing a stream of events in Go

debounce timers channels
Advanced 7 steps
go
package logging
 
import (
	"context"

Deduplicating log attributes in Go's slog

decorator-pattern structured-logging immutability
Intermediate 8 steps
go
package middleware
 
import (
	"net/http"

Per-plan export limits in Gin middleware

middleware rate-limiting authorization
Intermediate 7 steps
go
package httputil
 
import (
	"net"

Safely extracting the real client IP in Go

security http ip-spoofing
Intermediate 7 steps
go
package signup
 
var wizardCodec = securecookie.New(
	[]byte(os.Getenv("WIZARD_HASH_KEY")),

A cookie-backed signup wizard in Gin

cookies encryption multi-step-form
Intermediate 9 steps
go
func UpdateArticle(store *ArticleStore) gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")
 

Optimistic concurrency with If-Match in Gin

optimistic-locking http-preconditions etag
Intermediate 9 steps
go
type Counter struct {
	counts sync.Map
}
 

A concurrent counter with sync.Map in Go

concurrency atomics sync.map
Advanced 8 steps
go
package handlers
 
import (
	"net/http"

Custom struct-level validation in Gin

validation struct-tags cross-field
Intermediate 7 steps
go
package config
 
import (
	"os"

Layered config resolution in Go

configuration closures higher-order-functions
Intermediate 8 steps
go
package routing
 
import (
	"net/http"

Region-aware upstream routing in Gin

middleware concurrency load-balancing
Advanced 8 steps
go
package api
 
import (
	"encoding/json"

Safely decoding JSON in a Go HTTP handler

http json-decoding input-validation
Intermediate 8 steps
go
package textutil
 
import "strings"
 

Extracting delimited text with strings.Cut

string-parsing delimiters standard-library
Beginner 7 steps
go
package flatten
 
import (
	"encoding/json"

Flattening nested JSON into dotted keys

recursion json type-switch
Intermediate 7 steps
go
func HandleWebhook(store WebhookStore, dispatcher *EventDispatcher) gin.HandlerFunc {
	return func(c *gin.Context) {
		body, err := c.GetRawData()
		if err != nil {

Building a resilient webhook handler in Gin

webhooks dependency-injection signature-verification
Intermediate 9 steps
go
func StreamLogs(c *gin.Context) {
	path := c.Query("file")
	if path == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "missing file parameter"})

Streaming a live log tail with SSE in Gin

server-sent-events streaming channels
Advanced 10 steps
go
func MergeSorted(a, b []int) []int {
	merged := make([]int, 0, len(a)+len(b))
	i, j := 0, 0
 

Merging two sorted slices in Go

two-pointers merge slices
Intermediate 6 steps