Recursive AI

Keep YAML quotes when changing a value in Go

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

Use Set to change the text. It keeps the original quotes if they can hold the new value.

Terminal
go get oneyaml.dev/go

Requires Go 1.24+. Run in a Go module.

GO
package main
 
import (
	"fmt"
 
	"oneyaml.dev/go"
)
 
func main() {
	input := []byte("name: 'draft'\n")
	doc, err := oneyaml.Parse(input)
	if err != nil {
		panic(err)
	}
	err = doc.Set("/name", "editor's pick")
	if err != nil {
		panic(err)
	}
	fmt.Print(doc)
}

Unzip the example, then go run ..

Prints the updated YAML with the original single-quoted style:

Text
name: 'editor''s pick'

The full program

The download also replaces an inline value with text containing a newline, and changes a plain value to the string true.

GO
package main
 
import (
	"fmt"
 
	"oneyaml.dev/go"
)
 
func main() {
	input := []byte(`name: 'draft' # label
note: 'draft'
mode: draft
`)
	doc, err := oneyaml.Parse(input)
	if err != nil {
		panic(err)
	}
	if err := doc.Set("/name", "editor's pick"); err != nil {
		panic(err)
	}
	if err := doc.Set("/note", "line 1\nline 2"); err != nil {
		panic(err)
	}
	if err := doc.Set("/mode", "true"); err != nil {
		panic(err)
	}
	fmt.Print(doc)
}
Text
name: 'editor''s pick' # label
note: "line 1\nline 2"
mode: 'true'

If the edit fails, Set returns an error and leaves the document unchanged.

Variations

Write a string for a YAML 1.1 reader. In the snippet at the top, change the input and the Parse and Set assignments to these lines. Keep the error check after each call.

GO
input := []byte("mode: off\n")
GO
doc, err := oneyaml.Parse(
	input, oneyaml.YAML11Strings(),
)
GO
err = doc.Set("/mode", "yes")
Text
mode: 'yes'

The short answer

Use Set on a parsed document and pass a Go string, not a string with YAML quote marks added around it. The writer keeps the original scalar style where it can. The apostrophe in editor's pick fits in a single-quoted scalar by doubling it. A line break in an inline replacement needs a different style: the example’s note goes from single quotes to double quotes with a \n escape.

Should I use single or double quotes in YAML?

For an existing value, let Set keep the existing style unless the new text needs a change. The doubled apostrophe in the sample is YAML syntax for one apostrophe in the decoded value; it is not an extra apostrophe in your Go string. Double quotes support backslash escapes, so the printed \n represents a real line break when the YAML is read again. See the YAML specification’s double-quoted style for the escape rules. If the file already uses a block scalar for multiline text, that is a different starting style; the example here starts with inline values.

When do YAML strings need quotes?

Quotes matter when the plain text would mean something other than a string. Under the YAML 1.2 Core schema, unquoted true is a boolean, so Set writes the Go string "true" as 'true' even though the old value was plain. Passing the Go boolean true instead expresses a different value; it is not another way to write the same string. Plain text that still reads as the intended string, such as nginx:1.25, can stay plain.

If the file is also read by YAML 1.1 tools, opt in to YAML11Strings() when parsing. For instance, plain yes is a string under YAML 1.2 Core but can be read as a boolean under YAML 1.1. The option makes the writer quote that replacement, as in the variation. The editing guide notes that this option does not rewrite values you leave untouched. To change an existing file, save doc.Bytes() after checking the edit; neither Parse nor Set writes it to disk.

All oneyaml Go recipes