Skip to content

Learn from runnable examples ​

Language: English | 简体中文

The examples form one executable learning path. Run them from a repository checkout, then read the neighboring test to see the public behavior each slice guarantees.

Run and test the catalog ​

Run one command from the repository root, where go.work connects current modules:

sh
go run ./examples/hello

Test every command from the example module:

sh
cd examples
go test ./...

Start with the first group when the runtime contract is new. Jump to a later group when you already understand its prerequisites.

Learn the runtime contract ​

These examples introduce drawing, input ownership, key sequences, and headless fields:

ExampleRunControlsFocus
hellogo run ./examples/helloAny key counts; q or Ctrl+C quitsprogram.Component, Config.Root, grid.View
keysgo run ./examples/keysg moves after a deadline; gg jumps; q quitsNamed actions, exact-prefix sequences, caller-owned timeout
formgo run ./examples/formTab changes field; arrows choose; Enter submitsControlled fields, validation, grid and spoken rendering
statego run ./examples/stateType in four fields; Tab advances; Enter submitsLocal state, exact binding, normalization, and rejection through one Accessor seam

Pipe the form to exercise the same headless fields without a terminal:

sh
go run ./examples/form | cat

Read Build your first interface before modifying these commands.

Compose headless behavior ​

These examples combine reusable behavior while the application keeps product layout and meaning:

ExampleRunControlsFocus
pickergo run ./examples/pickerType to filter; arrows move; Enter picksText field, fuzzy ranking, list, highlighted matches
composergo run ./examples/composerType @; arrows choose; Enter submitsCompletion, draft history, atomic paste elements
filesgo run ./examples/files .Tab changes pane; arrows navigate; q quitsFocus, tree identity, viewport, pointer routing
dashboardgo run ./examples/dashboardPress 1–3, sort headers, adjust slidersCaller-owned tabs and slider, tables, passive charts, bounded samples, animation lifetime

The library does not add a second Picker or FileBrowser API. Each product interaction is a composition over the same controllers.

Read Compose a themeable picker for the ownership and appearance seams used in this group.

Render optional content ​

These examples prove that each content module works independently before composition:

ExampleRunControlsFocus
markdowngo run ./examples/markdownq quitsFinished blocks, measurement, width-dependent layout
mermaidgo run ./examples/mermaidr replaces, q quitsWorker diagram preparation, stale results, image upload and release
latexgo run ./examples/latexq quitsStandalone formula, two-dimensional text layout
contentgo run ./examples/content1/2/3 selects format; q quitsShared registry for top-level content and Markdown extensions

Read Render Markdown, code, and mathematics to use the three natural entry points and their consumer-owned composition seam.

Publish streamed output ​

These examples add background work, incremental transforms, inline publication, and application policy:

ExampleRunControlsFocus
rungo run ./examples/run -- go test ./core/...Ctrl+E edits; Ctrl+Z suspends; Ctrl+C quitsANSI decoding, subprocess output, terminal handover
readgo run ./examples/readCtrl+C quits after the answerIncremental Markdown, extensions, stable blocks, open tail
streaminggo run ./examples/streamingEnter approves; Ctrl+X cancelsControlled dialog, bounded ingress, transcript, failure, resize
agentgo run ./examples/agentEnter sends; /help lists commandsPlan, completion, tool review, diff, bounded history

streaming is the canonical library integration. agent adds product policy without moving model names, tool grammar, or workspace effects into the framework. Neither command performs network requests or changes files.

Read Build bounded streaming output before the canonical integration, then Build a bounded agent interface for the advanced boundary.

Review tests and visual goldens ​

Each main_test.go starts the command through programtest.Host, sends events, and asserts visible behavior. The streaming example also tests terminal scrollback and idle output on a real pseudoterminal (PTY).

The agent command stores narrow and wide text goldens. Regenerate them only when the new geometry is intended:

sh
cd examples
go test -update ./agent

Review every changed golden beside the code change. Ordinary go test ./... never rewrites fixtures.

Released under the Apache 2.0 License.