---
title: "Append to a YAML list without reformatting in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/append-to-yaml-list-in-place/
description: "Append to an existing YAML list in Go without rewriting unrelated keys. Keep the existing item comments, quoting, and spacing."
version: "1.0.0"
---

# Append to a YAML list without reformatting 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/append-to-yaml-list-in-place/append-to-yaml-list-in-place.zip)

Call `doc.Set("/tags/-", "api")` to append `api` to `tags` without reformatting the rest of the file. In this JSON Pointer, `/-` means after the last item.

```shell
go get oneyaml.dev/go@v1.0.0
```

Requires Go 1.24 or later. Run the install command in a Go module.

```go
package main

import (
	"fmt"
	"log"

	"oneyaml.dev/go"
)

func main() {
	input := "tags:\n  - web # public\n"
	doc, err := oneyaml.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/tags/-", "api"); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
```

Running this snippet prints:

```text
tags:
  - web # public
  - api
```

## The full program

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/append-to-yaml-list-in-place/append-to-yaml-list-in-place.zip), then `go run .`.

The download reads this `config.yaml` and writes `updated.yaml` without changing `config.yaml`.

```yaml
name: 'site'
tags:
  - web # public

port: 80
```

```go
package main

import (
	"fmt"
	"log"
	"os"

	"oneyaml.dev/go"
)

func main() {
	input, err := os.ReadFile("config.yaml")
	if err != nil {
		log.Fatal(err)
	}
	doc, err := oneyaml.Parse(input)
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/tags/-", "api"); err != nil {
		log.Fatal(err)
	}
	out := doc.Bytes()
	err = os.WriteFile("updated.yaml", out, 0644)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(out))
}
```

`go run .` prints the contents of `updated.yaml`:

```text
name: 'site'
tags:
  - web # public
  - api

port: 80
```

Printing `doc` shows the same YAML as printing `string(doc.Bytes())`.

If `tags` isn’t an existing list, check the path and the value’s type before appending; the program stops on a `Set` error rather than saving the edit.

## Variations

**Use the length as the index.** In the snippet at the top, replace the path with `/tags/1`: there is one item before the append. The error check and output are the same.

```go
if err := doc.Set("/tags/1", "api"); err != nil {
	log.Fatal(err)
}
```

**Append to a list on one line.** Use this `input` in the snippet at the top and leave the append path as `/tags/-`:

```go
input := "tags: [web] # public\n"
```

```text
tags: [web, api] # public
```

## Related recipes

- [Edit a YAML file and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)
- [Add a YAML mapping key without reformatting](https://recursiveai.org/oneyaml/go/cookbook/add-yaml-mapping-key-without-reformatting/)

## The short answer

Parse the existing YAML with oneyaml-go, call `Set` on the list path followed by `/-`, and save `doc.Bytes()`. In the example, this adds `api` after `web`, leaving the `# public` comment beside `web`. The downloaded example writes `updated.yaml` instead of overwriting the input, so you can compare the two files before using the result.

## How do you append to a YAML file without reformatting it?

Edit the parsed document, not a Go slice that you later marshal as a new file. The [editing guide](https://recursiveai.org/oneyaml/go/docs/editing/) explains that the document holds the original bytes and preserves comments and formatting elsewhere. A new list item still needs YAML syntax: the editor places it on a new line for the indented list here, or adds a comma inside the brackets for the inline variation. “Without reformatting” doesn’t mean the new item’s bytes are copied from your Go string; it means the existing text doesn’t need to be emitted again from Go values.

Appending plain bytes to the end of a file doesn’t necessarily append to the list. In the sample, `port` comes after `tags`, and the new item belongs before `port` at the list’s indentation. The pointer finds that place even if someone adds keys or comments later in the file.

## What does the dash at the end of a YAML path mean?

The dash in `/tags/-` is a pointer token, not the dash that starts an item in an indented YAML list. In this `Set` operation it means the position after the current last item. The [guide also allows an index equal to the length](https://github.com/recursive-ai-oss/oneyaml-go/blob/7a4c8e39f041b945ee5f121c055f03bc2d7d01d9/docs/editing.md#L214-L216). With one item, `/tags/1` appends, but `/tags/0` addresses the existing first item and sets its value instead. Prefer `/-` when you don’t already know how many items are in the list.

The path is relative to the document root. If the list were under `service`, the append path would be `/service/tags/-`. It still addresses the list by its keys, not by the line where it happens to appear.
