【Go学习】YAML格式的语法

基本格式

  • 用空格缩进, 不能用tab
  • 用#标记注释
  • 列表: 用短划(-)标记元素
  • 映射: 用冒号(:)分隔key, value. 如果写在一行, 需要用逗号分隔并前后加花括号
  • 字符串: 不加引号, 加单引号或者加双引号都可以, 加双引号时可以使用\开头的转义字符
  • 多行字符串可以用 | 或 > 符号, 紧接着换行符
  • 重复的节点, 可以用 & 标识, 并用 * 来引用

基本元素

列表

复制代码
# 普通
 - Casablanca
 - North by Northwest
 - The Man Who Wasn't There

# 单行
[milk, pumpkin pie, eggs, juice]
复制代码
men: [John Smith, Bill Jones]
women:
  - Mary Smith
  - Susan Williams

 

映射

# Indented Block
   name: John Smith
   age: 33
# Inline Block
 {name: John Smith, age: 33}
- {name: John Smith, age: 33}
- name: Mary Smith
  age: 27

 

多行字符串

复制代码
data: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don't spit on the floor"
   So he carefully spat on the ceiling

data: >
   Wrapped text
   will be folded
   into a single
   paragraph

   Blank lines denote
   paragraph breaks
复制代码

 

指定类型

复制代码
a: 123                     # an integer
b: "123"                   # a string, disambiguated by quotes
c: 123.0                   # a float
d: !!float 123             # also a float via explicit data type prefixed by (!!)
e: !!str 123               # a string, disambiguated by explicit type
f: !!str Yes               # a string via explicit type
g: Yes                     # a boolean True (yaml1.1), string "Yes" (yaml1.2)
h: Yes we have No bananas  # a string, "Yes" and "No" disambiguated by context.

picture: !!binary |
  R0lGODdhDQAIAIAAAAAAANn
  Z2SwAAAAADQAIAAACF4SDGQ
  ar3xxbJ9p0qa7R0YxwzaFME
  1IAADs=
myObject:  !myClass { name: Joe, age: 15 }
复制代码

 

另一篇简明的介绍, from https://learnxinyminutes.com/docs/yaml/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---  # document start
 
# Comments in YAML look like this.
 
################
# SCALAR TYPES #
################
 
# Our root object (which continues for the entire document) will be a map,
# which is equivalent to a dictionary, hash or object in other languages.
key: value
another_key: Another value goes here.
a_number_value: 100
scientific_notation: 1e+12
# The number 1 will be interpreted as a number, not a boolean. if you want
# it to be interpreted as a boolean, use true
boolean: true
null_value: null
key with spaces: value
# Notice that strings don't need to be quoted. However, they can be.
however: 'A string, enclosed in quotes.'
'Keys can be quoted too.': "Useful if you want to put a ':' in your key."
single quotes: 'have ''one'' escape pattern'
double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more."
 
# Multiple-line strings can be written either as a 'literal block' (using |),
# or a 'folded block' (using '>').
literal_block: |
    This entire block of text will be the value of the 'literal_block' key,
    with line breaks being preserved.
 
    The literal continues until de-dented, and the leading indentation is
    stripped.
 
        Any lines that are 'more-indented' keep the rest of their indentation -
        these lines will be indented by 4 spaces.
folded_style: >
    This entire block of text will be the value of 'folded_style', but this
    time, all newlines will be replaced with a single space.
 
    Blank lines, like above, are converted to a newline character.
 
        'More-indented' lines keep their newlines, too -
        this text will appear over two lines.
 
####################
# COLLECTION TYPES #
####################
 
# Nesting uses indentation. 2 space indent is preferred (but not required).
a_nested_map:
  key: value
  another_key: Another Value
  another_nested_map:
    hello: hello
 
# Maps don't have to have string keys.
0.25: a float key
 
# Keys can also be complex, like multi-line objects
# We use ? followed by a space to indicate the start of a complex key.
? |
  This is a key
  that has multiple lines
: and this is its value
 
# YAML also allows mapping between sequences with the complex key syntax
# Some language parsers might complain
# An example
? - Manchester United
  - Real Madrid
: [2001-01-01, 2002-02-02]
 
# Sequences (equivalent to lists or arrays) look like this
# (note that the '-' counts as indentation):
a_sequence:
  - Item 1
  - Item 2
  - 0.5  # sequences can contain disparate types.
  - Item 4
  - key: value
    another_key: another_value
  -
    - This is a sequence
    - inside another sequence
  - - - Nested sequence indicators
      - can be collapsed
 
# Since YAML is a superset of JSON, you can also write JSON-style maps and
# sequences:
json_map: {"key""value"}
json_seq: [3, 2, 1, "takeoff"]
and quotes are optional: {key: [3, 2, 1, takeoff]}
 
#######################
# EXTRA YAML FEATURES #
#######################
 
# YAML also has a handy feature called 'anchors', which let you easily duplicate
# content across your document. Both of these keys will have the same value:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name
 
# Anchors can be used to duplicate/inherit properties
base: &base
  name: Everyone has same name
 
# The regexp << is called Merge Key Language-Independent Type. It is is used to
# indicate that all the keys of one or more specified maps should be inserted
# into the current map.
 
foo: &foo
  <<: *base
  age: 10
 
bar: &bar
  <<: *base
  age: 20
 
# foo and bar would also have name: Everyone has same name
 
# YAML also has tags, which you can use to explicitly declare types.
explicit_string: !!str 0.5
# Some parsers implement language specific tags, like this one for Python's
# complex number type.
python_complex_number: !!python/complex 1+2j
 
# We can also use yaml complex keys with language specific tags
? !!python/tuple [5, 7]
: Fifty Seven
# Would be {(5, 7): 'Fifty Seven'} in Python
 
####################
# EXTRA YAML TYPES #
####################
 
# Strings and numbers aren't the only scalars that YAML can understand.
# ISO-formatted date and datetime literals are also parsed.
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14
 
# The !!binary tag indicates that a string is actually a base64-encoded
# representation of a binary blob.
gif_file: !!binary |
  R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
  OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
  +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
  AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
 
# YAML also has a set type, which looks like this:
set:
  ? item1
  ? item2
  ? item3
or: {item1, item2, item3}
 
# Sets are just maps with null values; the above is equivalent to:
set2:
  item1: null
  item2: null
  item3: null
 
...  # document end
posted @ 2022-02-26 22:34  gtea  阅读(347)  评论(0编辑  收藏  举报