{"id":207099,"date":"2025-07-08T11:06:52","date_gmt":"2025-07-08T03:06:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207099\/"},"modified":"2025-07-08T11:06:52","modified_gmt":"2025-07-08T03:06:52","slug":"golang%e6%a8%a1%e6%9d%bfjson%e8%bd%acyml%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207099\/","title":{"rendered":"golang\u6a21\u677fjson\u8f6cyml\u683c\u5f0f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>golang\u6a21\u677fjson\u8f6cyml\u683c\u5f0f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 08:03:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300agolang\u6a21\u677fjson\u8f6cyml\u683c\u5f0f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u60a8\u597d\uff0c\u6211\u5e0c\u671b\u4f7f\u7528\u4f20\u9012\u5230\u6211\u4eec\u7684 go api \u7684 json \u6570\u636e\u521b\u5efa\u4ee5\u4e0b\u683c\u5f0f\u7684 yml\u3002<\/p>\n<pre>zookeeper:\n  configs:\n  \n    zookeeper_user: custom-user\n    zookeeper_group: custom-group\n      \n    zookeeper_copy_files:\n       - source_path: \/path\/to\/file.txt\n         destination_path: \/tmp\/file.txt\n\n    zookeeper_some_otherfile:\n       - source_path: \/path\/to\/file.txt\n         destination_path: \/tmp\/file.txt\n  hosts:\n    ip-172-31-34-246.us-east-2.compute.internal:\n      zookeeper_id: 2\n    ip-172-31-37-15.us-east-2.compute.internal:\n      zookeeper_id: 3\n    ip-172-31-34-231.us-east-2.compute.internal:\n      zookeeper_id: 1\n\n\n      # zookeeper:\n      #   properties:\n      #     initlimit: 6\n      #     synclimit: 3\n      # zookeeper_server:\n      #   properties:\n      #     num.io: 15\n      # zookeeper_registry:\n      #   properties:\n      #     key: val\n      # zookeeper_center:\n      #   properties:\n      #     key: val<\/pre>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u7ed3\u6784\u6765\u89e3\u7ec4 json \u6570\u636e<\/p>\n<pre>type service struct {\n    name string `json:\"name\"`\n    hosts []host `json:\"hosts\"`\n    configs  map[string]interface{} `json:\"configs\"` \n\n}\n\ntype host struct {\n    hostname         string                 `json:\"host_name\"`\n    customhostconfig map[string]interface{} `json:\"config\"`\n}<\/pre>\n<pre>{{ range $s := $.services }}\n{{ $s.name }}:\n  {{ range $k,$v := $s.vars }}{{ $k }}: \"{{ $v }}\"\n  {{ end }}\n  hosts:\n    {{ range $h := $s.hosts }}\n    {{ $h.hostname }}: {{ range $k,$v := $h.customhostconfig }}\n       {{ $k }}: \"{{ $v }}\"\n      {{ end }}\n{{ end }}{{ end }}<\/pre>\n<p>\u6211\u6b63\u5728\u4f7f\u7528\u4e0a\u9762\u7684\u6a21\u677f\u4ee3\u7801\uff0c\u5b83\u53ef\u4ee5\u5f88\u597d\u5730\u521b\u5efa\u4ee5\u4e0b\u4ee3\u7801<\/p>\n<pre>zookeeper:\n  configs:\n\n    zookeeper_user: custom-user\n    zookeeper_group: custom-group\n\n\n  hosts:\n    ip-172-31-34-246.us-east-2.compute.internal:\n      zookeeper_id: 2\n    ip-172-31-37-15.us-east-2.compute.internal:\n      zookeeper_id: 3\n    ip-172-31-34-231.us-east-2.compute.internal:\n      zookeeper_id: 1<\/pre>\n<p>\u6211\u9700\u8981\u5e2e\u52a9\uff0c\u6839\u636e\u4f7f\u7528 <code>configs map[string]interface{} <\/code>json:&#8221;configs&#8221; \u4f5c\u4e3a\u952e\u503c json \u7684\u4e00\u90e8\u5206\u4f20\u9012\u7684\u7528\u6237\u8f93\u5165\uff0c\u4ee5\u52a8\u6001\u65b9\u5f0f\u5c06\u4ee5\u4e0b\u914d\u7f6e\u90e8\u5206\u6dfb\u52a0\u5230\u6700\u7ec8 yml \u6587\u4ef6\u4e2d`\u3002\u5177\u6709\u6b63\u786e\u7684\u7f29\u8fdb\u7ea7\u522b\u3002<\/p>\n<pre>zookeeper_copy_files:\n       - source_path: \/path\/to\/file.txt\n         destination_path: \/tmp\/file.txt\n\n    zookeeper_Some_otherfile:\n       - source_path: \/path\/to\/file.txt\n         destination_path: \/tmp\/file.txt\n\n      # zookeeper:\n      #   properties:\n      #     initLimit: 6\n      #     syncLimit: 3\n      # zookeeper_server:\n      #   properties:\n      #     num.io: 15\n      # zookeeper_registry:\n      #   properties:\n      #     key: val\n      # Zookeeper_center:\n      #   properties:\n      #     key: val<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4e0d\u8981\u4f7f\u7528 go \u6a21\u677f\u7f16\u5199 yaml\uff0c\u53ea\u9700\u4f7f\u7528 yaml \u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n\n    \"gopkg.in\/yaml.v3\"\n)\n\ntype service struct {\n    name    string                 `json:\"name\"`\n    hosts   []host                 `json:\"hosts\"`\n    configs map[string]interface{} `json:\"configs\"`\n}\n\ntype host struct {\n    hostname         string                 `json:\"host_name\"`\n    customhostconfig map[string]interface{} `json:\"config\"`\n}\n\nvar services []service\n\n\/\/ create some sample data similar to what you show\nfunc init() {\n    var zk service\n    zk.name = \"zookeeper\"\n    zk.configs = map[string]interface{}{\n        \"zookeeper_user\":  \"custom-user\",\n        \"zookeeper_group\": \"custom-group\",\n    }\n    zk.hosts = []host{\n        {\"ip-172-31-34-246.us-east-2.compute.internal\",\n            map[string]interface{}{\n                \"zookeeper_id\": 2,\n            }},\n        {\"ip-172-31-37-15.us-east-2.compute.internal\",\n            map[string]interface{}{\n                \"zookeeper_id\": 3,\n            }},\n    }\n    services = []service{zk}\n}\n\nfunc main() {\n    out := make(map[string]interface{})\n    for _, service := range services {\n        var hosts = make(map[string]interface{})\n        for _, host := range service.hosts {\n            hosts[host.hostname] = host.customhostconfig\n        }\n        out[service.name] = map[string]interface{}{\n            \"hosts\":   hosts,\n            \"configs\": service.configs,\n        }\n    }\n    bytes, err := yaml.marshal(out)\n    if err != nil {\n        panic(err)\n    }\n    fmt.println(string(bytes))\n}\n<\/pre>\n<p>\u8f93\u51fa\uff08\uff09\uff1a<\/p>\n<pre>zookeeper:\n    configs:\n        zookeeper_group: custom-group\n        zookeeper_user: custom-user\n    hosts:\n        ip-172-31-34-246.us-east-2.compute.internal:\n            zookeeper_id: 2\n        ip-172-31-37-15.us-east-2.compute.internal:\n            zookeeper_id: 3\n<\/pre>\n<p>\u8fd9\u4f1a\u7acb\u5373\u89e3\u51b3\u6df1\u5ea6\u5d4c\u5957\u6570\u636e\u7684\u4efb\u4f55\u95ee\u9898\uff0c\u56e0\u4e3a yaml \u53ea\u662f\u6309\u539f\u6837\u8f6c\u50a8\u7ed3\u6784\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300agolang\u6a21\u677fjson\u8f6cyml\u683c\u5f0f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207099","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207099"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207099\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}