Cookbook#
Complete programs for common jobs. Pick the one closest to what you are building, copy it, and change it.
Programs and files#
- Command-line programs : print, read arguments, subcommands and flags
- Files and standard input : read and write text, process lines from stdin, find files by pattern
- Watching files : react to changes in a file or folder
- Tables and colors : aligned columns and color that switches off in a pipe
Data and text#
- Numbers and JSON : parse integers, parse JSON, read and write typed JSON
- YAML and XML : read a YAML setting or an XML document
- Patterns and URLs : regular expressions, URL parts and queries
- Hashes, IDs and random values : SHA-256, UUIDs, tokens, repeatable random numbers
- Exact numbers : numbers beyond Int and exact fractions
- Images : build, resize, encode and decode PNG and JPEG
Networking#
- HTTP requests : fetch a URL and read the body
- A JSON API : routes, typed bodies and tests without a socket
- WebSockets and server-sent events : push data to a client as it happens
- HTTPS and certificates : serve TLS, trust a private authority, watch expiry
Services#
- Service configuration : settings from a file with environment overrides
- Logging : structured records to stdout or a rotating file
- Retries and limits : retry with backoff, stop calling a failing dependency
- Background jobs : bounded batches, a worker queue, an interval
- Stopping cleanly : handle Ctrl-C and SIGTERM
- Tests with fakes : replace a dependency in a test
- Deploying one binary : a static executable and a container
Shell commands assume POSIX; the HTTP recipe needs Python 3 as a local server. New to the language? Start with the guide .