Golang YAML
Overview
Golang YAML also known as Yet Another Markup Language is used for the serialization of data and focuses on the readability of humans. It is generally used for configuring language for the majority of the tools as well as applications, document headers, and logs of debugging. It has become increasingly popular in recent years and hence learning to work with YAML files is beneficial.
Introduction to Golang YAML
Golang YAML is a human-friendly data serialization technique and is standard for all programming languages. The YAML is similar to JSON in many different ways. It stores the data in a custom pair of {key: value} which can be read easily. Nowadays it is used as a format for configuring files but the features like serialization of the object make it suitable for replacing language like JSON.
YAML Format
Golang YAML is not a markup language but a data serialization language that is readable by humans. It is mainly used for configuring the files and is used for storing the data of debugging the output, document headers, etc. Generally, three types of basic data types are supported by YAML.
- Scalars like strings, float, and integers
- Lists
- Associative array
The filename extension for YAML files is .yaml.
Golang YAML Package
To work with Golang YAML, the package Yaml.v3 is used since it provides methods and tools for performing encoding and decoding of Yaml data. To install the package, run the get command of go as shown below:
Once the package is installed, we can import it into the code by using the command given below:
Golang Read YAML
Lets us understand how to read in Golang YAML. Look at the sample file given below for understanding this concept better.
Example 1
In this example, we will use the unmarshal method for the YAML file. The code for the same is given below:
Output:
Explanation: In this example, the file is read by using the method ioutil.ReadFile() after which a map is created for storing the data which are of type interface, interface. We can now unmarshal the data using the unmarshal method from the file and then iterate over the key and value of the map by using the operator range. The above code then returns the range operator of the map for iterating over the pair of keys and values.
Example 2
This example consists of nested users, we have to read this file and write a code for the same.
user.yaml file
readtheuser.go
Output
Explanation: In this example, we have the struct of a user which represents the record of a user in the file. data := make(map[string]User) The command is used for defining the map of the user. err2 := yaml.Unmarshal(yfile, &data)
Golang Write YAML
To write into a Golang YAML file, the marshal method can be used. Look at the example given below to understand the writing method better.
Example 1
The following output is printed when the code is executed.
fruits.yaml
Explanation: In this example, the marshal method is used for serializing the slice of the strings in the YAML file. This serialized data can then be used for writing into the file. In case the file does not exist, the program will create it by using the permission specified and will write the data in it.
Example 2
In this example, we will write the users in the YAML file. Create a file readuser.go and paste the following code.
The output for the same is as shown below:
go run write_users.go
users.yaml
Explanation: In this example, we have a map of the user and each user is represented by a structure called User. the map can be serialized into the YAML file by using marshal and writing the user data into the users.yaml file by using the method WriteFile.
Golang YAML Closing
This article covered the fundamentals of YAML in golang, how to use it, read and write method, etc. to learn more about this, check the official documentation of YAML in golang.
Conclusion
- YAML also known as Yet Another Markup Language is used for the serialization of data and focuses on the readability of humans. It is generally used for configuring language for the majority of the tools as well as applications, document headers, and logs of debugging.
- YAML is a human-friendly data serialization technique and is standard for all programming languages. The YAML is similar to JSON in many different ways. It stores the data in a custom pair of {key: value} which can be read easily. Nowadays it is used as a format for configuring files but the features like serialization of the object make it suitable for replacing language like JSON.