Go Examples
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.