为什么 --- (3 个破折号/连字符)在 yaml 文件中? [英] why --- (3 dashes/hyphen) in yaml file?

查看:40
本文介绍了为什么 --- (3 个破折号/连字符)在 yaml 文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚开始使用 YAML 文件而不是 application.properties 因为它更具可读性.我在 YAML 文件中看到它们以 --- 开头.我用谷歌搜索并找到了以下解释.

So I just started using YAML file instead of application.properties as it is more readable. I see in YAML files they start with ---. I googled and found the below explanation.

YAML 使用三个破折号(---")将指令与文档分开内容.如果没有,这也用于表示文档的开始存在指令.

YAML uses three dashes ("---") to separate directives from document content. This also serves to signal the start of a document if no directives are present.

此外,我尝试了一个没有 --- 的示例,并了解到拥有它们并不是强制性的.

Also, I tried a sample without --- and understood that it is not mandatory to have them.

我想我对directivedocument 的理解不是很清楚.谁能用一个简单的例子解释一下?

I think I don't have a clear understanding of directive and document. Can anyone please explain with a simple example?

推荐答案

正如您已经发现的,三个破折号 --- 用于表示文档的开始强>,即:

As you already found out, the three dashes --- are used to signal the start of a document, i.e.:

  1. 根据当前规范,在指令之后(即%YAML%TAG行)通知文档开始.例如:

  1. To signal the document start after directives, i.e., %YAML or %TAG lines according to the current spec. For example:

%YAML 1.2
%TAG !foo! !foo-types/
---
myKey: myValue

  • 要在同一流中有多个 yaml 文档(例如一个 yaml 文件)时发出文档开始的信号:

  • To signal the document start when you have multiple yaml documents in the same stream, e.g., a yaml file:

    doc 1
    ---
    doc 2
    

    如果 doc 2 有一些前面的指令,那么我们必须使用三个点 ... 来向解析器指示 doc 1 的结束(以及 doc 2 之前的潜在指令的开始).例如:

    If doc 2 has some preceding directives, then we have to use three dots ... to indicate the end of doc 1 (and the start of potential directives preceding doc 2) to the parser. For example:

    doc 1
    ...
    %TAG !bar! !bar-types/
    ---
    doc 2
    

  • 该规范适用于 yaml 解析器实现者.但是,我发现这篇文章从用户的角度更易于阅读.

    The spec is good for yaml parser implementers. However, I find this article easier to read from a user perspective.

    这篇关于为什么 --- (3 个破折号/连字符)在 yaml 文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆