{"id":42901,"date":"2024-12-01T08:15:13","date_gmt":"2024-12-01T00:15:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42901\/"},"modified":"2024-12-01T08:15:13","modified_gmt":"2024-12-01T00:15:13","slug":"%e5%b0%86%e6%95%b0%e7%bb%84%e7%9a%84%e7%b4%a2%e5%bc%95%e4%bc%a0%e9%80%92%e7%bb%99%e6%a8%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42901\/","title":{"rendered":"\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f"},"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>\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f<\/span><\/p>\n<h1>\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 11:57: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><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f\uff1f \u6211\u77e5\u9053\u6211\u53ef\u4ee5\u8fd9\u6837\u505a\u6765\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>{{ with index . 0 }}<\/pre>\n<p>\u4f46\u6211\u9700\u8981\u505a\u8fd9\u6837\u7684\u4e8b\u60c5\uff1a<\/p>\n<pre>{{ template \"mytemp\" index . 0 }}<\/pre>\n<p>\u8fd9\u4f3c\u4e4e\u4e0d\u8d77\u4f5c\u7528\u3002\u6211\u4e5f\u5c1d\u8bd5\u8fc7\uff0c\u4f46\u6ca1\u6709\u6210\u529f\uff1a<\/p>\n<pre>{{ with index . 0 }}\n  {{ template \"mytemp\" . }}\n{{ end }}<\/pre>\n<p>\u6211\u4f3c\u4e4e\u4e0d\u77e5\u9053\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u9700\u8981 <code>index<\/code> \u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5728  \u4e2d\u9605\u8bfb\u66f4\u591a\u76f8\u5173\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6709\u6548\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"log\"\n    \"os\"\n    \"text\/template\"\n)\n\ntype inventory struct {\n    material []string\n    count    uint\n}\n\nfunc main() {\n    sweaters := inventory{[]string{\"wool\"}, 17}\n    tmpl, err := template.new(\"test\").parse(\"{{.count}} items are made of {{index .material 0}}\")\n    if err != nil {\n        log.fatal(err)\n    }\n    err = tmpl.execute(os.stdout, sweaters)\n    if err != nil {\n        log.fatal(err)\n    }\n\n}\n<\/pre>\n<\/p>\n<p>\u8fd9\u662f\u53e6\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"os\"\n    \"text\/template\"\n)\n\n\nfunc main() {\n    data:=map[string]interface{}{ \"item\": []interface{}{\"str1\",\"str2\"}}\n    tmpl, _ := template.New(\"test\").Parse(`Some text\n{{define \"mytp\"}}\n{{.}}\n{{end}}\n\n{{template \"mytp\" index .item 0}}`)\n\n    tmpl.Execute(os.Stdout, data)\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f\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\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f \u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f \u6765\u6e90\uff1astackoverflow 2024-04-27 11:57:36 0\u6d4f\u89c8 \u6536\u85cf \u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60Golang\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u95ee\u9898\u5185\u5bb9 \u5982\u4f55\u5c06\u6570\u7ec4\u7684\u7d22\u5f15\u4f20\u9012\u7ed9\u6a21\u677f\uff1f \u6211\u77e5\u9053\u6211\u53ef\u4ee5\u8fd9\u6837\u505a\u6765\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a {{ with index . 0 }} \u4f46\u6211\u9700\u8981\u505a\u8fd9\u6837\u7684\u4e8b\u60c5\uff1a {{ template &#8220;mytemp&#8221; index . 0 }} \u8fd9\u4f3c\u4e4e\u4e0d\u8d77\u4f5c\u7528\u3002\u6211\u4e5f\u5c1d\u8bd5\u8fc7\uff0c\u4f46\u6ca1\u6709\u6210\u529f\uff1a {{ with index . 0 }} {{ template &#8220;mytemp&#8221; . }} {{ end }} \u6211\u4f3c\u4e4e\u4e0d\u77e5\u9053\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002 \u89e3\u51b3\u65b9\u6848 \u60a8\u9700\u8981 index \u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5728 \u4e2d\u9605\u8bfb\u66f4\u591a\u76f8\u5173\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6709\u6548\u793a\u4f8b\uff1a package main import ( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42901","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42901"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42901\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}