---
title: "Read inherited YAML merge values in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/read-effective-yaml-merge-value/
description: "Read a value inherited through a YAML merge key in Go. See which value wins when a local key overrides a default, without rewriting the YAML."
version: "1.0.0"
---

# Read inherited YAML merge values in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/read-effective-yaml-merge-value/read-effective-yaml-merge-value.zip)

An anchor (`&base`) names the source mapping; an alias (`*base`) refers back to it. The merge key (`<<`) uses that mapping’s entries as defaults. Enable `MergeKeys()` and use `Get` to read an inherited value.

```shell
go get oneyaml.dev/go
```

Requires Go 1.24+. Run in a Go module.

```go
package main

import (
	"fmt"

	"oneyaml.dev/go"
)

func main() {
	input := []byte(`base: &base
  image: web
web: {<<: *base}
`)
	merge := oneyaml.MergeKeys()
	doc, err := oneyaml.Parse(input, merge)
	if err != nil {
		panic(err)
	}
	var image string
	err = doc.Get("/web/image", &image)
	if err != nil {
		panic(err)
	}
	fmt.Println(image)
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/read-effective-yaml-merge-value/read-effective-yaml-merge-value.zip), then `go run .`.

Prints the image inherited by `web`:

```text
web
```

## The full program

The sample adds a shared port and a local override, then compares the document bytes with the input after the reads.

```go
package main

import (
	"bytes"
	"fmt"

	"oneyaml.dev/go"
)

func main() {
	input := []byte(`base: &base {image: web, port: 80}
web:
  <<: *base
  port: 8080 # local override
`)
	doc, err := oneyaml.Parse(
		input, oneyaml.MergeKeys(),
	)
	if err != nil {
		panic(err)
	}
	var image string
	if err := doc.Get("/web/image", &image); err != nil {
		panic(err)
	}
	var port int
	if err := doc.Get("/web/port", &port); err != nil {
		panic(err)
	}
	fmt.Println("Image:", image)
	fmt.Println("Port:", port)
	fmt.Println("YAML unchanged:", bytes.Equal(input, doc.Bytes()))
}
```

```text
Image: web
Port: 8080
YAML unchanged: true
```

Without `MergeKeys()`, `<<` is an ordinary key, so the lookup of a key present only in the defaults returns an error.

## Variations

**Read a merge with two sources.** Replace `input` in the snippet at the top. The first source supplies `image`.

```go
input := []byte(`first: &first {image: web}
second: &second {image: worker}
web: {<<: [*first, *second]}
`)
```

```text
web
```

## Related recipes

- [Read YAML anchors, aliases and merge keys in Go](https://recursiveai.org/oneyaml/go/cookbook/anchors-aliases-merge-keys/)
- [Read YAML in Go without a struct](https://recursiveai.org/oneyaml/go/cookbook/read-yaml-without-a-struct/)

## The short answer

Pass `MergeKeys()` to `Parse`, then call `Get` on the destination path, such as `/web/image`. Give `Get` a pointer to the Go variable that will hold the value. You don’t need to follow `*base` yourself or expand the merge into the source file. The full example reads an inherited image and a locally overridden port from the same mapping.

## Which YAML value wins when a merge key supplies a default?

The key written in the destination mapping wins. In the download, `web.port` is `8080` because it is explicit under `web`. The merge source still has port `80`. For `image`, there is no local value, so the read returns `web` from the defaults. When `<<` lists several sources, the first source with the key wins, not the last. This matches the [merge-key definition](https://yaml.org/type/merge.html) and makes source order important even if the destination has no overrides.

A merge key reads defaults from within the same document; it doesn’t combine two YAML files.

## How do you read merged YAML values without flattening the file?

Read the destination key with `Get`. If you already have the destination as a document node, `Find` also follows merge keys. That gives you an effective lookup, which answers “what value does this service use?” By contrast, `Entries` lists the mapping as written, which answers “what keys are actually in the file?” In the snippet at the top, the written entry is `<<`, not `image`. Looking up `/web/<<` reads the merge source as written, rather than asking for the effective value of a service setting.

The [editing guide](https://recursiveai.org/oneyaml/go/docs/editing/#merge-keys) explains how lookup errors differ from missing values. A merge search can fail if it reaches the library’s search bound. If your code substitutes a default for missing keys, use `Get` or `Find` and distinguish a missing key from a failed search; `Lookup` reports `false` for both. You don’t have to modify or save the document to perform any of these reads.
