For 14 years Gustavo Niemeyer has maintained the de-facto standard library for working with YAML in Go. As is so often the case, life just happened and so he eventually marked the repository of go-yaml as unmaintained. After a bit of uncertainty OpenSource worked: The library was forked and is now managed by the folks who also own the YAML specification and so of the original downstream users.
The “old” v1, v2, and v3 branches have been frozen, to only receive security fixes, ensuring an easy upgrade from gopkg.in/yaml.vX. All the new stuff is happening in v4 (in the main branch)!
New configuration API
The biggest change of v4 is a new configuration API that should fit better with the YAML vocabulary. While Marshal and Unmarshal are very common in Go libraries, PyYAML and others primarily us load and dump. So that’s what is now also in go.yaml.in/yaml/v4 (alongside the “old” API):
# func Load(in []byte, out any, opts ...Option) error
var data []Datum
err := yaml.Load(
someBytes,
&data,
// To load all documents from the input
yaml.WithAllDocuments(true),
)
//...
# func Dump(in any, opts ...Option) (out []byte, err error)
output, err := yaml.Dump(
data,
yaml.WithExplicitStart(true),
yaml.WithExplicitEnd(true),
// If the input is a slice, multiple documents
// will be created in the output
yaml.WithAllDocuments(true),
)
//...
For streaming from io.Reader and to io.Writer objects, there are replacements for Decoder and Encoder with Loader and Dumper.
For more details see the v3-to-v4 migration guide. V4 is currently in the pre-release phase with the 4ths release candidate.
Adoption
While the fork “started” only about a year ago, a lot of libraries have already switched to it. Most prominently probably Kubernetes and Prometheus. So… I should finally get my act together and update my own stuff. While I’m at it, I can also go to the new API 😂
Do you want to give me feedback about this article in private? Please send it to comments@zerokspot.com.
Alternatively, this website also supports Webmentions. If you write a post on a blog that supports this technique, I should get notified about your link 🙂