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]
text: |+
starts on a blank line
&k key: value
*k : again
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
moves: !!pairs
- e2: e4
- e7: e5
- e2: e3
when: !!timestamp 2026-11-25 10:30:00 +01:00