Quick Guides
One task each, from an empty directory to a program that works, with every file shown whole. Each guide stands on its own.
- 1Database migrationsKeep a database's schema in numbered SQL files and let your program bring any database up to date with ktm-migrate. This guide builds a small notes program on SQLite, step by step: the first migrations, status, going back, migrating at start-up, changes SQLite can't make with ALTER TABLE, failures, tests and migrations built into the binary. Then the same program on PostgreSQL, several instances starting at once, migrations from merged branches, and a driver of your own.
- 2LoggingWrite what a program does to stderr, to stdout or to a file, with the standard library's log module. This guide starts with one line and adds a step at a time: levels, a level from the environment, a log file, where each line came from, rotation, a second log of your own, threads, and the access log of an HTTP server. Every step is a whole program you can build and run.
- 3SQLiteKeep a program's data in one file with SQLite, through the standard library's sql module. This guide builds up a small catalogue of books, one whole program at a time: creating a table, statements with arguments, reading rows into structs, NULLs, errors, transactions, the settings a real program wants, enums, blobs and JSON columns, tests on a database in memory, and prepared statements for the fastest bulk inserts.
- 4PostgreSQLTalk to PostgreSQL from ktm with the ktm-pg package. This guide builds up a catalogue of books, one whole program at a time: connecting, running statements with parameters, reading rows into structs, NULLs, rows one by one, transactions, errors, a connection pool behind an HTTP API, batches and fast bulk loading, query timeouts, and LISTEN/NOTIFY.
- 5Command-line flagsRead flags like --port 9000 and -v from the command line with the standard library's flag module. This guide builds one program, serve, a step at a time: one flag, then the four kinds of flag and their short names, the ways to write them, the arguments that aren't flags, checking values, a fallback to an environment variable and your own error messages. The last section builds a second program with subcommands, like git commit. Every step is a whole program you can build and run.
- 6Terminal outputMake a command-line program's output easy to read with the standard library's term module: colors, lines that say what happened, tables whose columns line up, and a progress bar. Then read what people type, with line editing and a history, as GNU readline does. This guide shows each one, then puts them together in a small report. Every step is a whole program you can build and run.