Go Examples

GoOct 23, 2026

WriteTable aligned columns with tabwriter

Render headers and rows as aligned columns with text/tabwriter, including a dashed rule and a guard that rejects rows with the wrong number of cells.

formattingtabwritertablecli
000
GoOct 23, 2026

Base64 encode and decode with whitespace checks

Encode text as unpadded base64 and decode both padded and raw input, rejecting embedded whitespace and reporting malformed input instead of returning junk.

decodingvalidationencodingbase64
000
GoOct 23, 2026

ValidateSignup returning field-level error map

Validate a signup payload field by field, returning a map of field name to error so an API can report every problem in a single response.

inputerrorsvalidationforms
000
GoOct 23, 2026

BuildURL and QueryInt for safe query handling

Merge default query parameters into a URL, apply caller overrides, and parse integer parameters with validation instead of silently defaulting to zero.

query-stringurlparsingencoding
000
GoOct 23, 2026

DecodeJSON with byte offsets for parse failures

Decode JSON bytes with errors that name the byte offset for syntax problems, the field and type for mismatches, and truncated input as such.

error-handlingdecodingsyntax-errorjson
000
GoOct 23, 2026

RunPool bounded worker pool with indexed results

Process N indexed tasks through a fixed number of goroutines, then read results and errors back in input order without a mutex in the hot path.

concurrencyworker-poolgoroutineswaitgroup
000
GoOct 23, 2026

GetJSON HTTP helper with timeout and status checks

Fetch a URL with a hard ten-second timeout, reject non-2xx responses with the response body, and decode the JSON body into a typed value in one call.

httpjsontimeoutcontext
000
GoOct 23, 2026

LoadEnv config loader with defaults and validation

Read service settings from APP_* environment variables, apply defaults, and fail fast with a descriptive error when a value is missing or malformed.

environmentconfigurationvalidationdefaults
000
GoOct 23, 2026

TokenBucket limiter with burst capacity and refill

Implement a token bucket that allows bursts up to capacity while refilling at a fixed rate, with a mutex so a single bucket can be shared across goroutines.

concurrencytoken-bucketburstsrate-limiting
000
GoOct 23, 2026

LRU cache backed by map and container/list

A compact LRU cache built from a map plus container/list that evicts the least recently used entry past a fixed capacity, with O(1) get, put, and eviction.

container-listevictionlrucache
000
GoOct 23, 2026

SortPlayers stable multi-key comparator with nil scores

Order records by several keys at once: score descending with nils last, then name ascending, using a stable comparator that never dereferences a nil pointer.

sortingnil-safetycomparatorstable-sort
000
GoOct 23, 2026

ScanFileLines streaming reader with bufio.Scanner

Stream a text file one line at a time with bufio.Scanner and a 1-based counter, so huge logs never need to fit in memory and callbacks can stop early.

streamingfile-iobufioscanner
000
GoOct 23, 2026

Chunk generic batching for fixed-size batches

Split a slice into copied batches of at most N elements, useful for bulk API calls, SQL IN lists, and pagination without aliasing the original backing array.

batchinggenericspaginationslices
000
GoOct 23, 2026

DeepCopy and MergeMaps for nested JSON-like values

Copy nested map[string]any and []any values without sharing references, then merge a patch into the copy so nested maps merge key by key instead of replacing.

mapsmergerecursiondeep-copy
000
GoOct 23, 2026

RetryFunc with capped exponential backoff and jitter

Retry a context-aware operation with exponential backoff, a hard delay cap, and full jitter so retries from many clients spread out instead of thundering together.

backoffretrycontextjitter
000