📖 5 min read (~ 1000 words).

Advanced YAML

The features on this page are rare in files people write by hand. Generators emit them, and a parser has to read them all the same.

The %YAML directive

%YAML 1.1
---
answer: yes
octal: 0100
{
  "answer": true,
  "octal": 64
}

A directive is a line starting with %, written before the --- that opens a document. %YAML 1.1 reads that one document as YAML 1.1, where yes is a boolean and 0100 an octal number, 64. The next document goes back to the reader’s default, YAML 1.2. See YAML versions for every scalar the two versions read differently.

The %TAG directive

%TAG !e! tag:example.com,2026:
---
price: !e!money 12.50
{
  "price": "12.50"
}

%TAG names a prefix: !e!money stands for tag:example.com,2026:money, so a document can use long tag names without writing them out. go-yaml has no type for this tag, so the node keeps its text, and the JSON holds the string "12.50".

Merge keys

base: &base
  retries: 3
  timeout: 10s
service:
  <<: *base
  timeout: 30s

Under YAML 1.2, << is an ordinary key:

{
  "base": {
    "retries": 3,
    "timeout": "10s"
  },
  "service": {
    "<<": {
      "retries": 3,
      "timeout": "10s"
    },
    "timeout": "30s"
  }
}

The merge key is a YAML 1.1 type. parser.WithMergeKeys reads it under every version: the mapping << names merges into the one that holds it, and a key written in place wins over a merged one.

{
  "base": {
    "retries": 3,
    "timeout": "10s"
  },
  "service": {
    "timeout": "30s",
    "retries": 3
  }
}

An explicit tag does the same without the option. go-yaml honors a tag written in the document under every version, so !!merge << merges under YAML 1.2 too:

base: &base
  retries: 3
  timeout: 10s
service:
  !!merge <<: *base
  timeout: 30s
{
  "base": {
    "retries": 3,
    "timeout": "10s"
  },
  "service": {
    "timeout": "30s",
    "retries": 3
  }
}

See Reading a merge key for the options and the decoder’s rules.

Indentation indicators

code: |2
    indented by two more
  than the block
{
  "code": "  indented by two more\nthan the block\n"
}

A block scalar takes its indentation from its first line. When that line starts with spaces that belong to the text, a digit after | or > gives the indentation instead: |2 means two spaces, and the first line keeps the two it has beyond them.

Tags beyond the standard ones

plain: 12
nonspecific: ! 12
{
  "plain": 12,
  "nonspecific": "12"
}

The non-specific tag ! tells the reader not to guess the type of a plain scalar from its text: ! 12 is a string.

point: !point
  x: 1
  y: 2
{
  "point": {
    "x": 1,
    "y": 2
  }
}

A tag with a single ! and a name, such as !point, is a local tag: its meaning belongs to the application that reads the document. JSON has no tags, so the JSON holds the mapping as it is.

GitLab CI is one such application: its !reference [.setup, script] names part of the same document, to be copied where the tag stands. The ast package’s resolve a custom tag example finds each !reference in a parsed document and splices in the entries it names.

Quizz: test your YAML knowledge (guru level)

a: &a {x: 1, y: 1}
b: &b {y: 2, z: 2}
c:
  <<: [*a, *b]
What does c hold, with merge keys on?
{
  "a": {
    "x": 1,
    "y": 1
  },
  "b": {
    "y": 2,
    "z": 2
  },
  "c": {
    "x": 1,
    "y": 1,
    "z": 2
  }
}

<< may name a list of mappings. They merge in order, and the first one to hold a key wins: y comes from a. Under YAML 1.2 without the option, << is an ordinary key holding a list of two mappings.

text: |+

  starts on a blank line

What does it hold?
{
  "text": "\nstarts on a blank line\n\n"
}

|+ keeps every line break: the blank line before the text, and the blank line after it.

&k key: value
*k : again
What does it hold?
[2:1] mapping key "key" already defined at [1:1]
   1 | &k key: value
>  2 | *k : again
       ^

Nothing: the document is not valid. A key can carry an anchor, and an alias can stand as a key. Here the alias stands for key, so the mapping holds the same key twice, and YAML forbids that.

The last three use types from the YAML 1.1 type repository: standard tags, outside the core schema, that a document must name to get.

colors: !!set
  ? red
  ? green
  ? red
What does it hold?
[4:3] mapping key "red" already defined at [2:3]
   1 | colors: !!set
   2 |   ? red
   3 |   ? green
>  4 |   ? red
         ^

Nothing: !!set is a mapping of keys with no values, and a set holds each member once. red appears twice, so go-yaml rejects the document.

moves: !!pairs
  - e2: e4
  - e7: e5
  - e2: e3
What does it hold?
{
  "moves": [
    {
      "e2": "e4"
    },
    {
      "e7": "e5"
    },
    {
      "e2": "e3"
    }
  ]
}

!!pairs is a sequence of single-pair mappings that keeps their order and, unlike a mapping or an !!omap, may repeat a key: e2 moves twice. JSON has no such type, so the JSON holds a list of one-member objects.

when: !!timestamp 2026-11-25 10:30:00 +01:00
What does it hold?
{
  "when": "2026-11-25T10:30:00+01:00"
}

!!timestamp reads a date and time written with a space before the time and before the offset, as YAML 1.1 allows. go-yaml writes the instant in RFC 3339, with a T and the offset it was given.