{"id":205854,"date":"2025-06-12T12:13:27","date_gmt":"2025-06-12T04:13:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205854\/"},"modified":"2025-06-12T12:13:27","modified_gmt":"2025-06-12T04:13:27","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e7%9a%84-http-%e5%a4%84%e7%90%86%e7%a8%8b%e5%ba%8f%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205854\/","title":{"rendered":"\u5982\u4f55\u5728 Golang \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f"},"content":{"rendered":"<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGOLANG\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01,\u5728 GOLANG HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff0c\u53ea\u9700\u5728 http.HandleFunc() \u4e2d\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u4ee3\u7801\u7f16\u5199\uff0c\u6bd4\u5982\u5904\u7406 HTTP \u8bf7\u6c42\u3002\u5728\u5b9e\u6218\u4e2d\uff0c\u533f\u540d\u51fd\u6570\u53ef\u7528\u4e8e\u54cd\u5e94 JSON \u8bf7\u6c42\uff0c\u901a\u8fc7\u7f16\u7801 JSON \u6570\u636e\u5e76\u5c06\u5176\u5199\u5165 HTTP \u54cd\u5e94\u6b63\u6587\u5373\u53ef\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730019694671e016e66247.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f<\/strong>,<br \/>\n<strong>\u80cc\u666f<\/strong>,\u533f\u540d\u51fd\u6570\uff0c\u4e5f\u79f0\u4e3a lambda \u8868\u8fbe\u5f0f\uff0c\u662f\u4e00\u79cd\u65e0\u9700\u547d\u540d\u5373\u53ef\u5b9a\u4e49\u5e76\u4f7f\u7528\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u65e8\u5728\u7b80\u5316\u4ee3\u7801\u7f16\u5199\u3002\u5728 GOLANG \u4e2d\uff0c\u533f\u540d\u51fd\u6570\u53ef\u7528\u4e8e\u5904\u7406 HTTP \u8bf7\u6c42\u3002,<br \/>\n<strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>,\u8981\u5728 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff0c\u8bf7\u5728<br \/>\n<code>http.HandleFunc()<\/code> \u4e2d\u76f4\u63a5\u4f20\u9012\u5b83\uff1a,\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u6765\u5904\u7406 &#8220;\/hello&#8221; \u8def\u7531\u3002\u5f53\u5ba2\u6237\u7aef\u901a\u8fc7 HTTP \u8bf7\u6c42\u8bbf\u95ee\u8be5\u8def\u7531\u65f6\uff0c\u533f\u540d\u51fd\u6570\u5c06\u88ab\u6267\u884c\u5e76\u54cd\u5e94 &#8220;Hello, World!&#8221;\u3002,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u5728\u4ee5\u4e0b\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u5728 GOLANG HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\u6765\u54cd\u5e94\u4e00\u4e2a\u7b80\u5355\u7684 JSON \u8bf7\u6c42\uff1a,<br \/>\n<strong>\u4ee3\u7801<\/strong>,<br \/>\n<strong>\u4f7f\u7528<\/strong>,\u8bbf\u95ee &#8220;\/json&#8221; \u8def\u7531\u5c06\u8fd4\u56de\u4e00\u4e2a JSON \u54cd\u5e94\uff1a,\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8egolang,HTTP\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGOLANG\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\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\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f<\/h1>\n<p><span>2024-10-27 17:01:49<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGOLANG\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u5728 GOLANG HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff0c\u53ea\u9700\u5728 http.HandleFunc() \u4e2d\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u4ee3\u7801\u7f16\u5199\uff0c\u6bd4\u5982\u5904\u7406 HTTP \u8bf7\u6c42\u3002\u5728\u5b9e\u6218\u4e2d\uff0c\u533f\u540d\u51fd\u6570\u53ef\u7528\u4e8e\u54cd\u5e94 JSON \u8bf7\u6c42\uff0c\u901a\u8fc7\u7f16\u7801 JSON \u6570\u636e\u5e76\u5c06\u5176\u5199\u5165 HTTP \u54cd\u5e94\u6b63\u6587\u5373\u53ef\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730019694671e016e66247.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f<\/strong><\/p>\n<p><strong>\u80cc\u666f<\/strong><\/p>\n<p>\u533f\u540d\u51fd\u6570\uff0c\u4e5f\u79f0\u4e3a lambda \u8868\u8fbe\u5f0f\uff0c\u662f\u4e00\u79cd\u65e0\u9700\u547d\u540d\u5373\u53ef\u5b9a\u4e49\u5e76\u4f7f\u7528\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u65e8\u5728\u7b80\u5316\u4ee3\u7801\u7f16\u5199\u3002\u5728 GOLANG \u4e2d\uff0c\u533f\u540d\u51fd\u6570\u53ef\u7528\u4e8e\u5904\u7406 HTTP \u8bf7\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u5728 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff0c\u8bf7\u5728 <code>http.HandleFunc()<\/code> \u4e2d\u76f4\u63a5\u4f20\u9012\u5b83\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"net\/http\"\n)\n\nfunc main() {\n    http.HandleFunc(\"\/hello\", func(w http.ResponseWriter, r *http.Request) {\n        w.Write([]byte(\"Hello, World!\"))\n    })\n\n    http.ListenAndServe(\":8080\", nil)\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u6765\u5904\u7406 &#8220;\/hello&#8221; \u8def\u7531\u3002\u5f53\u5ba2\u6237\u7aef\u901a\u8fc7 HTTP \u8bf7\u6c42\u8bbf\u95ee\u8be5\u8def\u7531\u65f6\uff0c\u533f\u540d\u51fd\u6570\u5c06\u88ab\u6267\u884c\u5e76\u54cd\u5e94 &#8220;Hello, World!&#8221;\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5728\u4ee5\u4e0b\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u5728 GOLANG HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\u6765\u54cd\u5e94\u4e00\u4e2a\u7b80\u5355\u7684 JSON \u8bf7\u6c42\uff1a<\/p>\n<p><strong>\u4ee3\u7801<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"net\/http\"\n)\n\ntype Response struct {\n    Message string\n}\n\nfunc main() {\n    http.HandleFunc(\"\/json\", func(w http.ResponseWriter, r *http.Request) {\n        \/\/ \u5c06 JSON \u6570\u636e\u7f16\u7801\u6210 HTTP \u54cd\u5e94\u6b63\u6587\n        json.NewEncoder(w).Encode(Response{\n            Message: \"Hello, JSON!\",\n        })\n    })\n\n    http.ListenAndServe(\":8080\", nil)\n}<\/pre>\n<p><strong>\u4f7f\u7528<\/strong><\/p>\n<p>\u8bbf\u95ee &#8220;\/json&#8221; \u8def\u7531\u5c06\u8fd4\u56de\u4e00\u4e2a JSON \u54cd\u5e94\uff1a<\/p>\n<pre>{\n    \"Message\": \"Hello, JSON!\"\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5728 GOLANG \u7684 HTTP \u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8egolang,HTTP\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   CSS \u52a8\u753b\u4e2d\u5982\u4f55\u5b9e\u73b0\u7a81\u53d8\u6548\u679c\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP\u51fd\u6570\u5982\u4f55\u83b7\u53d6\u53c2\u6570\u7684\u6570\u91cf\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684&#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-205854","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205854","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=205854"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205854\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}