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.
go get oneyaml.dev/goRequires Go 1.24+. Run in a Go module.
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, then go run ..
Prints the image inherited by web:
webThe full program
The sample adds a shared port and a local override, then compares the document bytes with the input after the reads.
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())) }
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.
input := []byte(`first: &first {image: web} second: &second {image: worker} web: {<<: [*first, *second]} `)
webRelated recipes
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 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 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.