{"id":749,"date":"2024-07-11T11:41:34","date_gmt":"2024-07-11T03:41:34","guid":{"rendered":"https:\/\/www.madbull.site\/?p=749"},"modified":"2024-10-14T20:24:36","modified_gmt":"2024-10-14T12:24:36","slug":"golang%e5%88%9b%e5%bb%bahttp%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%8c%e5%ae%9e%e7%8e%b0get%e5%92%8cpost%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=749","title":{"rendered":"golang\u521b\u5efahttp\u670d\u52a1\u5668\uff0c\u5b9e\u73b0GET\u548cPOST\u8bf7\u6c42"},"content":{"rendered":"\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"github.com\/gin-gonic\/gin\"\n    \"net\/http\"\n    \"strconv\"\n)\n\nvar id int = 0\n\n\/\/ get\u5904\u7406\u51fd\u6570\nfunc t1_get(c *gin.Context) {\n    name := c.Query(\"name\")\n    id += 1\n    c.JSON(http.StatusOK, gin.H{\n        \"title\": \"get\",\n        \"msg\": \"hello \" + name + \" you are the \" + strconv.Itoa(id) + \"th guest !\",\n    })\n}\n\n\/\/ \u5b9a\u4e49json\u5e8f\u5217\u5316\u7ed3\u6784\ntype PostData struct {\n    Name    string `json:\"name\"`\n    Tel     string `json:\"tel\"`\n    Email   string `json:\"email\"`\n}\n\n\/\/ post\u5904\u7406\u51fd\u6570\nfunc t1_post(c *gin.Context) {\n    var postData PostData\n    \/\/ \u89e3\u6790body\u90e8\u5206\n    if err := c.ShouldBindBodyWithJSON(&amp;postData); err != nil {\n        c.JSON(http.StatusBadRequest, gin.H{\n            \"error\": err.Error(),\n        })\n        return\n    }\n\n    id += 1\n    \/\/ \u8bbe\u7f6e\u8fd4\u56de\u503c\n    c.JSON( http.StatusOK, gin.H{\n        \"title\": \"post\",\n        \"content\": postData,\n        \"msg\": strconv.Itoa(id) + \"th guest !\",\n    })\n}\n\n\nfunc main() {\n\n    \/\/ \u521b\u5efa\u670d\u52a1\n    serv := gin.Default()\n    serv.UseH2C = true\n\n    \/\/ \u521b\u5efa\u8def\u7531\n    route := serv.Group(\"\/test\")\n\n    \/\/ \u6dfb\u52a0\u4e24\u4e2a\u8def\u7531\n    route.GET(\"\/t1\", t1_get)\n    route.POST(\"\/t1\", t1_post)\n\n    \/\/ \u542f\u52a8\n    serv.Run(\":3322\")\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p>\u521b\u5efa go.mod\uff0c\u5185\u5bb9 module gohttp<\/p>\n\n\n\n<p>\u6267\u884cgo mod tidy\uff0c\u81ea\u52a8\u67e5\u627e\u5e76\u4e0b\u8f7d\u4f9d\u8d56\u3002\u5982\u679c\u4e0d\u80fd\u8054\u7f51\u8bbf\u95ee\u6e90\u7684\uff0c\u6ce8\u610f\u8bbe\u7f6eGOPROXY\u4ee3\u7406\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u8fd0\u884c<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"871\" height=\"220\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-3.png\" alt=\"\" class=\"wp-image-750\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-3.png 871w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-3-300x76.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/111-3-768x194.png 768w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/figure>\n<\/div>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"421\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-2.png\" alt=\"\" class=\"wp-image-751\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-2.png 770w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-2-300x164.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/07\/222-2-768x420.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>go\u521b\u5efahttp\u670d\u52a1\u5668\uff0c\u5b9e\u73b0GET\u548cPOST\u8bf7\u6c42<\/p>\n","protected":false},"author":1,"featured_media":727,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[286,154],"tags":[301,303,298,287,299,302,300],"class_list":["post-749","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-154","tag-get","tag-gin-gonic","tag-go","tag-golang","tag-http","tag-net","tag-post"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=749"}],"version-history":[{"count":3,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/749\/revisions"}],"predecessor-version":[{"id":1539,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/749\/revisions\/1539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/727"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}