---
title: "Change a value shared by YAML aliases in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-anchor-target/
description: "Update a YAML anchor’s value in Go so every alias still points to it, without replacing the alias with a standalone value."
version: "1.0.0"
---

# Change a value shared by YAML aliases 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/edit-yaml-anchor-target/edit-yaml-anchor-target.zip)

An alias (`*shared`) refers to an anchor (`&shared`). Here `defaults` carries the anchor, so set `/defaults/port` to change the port both aliases read.

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

Requires Go 1.24+. Run in a Go module.

```go
package main

import (
	"fmt"
	"log"

	"oneyaml.dev/go"
)

func main() {
	input := "defaults: &shared\n  port: 80\n" +
		"api: *shared\nworker: *shared\n"
	doc, err := oneyaml.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}
	if err := doc.Set("/defaults/port", 8080); err != nil {
		log.Fatal(err)
	}
	fmt.Print(doc)
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-anchor-target/edit-yaml-anchor-target.zip), then `go run .`.

The output changes the shared port to `8080` and leaves both aliases in place.

```text
defaults: &shared
  port: 8080
api: *shared
worker: *shared
```

Writing through an alias, such as `Set("/api/port", 8080)`, returns `ErrAliasWrite`.

## Variations

**Read the updated port through an alias.** Add this after `fmt.Print(doc)` to see what `api` reads without replacing `*shared`.

```go
var port int
if err := doc.Get("/api/port", &port); err != nil {
	log.Fatal(err)
}
fmt.Println(port)
```

```text
defaults: &shared
  port: 8080
api: *shared
worker: *shared
8080
```

## Related recipes

- [Read YAML anchors, aliases, and merge keys](https://recursiveai.org/oneyaml/go/cookbook/anchors-aliases-merge-keys/)
- [Edit a YAML file and keep its comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)

## The short answer

Find the path to the value that carries the anchor, then edit the field there. In this example, `&shared` is on `defaults`, so the port lives at `/defaults/port`. Both `api` and `worker` still contain `*shared` in the edited YAML. A read through either alias reaches that same updated mapping.

## What are YAML anchors and aliases?

An anchor labels a node in the document. Here `&shared` labels the entire `defaults` mapping, not just its `port` field. Each `*shared` references that mapping. The alias is not a second written copy of it, so there’s only one `port: 80` line to edit. You can see that in the output: the port changes in one place and the two references don’t change at all.

An anchor name isn’t part of the path. Use `/defaults/port`, not `/shared/port` or `/&shared/port`. The pointer follows the keys that contain the target in the YAML source. The difference matters if you rename the anchor: even if it has a different label, `defaults` is still the path component as long as that mapping key doesn’t change.

## How do you set a YAML value that an alias refers to?

Use `Set` at the target. A read through `/api/port` is allowed, but a write there fails with `ErrAliasWrite`. Otherwise a path that appears to name one service would change the value for every service sharing the mapping. A path through `defaults` makes that shared edit explicit. The variation reads the alias into an `int` after the write so you can check the result without changing the YAML again.

If you mean to change only `api`, that’s a different edit: replacing the alias itself at `/api` with a mapping makes `api` independent. It does not update `defaults` or `worker`. And if you try to remove `defaults` while an alias still points to it, the document rejects the removal with `ErrAnchorInUse`. See the [editing guide](https://recursiveai.org/oneyaml/go/docs/editing/) for the difference between editing a shared value, replacing a reference, and removing a target.
