Range over Entries() and call doc.Set(path, newValue) on each existing value.
go get oneyaml.dev/goRequires Go 1.24+. Run in a Go module.
package main import ( "fmt" "log" "oneyaml.dev/go" ) func main() { input := "timeouts:\n web: 10 # seconds\n db: 20\n" doc, err := oneyaml.Parse([]byte(input)) if err != nil { log.Fatal(err) } timeouts, err := doc.Root().Find("timeouts") if err != nil { log.Fatal(err) } for key, value := range timeouts.Entries() { var seconds int if err := value.Decode(&seconds); err != nil { log.Fatal(err) } path := oneyaml.Pointer("timeouts", key.Value()) if err := doc.Set(path, seconds+5); err != nil { log.Fatal(err) } } fmt.Print(doc) }
Unzip the example, then go run ..
The output adds five seconds to both timeouts and keeps the inline comment.
timeouts: web: 15 # seconds db: 25
If a value doesn’t decode into an int, the example stops at that entry; earlier edits in memory aren’t rolled back.
Variations
Get the path from the value node. Replace the path := line with this block and change the loop header to for _, value := range timeouts.Entries() {. Read the path before calling Set.
path, err := value.Pointer() if err != nil { log.Fatal(err) }
Same output.
Related recipes
The short answer
Loop over mapping.Entries(). Read each key and value before calling doc.Set, then write back to the path for that entry. Replacing a value doesn’t move any entries, and the iterator yields fresh nodes for the next position. This edits the YAML document, so the example can update both numbers without rebuilding the mapping or dropping the inline comment.
Can you modify a map while iterating in Go?
For this YAML mapping, update existing values in place. The Go range loop here calls Entries(), an iterator over the parsed document, not a range over a native Go map. If you call Set during an iteration, the iterator finds its mapping again and continues from the next position. Each loop body can read its own key and value. After the write, don’t read those same nodes again or save them for later: the edit makes them stale. If you need something afterward, keep the decoded value or read a new node.
The pointer says where to write. In the example it’s built from the parent key timeouts and the current entry’s key. Using oneyaml.Pointer instead of concatenating strings keeps keys with literal slashes or tildes from being mistaken for pointer syntax. The variation gets the same address directly from the value node. In either case, get the address before the edit.
What happens if you add or remove YAML keys during the loop?
Changing the set of entries isn’t the same as replacing values. An insertion or deletion can shift positions, so the loop can skip an entry or see one twice. A key appended at the end can be visited too; adding one on every pass can keep the loop going indefinitely. If you replace or remove the mapping that the iterator is walking, the loop ends.
For structural changes, collect the paths you mean to edit and finish the loop before applying them. Save paths or Go values, not nodes. If you rename or remove a parent, any saved path through that parent needs to be reconsidered. The editing guide explains how iteration resumes and what is safe to keep across edits.