{"id":207307,"date":"2025-07-08T11:20:13","date_gmt":"2025-07-08T03:20:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207307\/"},"modified":"2025-07-08T11:20:13","modified_gmt":"2025-07-08T03:20:13","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%a8%a1%e6%9d%bf%e4%b9%8b%e9%97%b4%e4%bc%a0%e9%80%92%e5%a4%9a%e4%b8%aa%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207307\/","title":{"rendered":"\u5982\u4f55\u5728\u6a21\u677f\u4e4b\u95f4\u4f20\u9012\u591a\u4e2a\u503c\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\u5728\u6a21\u677f\u4e4b\u95f4\u4f20\u9012\u591a\u4e2a\u503c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 16:54:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728\u6a21\u677f\u4e4b\u95f4\u4f20\u9012\u591a\u4e2a\u503c\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7684 <code>city<\/code> \u7ed3\u6784\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>type city struct {\n    id      int\n    name    string\n    regions []region\n}<\/pre>\n<p><code>region<\/code> \u7ed3\u6784\u662f\uff1a<\/p>\n<pre>type region struct {\n    id               int\n    name             string\n    shops            []destination\n    masters          []master\n    educationcenters []destination\n}<\/pre>\n<p>\u4e3b\u8981\u662f\u6211\u5c1d\u8bd5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>tpl.executetemplate(reswriter,\"cities.gohtml\",citywithsomedata)<\/pre>\n<p>\u662f\u5426\u53ef\u4ee5\u5728\u6a21\u677f\u5185\u6267\u884c\u7c7b\u4f3c\u7684\u64cd\u4f5c\uff1f <\/p>\n<pre>{{range .}}\n        {{$city:=.Name}}\n            {{range .Regions}}\n                      {{$region:=.Name}}\n                      {{template \"data\" .Shops $city $region}}\n            {{end}}\n{{end}}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f15\u7528\u7684\u6587\u6863\uff0c<code>{{template}}<\/code>\u64cd\u4f5c\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>{{template \"name\"}}\n    the template with the specified name is executed with nil data.\n\n{{template \"name\" pipeline}}\n    the template with the specified name is executed with dot set\n    to the value of the pipeline.<\/pre>\n<p>\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5c06\u4e00\u4e2a\u53ef\u9009\u6570\u636e\u4f20\u9012\u7ed9\u6a21\u677f\u6267\u884c\uff0c\u800c\u4e0d\u662f\u66f4\u591a\u3002\u5982\u679c\u8981\u4f20\u9012\u591a\u4e2a\u503c\uff0c\u5219\u5fc5\u987b\u5c06\u5b83\u4eec\u5305\u88c5\u6210\u4f20\u9012\u7684\u67d0\u4e2a\u503c\u3002\u8be6\u60c5\u89c1<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5e94\u8be5\u5c06\u8fd9\u4e9b\u6570\u636e\u5305\u88c5\u5230\u4e00\u4e2a\u7ed3\u6784\u4f53\u6216\u4e00\u4e2a\u6620\u5c04\u4e2d\u3002\u4f46\u6211\u4eec\u4e0d\u80fd\u5728\u6a21\u677f\u4e2d\u7f16\u5199go\u4ee3\u7801\u3002\u6211\u4eec\u53ef\u4ee5\u505a\u7684\u662f\u6ce8\u518c\u4e00\u4e2a\u51fd\u6570\u6765\u4f20\u9012\u8fd9\u4e9b\u6570\u636e\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u201c\u6253\u5305\u201d\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u4f20\u9012\u7ed9 <code>{{template}}<\/code> \u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u5305\u88c5\u5668\uff0c\u5b83\u53ea\u662f\u5c06\u5b83\u4eec\u6253\u5305\u5230\u5730\u56fe\u4e2d\uff1a<\/p>\n<pre>func wrap(shops []destination, cityname, regionname string) map[string]interface{} {\n    return map[string]interface{}{\n        \"shops\":      shops,\n        \"cityname\":   cityname,\n        \"regionname\": regionname,\n    }\n}<\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528 \u65b9\u6cd5\u6ce8\u518c\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u4e14\u4e0d\u8981\u5fd8\u8bb0\u5728\u89e3\u6790\u6a21\u677f\u6587\u672c\u4e4b\u524d\u5fc5\u987b\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4fee\u6539\u540e\u7684\u6a21\u677f\uff0c\u5b83\u8c03\u7528\u6b64 <code>wrap()<\/code> \u51fd\u6570\u6765\u751f\u6210\u5355\u4e2a\u503c\uff1a<\/p>\n<pre>const src = `\n{{define \"data\"}}\n    city: {{.cityname}}, region: {{.regionname}}, shops: {{.shops}}\n{{end}}\n{{- range . -}}\n        {{$city:=.name}}\n        {{- range .regions -}}\n              {{$region:=.name}}\n              {{- template \"data\" (wrap .shops $city $region) -}}\n        {{end}}\n{{- end}}`<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u8fd9\u4e9b\u64cd\u4f5c\uff1a<\/p>\n<pre>t := template.must(template.new(\"cities.gohtml\").funcs(template.funcmap{\n    \"wrap\": wrap,\n}).parse(src))\ncitywithsomedata := []city{\n    {\n        name: \"citya\",\n        regions: []region{\n            {name: \"ca-ra\", shops: []destination{{\"ca-ra-sa\"}, {\"ca-ra-sb\"}}},\n            {name: \"ca-rb\", shops: []destination{{\"ca-rb-sa\"}, {\"ca-rb-sb\"}}},\n        },\n    },\n    {\n        name: \"cityb\",\n        regions: []region{\n            {name: \"cb-ra\", shops: []destination{{\"cb-ra-sa\"}, {\"cb-ra-sb\"}}},\n            {name: \"cb-rb\", shops: []destination{{\"cb-rb-sa\"}, {\"cb-rb-sb\"}}},\n        },\n    },\n}\nif err := t.executetemplate(os.stdout, \"cities.gohtml\", citywithsomedata); err != nil {\n    panic(err)\n}<\/pre>\n<p>\u8f93\u51fa\uff08\u5728 \u4e0a\u5c1d\u8bd5\uff09\uff1a<\/p>\n<pre>city: citya, region: ca-ra, shops: [{ca-ra-sa} {ca-ra-sb}]\n\ncity: citya, region: ca-rb, shops: [{ca-rb-sa} {ca-rb-sb}]\n\ncity: cityb, region: cb-ra, shops: [{cb-ra-sa} {cb-ra-sb}]\n\ncity: cityb, region: cb-rb, shops: [{cb-rb-sa} {cb-rb-sb}]<\/pre>\n<p>\u6211\u731c <code>citywithsomedata<\/code> \u662f\u4e00\u4e2a\u5207\u7247\uff0c\u5982\u679c\u662f\u8fd9\u6837\uff0c\u8bf7\u5c1d\u8bd5\u8fd9\u6837\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre>type viewmodel struct {\n    list   []city\n}<\/pre>\n<p>\u7136\u540e\uff0c\u5728\u60a8\u7684\u6a21\u677f\u4e2d\uff1a<\/p>\n<pre>{{range .List}}\n        {{$city:=.Name}}\n            {{range .Regions}}\n                      {{$region:=.Name}}\n                      {{template \"data\" .Shops $city $region}}\n            {{end}}\n{{end}}<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5728\u6a21\u677f\u4e4b\u95f4\u4f20\u9012\u591a\u4e2a\u503c\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\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-207307","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207307","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=207307"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207307\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}