{"id":10625,"date":"2022-11-11T10:25:48","date_gmt":"2022-11-11T02:25:48","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=10625"},"modified":"2023-05-06T17:03:42","modified_gmt":"2023-05-06T09:03:42","slug":"page","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/11\/11\/page\/","title":{"rendered":"go\u8bed\u8a00\u4e4b\u5206\u9875\u529f\u80fd"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u524d\u53f0\u9875\u9762\u5728\u8c03\u7528\u540e\u53f0\u63a5\u53e3\u7684\u65f6\u5019\uff0c\u662f\u9700\u8981\u6839\u636e\u70b9\u51fb\u9875\u6570\u663e\u793a\u5bf9\u5e94\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"273\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/19.png\" alt=\"\" class=\"wp-image-10626\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/19.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/19-300x116.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/19-600x231.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a\u524d\u53f0\u4f20\u9012\u7684\u662f\u9875\u6570\uff0c\u7b2c\u51e0\u9875\u7b2c\u51e0\u9875\u53c2\u6570\u5b9a\u4e49\u4e3aPageNum\uff0c\u540e\u53f0\u63a5\u6536\u9875\u6570\u540e\uff0c\u7136\u540e\u6839\u636e\u9875\u6570\u4ee5\u53ca\u6bcf\u9875\u663e\u793a\u7684\u6570\u636e\u91cf(PageSize)\u6765\u8fd4\u56de\u5bf9\u5e94\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u4f8b\u5b50\u4e2d\u4ee5\u8fd0\u7ef4\u5e73\u53f0oamp\u4e2d\u83b7\u53d6\u4e2d\u95f4\u4ef6\u529f\u80fd\u4e3a\u4f8b\u5b50\u52a0\u4ee5\u6f14\u793a\uff0c\u9879\u76ee\u8ddf\u76ee\u5f55\u4e3a\/data\/oamp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u9996\u5148\u5728\u914d\u7f6e\u6587\u4ef6oamp\/conf\/app.ini\u4e2d\u5b9a\u4e49PageSize\u7684\u503c\uff0c\u672c\u4f8b\u5b50\u4e2d\u5b9a\u4e49\u4e3a10\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"188\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/22.png\" alt=\"\" class=\"wp-image-10631\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/22.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/22-300x80.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/22-600x159.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u5b9a\u4e49\u83b7\u53d6\u9875\u6570\u7684\u51fd\u6570\uff0c\u5728\u6587\u4ef6oamp\/pkg\/util\/page.go\u4e2d\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"247\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-7.png\" alt=\"\" class=\"wp-image-10633\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-7.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-7-300x105.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-7-600x209.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u4e00\u4e2a\u521d\u59cb\u503cresult\u4e3a0<\/li>\n\n\n\n<li>c.Query(&#8220;PageNum&#8221;)\u8868\u793a\u8bf7\u6c42\u63a5\u53e3\u7684\u65f6\u5019\u4f20\u9012\u7684\u53c2\u6570PageNum\u7684\u503c\uff0c\u5982\u679c\u5b58\u5728\u8fd4\u56de\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>com.StrTo\u8868\u793a\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff0c\u6b64\u5904\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u6362\u4e3aint\uff0c\u9664\u4e86Int()\u8fd8\u53ef\u7528MustInt()\uff0c\u533a\u522b\u662fInt()\u6709\u4e24\u4e2a\u8fd4\u56de\u503c\uff0c\u5176\u4e2d\u4e00\u4e2a\u662ferr\uff0c\u800cMustInt()\u53ea\u8fd4\u56de\u4e00\u76f4\u8f6c\u6362\u540e\u7684\u503c<\/li>\n\n\n\n<li>\u5982\u679c\u8f6c\u6362\u540e\u7684pageNum\u5927\u4e8e0\uff0c\u9700\u8981\u5c06\u5176\u51cf\u53bb1,\u4e3a\u4ec0\u4e48\u51cf\u53bb1\uff0c\u5047\u5982pageNum\u4e3a1\uff0c\u8868\u793a\u83b7\u53d6\u7b2c\u4e00\u9875\u7684\u6570\u636e\uff0c\u5982\u679c\u5728\u6267\u884c\u67e5\u8be2\u6570\u636e\u524d\u5341\u6761\u7684\u65f6\u5019\uff0cwhere\u6761\u4ef6\u5c06\u53d8\u4e3aid &gt; 1*10\uff0c\u90a3\u4e48\u5c06\u4ece\u7b2c10\u6761\u5f00\u59cb\u67e5\uff0c\u672c\u5e94\u8be5\u4ece\u7b2c\u4e00\u6761\u5f00\u59cb\u67e5\uff0c\u4e5f\u5c31\u662fid &gt; 0*10\uff0c\u4ece\u7b2c\u4e00\u6761\u5f00\u59cb\uff0c\u5f80\u4e0b\u770b\u5c31\u53ef\u4ee5\u660e\u767d\u4e86<\/li>\n\n\n\n<li>return -1\uff1a-1\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u8868\u793a\u5982\u679cc.Query\u4e2d\u6ca1\u6709\u67e5\u8be2\u5230\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u4f20\u9012\u9875\u6570\uff0c\u5176\u5b9e\u83b7\u53d6\u7684\u662f\u5168\u90e8\u6570\u636e<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u58f0\u660e\u7ed3\u6784\u4f53\uff0c\u5b9a\u4e49\u51fd\u6570\u5e76\u5c06\u9875\u6570\u7ed1\u5b9a\u5230\u7ed3\u6784\u4f53\u5b57\u6bb5PageNum\u4e0a\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"472\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-6.png\" alt=\"\" class=\"wp-image-10637\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-6.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-6-300x200.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-6-600x399.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u672c\u4f8b\u5b50\u53ea\u6f14\u793a\u5206\u9875\uff0c\u5176\u4f59\u7684\u7ed1\u5b9a\u95ee\u9898\u5728\u6b21\u4e0d\u8ba8\u8bba<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u7ed1\u5b9a\u540e\u5bf9\u7ed3\u6784\u4f53GetMiddle\u8fdb\u884c\u5b9e\u4f8b\u5316\u7136\u540e\u8c03\u7528\u5176\u4e2d\u7684\u65b9\u6cd5GetMiddleware()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u67e5\u770bGetMiddleware\u65b9\u6cd5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"460\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-3.png\" alt=\"\" class=\"wp-image-10642\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-3-300x195.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-3-600x389.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u4e2ag.PageNum\u4f20\u9012\u7ed9\u65b9\u6cd5GetMiddleKeysPagenum\u7528\u4e8e\u83b7\u53d6\u9875\u6570\u5bf9\u5e94\u7684redis\u7684key<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u4e2ag.PageNum\u4f20\u9012\u7ed9\u51fd\u6570MiddlePage\u7528\u4e8e\u83b7\u53d6\u6570\u636e\u5e76\u8bbe\u7f6eRedis\u7684key<\/li>\n\n\n\n<li>PageSize\uff1a\u6bcf\u9875\u663e\u793a\u7684\u6570\u636e\u6761\u6570\uff0c\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u5b9a\u7684<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001\u67e5\u770bmodels\u4e2dMiddlePage\u51fd\u6570\uff0c\u6839\u636e\u9875\u6570\u83b7\u53d6\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"835\" height=\"217\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-3.png\" alt=\"\" class=\"wp-image-10643\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-3.png 835w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-3-300x78.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-3-768x200.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-3-600x156.png 600w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\uff0cPageNum*PageSize\u8868\u793aid\u9700\u8981\u8df3\u8fc7\u7684\u6570\u636e\uff0c\u6bd4\u5982\u4f20\u7684\u53c2\u6570PageNum\u4e3a1,\u90a3\u4e48\u901a\u8fc7util\u5305\u7684GetPage\u540e\u5b9e\u9645\u4f20\u9012\u5230\u540e\u53f0\u7684PageNum\u4e3a0\uff0c\u90a3\u4e48\u6b64\u65f6id &gt;0*10\uff0c\u83b7\u53d6\u7684\u6570\u636e\u5c31\u662f\u4eceid\u4e3a1\u5f00\u59cb\u7684\uff0c\u5f53PageNum\u4e3a2\uff0c\u5b9e\u9645\u4f20\u9012\u8fc7\u53bb\u7684PageNum\u4e3a1,\u6b64\u65f6id &gt; 1* 10\uff0c\u90a3\u4e48\u83b7\u53d6\u7684\u6570\u636e\u5c31\u662f\u4eceid\u4e3a11\u5f00\u59cb,Limit\u8868\u793a\u83b7\u53d6\u7684\u6570\u636e\u6761\u6570<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"450\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-4.png\" alt=\"\" class=\"wp-image-10645\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-4.png 712w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-4-300x190.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-4-600x379.png 600w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">c.Query\u548cc.Param\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c.Query\u7528\u6765\u83b7\u53d6GET\u8bf7\u6c42\u4e2d\u7684key\u7684\u503c\u5bf9\u5e94\u7684\u7ed3\u679c\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET \/path?id=1234&amp;name=Manu&amp;value=\r\n       c.Query(\"id\") == \"1234\"\r\n       c.Query(\"name\") == \"Manu\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">c.Param\u7528\u6765\u83b7\u53d6\u52a8\u6001\u53c2\u6570\u7684\u503c\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>router.GET(\"\/user\/:id\", func(c *gin.Context) {\r\n            \/\/ a GET request to \/user\/john\n\r            \/\/id\u8868\u793a\u52a8\u6001\u53c2\u6570\n            id := c.Param(\"id\") \/\/ id == \"john\"\r\n        })\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u53f0\u9875\u9762\u5728\u8c03\u7528\u540e\u53f0\u63a5\u53e3\u7684\u65f6\u5019\uff0c\u662f\u9700\u8981\u6839\u636e\u70b9\u51fb\u9875\u6570\u663e\u793a\u5bf9\u5e94\u6570\u636e\uff0c\u5982\u56fe\uff1a \u8bf4\u660e\uff1a\u524d\u53f0\u4f20\u9012\u7684\u662f\u9875\u6570\uff0c\u7b2c\u51e0\u9875\u7b2c\u51e0\u9875\u53c2\u6570 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":10629,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-10625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=10625"}],"version-history":[{"count":10,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10625\/revisions"}],"predecessor-version":[{"id":13338,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10625\/revisions\/13338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/10629"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=10625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=10625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=10625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}