---
title: "Keep YAML quotes when changing a value in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/choose-yaml-scalar-quote-style/
description: "Change a YAML scalar in Go and keep its original quotes when possible. See why a newline needs double quotes and why the string true needs quotes."
version: "1.0.0"
---

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

[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/choose-yaml-scalar-quote-style/choose-yaml-scalar-quote-style.zip)

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

```shell
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](https://recursiveai.org/oneyaml/go/cookbook/choose-yaml-scalar-quote-style/choose-yaml-scalar-quote-style.zip), 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'
```

## Related recipes

- [Edit a multiline YAML string without changing its block style](https://recursiveai.org/oneyaml/go/cookbook/preserve-yaml-block-scalar-style/)
- [Edit a YAML file and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)

## 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](https://yaml.org/spec/1.2.2/#731-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](https://recursiveai.org/oneyaml/go/docs/editing/#when-the-style-cannot-hold-the-value) 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.
