Examples
Every directory here is a self-contained, runnable program built around one
concept:
go run ./examples/config_manager
They are meant to be read as much as run — each prints its intermediate state so
you can follow what the library did and why.
Start here
If you are new to the library, these four cover the shape of everything else:
config_manager — the core loop: diff two values,
apply the patch, roll it back with Reverse.
nested_structs — how paths address fields, including
nested and embedded structs, and how a diff pinpoints what changed.
policy_engine — conditions travelling inside the
patch, so the rule is enforced wherever it is applied.
json_interop — what a patch looks like on the wire, in
both the native format and RFC 6902.
Core operations and patches
| Example |
Shows |
config_manager |
Diff → Apply → Reverse for rollback |
state_management |
An undo stack built by keeping each edit's reverse patch |
nested_structs |
Paths into nested structs; embedded fields addressed by type name; a selector targeting one nested field |
slice_paths |
At for positional elements; why a length-changing diff becomes one whole-slice replace while a same-length one stays per-index |
keyed_inventory |
deep:"key" — elements addressed by identity, so reordering produces no operations and a changed element diffs down to the changed field |
struct_map_keys |
Non-string map keys rendered into paths |
move_copy_ops |
Move (empties the source) versus Copy (leaves it intact) |
multi_error |
Apply collecting every failure instead of stopping at the first; unwrapping ApplyError |
| Example |
Shows |
atomic_config |
deep:"readonly" rejecting writes; deep:"atomic" replacing a struct as one unit |
ignored_fields |
json:"-" and deep:"-" keeping secrets out of patches — and the sharp edge that invisible means invisible to Clone too |
policy_engine |
A patch-level Guard built from And/Or/Matches: all-or-nothing |
conditional_ops |
Per-operation If/Unless: one patch, each operation deciding for itself |
conditional_writes |
Applying a patch where the data lives, so unrelated writers stop conflicting — plus what ApplyResult tells you that an error cannot |
concurrent_updates |
Strict mode as optimistic locking — a stale patch is rejected |
three_way_merge |
Merge with a custom ConflictResolver |
reflection_fallback |
What the reflection engine handles for a type with no generated code at all: unexported fields, and cycles |
cyclic_graph |
A team chart that points back at itself: Clone rebuilds the cycles and keeps a person reached four ways one person, Equal terminates, and Diff emits alias operations so applying a patch rewires every route |
Transport and interop
| Example |
Shows |
json_interop |
Native JSON, RFC 6902 export, and ingesting an externally-produced JSON Patch with ParseJSONPatch |
http_patch_api |
A patch-driven HTTP PATCH endpoint, client and server |
websocket_sync |
Broadcasting per-tick state deltas to a client |
audit_logging |
A diff read as an audit trail, plus OpLog tracing through an injected slog.Logger |
CRDTs
| Example |
Shows |
crdt_sync |
Two nodes exchanging deltas and converging |
crdt_undo_redo |
Distributed undo/redo via Reverse, and why redelivering a delta is harmless |
crdt_containers |
Counter, Set (add-wins) and Map (last-write-wins per key) |
crdt_list |
List[T]: concurrent insertions and deletions in a sequence all survive, where an ordinary slice would keep one writer's version |
crdt_observers |
OnChange: the operations actually applied, for redrawing only what moved |
crdt_compaction |
Compact: reclaiming the bookkeeping and tombstones a long-lived replica accumulates, and the watermark that makes it safe |
crdt_document |
Document: the same text CRDT as Text but indexed by position, so an edit costs the same however fragmented the document |
crdt_presence |
Awareness: cursors and selections that live beside the document rather than in it, and expire when their owner goes quiet |
crdt_sync_incremental |
StateVector, Since and Apply: exchanging only what a peer is missing rather than whole documents |
crdt_custom_type |
Convergent and Compactable: a type of your own that merges rather than losing one writer, and that drops its own history when told |
lww_fields |
Per-field LWW[T] registers resolving a genuine write conflict |
text_sync |
Collaborative text: concurrent edits across a partition, merged with MergeTextRuns |
Generated code
Examples whose model types have a //go:generate directive ship the generated
*_deep.go file alongside, exercising the reflection-free fast path; the rest
run on the reflection engine. Both are the same API — go generate ./...
regenerates every one of them.