---
title: "Update YAML map values while iterating in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/update-yaml-mapping-values-in-a-loop/
description: "Use Entries to update existing YAML mapping values in a Go range loop. Each pass gets fresh nodes after the previous write."
version: "1.0.0"
---

# Update YAML map values while iterating in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/update-yaml-mapping-values-in-a-loop/update-yaml-mapping-values-in-a-loop.zip)

Range over `Entries()` and call `doc.Set(path, newValue)` on each existing value.

```shell
go get oneyaml.dev/go
```

Requires Go 1.24+. Run in a Go module.

```go
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](https://recursiveai.org/oneyaml/go/cookbook/update-yaml-mapping-values-in-a-loop/update-yaml-mapping-values-in-a-loop.zip), then `go run .`.

The output adds five seconds to both timeouts and keeps the inline comment.

```text
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`.

```go
path, err := value.Pointer()
if err != nil {
	log.Fatal(err)
}
```

Same output.

## Related recipes

- [Refresh a stale YAML node after editing](https://recursiveai.org/oneyaml/go/cookbook/refresh-yaml-node-after-edit/)
- [Add a YAML key without losing comments](https://recursiveai.org/oneyaml/go/cookbook/add-yaml-mapping-key-without-reformatting/)

## 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](https://recursiveai.org/oneyaml/go/docs/editing/#reading-while-you-edit) explains how iteration resumes and what is safe to keep across edits.
