Recursive AI

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.

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

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() {
	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, 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.

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"

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

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 for how the editor chooses another representation.

All oneyaml Go recipes