{"id":3748,"date":"2022-12-30T14:07:52","date_gmt":"2022-12-30T06:07:52","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3748"},"modified":"2022-12-30T17:37:43","modified_gmt":"2022-12-30T09:37:43","slug":"%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84http%e5%ae%a2%e6%88%b6%e7%ab%af%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/go\/%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84http%e5%ae%a2%e6%88%b6%e7%ab%af%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5b78\u7fd2Go\u8a9e\u8a00\u4e2d\u7684HTTP\u5ba2\u6236\u7aef\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Go, HTTP \u5ba2\u6236\u7aef\"><\/p>\n<h1>\u4f7f\u7528 Go \u8a9e\u8a00\u7684 HTTP \u5ba2\u6236\u7aef<\/h1>\n<p>Go \u8a9e\u8a00\u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u3001\u53ef\u64f4\u5c55\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec HTTP \u5ba2\u6236\u7aef\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u4f86\u958b\u767c HTTP \u5ba2\u6236\u7aef\u3002<br \/>\n<!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f HTTP \u5ba2\u6236\u7aef\uff1f<\/h2>\n<p>HTTP \u5ba2\u6236\u7aef\u662f\u4e00\u7a2e\u8edf\u4ef6\uff0c\u5b83\u53ef\u4ee5\u8207 HTTP \u4f3a\u670d\u5668\u9032\u884c\u901a\u4fe1\uff0c\u4ee5\u7372\u53d6\u6216\u63d0\u4ea4\u6578\u64da\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u767c\u9001 HTTP \u8acb\u6c42\uff0c\u4e26\u63a5\u6536\u4f3a\u670d\u5668\u7684\u56de\u61c9\u3002<\/p>\n<h2>\u4f7f\u7528 Go \u8a9e\u8a00\u958b\u767c HTTP \u5ba2\u6236\u7aef<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5167\u7f6e\u7684 <code>net\/http<\/code> \u5305\uff0c\u53ef\u4ee5\u7528\u65bc\u958b\u767c HTTP \u5ba2\u6236\u7aef\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684 API\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u767c\u9001 HTTP \u8acb\u6c42\uff0c\u4e26\u63a5\u6536\u4f3a\u670d\u5668\u7684\u56de\u61c9\u3002<\/p>\n<h3>\u767c\u9001 GET \u8acb\u6c42<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u767c\u9001\u4e00\u500b GET \u8acb\u6c42\uff1a<\/p>\n<pre class=\"brush: go\">\npackage main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n)\n\nfunc main() {\n    resp, err := http.Get(\"http:\/\/example.com\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer resp.Body.Close()\n    fmt.Println(resp.Status)\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>http.Get()<\/code> \u65b9\u6cd5\u767c\u9001\u4e86\u4e00\u500b GET \u8acb\u6c42\uff0c\u4e26\u63a5\u6536\u4e86\u4f3a\u670d\u5668\u7684\u56de\u61c9\u3002<\/p>\n<h3>\u767c\u9001 POST \u8acb\u6c42<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u767c\u9001\u4e00\u500b POST \u8acb\u6c42\uff1a<\/p>\n<pre class=\"brush: go\">\npackage main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n    \"strings\"\n)\n\nfunc main() {\n    resp, err := http.Post(\"http:\/\/example.com\",\n        \"application\/x-www-form-urlencoded\",\n        strings.NewReader(\"name=foo&age=20\"))\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer resp.Body.Close()\n    fmt.Println(resp.Status)\n}\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>http.Post()<\/code> \u65b9\u6cd5\u767c\u9001\u4e86\u4e00\u500b POST \u8acb\u6c42\uff0c\u4e26\u63a5\u6536\u4e86\u4f3a\u670d\u5668\u7684\u56de\u61c9\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u958b\u767c HTTP \u5ba2\u6236\u7aef\u3002\u6211\u5011\u770b\u5230\uff0cGo \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684 API\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u767c\u9001 HTTP \u8acb\u6c42\uff0c\u4e26\u63a5\u6536\u4f3a\u670d\u5668\u7684\u56de\u61c9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Go\u8a9e\u8a00\u4e2d\u7684HTTP\u5ba2\u6236\u7aef\u529f\u80fd\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Go\u8a9e\u8a00\u4e2d\u7684HTTP\u5ba2\u6236\u7aef\u4f86\u767c\u9001\u8acb\u6c42\uff0c\u4ee5\u53ca\u5982\u4f55\u8655\u7406\u56de\u61c9\u3002\u901a\u904e\u5b78\u7fd2Go\u8a9e\u8a00\u4e2d\u7684HTTP\u5ba2\u6236\u7aef\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1Go\u8a9e\u8a00\u7684\u529f\u80fd\uff0c\u4e26\u66f4\u6709\u6548\u5730\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[171,172],"tags":[169,170],"class_list":["post-3748","post","type-post","status-publish","format-standard","hentry","category-go","category-golang","tag-go","tag-golang"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ys","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3748"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3748\/revisions"}],"predecessor-version":[{"id":3931,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3748\/revisions\/3931"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}