Edit a value¶
Change an existing scalar¶
The node is mutated in place, so its inline comment and quoting style survive:
A value that was quoted stays quoted; one that was bare stays bare.
doc.Set("name", "after") // quoted: "before" → quoted: "after"
doc.Set("kind", "after") // kind: before → kind: after
Set accepts strings, booleans, all integer and float widths, and maps and
slices for whole subtrees.
Create a key¶
Setting a path that does not exist creates it, appended to the deepest existing ancestor. Appending rather than inserting is deliberate: existing keys keep their comments and their order.
Missing intermediate mappings are synthesised:
Replace a subtree¶
Passing a map replaces the addressed node wholesale — keys absent from the map are gone.
You own what you replace
A subtree replaced from a Go value cannot keep what the Go value does not carry. Comments inside that subtree, anchors, and block scalar styles are lost, because nothing in the map describes them.
For a targeted change, address the leaf directly (avatars.matt.likeness)
rather than replacing its parent. Reach for subtree replacement when you
genuinely own the whole value.
What cannot be created¶
A path that runs through a scalar is refused rather than silently rewriting it:
Creating it would mean destroying a, which you did not ask for.