You can use a YAML file to store data in a format that can be easily read and understood by humans. It is a data serialization language that is often used for configuration files and data transfer between applications.
YAML is similar to XML and JSON as they can all be used to store data in different formats. The main difference is their syntax.
Here's what XML format looks like:
<user>
<name>John Doe</name>
<phone>00223344</phone>
<age>80</age>
</user>
Here's what JSON format looks like:
{
"user": {
"name": "John Doe",
"phone": "00223344",
"age": 80
}
}
Here's what YAML format looks like:
user:
name: John Doe
phone: 00223344
age: 80
Each of the formats above is used to store data about a user's name, phone number, and age.
You can read more about the features, basic rules, and syntax of YAML, and its differences from JSON and XML in this article.
In this article, you'll learn about multiline comments in YAML.
How to Add a Multiline Comment in YAML
You can use comments for various reasons like documenting your code, collaborating with others, stopping a block of code from running, and so on.
You can use the #
symbol to create comments in a YAML file. That is:
# The object below represents a user
user:
name: John Doe
email: john.doe@example.com
age: 30
Unlike some other languages, YAML doesn't have a different format for creating block or multiline comments.
You'll have to use the #
symbol on every line the comments spans into. Here's an example:
# The object below is an example that represents a
# user's name, phone number and age
user:
name: John Doe
email: john.doe@example.com
age: 30
If you remove the #
symbol on the second line, the text may still appear as a comment but the YAML parser may interpret it as plain text which may lead to an error.
To be on the safe side, use the #
symbol at the start of each comment line.
Summary
In this article, we talked about YAML. It is mostly used to store and transfer data.
We saw how to create inline and multiline comments. In YAML, the #
symbol is used for both inline and multiline comments.
Happy coding! Check out my blog for more programming content.