{"id":525,"date":"2024-10-30T22:02:39","date_gmt":"2024-10-30T14:02:39","guid":{"rendered":"https:\/\/www.hyy.net\/?p=525"},"modified":"2024-10-30T22:02:39","modified_gmt":"2024-10-30T14:02:39","slug":"yaml-beginner","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=525","title":{"rendered":"YAML \u5165\u95e8\u6559\u7a0b"},"content":{"rendered":"<p>YAML \u5165\u95e8\u6559\u7a0b<\/p>\n<p>YAML \u662f \"YAML Ain't a Markup Language\"\uff08YAML \u4e0d\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff09\u7684\u9012\u5f52\u7f29\u5199\u3002\u5728\u5f00\u53d1\u7684\u8fd9\u79cd\u8bed\u8a00\u65f6\uff0cYAML \u7684\u610f\u601d\u5176\u5b9e\u662f\uff1a\"Yet Another Markup Language\"\uff08\u4ecd\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff09\u3002<\/p>\n<p>YAML \u7684\u8bed\u6cd5\u548c\u5176\u4ed6\u9ad8\u7ea7\u8bed\u8a00\u7c7b\u4f3c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7b80\u5355\u8868\u8fbe\u6e05\u5355\u3001\u6563\u5217\u8868\uff0c\u6807\u91cf\u7b49\u6570\u636e\u5f62\u6001\u3002\u5b83\u4f7f\u7528\u7a7a\u767d\u7b26\u53f7\u7f29\u8fdb\u548c\u5927\u91cf\u4f9d\u8d56\u5916\u89c2\u7684\u7279\u8272\uff0c\u7279\u522b\u9002\u5408\u7528\u6765\u8868\u8fbe\u6216\u7f16\u8f91\u6570\u636e\u7ed3\u6784\u3001\u5404\u79cd\u914d\u7f6e\u6587\u4ef6\u3001\u503e\u5370\u8c03\u8bd5\u5185\u5bb9\u3001\u6587\u4ef6\u5927\u7eb2\uff08\u4f8b\u5982\uff1a\u8bb8\u591a\u7535\u5b50\u90ae\u4ef6\u6807\u9898\u683c\u5f0f\u548cYAML\u975e\u5e38\u63a5\u8fd1\uff09\u3002<\/p>\n<p>YAML \u7684\u914d\u7f6e\u6587\u4ef6\u540e\u7f00\u4e3a .yml\uff0c\u5982\uff1arunoob.yml \u3002<\/p>\n<p>\u57fa\u672c\u8bed\u6cd5<\/p>\n<li>\u5927\u5c0f\u5199\u654f\u611f<\/li>\n<li>\u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5c42\u7ea7\u5173\u7cfb<\/li>\n<li>\u7f29\u8fdb\u4e0d\u5141\u8bb8\u4f7f\u7528tab\uff0c\u53ea\u5141\u8bb8\u7a7a\u683c<\/li>\n<li>\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u4e0d\u91cd\u8981\uff0c\u53ea\u8981\u76f8\u540c\u5c42\u7ea7\u7684\u5143\u7d20\u5de6\u5bf9\u9f50\u5373\u53ef<\/li>\n<li>'#'\u8868\u793a\u6ce8\u91ca<\/li>\n<p>\u6570\u636e\u7c7b\u578b<br \/>\nYAML \u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<li>\u5bf9\u8c61\uff1a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u53c8\u79f0\u4e3a\u6620\u5c04\uff08mapping\uff09\/ \u54c8\u5e0c\uff08hashes\uff09 \/ \u5b57\u5178\uff08dictionary\uff09<\/li>\n<li>\u6570\u7ec4\uff1a\u4e00\u7ec4\u6309\u6b21\u5e8f\u6392\u5217\u7684\u503c\uff0c\u53c8\u79f0\u4e3a\u5e8f\u5217\uff08sequence\uff09 \/ \u5217\u8868\uff08list\uff09<\/li>\n<li>\u7eaf\u91cf\uff08scalars\uff09\uff1a\u5355\u4e2a\u7684\u3001\u4e0d\u53ef\u518d\u5206\u7684\u503c<\/li>\n<p>YAML \u5bf9\u8c61<br \/>\n\u5bf9\u8c61\u952e\u503c\u5bf9\u4f7f\u7528\u5192\u53f7\u7ed3\u6784\u8868\u793a key: value\uff0c\u5192\u53f7\u540e\u9762\u8981\u52a0\u4e00\u4e2a\u7a7a\u683c\u3002<br \/>\n\u4e5f\u53ef\u4ee5\u4f7f\u7528 key:{key1: value1, key2: value2, ...}\u3002<br \/>\n\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5c42\u7ea7\u5173\u7cfb\uff1b<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nkey: \n    child-key: value\n    child-key2: value2\n<\/pre>\n<p>\u8f83\u4e3a\u590d\u6742\u7684\u5bf9\u8c61\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u95ee\u53f7\u52a0\u4e00\u4e2a\u7a7a\u683c\u4ee3\u8868\u4e00\u4e2a\u590d\u6742\u7684 key\uff0c\u914d\u5408\u4e00\u4e2a\u5192\u53f7\u52a0\u4e00\u4e2a\u7a7a\u683c\u4ee3\u8868\u4e00\u4e2a value\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n?  \n    - complexkey1\n    - complexkey2\n:\n    - complexvalue1\n    - complexvalue2\n<\/pre>\n<p>\u610f\u601d\u5373\u5bf9\u8c61\u7684\u5c5e\u6027\u662f\u4e00\u4e2a\u6570\u7ec4 [complexkey1,complexkey2]\uff0c\u5bf9\u5e94\u7684\u503c\u4e5f\u662f\u4e00\u4e2a\u6570\u7ec4 [complexvalue1,complexvalue2]<\/p>\n<p>YAML \u6570\u7ec4<br \/>\n\u4ee5 - \u5f00\u5934\u7684\u884c\u8868\u793a\u6784\u6210\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- A\n- B\n- C\n<\/pre>\n<p>YAML \u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u884c\u5185\u8868\u793a\uff1a<\/p>\n<p>key: [value1, value2, ...]<br \/>\n\u6570\u636e\u7ed3\u6784\u7684\u5b50\u6210\u5458\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u53ef\u4ee5\u5728\u8be5\u9879\u4e0b\u9762\u7f29\u8fdb\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n-\n - A\n - B\n - C\n<\/pre>\n<p>\u4e00\u4e2a\u76f8\u5bf9\u590d\u6742\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\ncompanies:\n    -\n        id: 1\n        name: company1\n        price: 200W\n    -\n        id: 2\n        name: company2\n        price: 500W\n<\/pre>\n<p>\u610f\u601d\u662f companies \u5c5e\u6027\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\u53c8\u662f\u7531 id\u3001name\u3001price \u4e09\u4e2a\u5c5e\u6027\u6784\u6210\u3002<\/p>\n<p>\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6d41\u5f0f(flow)\u7684\u65b9\u5f0f\u8868\u793a\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\ncompanies: &#x5B;{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]\n<\/pre>\n<p>\u590d\u5408\u7ed3\u6784<\/p>\n<p>\u6570\u7ec4\u548c\u5bf9\u8c61\u53ef\u4ee5\u6784\u6210\u590d\u5408\u7ed3\u6784\uff0c\u4f8b\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nlanguages:\n  - Ruby\n  - Perl\n  - Python \nwebsites:\n  YAML: yaml.org \n  Ruby: ruby-lang.org \n  Python: python.org \n  Perl: use.perl.org\n<\/pre>\n<p>\u8f6c\u6362\u4e3a json \u4e3a\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n{ \n  languages: &#x5B; &#039;Ruby&#039;, &#039;Perl&#039;, &#039;Python&#039;],\n  websites: {\n    YAML: &#039;yaml.org&#039;,\n    Ruby: &#039;ruby-lang.org&#039;,\n    Python: &#039;python.org&#039;,\n    Perl: &#039;use.perl.org&#039; \n  } \n}\n<\/pre>\n<p>\u7eaf\u91cf<br \/>\n\u7eaf\u91cf\u662f\u6700\u57fa\u672c\u7684\uff0c\u4e0d\u53ef\u518d\u5206\u7684\u503c\uff0c\u5305\u62ec\uff1a<\/p>\n<li>\u5b57\u7b26\u4e32<\/li>\n<li>\u5e03\u5c14\u503c<\/li>\n<li>\u6574\u6570<\/li>\n<li>\u6d6e\u70b9\u6570<\/li>\n<li>Null<\/li>\n<li>\u65f6\u95f4<\/li>\n<li>\u65e5\u671f<\/li>\n<p>\u4f7f\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u5feb\u901f\u4e86\u89e3\u7eaf\u91cf\u7684\u57fa\u672c\u4f7f\u7528\uff1a<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nboolean: \n    - TRUE  #true,True\u90fd\u53ef\u4ee5\n    - FALSE  #false\uff0cFalse\u90fd\u53ef\u4ee5\nfloat:\n    - 3.14\n    - 6.8523015e+5  #\u53ef\u4ee5\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\nint:\n    - 123\n    - 0b1010_0111_0100_1010_1110    #\u4e8c\u8fdb\u5236\u8868\u793a\nnull:\n    nodeName: &#039;node&#039;\n    parent: ~  #\u4f7f\u7528~\u8868\u793anull\nstring:\n    - \u54c8\u54c8\n    - &#039;Hello world&#039;  #\u53ef\u4ee5\u4f7f\u7528\u53cc\u5f15\u53f7\u6216\u8005\u5355\u5f15\u53f7\u5305\u88f9\u7279\u6b8a\u5b57\u7b26\n    - newline\n      newline2    #\u5b57\u7b26\u4e32\u53ef\u4ee5\u62c6\u6210\u591a\u884c\uff0c\u6bcf\u4e00\u884c\u4f1a\u88ab\u8f6c\u5316\u6210\u4e00\u4e2a\u7a7a\u683c\ndate:\n    - 2018-02-17    #\u65e5\u671f\u5fc5\u987b\u4f7f\u7528ISO 8601\u683c\u5f0f\uff0c\u5373yyyy-MM-dd\ndatetime: \n    -  2018-02-17T15:02:31+08:00    #\u65f6\u95f4\u4f7f\u7528ISO 8601\u683c\u5f0f\uff0c\u65f6\u95f4\u548c\u65e5\u671f\u4e4b\u95f4\u4f7f\u7528T\u8fde\u63a5\uff0c\u6700\u540e\u4f7f\u7528+\u4ee3\u8868\u65f6\u533a\n<\/pre>\n<p>\u5f15\u7528<br \/>\n& \u951a\u70b9\u548c * \u522b\u540d\uff0c\u53ef\u4ee5\u7528\u6765\u5f15\u7528:<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\ndefaults: &amp;defaults\n  adapter:  postgres\n  host:     localhost\n\ndevelopment:\n  database: myapp_development\n  &lt;&lt;: *defaults\n\ntest:\n  database: myapp_test\n  &lt;&lt;: *defaults\n<\/pre>\n<p>\u76f8\u5f53\u4e8e:<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\ndefaults:\n  adapter:  postgres\n  host:     localhost\n\ndevelopment:\n  database: myapp_development\n  adapter:  postgres\n  host:     localhost\n\ntest:\n  database: myapp_test\n  adapter:  postgres\n  host:     localhost\n<\/pre>\n<p>&\u7528\u6765\u5efa\u7acb\u951a\u70b9\uff08defaults\uff09\uff0c<< \u8868\u793a\u5408\u5e76\u5230\u5f53\u524d\u6570\u636e\uff0c* \u7528\u6765\u5f15\u7528\u951a\u70b9\u3002\n<\/p>\n<p>\u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50:<\/p>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- &amp;showell Steve \n- Clark \n- Brian \n- Oren \n- *showell \n<\/pre>\n<p>\u8f6c\u4e3a JavaScript \u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&#x5B; &#039;Steve&#039;, &#039;Clark&#039;, &#039;Brian&#039;, &#039;Oren&#039;, &#039;Steve&#039; ]\n<\/pre>\n<p>\u53c2\u8003\u5730\u5740\uff1a<\/p>\n<p>https:\/\/www.ruanyifeng.com\/blog\/2016\/07\/yaml.html<br \/>\nhttps:\/\/www.jianshu.com\/p\/97222440cd08<br \/>\nhttps:\/\/daihainidewo.github.io\/blog\/yaml%E6%95%99%E7%A8%8B\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>YAML \u5165\u95e8\u6559\u7a0b YAML \u662f &#8220;YAML Ain&#8217;t a Markup Language&#8221;\uff08YAML \u4e0d\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff09\u7684\u9012\u5f52\u7f29\u5199\u3002\u5728\u5f00\u53d1\u7684\u8fd9\u79cd\u8bed\u8a00\u65f6\uff0cYAML \u7684\u610f\u601d\u5176\u5b9e\u662f\uff1a&#8221;Yet Another Markup Language&#8221;\uff08\u4ecd\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff09\u3002 YAML \u7684\u8bed\u6cd5\u548c\u5176\u4ed6\u9ad8\u7ea7\u8bed\u8a00\u7c7b\u4f3c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7b80\u5355\u8868\u8fbe\u6e05\u5355\u3001\u6563\u5217\u8868\uff0c\u6807\u91cf\u7b49\u6570\u636e\u5f62\u6001\u3002\u5b83\u4f7f\u7528\u7a7a\u767d\u7b26\u53f7\u7f29\u8fdb\u548c\u5927\u91cf\u4f9d\u8d56\u5916\u89c2\u7684\u7279\u8272\uff0c\u7279\u522b\u9002\u5408\u7528\u6765\u8868\u8fbe\u6216\u7f16\u8f91\u6570\u636e\u7ed3\u6784\u3001\u5404\u79cd\u914d\u7f6e\u6587\u4ef6\u3001\u503e\u5370\u8c03\u8bd5\u5185\u5bb9\u3001\u6587\u4ef6\u5927\u7eb2\uff08\u4f8b\u5982\uff1a\u8bb8\u591a\u7535\u5b50\u90ae\u4ef6\u6807\u9898\u683c\u5f0f\u548cYAML\u975e\u5e38\u63a5\u8fd1\uff09\u3002 YAML \u7684\u914d\u7f6e\u6587\u4ef6\u540e\u7f00\u4e3a .yml\uff0c\u5982\uff1arunoob.yml \u3002 \u57fa\u672c\u8bed\u6cd5 \u5927\u5c0f\u5199\u654f\u611f \u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5c42\u7ea7\u5173\u7cfb \u7f29\u8fdb\u4e0d\u5141\u8bb8\u4f7f\u7528tab\uff0c\u53ea\u5141\u8bb8\u7a7a\u683c \u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u4e0d\u91cd\u8981\uff0c\u53ea\u8981\u76f8\u540c\u5c42\u7ea7\u7684\u5143\u7d20\u5de6\u5bf9\u9f50\u5373\u53ef &#8216;#&#8217;\u8868\u793a\u6ce8\u91ca \u6570\u636e\u7c7b\u578b YAML \u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u6570\u636e\u7c7b\u578b\uff1a \u5bf9\u8c61\uff1a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u53c8\u79f0\u4e3a\u6620\u5c04\uff08mapping\uff09\/ \u54c8\u5e0c\uff08hashes\uff09 \/ \u5b57\u5178\uff08dictionary\uff09 \u6570\u7ec4\uff1a\u4e00\u7ec4\u6309\u6b21\u5e8f\u6392\u5217\u7684\u503c\uff0c\u53c8\u79f0\u4e3a\u5e8f\u5217\uff08sequence\uff09 \/ \u5217\u8868\uff08list\uff09 \u7eaf\u91cf\uff08scalars\uff09\uff1a\u5355\u4e2a\u7684\u3001\u4e0d\u53ef\u518d\u5206\u7684\u503c YAML \u5bf9\u8c61 \u5bf9\u8c61\u952e\u503c\u5bf9\u4f7f\u7528\u5192\u53f7\u7ed3\u6784\u8868\u793a key: value\uff0c\u5192\u53f7\u540e\u9762\u8981\u52a0\u4e00\u4e2a\u7a7a\u683c\u3002 \u4e5f\u53ef\u4ee5\u4f7f\u7528 key:{key1: value1, key2: value2, &#8230;}\u3002 \u8fd8\u53ef\u4ee5\u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5c42\u7ea7\u5173\u7cfb\uff1b key: child-key: value child-key2: value2 \u8f83\u4e3a\u590d\u6742\u7684\u5bf9\u8c61\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u95ee\u53f7\u52a0\u4e00\u4e2a\u7a7a\u683c\u4ee3\u8868\u4e00\u4e2a\u590d\u6742\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17],"tags":[],"class_list":["post-525","post","type-post","status-publish","format-standard","hentry","category-docker","category-yaml"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=525"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}