{"id":42468,"date":"2024-12-01T09:47:37","date_gmt":"2024-12-01T01:47:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42468\/"},"modified":"2024-12-01T09:47:37","modified_gmt":"2024-12-01T01:47:37","slug":"go-web%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%8f%a5%e6%9f%84%e5%87%bd%e6%95%b0%e7%9a%84%e4%bb%a3%e7%a0%81%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42468\/","title":{"rendered":"Go Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1"},"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>Go Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1<\/span><\/p>\n<h1>Go Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 23:27:26<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5b66\u4e60 go\uff0c\u5e76\u5728\u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u4e00\u4e9b\u8bbe\u8ba1\u95ee\u9898\u3002\u8be5\u5e94\u7528\u7a0b\u5e8f\u6709\u4e3b\u8def\u5f84\u201c\/\u201d\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5176\u4e2d\u63d0\u4ea4\u7b80\u5355\u7684\u8868\u5355\u3002\u901a\u8fc7\u8fd9\u4e9b\u8868\u5355\u503c\uff0c\u6211\u8c03\u7528\u5916\u90e8 api \u5e76\u5c06\u54cd\u5e94\u89e3\u7ec4\u5230\u67d0\u4e2a\u7ed3\u6784\u4e2d\u3002\u73b0\u5728\uff0c\u6211\u60f3\u6839\u636e\u68c0\u7d22\u5230\u7684\u503c\u5bf9\u53e6\u4e00\u4e2a\u5916\u90e8 api \u8fdb\u884c\u53e6\u4e00\u4e2a\u8c03\u7528\uff0c\u4f46\u6211\u4e0d\u786e\u5b9a\u6267\u884c\u6b64\u64cd\u4f5c\u7684\u6b63\u786e\u65b9\u6cd5\u662f\u4ec0\u4e48\u3002\u4ee5\u4e0b\u662f\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u7684\u7247\u6bb5\uff1a<\/p>\n<pre>func main() {\n    http.HandleFunc(\"\/\", mainHandler)\n    log.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n\nfunc mainHandler(w http.ResponseWriter, r *http.Request) {\n    \/\/renders form template\n    \/\/makes post and retrieves data from api\n    \/\/here with retrieved data I want to make another call to different API,\n    \/\/ but mainHandler would get too big and complex. I'm not sure how should I pass this data to\n    \/\/ another handler or redirect to another handler with this data.\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5904\u7406\u7a0b\u5e8f\u7684\u8bed\u4e49\u5e94\u8bbe\u8ba1\u4e3a\u5339\u914d\u6240\u9700\u7684 http \u884c\u4e3a\uff0c\u65e0\u8bba\u4ee3\u7801\u590d\u6742\u7a0b\u5ea6\u5982\u4f55\u3002\u5982\u679c\u60a8\u60f3\u901a\u8fc7\u505a\u4e00\u5806\u4e8b\u60c5\u6765\u5904\u7406\u5355\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u90a3\u4e48\u5b83\u5e94\u8be5\u662f\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002\u5982\u679c\u5904\u7406\u7a0b\u5e8f\u53d8\u5f97\u592a\u590d\u6742\uff0c\u8bf7\u5c06\u5176\u5206\u89e3\u3002\u5904\u7406\u7a0b\u5e8f\u53ea\u662f\u51fd\u6570\uff0c\u53ef\u4ee5\u50cf\u4efb\u4f55\u5176\u4ed6\u51fd\u6570\u4e00\u6837\u8fdb\u884c\u5206\u89e3 &#8211; \u901a\u8fc7\u5c06\u5176\u67d0\u4e9b\u90e8\u5206\u63d0\u53d6\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\u5e76\u8c03\u7528\u8be5\u65b0\u51fd\u6570\u3002\u7ed9\u4f60\u4f2a\u4ee3\u7801\uff1a<\/p>\n<pre>func mainHandler(w http.ResponseWriter, r *http.Request) {\n    err := renderTemplate(w)\n    if err != nil { ... }\n    err, data := postToApi()\n    if err != nil { ... }\n    err, data2 := postToApi2(data)\n    if err != nil { ... }\n}<\/pre>\n<p>\u6ca1\u6709\u7406\u7531\u8ba9\u8fd9\u4e9b\u51fd\u6570\u672c\u8eab\u6210\u4e3a\u5904\u7406\u7a0b\u5e8f\u6216\u8ba9\u5ba2\u6237\u7aef\u53c2\u4e0e\u91cd\u5b9a\u5411\u3002\u53ea\u9700\u6309\u7167\u901a\u5e38\u5206\u89e3\u903b\u8f91\u7684\u65b9\u5f0f\u5206\u89e3\u903b\u8f91\u5373\u53ef &#8211; \u5b83\u662f http \u5904\u7406\u7a0b\u5e8f\u5e76\u4e0d\u91cd\u8981\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aGo Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Go Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1 Go Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1 \u6765\u6e90\uff1astackoverflow 2024-04-23 23:27:26 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aGo Web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53e5\u67c4\u51fd\u6570\u7684\u4ee3\u7801\u8bbe\u8ba1\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u5b66\u4e60 go\uff0c\u5e76\u5728\u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\u65f6\u9047\u5230\u4e86\u4e00\u4e9b\u8bbe\u8ba1\u95ee\u9898\u3002\u8be5\u5e94\u7528\u7a0b\u5e8f\u6709\u4e3b\u8def\u5f84\u201c\/\u201d\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5176\u4e2d\u63d0\u4ea4\u7b80\u5355\u7684\u8868\u5355\u3002\u901a\u8fc7\u8fd9\u4e9b\u8868\u5355\u503c\uff0c\u6211\u8c03\u7528\u5916\u90e8 api \u5e76\u5c06\u54cd\u5e94\u89e3\u7ec4\u5230\u67d0\u4e2a\u7ed3\u6784\u4e2d\u3002\u73b0\u5728\uff0c\u6211\u60f3\u6839\u636e\u68c0\u7d22\u5230\u7684\u503c\u5bf9\u53e6\u4e00\u4e2a\u5916\u90e8 api \u8fdb\u884c\u53e6\u4e00\u4e2a\u8c03\u7528\uff0c\u4f46\u6211\u4e0d\u786e\u5b9a\u6267\u884c\u6b64\u64cd\u4f5c\u7684\u6b63\u786e\u65b9\u6cd5\u662f\u4ec0\u4e48\u3002\u4ee5\u4e0b\u662f\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u7684\u7247\u6bb5\uff1a func main() { http.HandleFunc(&#8220;\/&#8221;, mainHandler) log.Fatal(http.ListenAndServe(&#8220;:8080&#8221;, nil)) } func mainHandler(w http.ResponseWriter, r *http.Request) { \/\/renders form template \/\/makes post and retrieves data from api \/\/here with retrieved data I want [&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-42468","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42468","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=42468"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42468\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}