How to Work With YAML Files in Go

YAML is a popular human-readable data serialization format. The data language is suitable for configuration files, data exchange, and structured data storage.

Many Go-based projects, including Gqlgen and build tools like Docker-compose, use YAML files for configuration. As a Go developer, you may use tools like the yaml package to parse and manipulate YAML data.

4

Understanding a YAML File

YAML (YAML Ain’t Markup Language) files consist of key-value pairs, lists, and nested structures. YAML syntax is designed to be visually appealing and easy to read. This makes it a popular choice for configuration files, with Kubernetes and otherorchestration tools using YAML.

Unlike other data formats likeXML and JSON, YAML relies on indentation to represent hierarchy.

The Golang mascot, a blue gopher, climbing a ladder superimposed on a photograph of a laptop.

Here’s how you can define basic fields with YAML:

A colon separates the key from its value, which can be any valid YAML data type including strings, numbers, booleans, or nested structures.

result of reading YAML files

Lists allow you to represent a collection of values. To define a list in YAML, use a hyphen followed by a space before each item:

Here,fruitsis the key, and the hyphenated lines define a list of fruits.

result from writing to the YAML file

YAML also supports nested structures, enabling you to represent complex data hierarchies:

Thepersonkey contains another set of key-value pairs, forming a nested structure. Theaddresskey has its own set of key-value pairs.

docker logo image—a whale carrying blocks on its back—on a black background with gold inscriptions

Working With YAML in Go

Go doesn’t provide built-in functionality for YAML files, but there are third-party packages that do.

Theyamlpackageis a popular package for working with YAML files. It provides:

Open your terminal and run this command to install the YAML package for your project:

After installing the package, you can use theimportstatement to import the package into your Go files.

You can choose multiple YAML versions based on the version of the YAML specification you want to use.

Reading and Parsing YAML to Go Data Structures

One essential task you’ll want to carry out is parsing YAML to Go data structures. Theyamlpackage provides a simple, convenient API for doing this.

Consider this YAML data:

You can define a corresponding struct with matching field names to parse the YAML data into a Go data structure with theyamlpackage.

Theyamlstruct tags help map the YAML keys to the struct fields during the parsing operation.

Here’s how you can parse the YAML data into a Go data structure:

Themainfunction reads theoutput.yamlfile with theioutilpackage’sReadFilefunction. It then creates an instance of thePersonstruct and parses the data into the struct with theUnmarshalmethod of theyamlpackage. Themainfunction prints the fields from the struct instance; here’s the result:

Inserting Data in a YAML File

You can use Go data structures to insert data into YAML files. Here’s how you can insert data to a YAML file with an instance of thePersonstruct:

Thepersonvariable is an instance of thePersonstruct type. Use theMarshalmethod of theyamlpackage to convert the struct to YAML. It takes in the instance of the struct and returns the YAML representation and an error.

it’s possible to use theWriteFilefunction of theospackage to write the YAML data to your file (in this case,output.yaml).

After a successful YAML data serialization and write operation, themainfunction prints a message to the console.

You can marshal and unmarshal YAML into maps as you do with structs.

Here’s an example of marshaling and unmarshalling YAML data with maps:

The process is the same as forstruct types, except that you’re using maps for the data representation. TheUnmarshalandMarshalmethods work for both data types.

Docker Compose Uses YAML Files for Configuration

Working with YAML files in Go provides a powerful and flexible approach for managing configuration data.

One prominent use case for YAML files is Docker Compose, where a YAML file serves as the configuration for defining and managing multi-container Docker applications.

Docker Compose leverages the simplicity and readability of YAML to define services, networks, volumes, and other components of containerized applications to make container orchestration easy.

Docker is a powerful tool, but too many containers can overwhelm. Take the heat out of this scenario with Docker Compose.

Unlock a world of entertainment possibilities with this clever TV hack.

My iPhone does it all, but I still need my dumb phone.

I gripped my chair the entire time—and then kept thinking about it when the screen turned off.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

Technology Explained

PC & Mobile