Recursive AI

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.

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.

Terminal
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, 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

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

All oneyaml Go recipes