Intermediate YAML
The features on this page take YAML past JSON: a value written once and reused later, text kept as it was typed, several documents in one file, and types written out instead of guessed.
Anchors and aliases
defaults: &defaults
retries: 3
timeout: 10s
service:
settings: *defaults
{
"defaults": {
"retries": 3,
"timeout": "10s"
},
"service": {
"settings": {
"retries": 3,
"timeout": "10s"
}
}
}&defaults names the mapping it stands in front of: it is an anchor. *defaults is an alias: it stands for the
anchored node, so the mapping appears twice in the JSON. An alias names an anchor written before it, in the same
document. For how go-yaml decodes aliases into Go values, see
Anchors and aliases.
Block scalars
A block scalar holds text over several lines. Its first line is a header: | or >, and an optional chomping
indicator.
script: |
echo one
echo two
{
"script": "echo one\necho two\n"
}| makes a literal scalar: every line break is kept, and the indentation of the first line is taken off every line.
summary: >
This sentence
is folded into one line.
A blank line keeps a break.
{
"summary": "This sentence is folded into one line.\nA blank line keeps a break.\n"
}> makes a folded scalar: a line break between two lines of text becomes a space, and a blank line becomes one break.
strip: |-
text
clip: |
text
keep: |+
text
end: 1
{
"strip": "text",
"clip": "text\n",
"keep": "text\n\n",
"end": 1
}The chomping indicator decides what happens to the line breaks at the end of the text:
-strips them all.- No indicator clips them to one. Clip is the default.
+keeps them all, blank lines included.
Several documents
name: first
---
name: second
...
---
- a
- list
--- starts a document, and ... may end one. One file can hold several documents, and each is read on its own:
# document 1
{
"name": "first"
}
# document 2
{
"name": "second"
}
# document 3
[
"a",
"list"
]codec.ToJSON converts the first document of a stream, and a Decoder reads one document per call to Decode.
Tags
number: !!str 42
quoted: !!int "7"
date: 2026-11-25
{
"number": "42",
"quoted": 7,
"date": "2026-11-25"
}A tag names the type of a node instead of leaving it to the node’s text. !!str and !!int are short for the standard
types tag:yaml.org,2002:str and tag:yaml.org,2002:int. With a tag, 42 is a string and the quoted "7" a number.
2026-11-25 has no tag, and YAML 1.2 has no date type, so it is a string. YAML 1.1 reads such text differently; see
YAML versions.
Explicit and complex keys
? a key written explicitly
: its value
simple: value
{
"a key written explicitly": "its value",
"simple": "value"
}A key written after ? , with its value after : on the next line, is an explicit key.
You should use explicit keys when a key runs over several lines or past 1,024 characters.
? [a, b]
: a sequence as a key
[1:3] a sequence cannot be a JSON key
> 1 | ? [a, b]
^
2 | : a sequence as a keyAn explicit key may also be a collection. YAML allows a sequence or a mapping as a key; JSON has only string keys, so
the conversion stops there. go-yaml reads the document, and ToJSON returns an error that points at the key.
Quizz: test your YAML knowledge (expert level)
country: NO
answer: yes
version: 1.10
a: &x 1
b: *x
c: &x 2
d: *x
-
- a
-
text: >
folded
line
kept as it is
folded
again
a: !!str true
b: !!float 1
c: !!bool "false"
d: !!null ""
[a, b]: pair
- ! 42
- ! true
- ! null
- ! [1, 2]