After an edit, use doc.Root().Find("title") to read a fresh node, even if the edit changed a different key. This behavior is specific to oneyaml-go nodes; if you’re switching libraries, see the switching guide.
go get oneyaml.dev/goRequires Go 1.24+. Run in a Go module.
package main import ( "fmt" "log" "oneyaml.dev/go" ) func main() { input := "title: handbook\npages: 3\n" doc, err := oneyaml.Parse([]byte(input)) if err != nil { log.Fatal(err) } title, err := doc.Root().Find("title") if err != nil { log.Fatal(err) } if err := doc.Set("/pages", 4); err != nil { log.Fatal(err) } fmt.Println("Old node stale:", title.Stale()) title, err = doc.Root().Find("title") if err != nil { log.Fatal(err) } fmt.Println("Fresh title:", title.Value()) }
Unzip the example, then go run ..
The program reports that the old node is stale, then reads the unchanged title from a fresh node.
Old node stale: true Fresh title: handbook
If the edit removed or renamed the key, a fresh lookup under the old name returns ErrNotFound instead of reviving the old node.
Variations
Read the Go value without keeping a node. Replace the second Find block and the final print with a Get into a string.
var freshTitle string if err := doc.Get("/title", &freshTitle); err != nil { log.Fatal(err) } fmt.Println("Fresh title:", freshTitle)
Old node stale: true Fresh title: handbook
Related recipes
The short answer
Start from the current document again. After Set, call doc.Root().Find("title") to get a fresh node, or call doc.Get with a pointer if you only need the decoded Go value. Calling Find on an old root node doesn’t work: the root you held before the edit is stale too.
Why is my YAML node stale after editing a different key?
A node isn’t a permanent handle to one key. It views the document as it was when you read it. An edit invalidates earlier nodes across the document, not just the node whose text changed. That’s why updating pages makes the old title node stale even though the title text stays handbook. Stale() reports that distinction directly.
Don’t use Value() == "" to diagnose the problem. An empty scalar can legitimately have that value, and a stale node also returns an empty string. Use Node.Err() if you need to distinguish a readable node, an ErrStaleNode, and an ErrNotFound from a zero node. Methods such as Decode and Pointer report staleness as an error instead of returning a usable decoded value or address. Find also distinguishes a stale receiver from a missing key; Lookup reports false in either case. See Reading while you edit for the exact return behavior.
How do you read a YAML value again after an edit?
Get a new root and descend from it, as the program does. For a nested node, repeat the lookup from that new root or use the node’s path. If you need the path from the original node, call Pointer before editing. A pointer is an address, not an identity: a rename may change the path, and inserting or removing a sequence item can change which value an index refers to. A saved pointer is only useful if it still points where you intend.
When you want the old data for comparison, decode or copy it before the edit and keep the Go value. When you want the current data, read again after the edit. Neither approach needs to keep a node alive across writes. Choose a fresh node when you also need information from the YAML source, such as its comment or position.