Working with YAML
In this section, we briefly discuss how to work with YAML files in Go. The Go standard library does not include support for YAML files, which means that you should look at external packages for YAML support. There exist three main packages that allow you to work with YAML from Go:
- https://github.com/kylelemons/go-gypsy
- https://github.com/go-yaml/yaml
- https://github.com/goccy/go-yaml
Choosing one is a matter of personal preference. We are going to work with go-yaml
in this section using the code found in yaml.go
. Due to the use of Go modules, yaml.go
is developed in ~/go/src/github.com/mactsouk/yaml
—you can also find it in the GitHub repository of this book. The most important part of it is the next:
var yamlfile = `
image: Golang
matrix:
docker: python
version: [2.7, 3.9]
`
The yamlfile
variable contains the YAML data; you usually read the data from a file—we are just using that to save some space...