yml初探
最近yaml格式的配置文件越来越流行,逐渐有替换json文件的势态
语法规则
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- 使用#符号进行注释,相比于json可以更清晰易懂
支持数据结构
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars):单个的、不可再分的值
纯量:
- 字符串
- 布尔值
- 整数
- 浮点数
- Null
- 时间
- 日期
语法格式如下
| 数据结构 | 语法 | 对比json格式 |
|---|---|---|
| 对象 | animal:dog hash: |
{ 'animal': 'dog', 'hash': {'name': 'Steve', 'foo': 'bar'} } |
| 数组 | 格式一: animal: -dog -cat -fish 格式二: animal : [dog, cat, fish] |
{ 'animal': ['dog', 'cat', 'fish'] } |
| 纯量 | 数值:number: 12.30 布尔:isSet: true null:parent: ~ 字符串:s:abc 字符串中带空格、特殊字符: s: 'abc d' |
'number':12.30 'isSet':true 'parent':null 's':'abc' |
锚点&以及引用,&用来建立锚点,<<表示合并到当前数据,用来引用锚点
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_development
<<: *defaults
test:
database: myapp_test
<<: *defaults
等同于:
defaults:
adapter: postgres
host: localhost
development:
database: myapp_development
adapter: postgres
host: localhost
test:
database: myapp_test
adapter: postgres
host: localhost
不知则问,不会则学

浙公网安备 33010602011771号