{"id":75621,"date":"2024-03-01T15:32:31","date_gmt":"2024-03-01T07:32:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=75621"},"modified":"2024-03-01T15:32:47","modified_gmt":"2024-03-01T07:32:47","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e8%af%ad%e8%a8%80%e4%b8%ad%e8%bf%9b%e8%a1%8chttp%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/75621.html","title":{"rendered":"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-75622\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/03\/generated_00-12.png?x-oss-process=image\/auto-orient,1\/format,webp\" width=\"600\" height=\"342\" alt=\"\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b\" \/><\/p>\n<p>### \u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b<\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u65b9\u9762\uff1a<strong>\u6784\u5efaHTTP\u670d\u52a1\u5668<\/strong>\u548c<strong>\u53d1\u8d77HTTP\u5ba2\u6237\u7aef\u8bf7\u6c42<\/strong>\u3002Go\u6807\u51c6\u5e93`net\/http`\u4e3a\u8fd9\u4e24\u4e2a\u4efb\u52a1\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u652f\u6301\u3002<strong>\u6784\u5efaHTTP\u670d\u52a1\u5668<\/strong>\u6d89\u53ca\u76d1\u542c\u4e00\u4e2a\u7aef\u53e3\u3001\u63a5\u6536HTTP\u8bf7\u6c42\u5e76\u4f5c\u51fa\u54cd\u5e94\uff1b\u800c<strong>\u53d1\u8d77HTTP\u5ba2\u6237\u7aef\u8bf7\u6c42<\/strong>\u5219\u5305\u62ec\u521b\u5efa\u8bf7\u6c42\u3001\u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u5e76\u5904\u7406\u54cd\u5e94\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e24\u4e2a\u65b9\u9762\uff0c\u5e2e\u52a9\u8bfb\u8005\u638c\u63e1Go\u8bed\u8a00\u5728HTTP\u7f16\u7a0b\u4e2d\u7684\u5173\u952e\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p>#### \u4e00\u3001\u6784\u5efaHTTP\u670d\u52a1\u5668<\/p>\n<p><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684HTTP\u670d\u52a1\u5668<\/strong>\u5728Go\u8bed\u8a00\u4e2d\u662f\u76f8\u5bf9\u7b80\u5355\u7684\u3002\u9996\u5148\uff0c\u9700\u8981\u5f15\u5165`net\/http`\u5305\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5904\u7406\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u63a5\u6536`http.ResponseWriter`\u548c`http.Request`\u4e24\u4e2a\u53c2\u6570\u3002`http.ResponseWriter`\u7528\u4e8e\u53d1\u9001HTTP\u54cd\u5e94\u7ed9\u5ba2\u6237\u7aef\uff0c\u800c`http.Request`\u5305\u542b\u4e86\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>package m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/p>\n<p>import (<\/p>\n<p>    &#8220;fmt&#8221;<\/p>\n<p>    &#8220;net\/http&#8221;<\/p>\n<p>)<\/p>\n<p>func helloHandler(w http.ResponseWriter, r *http.Request) {<\/p>\n<p>    fmt.Fprintf(w, &#8220;Hello, world!&#8221;)<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>    http.HandleFunc(&#8220;\/&#8221;, helloHandler)<\/p>\n<p>    http.ListenAndServe(&#8220;:8080&#8221;, nil)<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5904\u7406\u51fd\u6570`helloHandler`\uff0c\u5f53\u8bbf\u95ee\u6839\u8def\u5f84`&#8221;\/&#8221;`\u65f6\uff0c\u670d\u52a1\u5668\u5c31\u4f1a\u8fd4\u56de&#8221;Hello, world!&#8221;\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8c03\u7528`http.ListenAndServe`\u51fd\u6570\u5e76\u6307\u5b9a\u7aef\u53e3\u53f7\uff08\u8fd9\u91cc\u662f`:8080`\uff09\uff0c\u6211\u4eec\u542f\u52a8\u4e86HTTP\u670d\u52a1\u5668\u3002<\/p>\n<p><strong>\u5904\u7406\u52a8\u6001\u8bf7\u6c42<\/strong>\u662f\u6784\u5efaHTTP\u670d\u52a1\u5668\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002Go\u8bed\u8a00\u652f\u6301\u901a\u8fc7\u8def\u7531\uff08\u5982`http.ServeMux`\uff09\u6765\u5339\u914d\u4e0d\u540c\u7684URL\u8def\u5f84\u548c\u5904\u7406\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u4e3a\u4e0d\u540c\u7684URL\u8def\u5f84\u7f16\u5199\u7279\u5b9a\u7684\u5904\u7406\u903b\u8f91\uff0c\u4ece\u800c\u521b\u5efa\u52a8\u6001\u548c\u5bcc\u6709\u4ea4\u4e92\u6027\u7684Web\u5e94\u7528\u3002<\/p>\n<p>#### \u4e8c\u3001\u53d1\u8d77HTTP\u5ba2\u6237\u7aef\u8bf7\u6c42<\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c`net\/http`\u5305\u4e0d\u4ec5\u652f\u6301\u670d\u52a1\u5668\u7aef\u7f16\u7a0b\uff0c\u4e5f\u652f\u6301\u5ba2\u6237\u7aef\u7f16\u7a0b\u3002<strong>\u53d1\u8d77\u4e00\u4e2aHTTP GET\u8bf7\u6c42<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7`http.Get`\u51fd\u6570\u8f7b\u677e\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>    &#8220;fmt&#8221;<\/p>\n<p>    &#8220;io\/ioutil&#8221;<\/p>\n<p>    &#8220;net\/http&#8221;<\/p>\n<p>)<\/p>\n<p>func main() {<\/p>\n<p>    response, err := http.Get(&#8220;http:\/\/example.com&#8221;)<\/p>\n<p>    if err != nil {<\/p>\n<p>        fmt.Printf(&#8220;The HTTP request failed with error %s\\n&#8221;, err)<\/p>\n<p>    } else {<\/p>\n<p>        data, _ := ioutil.ReadAll(response.Body)<\/p>\n<p>        fmt.Println(string(data))<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5411`http:\/\/example.com`\u53d1\u8d77\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u6253\u5370\u51fa\u4e86\u54cd\u5e94\u7684\u6b63\u6587\u3002`http.Get`\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a`http.Response`\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u4e86\u54cd\u5e94\u7684\u72b6\u6001\u7801\u3001\u5934\u90e8\u4ee5\u53ca\u6b63\u6587\u3002<\/p>\n<p>\u9664\u4e86GET\u8bf7\u6c42\u4e4b\u5916\uff0cGo\u8bed\u8a00\u7684HTTP\u5ba2\u6237\u7aef\u8fd8\u652f\u6301\u5176\u4ed6\u7c7b\u578b\u7684\u8bf7\u6c42\uff0c\u5982POST\u3001PUT\u7b49\u3002\u8fd9\u4e9b\u8bf7\u6c42\u901a\u5e38\u9700\u8981\u8bbe\u7f6e\u8bf7\u6c42\u7684\u6b63\u6587\u548c\u5934\u90e8\u3002`http.NewRequest`\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684HTTP\u8bf7\u6c42\uff0c\u7136\u540e\u901a\u8fc7`http.Client`\u53d1\u9001\u8be5\u8bf7\u6c42\u3002<\/p>\n<p>#### \u4e09\u3001\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94<\/p>\n<p>\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u662fHTTP\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u90e8\u5206\u3002\u5728\u670d\u52a1\u5668\u7aef\uff0c\u5904\u7406\u51fd\u6570\u9700\u8981\u89e3\u6790\u8bf7\u6c42\u4e2d\u7684\u5404\u79cd\u53c2\u6570\uff0c\u5982URL\u8def\u5f84\u3001\u67e5\u8be2\u53c2\u6570\u3001\u8868\u5355\u6570\u636e\u3001JSON\u6b63\u6587\u7b49\u3002Go\u8bed\u8a00\u7684`net\/http`\u5305\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u51fd\u6570\u6765\u7b80\u5316\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<p><strong>\u89e3\u6790\u67e5\u8be2\u53c2\u6570<\/strong>\u53ef\u4ee5\u901a\u8fc7`http.Request`\u7684`URL.Query()`\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679cURL\u662f`http:\/\/example.com?name=John&#038;age=30`\uff0c\u53ef\u4ee5\u8fd9\u6837\u83b7\u53d6\u67e5\u8be2\u53c2\u6570\uff1a<\/p>\n<p>&#8220;`go<\/p>\n<p>name := r.URL.Query().Get(&#8220;name&#8221;)<\/p>\n<p>age := r.URL.Query().Get(&#8220;age&#8221;)<\/p>\n<p>&#8220;`<\/p>\n<p><strong>\u89e3\u6790\u8868\u5355\u6570\u636e<\/strong>\u5219\u9700\u8981\u5148\u8c03\u7528`http.Request`\u7684`ParseForm`\u65b9\u6cd5\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7`Form`\u5b57\u6bb5\u8bbf\u95ee\u8868\u5355\u6570\u636e\u3002<\/p>\n<p>\u5728\u5ba2\u6237\u7aef\uff0c\u5904\u7406HTTP\u54cd\u5e94\u540c\u6837\u91cd\u8981\u3002\u8fd9<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Go\u8bed\u8a00\u53d1\u9001HTTP\u8bf7\u6c42\uff1f<\/strong><\/p>\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528`net\/http`\u5305\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a`http.Client`\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528`client.Get()`\u6216`client.Post()`\u7b49\u65b9\u6cd5\u6765\u53d1\u9001\u8bf7\u6c42\u3002\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u3001\u8bf7\u6c42\u4f53\u548c\u53d1\u8d77\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4e0e\u8fdc\u7a0b\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\uff0c\u5e76\u83b7\u53d6\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u5904\u7406HTTP\u54cd\u5e94\uff1f<\/strong><\/p>\n<p>\u4e00\u65e6\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5f97\u5230\u54cd\u5e94\uff0c\u9700\u8981\u5728Go\u8bed\u8a00\u4e2d\u5904\u7406HTTP\u54cd\u5e94\u3002\u53ef\u4ee5\u901a\u8fc7`http.Response`\u5bf9\u8c61\u6765\u8bfb\u53d6\u54cd\u5e94\u4f53\u3001\u67e5\u770b\u54cd\u5e94\u72b6\u6001\u7801\u3001\u83b7\u53d6\u54cd\u5e94\u5934\u7b49\u4fe1\u606f\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u53ef\u4ee5\u5c06\u54cd\u5e94\u4f53\u89e3\u6790\u4e3a\u5b57\u7b26\u4e32\u3001JSON\u7b49\u683c\u5f0f\uff0c\u5e76\u6839\u636e\u54cd\u5e94\u72b6\u6001\u7801\u8fdb\u884c\u9519\u8bef\u5904\u7406\u6216\u903b\u8f91\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff1f<\/strong><\/p>\n<p>\u8981\u5728Go\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528`net\/http`\u5305\u63d0\u4f9b\u7684`http.ListenAndServe()`\u65b9\u6cd5\u3002\u9996\u5148\u9700\u8981\u6ce8\u518c\u5904\u7406\u51fd\u6570\uff0c\u7136\u540e\u6307\u5b9a\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u3002\u53ef\u4ee5\u901a\u8fc7`http.HandleFunc()`\u51fd\u6570\u6765\u6ce8\u518c\u5904\u7406\u51fd\u6570\uff0c\u5904\u7406\u4e0d\u540c\u7684HTTP\u8bf7\u6c42\u3002\u6700\u540e\u4f7f\u7528`http.ListenAndServe()`\u6765\u542f\u52a8HTTP\u670d\u52a1\u5668\uff0c\u76d1\u542c\u6307\u5b9a\u7684\u7aef\u53e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"### \u5982\u4f55\u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b \u5728Go\u8bed\u8a00\u4e2d\u8fdb\u884cHTTP\u7f16\u7a0b\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u65b9\u9762\uff1a\u6784\u5efaHTTP\u670d\u52a1\u5668\u548c\u53d1 [&hellip;]","protected":false},"author":1,"featured_media":75624,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75621"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=75621"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/75621\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/75624"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=75621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=75621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=75621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}