{"id":207029,"date":"2025-07-08T08:58:13","date_gmt":"2025-07-08T00:58:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207029\/"},"modified":"2025-07-08T08:58:13","modified_gmt":"2025-07-08T00:58:13","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e4%bb%bb%e4%bd%95%e7%bb%93%e6%9e%84%e4%bd%9c%e4%b8%ba%e5%8f%82%e6%95%b0%e5%8f%91%e9%80%81%e7%bb%99%e6%96%b9%e6%b3%95%e5%b9%b6%e8%bf%94%e5%9b%9e%e8%af%a5%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207029\/","title":{"rendered":"\u5982\u4f55\u5c06\u4efb\u4f55\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u53d1\u9001\u7ed9\u65b9\u6cd5\u5e76\u8fd4\u56de\u8be5\u7ed3\u6784\uff1f"},"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>\u5982\u4f55\u5c06\u4efb\u4f55\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u53d1\u9001\u7ed9\u65b9\u6cd5\u5e76\u8fd4\u56de\u8be5\u7ed3\u6784\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 16:12:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8Golang\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u5c06\u4efb\u4f55\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u53d1\u9001\u7ed9\u65b9\u6cd5\u5e76\u8fd4\u56de\u8be5\u7ed3\u6784\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u662f go \u65b0\u624b\uff0c\u6211\u60f3\u770b\u770b\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63a5\u6536\u4efb\u4f55\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<p>\u6211\u7684\u4ee3\u7801\u4e2d\u6709\u7c7b\u4f3c\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5b83\u5bf9 5 \u4e2a\u7ed3\u6784\u6267\u884c\u5b8c\u5168\u76f8\u540c\u7684\u64cd\u4f5c\u5e76\u8fd4\u56de\u76f8\u540c\u7684\u7ed3\u6784\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u662f\u5426\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u6211\u60f3\u77e5\u9053\u6211\u662f\u5426\u53ef\u4ee5\u505a\u8fd9\u6837\u7684\u4e8b\u60c5\uff1a<\/p>\n<pre>type car struct {\n     model   string `yaml:\"model\"`\n     color   string `yaml:\"color\"`\n     wheels  int    `yaml:wheels\"`\n     windows int    `yaml:\"windows\"` \n}\n\ntype motorcycle struct {\n     model    string `yaml:\"model\"`\n     color    string `yaml:\"color\"`\n     wheels   int      `yaml:wheels\"`\n}\n\ntype bus struct {\n     model      string `yaml:\"model\"`\n     color      string `yaml:\"color\"`\n     wheels     int    `yaml:wheels\"`\n     passengers int    `yaml:\"passengers\"`\n}\n\nfunc main () {\n\n    car := getyamldata(car)\n    motorcycle := getyamldata(motorcycle)\n    bus := getyamldata(bus)\n}\n\nfunc getyamldata(struct anystructure) (returnanystruct struct){\n\n       yaml.unmarshal(yamlfile, &amp;anystructure)\n\n       return anystructure\n}<\/pre>\n<p>\u662f\u5426\u53ef\u4ee5\u6267\u884c\u7c7b\u4f3c\u4e0a\u9762\u4ee3\u7801\u7684\u64cd\u4f5c\uff1f\u5176\u5b9e\u6211\u6709\u7684\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>func main(){\n   car, _, _ := GetYamlData(\"car\")\n    _,motorcycle,_ := GetYamlData(\"motorcycle\")\n    _,_,bus := GetYamlData(\"bus\")\n }\n\nfunc GetYamlData(structureType string) (car *Car, motorcycle *Motorcycle, bus *Bus){\n\n switch structureType{\n\n        case \"car\":\n                yaml.Unmarshal(Filepath, car)\n        case \"motorcycle\":\n                yaml.Unmarshal(Filepath, motorcycle)\n        case \"bus\":\n                yaml.Unmarshal(Filepath, bus)\n }\n\n return car, motorcycle, bus\n\n}<\/pre>\n<p>\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u8fd9\u4f1a\u589e\u52a0\uff0c\u5b83\u5c06\u8fd4\u56de\u5f88\u591a\u503c\uff0c\u800c\u6211\u4e0d\u60f3\u8981\u5f88\u591a\u8fd4\u56de\u503c\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u7528\u6211\u53d1\u5e03\u7684\u7b2c\u4e00\u4e2a\u4ee3\u7801\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u91c7\u7528\u4e0e <code>yaml.unmarshal<\/code> \u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u5373\u91c7\u7528\u8981\u89e3\u7ec4\u7684\u503c\uff1a<\/p>\n<pre>func getyamldata(i interface{}) {\n    yaml.unmarshal(filepath, i)    \n}<\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre>func main () {\n    var car Car\n    var motorcycle Motorcycle\n    var bus Bus\n    GetYamlData(&amp;car)\n    GetYamlData(&amp;motorcycle)\n    GetYamlData(&amp;bus)\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207029","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207029","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=207029"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207029\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}