📖 5 min read (~ 1100 words).

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 key

An 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
What does it hold?
{
  "country": "NO",
  "answer": "yes",
  "version": 1.10
}

Under YAML 1.2 the only booleans are true and false, so NO and yes are strings. 1.10 is a number, and go-yaml keeps its text instead of rounding it to 1.1.

The same document read as YAML 1.1, where NO, yes, on and their kin are booleans:

{
  "country": false,
  "answer": true,
  "version": 1.10
}

The country code for Norway reads as false, and this is known as the Norway problem.

a: &x 1
b: *x
c: &x 2
d: *x
What does it hold?
{
  "a": 1,
  "b": 1,
  "c": 2,
  "d": 2
}

An anchor name may be used again. An alias stands for the latest anchor of its name, so b reads 1 and d reads 2.

-
- a
-
What does it hold?
[
  null,
  "a",
  null
]

A - with nothing after it is an entry with no value, which is null.

text: >
  folded
  line
    kept as it is
  folded
  again
What does it hold?
{
  "text": "folded line\n  kept as it is\nfolded again\n"
}

A folded scalar does not fold a line indented further than the others. The breaks around it are kept, and its extra indentation stays in the text.

a: !!str true
b: !!float 1
c: !!bool "false"
d: !!null ""
What does it hold?
{
  "a": "true",
  "b": 1.0,
  "c": false,
  "d": null
}

A core tag sets the type whatever the text looks like. !!str makes true a string, !!float makes 1 the float 1.0, !!bool reads the quoted "false" as a boolean, and !!null makes the empty string a null.

[a, b]: pair
What does it hold?
[1:1] a sequence cannot be a JSON key
>  1 | [a, b]: pair
       ^

A mapping whose one key is the sequence [a, b]. A flow collection may stand as an implicit key, with no ? in front of it. The YAML is valid, but JSON has only string keys, so ToJSON stops at the key.

- ! 42
- ! true
- ! null
- ! [1, 2]
What does it hold?
[
  "42",
  "true",
  "null",
  [
    1,
    2
  ]
]

The non-specific tag ! tells the reader not to guess a plain scalar’s type from its text, so 42, true and null are all strings. On a collection it applies to the collection, not to its entries: [1, 2] stays a sequence of numbers.