---
title: "Edit a YAML multiline string in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/preserve-yaml-block-scalar-style/
description: "Update a YAML literal block in Go, see when | changes to |-, and read the value back to check its trailing newline."
version: "1.0.0"
---

# Edit a YAML multiline string 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/preserve-yaml-block-scalar-style/preserve-yaml-block-scalar-style.zip)

The `|` stays. If the new value has no final newline, it becomes `|-`; end the string with `\n` to keep a plain `|`.

```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() {
	const input = `message: |
  Hello
  World
`
	doc, err := oneyaml.Parse([]byte(input))
	if err != nil {
		panic(err)
	}
	next := "Hello\nGo"
	err = doc.Set("/message", next)
	if err != nil {
		panic(err)
	}
	var text string
	err = doc.Get("/message", &text)
	if err != nil {
		panic(err)
	}
	fmt.Print(doc)
	fmt.Printf("Read back: %q\n", text)
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/preserve-yaml-block-scalar-style/preserve-yaml-block-scalar-style.zip), then `go run .`.

```text
message: |-
  Hello
  Go
Read back: "Hello\nGo"
```

`Set` and `Get` return errors if the path cannot be used; check errors before writing the edited document. `%q` prints newlines as `\n` so you can see where the decoded string ends.

To save the result, use `os.WriteFile` as shown in [Edit a YAML file in Go and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/#the-full-program).

## Variations

**Keep one final newline.** Replace the `next` assignment. The original `|` stays because it can represent the new value exactly:

```go
next := "Hello\nGo\n"
```

```text
message: |
  Hello
  Go
Read back: "Hello\nGo\n"
```

## Related recipes

- [Edit a YAML file and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)
- [Find where a YAML value appears in the source](https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/)

## The short answer

Parse the YAML into a `Document`, then call `Set` at the existing block’s path. Pass the Go string you want a YAML reader to receive, including any final newline. In the example, the pipe stays but gains a minus sign: `|-` keeps the newline between `Hello` and `Go` while stripping the one at the end. `Get` reads back the value so you can distinguish an internal newline from one at the end.

## Does YAML support multiline strings?

Yes. For text whose line breaks carry meaning, put `|` after the key and indent the text below it. This is called a **literal block scalar**, meaning one string written over several lines. A shell script or a two-line message can go in a literal block because the reader keeps the internal line breaks. The indentation that places the block under the key isn’t part of the string.

A block starting with `>` is **folded** instead. Ordinary adjacent lines are usually joined with a space, with exceptions such as more-indented lines. That’s useful when you want to wrap prose in the file without inserting a newline at every wrap. `Set` keeps the folded `>` style when it can represent the new value exactly; otherwise it uses the nearest style that can. See the specification’s [literal style](https://yaml.org/spec/1.2.2/#812-literal-style) and [folded style](https://yaml.org/spec/1.2.2/#813-folded-style) rules.

## What is `|-` in YAML?

The second character is the **chomping indicator**: it controls the end of the string, not the newlines inside it. For a nonempty block followed by a line break, bare `|` keeps a single final newline and drops extra trailing blank lines. `|-` strips the final newline and any trailing blank lines. `|+` keeps them. These are [YAML’s block chomping rules](https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator).

The change from `|` to `|-` doesn’t turn the example into a different kind of scalar. It lets the same literal style represent the exact Go string. The ending newline in the *file* after `Go` isn’t necessarily a newline in the *decoded value*. Compare the two printed `Read back` lines in this recipe to see the distinction. If a multiline value has characters the literal style can’t carry exactly, see the [editing guide](https://recursiveai.org/oneyaml/go/docs/editing/#when-the-style-cannot-hold-the-value) for how the editor chooses another representation.
