{"id":206880,"date":"2025-06-12T08:59:37","date_gmt":"2025-06-12T00:59:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206880\/"},"modified":"2025-06-12T08:59:37","modified_gmt":"2025-06-12T00:59:37","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%8c%85%e5%90%ab%e5%86%92%e5%8f%b7%e5%88%86%e5%89%b2%e9%94%ae%e7%9a%84%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%9b%ae%e5%bd%95%e6%a0%91%e7%bb%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206880\/","title":{"rendered":"\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f"},"content":{"rendered":"<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/1732674815674684ffb283d.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u7684\u7b97\u6cd5<\/strong>,<br \/>\n<strong>\u95ee\u9898\uff1a<\/strong>,\u5982\u4f55\u5c06\u4e00\u7ec4\u5305\u542b\u5192\u53f7\u5206\u9694\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684\u76ee\u5f55\u6811\uff1a,<br \/>\n<strong>\u7b97\u6cd5\uff1a<\/strong>,\u8fd9\u662f\u4e00\u4e2a\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u7684\u9012\u5f52\u7b97\u6cd5\uff1a,<br \/>\n<strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong>,\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01,\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u7684\u7b97\u6cd5,\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f<\/h1>\n<p><span>2024-11-27 10:33:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/1732674815674684ffb283d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u7684\u7b97\u6cd5<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5c06\u4e00\u7ec4\u5305\u542b\u5192\u53f7\u5206\u9694\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684\u76ee\u5f55\u6811\uff1a<\/p>\n<pre>[\n  {\n    \"name\": \"answer\",\n    \"value\": \"\",\n    \"child\": [\n      {\n        \"name\": \"aa\",\n        \"value\": \"\",\n        \"child\": [\n          {\n            \"name\": \"bb\",\n            \"value\": \"answer:aa:bb\",\n            \"child\": []\n          },\n          {\n            \"name\": \"cc\",\n            \"value\": \"answer:aa:cc\",\n            \"child\": []\n          }\n        ]\n      }\n    ]\n  },\n  {\n    \"name\": \"question\",\n    \"child\": [\n      {\n        \"name\": \"book1\",\n        \"value\": \"question:book1\",\n        \"child\": [\n          {\n            \"name\": \"answer\",\n            \"value\": \"question:book1:answer\",\n            \"child\": [\n              {\n                \"name\": \"name1\",\n                \"value\": \"question:book1:answer:name1\",\n                \"child\": []\n              },\n              {\n                \"name\": \"name2\",\n                \"value\": \"question:book1:answer:name2\",\n              \"child\": []\n              }\n            ]\n          }\n        ]\n      }\n    ]\n  },\n  {\n    \"name\": \"timeout_1\",\n    \"value\": \"timeout_1\",\n    \"child\": []\n  },\n  {\n    \"name\": \"timeout_2\",\n    \"value\": \"timeout_2\",\n    \"child\": []\n  }\n]<\/pre>\n<p><strong>\u7b97\u6cd5\uff1a<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u7684\u9012\u5f52\u7b97\u6cd5\uff1a<\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u6839\u8282\u70b9\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5b57\u5178\u6765\u8868\u793a\u6839\u8282\u70b9\u3002<\/li>\n<li>\n<p><strong>\u904d\u5386\u6bcf\u4e2a\u952e\uff1a<\/strong>\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u952e\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>a. <strong>\u5206\u79bb\u952e\uff1a<\/strong>\u5c06\u952e\u6309\u5192\u53f7\u5206\u9694\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002<\/p>\n<p>b. <strong>\u4e34\u65f6\u8282\u70b9\u53d8\u91cf\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf t \u6765\u5f15\u7528\u5f53\u524d\u8282\u70b9\u3002\u521d\u59cb\u65f6\u5c06\u5176\u8bbe\u7f6e\u4e3a\u6839\u8282\u70b9\u3002<\/p>\n<p>c. <strong>\u904d\u5386\u5b57\u7b26\u4e32\u6570\u7ec4\uff1a<\/strong>\u901a\u8fc7\u5b57\u7b26\u4e32\u6570\u7ec4\u8fdb\u884c\u8fed\u4ee3\uff1a<\/p>\n<p>i. <strong>\u67e5\u627e\u73b0\u6709\u8282\u70b9\uff1a<\/strong>\u68c0\u67e5 t \u4e2d\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u540d\u4e3a k\uff08\u6570\u7ec4\u4e2d\u7684\u5f53\u524d\u5b57\u7b26\u4e32\uff09\u7684\u5b50\u8282\u70b9\u3002<\/p>\n<p>ii. <strong>\u5982\u679c\u6ca1\u6709\u73b0\u6709\u7684\u8282\u70b9\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8282\u70b9\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 t \u4e2d\u3002<\/p>\n<p>iii. <strong>\u5982\u679c\u6709\u73b0\u6709\u7684\u8282\u70b9\uff1a<\/strong>\u5c06 t \u79fb\u81f3\u8be5\u5b50\u8282\u70b9\u3002<\/p>\n<p>d. <strong>\u5904\u7406\u53f6\u5b50\u8282\u70b9\uff1a<\/strong>\u5982\u679c\u8fd9\u662f\u6700\u540e\u4e00\u4e2a\u952e\uff08\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff1a<\/p>\n<p>i. <strong>\u521b\u5efa\u53f6\u5b50\u8282\u70b9\uff1a<\/strong>\u5728 t \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8282\u70b9\uff0c\u5c06\u540d\u79f0\u8bbe\u7f6e\u4e3a\u6700\u540e\u4e00\u4e2a\u952e\uff0c\u503c\u4e3a\u539f\u59cb\u952e\u3002<\/p>\n<p>ii. <strong>\u5c06\u53f6\u5b50\u8282\u70b9\u8bbe\u7f6e\u4e3a nil\uff1a<\/strong>\u5c06\u65b0\u521b\u5efa\u7684\u53f6\u5b50\u8282\u70b9\u7684 child \u8bbe\u7f6e\u4e3a nil \u4ee5\u6307\u793a\u8fd9\u662f\u4e00\u4e2a\u53f6\u5b50\u8282\u70b9\u3002<\/p>\n<\/li>\n<li><strong>\u9012\u5f52\u8f6c\u6362\uff1a<\/strong>\u7b97\u6cd5\u4ee5\u9012\u5f52\u65b9\u5f0f\u5c06\u6bcf\u4e2a\u5b50\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u7ed3\u6784\u3002<\/li>\n<li><strong>\u8fd4\u56de\u7ed3\u679c\uff1a<\/strong>\u8be5\u7b97\u6cd5\u8fd4\u56de\u6839\u8282\u70b9\uff0c\u5b83\u662f\u4e00\u4e2a\u8868\u793a\u76ee\u5f55\u7ed3\u6784\u7684\u5b57\u5178\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"strings\"\n)\n\nvar values = []string{\n    \"answer:aa:bb\",\n    \"answer:aa:cc\",\n    \"question:book1\",\n    \"question:book1:answer\",\n    \"question:book1:answer:name1\",\n    \"question:book1:answer:name2\",\n    \"timeout_1\",\n    \"timeout_2\",\n}\n\ntype Node struct {\n    Name  string\n    Value string\n    Child map[string]Node\n}\n\nfunc main() {\n    root := make(map[string]Node)\n\n    for _, value := range values {\n        t := root\n        keys := strings.Split(value, \":\")\n        l := len(keys)\n        for i := 0; i &lt; l-1; i++ {\n            k := keys[i]\n            if tm, has := t[k]; has {\n                t = tm.Child\n            } else {\n                t[k] = Node{\n                    Name:  k,\n                    Child: map[string]Node{},\n                }\n                t = t[k].Child\n            }\n        }\n        t[value] = Node{\n            Name:  keys[l-1],\n            Value: value,\n            Child: nil,\n        }\n    }\n    r, _ := json.Marshal(root)\n    println(string(r))\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u5305\u542b\u5192\u53f7\u5206\u5272\u952e\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u76ee\u5f55\u6811\u7ed3\u6784\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Win10\u600e\u4e48\u4e00\u952e\u91cd\u88c5\u7cfb\u7edf \u7cfb\u7edf\u4e4b\u5bb6\u88c5\u673a\u5927\u5e08\u4e00\u952e\u91cd\u88c5Win10\u65b9\u6cd5\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u89e3\u51b3\u8f6e\u64ad\u56fe\u6700\u540e\u4e00\u9875\u5207\u6362\u5230\u7b2c\u4e00\u9875\u65f6\u7684\u95ea\u52a8\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca&#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-206880","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206880","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=206880"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206880\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}