How to Read and Write YAML in Golang
Introduction
If you have ever worked with any CI/CD pipelines such as Travis CI, Circle CI, or GitHub Actions, you have seen YAML files. YAML short for YAML Ain’t Markup Language is a human-readable data-serialization language. It is used for configuration files and in applications where data is being stored or transmitted as an alternative to other formats. YAML is used in DevOps-related projects such as Kubernetes, Docker, GitHub Actions, and more.
YAML has several advantages over conventional data serialization formats such as JSON or XML. YAML’s format is easy to read for both machines and humans which is why it is preferred in configuration files that need frequent editing. YAML also supports a diverse range of data types including key-value pairs, lists, and nested data structures. This makes a great choice for storing complex data that needs to be accessed and processed.
YAML Syntax
YAML uses indentation and whitespaces to denote the structure of the data and uses a colon to denote key-value pairs. The following is an example of a YAML file:
In this example, the name
and age
are key-value pairs, and the hobbies
is a list with three items denoted with a hyphen.
YAML in Golang
There are various packages that can be used to process YAML in Golang. The most popular package is go-yaml/yaml. This package is a fork of libyaml, a C library for parsing and emitting YAML.
To install the package, run the following command:
To use the package, import it into your project:
Basic YAML in Golang
Creating a YAML File in Golang
A YAML file is created by declaring a struct and then marshaling it into a YAML file. The following is an example of a YAML file created in Golang:
In this example, a struct is created with the name Person
. The struct has three fields: Name
, Age
, and Hobbies
. The yaml
tag is used to specify the key name in the YAML file. yaml.Marshal
is then used to marshal the struct into a YAML format and then written to a file.
Parsing YAML files in Golang
To parse a YAML file, the yaml.Unmarshal
function is used. The following is an example of parsing a YAML file:
Create a YAML file called blog.yaml
:
Create a Golang file called main.go
:
In this example, the struct Blog
is created with the same fields as the YAML file. The yaml.Unmarshal
function is then used to unmarshal the YAML file into the struct.
Structs are not the only golang data structure that can be used to parse YAML files. Maps can also be used to parse YAML files. The following is an example of parsing a YAML file into a map:
Advanced YAML in Golang
YAML Anchors and Aliases
YAML provides a way to reuse parts of a YAML file by using anchors and aliases. Anchors create a reference to a part of a YAML file, and aliases reference the anchor. The following is an example of using anchors and aliases:
In this example, the language
key is an anchor. The compiler
and runtime
keys are aliases. The compiler
and runtime
keys reference the language
key
To get the value of the language
key, the following code can be used:
Conclusion
In this article, we learned how to use YAML in Golang. We learned how to create a YAML file and parse a YAML file. We also learned how to use YAML anchors and aliases. Tune in for a future tutorial on building a CI/CD platform like Jenkins using Golang. Where we will be using learned in this article.