An alias (*shared) refers to an anchor (&shared). Here defaults carries the anchor, so set /defaults/port to change the port both aliases read.
go get oneyaml.dev/goRequires Go 1.24+. Run in a Go module.
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, then go run ..
The output changes the shared port to 8080 and leaves both aliases in place.
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.
var port int if err := doc.Get("/api/port", &port); err != nil { log.Fatal(err) } fmt.Println(port)
defaults: &shared port: 8080 api: *shared worker: *shared 8080
Related recipes
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 for the difference between editing a shared value, replacing a reference, and removing a target.