yamldoc¶
Edit a YAML document without destroying it.
Parse bytes, change one key, write bytes back. Comments stay attached to the keys they describe, key order holds, quoting and block styles survive, and everything you did not touch comes back semantically unchanged.
The problem it solves¶
Marshalling a map[string]any back to YAML throws away every comment the author
wrote, reorders keys, and reformats the file. That is fine for machine-written
data and unacceptable for a hand-authored config file someone has to keep
maintaining — which is exactly the file a CLI or settings UI needs to edit.
Go has the YAML value half many times over and the editing half not at all.
Every other ecosystem splits the two: Rust's toml_edit and toml, HashiCorp's
hclwrite and hclsyntax, Python's tomlkit and tomli. This is that split,
for Go and YAML.
See Why not just re-marshal? for the detail.
Where to start¶
-
Getting started Parse, change a value, write it back.
-
Edit a value Set scalars, create keys, replace subtrees.
-
API reference Every method, and what it returns when it fails.
-
Path syntax What a dotted path can address — and what it cannot.
-
What is preserved The guarantee, stated precisely — including its limits.
-
What this does not do Absent by design, out of reach, and known defects.
Scope¶
The library moves text. Meaning belongs to whoever is using it.
No file I/O. No merging, precedence or overlay semantics. No schema or validation. It assigns no meaning to the order of documents in a multi-document file. See Mechanism, not policy.
It also cannot do some things people expect it to: there is no way to address an element of a YAML list, and it edits an existing document rather than authoring one, so an empty file cannot be filled in. Those and the rest are in What yamldoc does not do.
Further reading¶
The blog carries a curated route through this subject: Building a command-line tool in Go collects everything written about it, ordered so you can start at the beginning rather than newest-first.
Ask phpbotscout

He answers questions about the projects over on the Discord, citing the docs where they already cover it, and offering to raise an issue where they don't. Bring a bug, an idea, or a questionable engineering decision.