{"id":206193,"date":"2025-06-12T08:35:18","date_gmt":"2025-06-12T00:35:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206193\/"},"modified":"2025-06-12T08:35:18","modified_gmt":"2025-06-12T00:35:18","slug":"gin%e6%a1%86%e6%9e%b6%e4%b8%adshouldbind%e6%96%b9%e6%b3%95%e7%bb%91%e5%ae%9a%e5%a4%9a%e4%b8%aa%e5%8f%82%e6%95%b0%e7%bb%93%e6%9e%84%e4%bd%93%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bd%95%e4%bc%9a%e5%87%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206193\/","title":{"rendered":"Gin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f"},"content":{"rendered":"<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span>,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730854475672abe4b20506.jpg\" class=\"aligncenter\">,<br \/>\n<strong>gin\u7684shouldbind\u65b9\u6cd5\u51b2\u7a81\u95ee\u9898<\/strong>,\u5728gin\u6846\u67b6\u4e2d\uff0c\u4f7f\u7528shouldbind\u65b9\u6cd5\u7ed1\u5b9a\u8bf7\u6c42\u53c2\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u51b2\u7a81\u95ee\u9898\u3002\u8be5\u95ee\u9898\u901a\u5e38\u8868\u73b0\u4e3a\uff0c\u5f53\u540c\u65f6\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u53ea\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53\u80fd\u6210\u529f\u7ed1\u5b9a\uff0c\u800c\u5176\u4ed6\u7ed3\u6784\u4f53\u4f1a\u4e3a\u7a7a\u503c\u3002,<br \/>\n<strong>\u793a\u4f8b<\/strong>,<br \/>\n<strong>\u95ee\u9898\u539f\u56e0<\/strong>,\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u662fgin\u7684shouldbind\u65b9\u6cd5\u5728\u7ed1\u5b9a\u8fc7\u7a0b\u4e2d\u4f1a\u5c06\u8bf7\u6c42body\u4e2d\u7684\u503c\u7f13\u5b58\u5230\u4e0a\u4e0b\u6587\u4e2d\u3002\u5f53\u7b2c\u4e8c\u4e2a\u7ed3\u6784\u4f53\u7ed1\u5b9a\u65f6\uff0c\u8bf7\u6c42body\u4e2d\u7684\u503c\u5df2\u7ecf\u88ab\u6d88\u8017\uff0c\u5bfc\u81f4\u53c2\u6570\u586b\u5145\u5931\u8d25\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6848<\/strong>,\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u4f7f\u7528gin\u63d0\u4f9b\u7684c.shouldbindbodywith\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u7ed1\u5b9a\u7279\u5b9a\u7684body\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982json\u6570\u636e\u3002,\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002,gin\u7684shouldbind\u65b9\u6cd5\u51b2\u7a81\u95ee\u9898,<br \/>\n<span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span>,<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>Gin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f<\/span><\/p>\n<h1>Gin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f<\/h1>\n<p><span>2024-11-06 08:54:50<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGin\u6846\u67b6\u4e2dShouldBind\u65b9\u6cd5\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u4e3a\u4f55\u4f1a\u51fa\u73b0\u51b2\u7a81\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730854475672abe4b20506.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>gin\u7684shouldbind\u65b9\u6cd5\u51b2\u7a81\u95ee\u9898<\/strong><\/p>\n<p>\u5728gin\u6846\u67b6\u4e2d\uff0c\u4f7f\u7528shouldbind\u65b9\u6cd5\u7ed1\u5b9a\u8bf7\u6c42\u53c2\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u51b2\u7a81\u95ee\u9898\u3002\u8be5\u95ee\u9898\u901a\u5e38\u8868\u73b0\u4e3a\uff0c\u5f53\u540c\u65f6\u7ed1\u5b9a\u591a\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\u65f6\uff0c\u53ea\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53\u80fd\u6210\u529f\u7ed1\u5b9a\uff0c\u800c\u5176\u4ed6\u7ed3\u6784\u4f53\u4f1a\u4e3a\u7a7a\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>\/\/ package models\ntype listreqinfo struct {\n    keyword   string `form:\"keyword\" default:\"\"`                       \/\/\u6a21\u7cca\u5173\u952e\u5b57\n    pagenum   int    `form:\"pagenum\" binding:\"required\" default:\"1\"`   \/\/\u9875\u6570\n    pagesize  int    `form:\"pagesize\" binding:\"required\" default:\"10\"` \/\/\u9875\u5927\u5c0f\n    ordername string `form:\"ordername\" default:\"created_on\"`           \/\/\u6392\u5e8f\u5217\n    ordertype string `form:\"ordertype\" default:\"desc\"`                 \/\/\u6392\u5e8f\u65b9\u5f0f desc asc\n}\n\n\/\/ @tags \u6587\u7ae0\n\/\/ @summary \u5206\u9875\u83b7\u53d6\u6587\u7ae0\n\/\/ @produce  json\n\/\/ @param reqinfo body models.listreqinfo false \"info\"\n\/\/ @success 200 {object} app.response\n\/\/ @failure 500 {object} app.response\n\/\/ @router \/api\/v1\/article\/list [post]\nfunc (a article) list(c *gin.context) {\n    var (\n        appg     = app.gin{c: c}\n        param    models.listreqinfo\n        parammap map[string]interface{}\n    )\n\n    \/\/ \u51b2\u7a81\uff1a\u540c\u65f6\u7ed1\u5b9a\u4e24\u4e2a\u53c2\u6570\u7ed3\u6784\u4f53\n    if err := c.shouldbind(&amp;param); err != nil {\n        appg.responseerror(http.statusbadrequest, err.error())\n        return\n    }\n\n    if err := c.shouldbind(&amp;parammap); err != nil {\n        appg.responseerror(http.statusbadrequest, err.error())\n        return\n    }\n}<\/pre>\n<p><strong>\u95ee\u9898\u539f\u56e0<\/strong><\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u662fgin\u7684shouldbind\u65b9\u6cd5\u5728\u7ed1\u5b9a\u8fc7\u7a0b\u4e2d\u4f1a\u5c06\u8bf7\u6c42body\u4e2d\u7684\u503c\u7f13\u5b58\u5230\u4e0a\u4e0b\u6587\u4e2d\u3002\u5f53\u7b2c\u4e8c\u4e2a\u7ed3\u6784\u4f53\u7ed1\u5b9a\u65f6\uff0c\u8bf7\u6c42body\u4e2d\u7684\u503c\u5df2\u7ecf\u88ab\u6d88\u8017\uff0c\u5bfc\u81f4\u53c2\u6570\u586b\u5145\u5931\u8d25\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u662f\u4f7f\u7528gin\u63d0\u4f9b\u7684c.shouldbindbodywith\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u7ed1\u5b9a\u7279\u5b9a\u7684body\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982json\u6570\u636e\u3002<\/p>\n<pre>func (a Article) List(c *gin.Context) {\n    var (\n        appG     = app.Gin{C: c}\n        param    models.ListReqInfo\n        paramMap map[string]interface{}\n    )\n\n    \/\/ \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528c.ShouldBindBodyWith\n    if err := c.ShouldBindBodyWith(&amp;param, binding.JSON); err != nil {\n        appG.ResponseError(http.StatusBadRequest, err.Error())\n        return\n    }\n\n    \/\/ ...\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u7535\u8111\u84dd\u5c4f\u6545\u969c\u7684\u89e3\u51b3\u65b9\u6cd5\u53ca\u5e38\u89c1\u539f\u56e0\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Go \u8bed\u8a00\u90e8\u7f72\u96be\u9898\uff1a\u5982\u4f55\u5b9e\u73b0\u5728\u7ebf\u70ed\u66f4\u65b0\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60GO&#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-206193","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206193","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=206193"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206193\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}