{"id":120539,"date":"2026-10-31T22:41:07","date_gmt":"2026-10-31T14:41:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/120539.html"},"modified":"2026-10-31T22:41:07","modified_gmt":"2026-10-31T14:41:07","slug":"%e6%88%90%e4%b8%ba%e5%85%a8%e6%a0%88%c2%b7go-%e5%90%8e%e7%ab%af%e7%af%87%c2%b7%e6%96%87%e7%ab%a0%e6%9f%a5%e8%af%a2%e4%b8%8e%e5%b1%95%e7%a4%ba%e6%8a%95%e5%bd%b1%ef%bc%9a%e5%ae%89%e5%85%a8%e5%88%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/120539.html","title":{"rendered":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664"},"content":{"rendered":"<h2>\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71&#xff1a;\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664<\/h2>\n<p>\u5217\u8868\u63a5\u53e3\u4e0d\u53ea\u662f\u62fc\u51e0\u4e2a WHERE \u6761\u4ef6\u3002\u5b83\u8fd8\u8981\u9694\u79bb\u672a\u53d1\u5e03\u5185\u5bb9\u3001\u7a33\u5b9a\u6392\u5e8f\u3001\u9650\u5236\u91cd\u67e5\u8be2\u6210\u672c&#xff0c;\u5e76\u786e\u4fdd\u6458\u8981\u67e5\u8be2\u4e0d\u4f1a\u987a\u624b\u52a0\u8f7d\u6574\u7bc7\u6b63\u6587\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144101-6ae5fdfdec353.png\" alt=\"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664\" \/><\/p>\n<p>\u672c\u6587\u4ee3\u7801\u5feb\u7167&#xff1a;\u63d0\u4ea4 ceac4e0\u3002\u67e5\u8be2\u884c\u4e3a\u4ee5\u5951\u7ea6\u548c internal\/article\/query.go\u3001internal\/article\/view.go \u4e3a\u51c6\u3002<\/p>\n<h3>\u524d\u8a00&#xff1a;\u4e00\u6761\u5217\u8868 SQL \u5bf9\u5e94\u591a\u6761\u4ea7\u54c1\u89c4\u5219<\/h3>\n<p>\u6587\u7ae0\u5217\u8868\u7684\u67e5\u8be2\u6761\u4ef6&#xff0c;\u6211\u6700\u521d\u6563\u5728\u5404\u4e2a handler \u91cc\u3002\u5199\u5230\u7b2c\u4e09\u4e2a\u63a5\u53e3\u65f6\u53d1\u73b0\u81ea\u5df1\u5728\u91cd\u590d\u540c\u4e00\u6bb5&#034;\u53ea\u770b published&#034;\u7684\u5224\u65ad&#xff0c;\u800c\u4e14\u5df2\u7ecf\u6f0f\u6389\u4e86\u4e00\u4e2a\u8f6f\u5220\u9664\u5206\u652f\u3002<\/p>\n<p>\u5185\u5bb9\u7ad9\u6587\u7ae0\u5217\u8868\u4f1a\u652f\u6301\u5206\u9875\u3001\u5173\u952e\u8bcd\u3001\u5206\u7c7b\u3001\u6807\u7b7e\u3001\u4f5c\u8005\u3001\u72b6\u6001\u548c\u6392\u5e8f\u3002\u540e\u53f0\u9700\u8981\u770b\u5230\u4e0d\u540c\u72b6\u6001&#xff0c;\u516c\u5f00\u9875\u9762\u53ea\u80fd\u770b\u5230 published&#xff1b;\u6587\u7ae0\u8f6f\u5220\u9664\u540e\u4e0d\u80fd\u518d\u51fa\u73b0\u5728\u4efb\u4f55\u516c\u5f00\u5217\u8868&#xff1b;\u540c\u4e00\u65f6\u95f4\u6233\u7684\u6587\u7ae0\u8fd8\u8981\u6709\u7a33\u5b9a\u6b21\u5e8f\u3002\u5173\u952e\u8bcd\u7edf\u8ba1\u82e5\u65e0\u4e0a\u9650&#xff0c;\u53ef\u80fd\u8ba9\u4e00\u4e2a\u641c\u7d22\u8bf7\u6c42\u5bf9\u6574\u5f20\u5185\u5bb9\u8868\u505a\u6602\u8d35\u626b\u63cf\u3002<\/p>\n<p>M6 \u5c06\u5e38\u89c1\u6761\u4ef6\u7ec4\u5408\u653e\u5728 article.Page&#xff0c;\u56fa\u5b9a\u6392\u5e8f\u5b57\u6bb5\u901a\u8fc7\u767d\u540d\u5355\u6620\u5c04\u5230 SQL&#xff0c;\u8ffd\u52a0 ID \u4f5c\u4e3a\u6b21\u7ea7\u6392\u5e8f&#xff1b;\u6240\u6709\u6587\u7ae0\u67e5\u8be2\u4f7f\u7528\u663e\u5f0f ActiveArticles scope \u8fc7\u6ee4\u8f6f\u5220\u9664\u6570\u636e\u3002\u5217\u8868\u53ea\u9009\u62e9 SummaryColumns&#xff0c;\u4e0d\u5305\u542b Markdown \u5168\u6587\u3002\u8fd9\u6837&#xff0c;\u5b89\u5168\u6027\u3001\u53ef\u89c1\u6027\u548c\u4f20\u8f93\u6210\u672c\u90fd\u5728\u540c\u4e00\u67e5\u8be2\u8def\u5f84\u4e0a\u53ef\u8bfb\u3002<\/p>\n<p>\u672c\u6587\u5173\u6ce8\u5217\u8868\u3001\u8be6\u60c5\u548c\u5c55\u793a\u6295\u5f71\u4e4b\u95f4\u7684\u8fb9\u754c&#xff0c;\u4e5f\u8bf4\u660e\u4e3a\u4ec0\u4e48 ORM \u94fe\u5f0f\u67e5\u8be2\u4ecd\u9700\u8981\u719f\u6089 SQL\u3002<\/p>\n<h3>\u4e00\u3001\u5206\u9875\u53c2\u6570\u8981\u89c4\u8303\u5316&#xff0c;\u7a7a\u7ed3\u679c\u4e5f\u8981\u7a33\u5b9a<\/h3>\n<p>values.Paging \u89e3\u6790 page \u4e0e pageSize&#xff1a;\u9875\u7801\u81f3\u5c11\u4e3a 1&#xff0c;pageSize \u4f7f\u7528\u9ed8\u8ba4\u503c\u5e76\u9650\u5236\u6700\u5927\u503c&#xff0c;offset \u7531\u89c4\u8303\u5316\u9875\u7801\u8ba1\u7b97\u3002\u54cd\u5e94\u5305\u542b list \u4e0e pagination&#xff0c;\u7a7a\u5217\u8868\u7684 totalPages \u4fdd\u6301\u81f3\u5c11\u4e3a 1&#xff0c;\u4ee5\u5339\u914d\u51bb\u7ed3\u5951\u7ea6\u7684\u7ea6\u5b9a\u3002<\/p>\n<p>\u8fd9\u5957\u89c4\u8303\u96c6\u4e2d\u5728 internal\/values\/values.go \u7684\u4e00\u4e2a\u51fd\u6570\u91cc&#xff0c;\u9ed8\u8ba4 20\u3001\u4e0a\u9650 100&#xff0c;\u9875\u7801\u81f3\u5c11\u4e3a 1&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/values\/values.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u9ed8\u8ba4 20\u3001\u4e0a\u9650 100&#xff0c;\u9875\u7801\u81f3\u5c11\u4e3a 1\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">PagingWith<\/span><span class=\"token punctuation\">(<\/span>q url<span class=\"token punctuation\">.<\/span>Values<span class=\"token punctuation\">,<\/span> defaultSize<span class=\"token punctuation\">,<\/span> maxSize <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">)<\/span> Page <span class=\"token punctuation\">{<\/span><br \/>\np<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">_<\/span> <span class=\"token operator\">:&#061;<\/span> strconv<span class=\"token punctuation\">.<\/span><span class=\"token function\">Atoi<\/span><span class=\"token punctuation\">(<\/span>q<span class=\"token punctuation\">.<\/span><span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;page&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\ns<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">_<\/span> <span class=\"token operator\">:&#061;<\/span> strconv<span class=\"token punctuation\">.<\/span><span class=\"token function\">Atoi<\/span><span class=\"token punctuation\">(<\/span>q<span class=\"token punctuation\">.<\/span><span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;pageSize&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> p <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">1<\/span> <span class=\"token punctuation\">{<\/span><br \/>\np <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> s <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">{<\/span><br \/>\ns <span class=\"token operator\">&#061;<\/span> defaultSize<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> s <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">1<\/span> <span class=\"token punctuation\">{<\/span><br \/>\ns <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> s <span class=\"token operator\">&gt;<\/span> maxSize <span class=\"token punctuation\">{<\/span><br \/>\ns <span class=\"token operator\">&#061;<\/span> maxSize<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> Page<span class=\"token punctuation\">{<\/span>Page<span class=\"token punctuation\">:<\/span> p<span class=\"token punctuation\">,<\/span> Size<span class=\"token punctuation\">:<\/span> s<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ Offset \u8ba1\u7b97\u5df2\u89c4\u8303\u5316\u9875\u7801\u5bf9\u5e94\u7684\u884c\u504f\u79fb\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>p Page<span class=\"token punctuation\">)<\/span> <span class=\"token function\">Offset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token builtin\">int<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">.<\/span>Page <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> p<span class=\"token punctuation\">.<\/span>Size <span class=\"token punctuation\">}<\/span><\/p>\n<p>\u628a\u4e09\u4e2a\u8fb9\u754c\u6536\u5728\u4e00\u5904\u5f88\u91cd\u8981&#xff1a;\u9875\u7801\u4e0b\u9650\u3001\u6bcf\u9875\u5927\u5c0f\u7684\u9ed8\u8ba4\u503c\u4e0e\u4e0a\u9650\u3001\u4ee5\u53ca\u7531\u6b64\u63a8\u5bfc\u7684\u504f\u79fb\u3002\u56e0\u4e3a Limit \u548c Offset \u6700\u7ec8\u76f4\u63a5\u8fdb\u5165\u6570\u636e\u5e93&#xff0c;page \u4e0e pageSize \u5fc5\u987b\u5148\u7ecf\u8fc7\u8fd9\u4e00\u5c42&#xff0c;\u624d\u80fd\u4ea4\u7ed9 ORM\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u5206\u9875\u6570\u5b57\u4e0d\u662f\u539f\u6837\u8f6c\u53d1&#xff0c;\u800c\u662f\u5148\u88ab\u89c4\u8303\u5316\u6210\u4e00\u7ec4\u53d7\u9650\u503c\u3002<\/p>\n<p>\u5206\u9875\u6570\u503c\u6765\u81ea URL&#xff0c;\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u65e0\u9650 Limit \u6216 Offset\u3002\u8fc7\u5927\u7684\u9875\u7801\u4e5f\u53ef\u80fd\u5bfc\u81f4\u6574\u6570\u504f\u79fb\u6ea2\u51fa\u6216\u6570\u636e\u5e93\u6267\u884c\u4ee3\u4ef7\u8fc7\u9ad8&#xff1b;\u76f8\u5173 helper \u8981\u5bf9\u6700\u5927\u8303\u56f4\u4f5c\u9632\u62a4\u3002\u4e0d\u540c\u63a5\u53e3\u6709\u81ea\u5df1\u7684\u9ed8\u8ba4\u503c\u548c\u4e0a\u9650\u65f6\u4f7f\u7528 PagingWith&#xff0c;\u4e0d\u8981\u4e3a\u4e86\u590d\u7528\u628a\u5951\u7ea6\u5dee\u5f02\u62b9\u6389\u3002<\/p>\n<p>\u7a7a\u7ed3\u679c\u7684 JSON \u5f62\u72b6\u4e5f\u6709\u610f\u4e49\u3002\u8fd4\u56de [] \u548c null \u5bf9\u5ba2\u6237\u7aef\u4e0d\u662f\u540c\u4e00\u79cd\u72b6\u6001\u3002Go \u8981\u521d\u59cb\u5316\u7a7a\u5207\u7247&#xff0c;\u4ee5\u514d nil slice \u88ab\u5e8f\u5217\u5316\u4e3a null\u3002Page.Result \u5728\u7ec4\u88c5\u5206\u9875\u5143\u6570\u636e\u65f6\u628a totalPages \u62ac\u5230\u81f3\u5c11 1&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/values\/values.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u7a7a\u96c6\u5408\u7684\u603b\u9875\u6570\u4ecd\u4e3a 1\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token punctuation\">(<\/span>p Page<span class=\"token punctuation\">)<\/span> <span class=\"token function\">Result<\/span><span class=\"token punctuation\">(<\/span>list any<span class=\"token punctuation\">,<\/span> total <span class=\"token builtin\">int64<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span>any <span class=\"token punctuation\">{<\/span><br \/>\np<span class=\"token punctuation\">.<\/span>Total <span class=\"token operator\">&#061;<\/span> total<br \/>\np<span class=\"token punctuation\">.<\/span>Pages <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>total <span class=\"token operator\">&#043;<\/span> <span class=\"token function\">int64<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">.<\/span>Size<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">\/<\/span> <span class=\"token function\">int64<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">.<\/span>Size<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> p<span class=\"token punctuation\">.<\/span>Pages <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">1<\/span> <span class=\"token punctuation\">{<\/span><br \/>\np<span class=\"token punctuation\">.<\/span>Pages <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span>any<span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;list&#034;<\/span><span class=\"token punctuation\">:<\/span> list<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;pagination&#034;<\/span><span class=\"token punctuation\">:<\/span> p<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>Result \u662f\u5217\u8868\u4e0e\u5206\u9875\u5143\u6570\u636e\u7684\u552f\u4e00\u51fa\u53e3\u3002\u5206\u9875\u63a5\u53e3\u53ea\u8981\u8d70\u5b83&#xff0c;\u5c31\u4e0d\u4f1a\u51fa\u73b0 totalPages \u4e3a 0 \u6216 list \u4e3a null \u7684\u5f62\u72b6\u6f02\u79fb&#xff1b;\u53cd\u8fc7\u6765&#xff0c;\u7ed5\u8fc7\u5b83\u81ea\u5df1\u62fc map&#xff0c;\u5c31\u5f88\u53ef\u80fd\u5fd8\u8bb0\u7a7a\u96c6\u5408\u8fd9\u4e00\u683c\u3002M6 \u5728\u5206\u7c7b\u3001\u8bc4\u8bba\u548c\u6587\u7ae0\u5217\u8868 mapper \u4e2d\u4fdd\u6301\u540c\u6837\u7684\u7a7a\u6570\u7ec4\u8bed\u4e49\u3002<\/p>\n<h3>\u4e8c\u3001\u6392\u5e8f\u5b57\u6bb5\u5fc5\u987b\u7531\u670d\u52a1\u7aef\u767d\u540d\u5355\u63a7\u5236<\/h3>\n<p>\u8bf7\u6c42\u53ef\u4ee5\u9009\u62e9 publishedAt\u3001createdAt\u3001viewCount \u7b49\u652f\u6301\u7684\u6392\u5e8f\u952e&#xff0c;\u5e76\u7528\u524d\u7f00\u8868\u8fbe\u65b9\u5411\u3002Sort \u4f1a\u5c06\u952e\u6620\u5c04\u5230\u56fa\u5b9a SQL \u8868\u8fbe\u5f0f&#xff0c;\u672a\u77e5\u9879\u56de\u9000\u5230\u9ed8\u8ba4\u6392\u5e8f&#xff0c;\u4e0d\u8ba9\u7528\u6237\u8f93\u5165\u5217\u540d\u76f4\u63a5\u8fdb\u5165 ORDER BY\u3002\u6240\u6709\u6392\u5e8f\u6700\u540e\u8ffd\u52a0 article ID&#xff0c;\u7ed9\u65f6\u95f4\u6216\u8ba1\u6570\u76f8\u540c\u7684\u8bb0\u5f55\u5efa\u7acb\u7a33\u5b9a\u6b21\u5e8f\u3002<\/p>\n<p>\u767d\u540d\u5355\u662f\u4e00\u5f20\u5305\u5185\u53ea\u8bfb\u7684\u6620\u5c04\u8868&#xff0c;Sort \u53ea\u5728\u8fd9\u5f20\u8868\u91cc\u67e5\u503c&#xff0c;\u67e5\u4e0d\u5230\u5c31\u56de\u9000&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/query.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u5305\u5185\u53ea\u8bfb\u6392\u5e8f\u767d\u540d\u5355\u3002<\/span><br \/>\n<span class=\"token keyword\">var<\/span> sortColumns <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token string\">&#034;publishedAt&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;COALESCE(articles.published_at,articles.created_at)&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token string\">&#034;createdAt&#034;<\/span><span class=\"token punctuation\">:<\/span>   <span class=\"token string\">&#034;articles.created_at&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token string\">&#034;viewCount&#034;<\/span><span class=\"token punctuation\">:<\/span>   <span class=\"token string\">&#034;articles.view_count&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ Sort \u5c06\u5141\u8bb8\u7684\u6392\u5e8f\u5b57\u6bb5\u6620\u5c04\u4e3a\u56fa\u5b9a SQL&#xff0c;\u5e76\u8ffd\u52a0 ID \u4fdd\u8bc1\u987a\u5e8f\u7a33\u5b9a\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">Sort<\/span><span class=\"token punctuation\">(<\/span>raw <span class=\"token builtin\">string<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token builtin\">string<\/span> <span class=\"token punctuation\">{<\/span><br \/>\ndesc <span class=\"token operator\">:&#061;<\/span> strings<span class=\"token punctuation\">.<\/span><span class=\"token function\">HasPrefix<\/span><span class=\"token punctuation\">(<\/span>raw<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;-&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\nfield <span class=\"token operator\">:&#061;<\/span> strings<span class=\"token punctuation\">.<\/span><span class=\"token function\">TrimPrefix<\/span><span class=\"token punctuation\">(<\/span>raw<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;-&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>col<span class=\"token punctuation\">,<\/span> ok <span class=\"token operator\">:&#061;<\/span> sortColumns<span class=\"token punctuation\">[<\/span>field<span class=\"token punctuation\">]<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token operator\">!<\/span>ok <span class=\"token punctuation\">{<\/span><br \/>\ncol <span class=\"token operator\">&#061;<\/span> sortColumns<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;publishedAt&#034;<\/span><span class=\"token punctuation\">]<\/span><br \/>\ndesc <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\ndir <span class=\"token operator\">:&#061;<\/span> <span class=\"token string\">&#034;ASC&#034;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> desc <span class=\"token punctuation\">{<\/span><br \/>\ndir <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;DESC&#034;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> col <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034; &#034;<\/span> <span class=\"token operator\">&#043;<\/span> dir <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034;, articles.id DESC&#034;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8bf7\u6c42\u8f93\u5165\u4e0e\u6700\u7ec8 SQL \u7684\u5bf9\u5e94\u5173\u7cfb\u53ef\u4ee5\u5217\u6210\u4e00\u5f20\u8868&#xff1a;<\/p>\n<table>\n<tr>\u8bf7\u6c42 sort\u547d\u4e2d SQL \u8868\u8fbe\u5f0f\u6392\u5e8f\u65b9\u5411<\/tr>\n<tbody>\n<tr>\n<td>publishedAt<\/td>\n<td>COALESCE(articles.published_at,articles.created_at)<\/td>\n<td>ASC<\/td>\n<\/tr>\n<tr>\n<td>-publishedAt<\/td>\n<td>\u540c\u4e0a<\/td>\n<td>DESC<\/td>\n<\/tr>\n<tr>\n<td>viewCount<\/td>\n<td>articles.view_count<\/td>\n<td>ASC<\/td>\n<\/tr>\n<tr>\n<td>\u672a\u77e5\u5b57\u6bb5<\/td>\n<td>\u56de\u9000 publishedAt<\/td>\n<td>DESC<\/td>\n<\/tr>\n<tr>\n<td>\u4efb\u610f\u5408\u6cd5\u503c<\/td>\n<td>\u7edf\u4e00\u8ffd\u52a0 , articles.id DESC<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7a33\u5b9a\u6b21\u5e8f\u4e0d\u662f\u89c6\u89c9\u7ec6\u8282\u3002\u504f\u79fb\u5206\u9875\u4f9d\u8d56\u540c\u4e00\u96c6\u5408\u6709\u786e\u5b9a\u6392\u5e8f&#xff1b;\u82e5\u8fb9\u754c\u884c\u6392\u5e8f\u76f8\u540c\u4e14\u6570\u636e\u5e93\u81ea\u7531\u6392\u5217&#xff0c;\u4e0b\u4e00\u9875\u53ef\u80fd\u91cd\u590d\u6216\u6f0f\u6389\u6587\u7ae0\u3002\u5373\u4f7f\u6709 ID \u6b21\u7ea7\u6392\u5e8f&#xff0c;\u5217\u8868\u671f\u95f4\u53d1\u751f\u65b0\u6587\u7ae0\u63d2\u5165\u4ecd\u53ef\u80fd\u6539\u53d8 offset \u4f4d\u7f6e&#xff1b;\u82e5\u4ea7\u54c1\u9700\u8981\u5f3a\u4e00\u81f4\u5feb\u7167&#xff0c;\u5e94\u8003\u8651\u6e38\u6807\u5206\u9875\u6216\u5feb\u7167\u7248\u672c&#xff0c;\u8fd9\u8d85\u51fa\u5f53\u524d\u5951\u7ea6\u3002<\/p>\n<p>SQL \u53c2\u6570\u5316\u4fdd\u62a4\u7684\u662f\u503c&#xff0c;\u4e0d\u80fd\u628a\u4efb\u610f\u5b57\u6bb5\u540d\u53d8\u6210\u5b89\u5168\u8868\u8fbe\u5f0f\u3002\u6392\u5e8f\u65b9\u5411\u3001\u8868\u540d\u548c SQL fragment \u90fd\u5c5e\u4e8e\u7ed3\u6784&#xff0c;\u4e0d\u5e94\u76f4\u63a5\u62fc\u5ba2\u6237\u7aef\u8f93\u5165\u3002\u8fd9\u91cc map \u4e2d\u7684\u8868\u8fbe\u5f0f\u56fa\u5b9a\u5728\u6e90\u4ee3\u7801&#xff0c;\u7528\u6237\u53ea\u80fd\u9009\u62e9\u5141\u8bb8\u9879\u2014\u2014\u5373\u4f7f\u8bf7\u6c42\u4f20\u6765\u4e00\u4e2a\u5f62\u5982 articles.id; DROP TABLE \u7684\u5b57\u7b26\u4e32&#xff0c;\u5b83\u4e5f\u53ea\u662f\u5728 map \u91cc\u67e5\u4e0d\u5230&#xff0c;\u4e8e\u662f\u56de\u9000\u9ed8\u8ba4\u503c&#xff0c;\u800c\u4e0d\u662f\u8fdb\u5165 SQL\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144103-6ae5fdff4f747.png\" alt=\"\u6392\u5e8f\u767d\u540d\u5355\" \/><\/p>\n<h3>\u4e09\u3001\u53ef\u89c1\u6027\u548c\u8f6f\u5220\u9664\u5fc5\u987b\u8fdb\u5165\u6bcf\u6761\u67e5\u8be2\u94fe<\/h3>\n<p>ActiveArticles \u662f\u663e\u5f0f scope&#xff0c;\u8fc7\u6ee4 deleted_at\u3002\u516c\u5f00\u9875\u9762\u8fd8\u8981\u8981\u6c42 status&#061;published&#xff1b;\u672a\u53d1\u5e03\u6587\u7ae0\u5bf9\u533f\u540d\u7edf\u4e00\u89c6\u4e3a\u4e0d\u5b58\u5728&#xff0c;\u8fd4\u56de 404&#xff0c;\u800c\u4e0d\u662f\u6cc4\u6f0f\u201c\u5b58\u5728\u4e00\u7bc7\u8349\u7a3f\u201d\u3002\u6587\u7ae0\u4f5c\u8005\u6216\u7ba1\u7406\u5458\u5bf9\u672a\u53d1\u5e03\u6587\u7ae0\u7684\u9884\u89c8\u5c5e\u4e8e\u53e6\u5916\u7684\u6388\u6743\u8def\u5f84\u3002<\/p>\n<p>\u8f6f\u5220\u9664\u8c13\u8bcd\u53ea\u6709\u4e00\u53e5&#xff0c;\u4f46\u5b83\u5fc5\u987b\u51fa\u73b0\u5728\u6bcf\u4e00\u6761\u6587\u7ae0\u67e5\u8be2\u4e0a&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/platform\/database\/scopes.go&#xff1a;\u8f6f\u5220\u9664\u7684\u552f\u4e00\u8c13\u8bcd\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u56fa\u5b9a articles \u8868\u540d\u907f\u514d JOIN \u65f6\u5217\u540d\u6b67\u4e49\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">ActiveArticles<\/span><span class=\"token punctuation\">(<\/span>db <span class=\"token operator\">*<\/span>gorm<span class=\"token punctuation\">.<\/span>DB<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span>gorm<span class=\"token punctuation\">.<\/span>DB <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> db<span class=\"token punctuation\">.<\/span><span class=\"token function\">Where<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;articles.deleted_at IS NULL&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u516c\u5f00\u8be6\u60c5\u5219\u5728\u6b64\u57fa\u7840\u4e0a\u518d\u53e0\u4e00\u5c42\u72b6\u6001\u5224\u65ad&#xff1a;\u672a\u53d1\u5e03\u4e0e\u4e0d\u5b58\u5728\u5bf9\u533f\u540d\u8fd4\u56de\u540c\u4e00\u4e2a\u9519\u8bef&#xff0c;\u907f\u514d\u7528\u72b6\u6001\u7801\u533a\u5206\u51fa\u201c\u8fd9\u91cc\u5176\u5b9e\u6709\u4e00\u7bc7\u7a3f\u5b50\u201d&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/query.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u672a\u53d1\u5e03\u72b6\u6001\u4e0e\u4e0d\u5b58\u5728\u7edf\u4e00\u8fd4\u56de 404\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">Published<\/span><span class=\"token punctuation\">(<\/span>db <span class=\"token operator\">*<\/span>gorm<span class=\"token punctuation\">.<\/span>DB<span class=\"token punctuation\">,<\/span> id <span class=\"token builtin\">int64<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">(<\/span>model<span class=\"token punctuation\">.<\/span>Article<span class=\"token punctuation\">,<\/span> <span class=\"token builtin\">error<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\na<span class=\"token punctuation\">,<\/span> err <span class=\"token operator\">:&#061;<\/span> <span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span>db<span class=\"token punctuation\">,<\/span> strconv<span class=\"token punctuation\">.<\/span><span class=\"token function\">FormatInt<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> err <span class=\"token operator\">!&#061;<\/span> <span class=\"token boolean\">nil<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> a<span class=\"token punctuation\">,<\/span> err<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> a<span class=\"token punctuation\">.<\/span>Status <span class=\"token operator\">!&#061;<\/span> <span class=\"token string\">&#034;published&#034;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> a<span class=\"token punctuation\">,<\/span> fault<span class=\"token punctuation\">.<\/span><span class=\"token function\">New<\/span><span class=\"token punctuation\">(<\/span>fault<span class=\"token punctuation\">.<\/span>NotFound<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> a<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">nil<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e0d\u8981\u53ea\u5728\u4e00\u4e2a\u9996\u9875 handler \u52a0 status \u6761\u4ef6\u3002\u641c\u7d22\u3001\u76f8\u5173\u5185\u5bb9\u3001\u76f8\u90bb\u6587\u7ae0\u3001\u6807\u7b7e\u5217\u8868\u3001\u4f5c\u8005\u6587\u7ae0\u548c\u8ba1\u6570\u805a\u5408\u90fd\u5fc5\u987b\u4f7f\u7528\u76f8\u540c\u7684\u53ef\u89c1\u6027\u89c4\u5219\u3002\u9057\u6f0f\u4e00\u4e2a\u805a\u5408\u63a5\u53e3&#xff0c;\u53ef\u80fd\u66b4\u9732\u79c1\u7a3f\u6807\u9898\u3001\u4f5c\u8005\u6216\u6570\u91cf\u3002scope \u80fd\u964d\u4f4e\u91cd\u590d&#xff0c;\u4f46\u6bcf\u4e2a\u67e5\u8be2\u4ecd\u8981\u5ba1\u67e5 join\u3001\u5b50\u67e5\u8be2\u4e0e\u7edf\u8ba1\u8def\u5f84\u662f\u5426\u4fdd\u7559\u6761\u4ef6\u3002<\/p>\n<p>\u8f6f\u5220\u9664\u5bf9\u67e5\u8be2\u7684\u9690\u85cf\u9700\u8981\u660e\u786e\u5e94\u7528\u3002GORM model \u4f7f\u7528\u81ea\u5b9a\u4e49\u5220\u9664\u65f6\u95f4\u800c\u975e gorm.Model \u81ea\u52a8\u89c4\u5219&#xff0c;\u56e0\u6b64\u6570\u636e\u5e93 scope \u662f\u53ef\u89c1\u6027\u8fb9\u754c\u3002\u5199\u67e5\u8be2\u65f6\u82e5\u4f7f\u7528 raw table \u6216\u624b\u5199 JOIN&#xff0c;\u8981\u786e\u8ba4 scope \u771f\u7684\u5e94\u7528\u4e8e\u76ee\u6807 articles \u8868&#xff0c;\u800c\u4e0d\u53ea\u662f\u4e3b\u67e5\u8be2\u522b\u540d\u2014\u2014ActiveArticles \u5199\u6b7b articles. \u524d\u7f00\u6b63\u662f\u4e3a\u6b64\u3002<\/p>\n<h3>\u56db\u3001\u5206\u7c7b\u3001\u6807\u7b7e\u4e0e\u5173\u952e\u8bcd\u6761\u4ef6<\/h3>\n<p>\u6807\u7b7e\u7b5b\u9009\u901a\u8fc7 EXISTS \u5b50\u67e5\u8be2\u5173\u8054 article_tags \u4e0e tags&#xff0c;\u5e76\u4ee5\u6807\u7b7e slug \u5339\u914d\u3002\u4f7f\u7528 EXISTS \u800c\u4e0d\u662f\u76f4\u63a5 JOIN \u53ef\u4ee5\u907f\u514d\u4e00\u7bc7\u6587\u7ae0\u56e0\u591a\u6761\u5173\u7cfb\u5728\u5206\u9875\u7ed3\u679c\u4e2d\u91cd\u590d\u3002\u5206\u7c7b\u7b5b\u9009\u8bfb\u53d6\u5206\u7c7b\u76ee\u5f55&#xff0c;\u4ece\u6307\u5b9a slug \u5f00\u59cb\u6269\u5c55\u5230\u6700\u591a\u56db\u5c42\u5b50\u5206\u7c7b&#xff0c;\u518d\u4ee5\u6587\u7ae0\u5197\u4f59 category_slug \u96c6\u5408\u8fc7\u6ee4\u3002\u5206\u7c7b\u6811\u6700\u5927\u5c42\u7ea7\u6765\u81ea\u5951\u7ea6&#xff0c;\u4e0d\u662f\u5047\u8bbe\u4efb\u610f\u65e0\u9650\u9012\u5f52\u3002<\/p>\n<p>\u5173\u952e\u8bcd\u641c\u7d22\u68c0\u67e5\u6807\u9898\u548c\u6458\u8981&#xff0c;\u4e3a\u9650\u5236\u603b\u6570\u7edf\u8ba1\u5f00\u9500&#xff0c;\u641c\u7d22 count \u53ea\u5bf9\u6700\u591a 2000 \u6761\u5339\u914d ID \u505a\u8ba1\u6570&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/query.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u6807\u7b7e EXISTS \u4e0e\u5173\u952e\u8bcd LIKE \u7684\u6761\u4ef6\u6784\u9020\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> tag <span class=\"token operator\">:&#061;<\/span> q<span class=\"token punctuation\">.<\/span><span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;tag&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> tag <span class=\"token operator\">!&#061;<\/span> <span class=\"token string\">&#034;&#034;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nfilter <span class=\"token operator\">&#061;<\/span> filter<span class=\"token punctuation\">.<\/span><span class=\"token function\">Where<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;EXISTS (SELECT 1 FROM article_tags atg JOIN tags tg ON tg.id&#061;atg.tag_id WHERE atg.article_id&#061;articles.id AND tg.slug &#061; ?)&#034;<\/span><span class=\"token punctuation\">,<\/span> tag<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> kw <span class=\"token operator\">:&#061;<\/span> q<span class=\"token punctuation\">.<\/span><span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;keyword&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> kw <span class=\"token operator\">!&#061;<\/span> <span class=\"token string\">&#034;&#034;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nfilter <span class=\"token operator\">&#061;<\/span> filter<span class=\"token punctuation\">.<\/span><span class=\"token function\">Where<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;(LOWER(articles.title) LIKE LOWER(?) OR LOWER(articles.summary) LIKE LOWER(?))&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;%&#034;<\/span><span class=\"token operator\">&#043;<\/span>kw<span class=\"token operator\">&#043;<\/span><span class=\"token string\">&#034;%&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;%&#034;<\/span><span class=\"token operator\">&#043;<\/span>kw<span class=\"token operator\">&#043;<\/span><span class=\"token string\">&#034;%&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u641c\u7d22\u7684\u8ba1\u6570\u5355\u72ec\u52a0\u62a4\u680f&#xff0c;\u53ea\u5728\u4e00\u6bb5\u6709\u754c ID \u4e0a\u505a\u805a\u5408&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/query.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u5173\u952e\u8bcd count \u9650\u5236\u5728 2000 \u6761\u4ee5\u5185\u3002<\/span><br \/>\ncountQuery <span class=\"token operator\">:&#061;<\/span> filter<span class=\"token punctuation\">.<\/span><span class=\"token function\">Session<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>gorm<span class=\"token punctuation\">.<\/span>Session<span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> q<span class=\"token punctuation\">.<\/span><span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;keyword&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">!&#061;<\/span> <span class=\"token string\">&#034;&#034;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nbounded <span class=\"token operator\">:&#061;<\/span> countQuery<span class=\"token punctuation\">.<\/span><span class=\"token function\">Select<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;articles.id&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">Limit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2000<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> err <span class=\"token operator\">:&#061;<\/span> db<span class=\"token punctuation\">.<\/span><span class=\"token function\">Table<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;(?) AS bounded_articles&#034;<\/span><span class=\"token punctuation\">,<\/span> bounded<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">Count<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>total<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Error<span class=\"token punctuation\">;<\/span> err <span class=\"token operator\">!&#061;<\/span> <span class=\"token boolean\">nil<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token boolean\">nil<\/span><span class=\"token punctuation\">,<\/span> err<br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> err <span class=\"token operator\">:&#061;<\/span> countQuery<span class=\"token punctuation\">.<\/span><span class=\"token function\">Count<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>total<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Error<span class=\"token punctuation\">;<\/span> err <span class=\"token operator\">!&#061;<\/span> <span class=\"token boolean\">nil<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token boolean\">nil<\/span><span class=\"token punctuation\">,<\/span> err<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd9\u4e2a\u62a4\u680f\u610f\u5473\u7740\u5206\u9875 total \u662f\u6709\u754c\u8ba1\u6570\u7b56\u7565\u7684\u7ed3\u679c&#xff0c;\u63a5\u53e3\u8bed\u4e49\u548c\u6d4b\u8bd5\u9700\u8981\u4e0e\u5951\u7ea6\u4fdd\u6301\u4e00\u81f4\u3002\u5168\u6587\u5185\u5bb9\u641c\u7d22\u662f\u72ec\u7acb discovery \u641c\u7d22&#xff0c;\u4e0d\u5e94\u548c\u666e\u901a\u5217\u8868\u5173\u952e\u8bcd\u8fc7\u6ee4\u6df7\u4e3a\u4e00\u8c08\u3002<\/p>\n<p>\u5206\u7c7b\u6269\u5c55\u7684\u5faa\u73af\u4e5f\u503c\u5f97\u770b\u6e05\u695a&#xff1a;\u5b83\u4ece\u547d\u4e2d\u7684\u5206\u7c7b\u51fa\u53d1&#xff0c;\u6700\u591a\u518d\u5411\u4e0b\u56db\u5c42&#xff0c;\u628a\u5b50\u5b59 slug \u6536\u8fdb\u4e00\u4e2a\u96c6\u5408&#xff0c;\u6700\u540e\u7528 IN \u8fc7\u6ee4&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/query.go&#xff08;\u8282\u9009&#xff09;&#xff1a;\u5206\u7c7b\u4ece\u547d\u4e2d slug \u5411\u4e0b\u6269\u5c55\u6700\u591a\u56db\u5c42\u3002<\/span><br \/>\nslugs <span class=\"token operator\">:&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\nids <span class=\"token operator\">:&#061;<\/span> <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">int64<\/span><span class=\"token punctuation\">]<\/span><span class=\"token builtin\">bool<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token boolean\">_<\/span><span class=\"token punctuation\">,<\/span> c <span class=\"token operator\">:&#061;<\/span> <span class=\"token keyword\">range<\/span> all <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> c<span class=\"token punctuation\">.<\/span>Slug <span class=\"token operator\">&#061;&#061;<\/span> cat <span class=\"token punctuation\">{<\/span><br \/>\nids<span class=\"token punctuation\">[<\/span>c<span class=\"token punctuation\">.<\/span>ID<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><br \/>\nslugs <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>slugs<span class=\"token punctuation\">,<\/span> c<span class=\"token punctuation\">.<\/span>Slug<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">for<\/span> d <span class=\"token operator\">:&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> d <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">4<\/span><span class=\"token punctuation\">;<\/span> d<span class=\"token operator\">&#043;&#043;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token boolean\">_<\/span><span class=\"token punctuation\">,<\/span> c <span class=\"token operator\">:&#061;<\/span> <span class=\"token keyword\">range<\/span> all <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> c<span class=\"token punctuation\">.<\/span>ParentID <span class=\"token operator\">!&#061;<\/span> <span class=\"token boolean\">nil<\/span> <span class=\"token operator\">&amp;&amp;<\/span> ids<span class=\"token punctuation\">[<\/span><span class=\"token operator\">*<\/span>c<span class=\"token punctuation\">.<\/span>ParentID<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token operator\">!<\/span>ids<span class=\"token punctuation\">[<\/span>c<span class=\"token punctuation\">.<\/span>ID<span class=\"token punctuation\">]<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nids<span class=\"token punctuation\">[<\/span>c<span class=\"token punctuation\">.<\/span>ID<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><br \/>\nslugs <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>slugs<span class=\"token punctuation\">,<\/span> c<span class=\"token punctuation\">.<\/span>Slug<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u67e5\u8be2\u94fe\u5f0f\u7ec4\u5408\u65f6&#xff0c;\u8981\u5173\u6ce8 count \u4e0e list \u662f\u5426\u5e94\u7528\u4e86\u5b8c\u5168\u76f8\u540c\u7684\u7b5b\u9009\u6761\u4ef6\u3002\u82e5 count \u6f0f\u4e86 tag \u6761\u4ef6&#xff0c;\u524d\u7aef\u4f1a\u770b\u5230\u9519\u8bef\u603b\u6570&#xff1b;\u82e5 list \u6f0f status&#xff0c;\u672a\u53d1\u5e03\u5185\u5bb9\u5c31\u53ef\u80fd\u6cc4\u6f0f\u3002\u5bf9\u6bcf\u4e2a\u7ec4\u5408\u81f3\u5c11\u505a\u4e00\u7ec4\u4e0d\u540c\u6761\u4ef6\u80fd\u533a\u5206\u7ed3\u679c\u7684 fixture\u3002<\/p>\n<h3>\u4e94\u3001SummaryColumns \u662f\u6027\u80fd\u4e0e\u9690\u79c1\u6295\u5f71<\/h3>\n<p>Article \u6301\u4e45\u5316\u6a21\u578b\u5305\u542b content \u6b63\u6587&#xff0c;\u800c\u6458\u8981\u5361\u7247\u901a\u5e38\u53ea\u9700\u8981\u6709\u9650\u5b57\u6bb5\u3002SummaryColumns \u660e\u786e\u9009\u62e9\u5217\u8868\u5c55\u793a\u6240\u9700\u5217&#xff0c;\u51cf\u5c11\u6570\u636e\u5e93\u8bfb\u5165\u3001Go \u5185\u5b58\u6301\u6709\u548c JSON \u7ec4\u88c5\u8d1f\u62c5\u3002\u5927\u6b63\u6587\u4f1a\u5f71\u54cd\u6570\u636e\u5e93 I\/O\u3001\u8fde\u63a5\u5360\u7528\u548c\u5783\u573e\u56de\u6536&#xff1b;\u628a\u6587\u7ae0\u5217\u8868\u4e5f\u67e5\u5168\u91cf\u5185\u5bb9\u4e0d\u4ec5\u6d6a\u8d39\u8d44\u6e90&#xff0c;\u8fd8\u53ef\u80fd\u65e0\u610f\u628a\u5185\u90e8\u5185\u5bb9\u5e26\u5165 response mapper\u3002<\/p>\n<p>\u5217\u8868\u5217\u767d\u540d\u5355\u5199\u5728 internal\/article\/view.go \u7684\u5e38\u91cf\u91cc&#xff0c;\u6309\u884c\u6392\u7248\u4ee5\u4fbf\u548c DTO \u5b57\u6bb5\u9010\u9879\u5bf9\u7167&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/view.go&#xff1a;SummaryColumns \u96c6\u4e2d\u4fdd\u7559\u5217\u8868\u5217\u767d\u540d\u5355\u3002<\/span><br \/>\n<span class=\"token keyword\">const<\/span> SummaryColumns <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#096;<br \/>\narticles.id,<br \/>\narticles.title,<br \/>\narticles.slug,<br \/>\narticles.summary,<br \/>\narticles.cover_image,<br \/>\narticles.author_id,<br \/>\narticles.author_name,<br \/>\narticles.category_id,<br \/>\narticles.category_name,<br \/>\narticles.tags,<br \/>\narticles.status,<br \/>\narticles.view_count,<br \/>\narticles.like_count,<br \/>\narticles.published_at,<br \/>\narticles.created_at,<br \/>\narticles.updated_at&#096;<\/span><\/p>\n<p>\u5217\u8868 DTO \u4e0e\u8be6\u60c5 DTO \u4e5f\u53ef\u8868\u8fbe\u4e0d\u540c\u53ef\u89c1\u8303\u56f4\u3002Summary \u6620\u5c04\u4f5c\u8005\u6635\u79f0\u3001\u5206\u7c7b\u5c55\u793a\u5b57\u6bb5\u548c\u7edf\u8ba1\u8ba1\u6570&#xff1b;Detail \u5728\u9700\u8981\u65f6\u624d\u5305\u542b\u6b63\u6587\u53ca\u5176\u5b83\u5b57\u6bb5&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ internal\/article\/view.go&#xff08;\u8282\u9009&#xff09;&#xff1a;Detail \u53ea\u5728 Summary \u4e4b\u4e0a\u52a0\u6b63\u6587\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">Detail<\/span><span class=\"token punctuation\">(<\/span>a model<span class=\"token punctuation\">.<\/span>Article<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span>any <span class=\"token punctuation\">{<\/span> r <span class=\"token operator\">:&#061;<\/span> <span class=\"token function\">Summary<\/span><span class=\"token punctuation\">(<\/span>a<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> r<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;content&#034;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> a<span class=\"token punctuation\">.<\/span>Content<span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">return<\/span> r <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ List \u8f6c\u6362\u6587\u7ae0\u884c\u5217\u8868&#xff0c;\u7a7a\u7ed3\u679c\u4e5f\u4fdd\u6301 JSON \u6570\u7ec4\u8bed\u4e49\u3002<\/span><br \/>\n<span class=\"token keyword\">func<\/span> <span class=\"token function\">List<\/span><span class=\"token punctuation\">(<\/span>rows <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span>model<span class=\"token punctuation\">.<\/span>Article<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span>any <span class=\"token punctuation\">{<\/span><br \/>\nr <span class=\"token operator\">:&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token keyword\">map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token builtin\">string<\/span><span class=\"token punctuation\">]<\/span>any<span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token boolean\">_<\/span><span class=\"token punctuation\">,<\/span> a <span class=\"token operator\">:&#061;<\/span> <span class=\"token keyword\">range<\/span> rows <span class=\"token punctuation\">{<\/span><br \/>\nr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">append<\/span><span class=\"token punctuation\">(<\/span>r<span class=\"token punctuation\">,<\/span> <span class=\"token function\">Summary<\/span><span class=\"token punctuation\">(<\/span>a<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">return<\/span> r<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e24\u4e2a\u6295\u5f71\u7684\u5b57\u6bb5\u5dee\u53ef\u4ee5\u5217\u6210\u4e00\u5f20\u8868&#xff1a;<\/p>\n<table>\n<tr>\u5b57\u6bb5\u7ec4Summary&#xff08;\u5217\u8868&#xff09;Detail&#xff08;\u8be6\u60c5&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u6807\u8bc6 id \/ slug \/ status<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u5c55\u793a title \/ summary \/ coverImage<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u5197\u4f59 authorName \/ categoryName \/ tags<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u8ba1\u6570 viewCount \/ likeCount<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u65f6\u95f4 publishedAt \/ createdAt \/ updatedAt<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u6b63\u6587 content<\/td>\n<td>\u65e0<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u663e\u5f0f mapper \u907f\u514d GORM model \u81ea\u52a8\u5e8f\u5217\u5316\u628a\u65b0\u52a0\u5217\u9ed8\u9ed8\u516c\u5f00\u3002\u5c55\u793a\u5b57\u6bb5\u5982 category_name\u3001author_name \u662f\u5197\u4f59\u6295\u5f71&#xff0c;\u5b83\u4eec\u8ba9\u5217\u8868\u5728\u5e38\u89c1\u573a\u666f\u5c11\u505a join\u3002\u5197\u4f59\u9700\u8981\u5728\u5199\u5165\u65f6\u540c\u6b65&#xff1a;\u4fee\u6539\u6587\u7ae0\u5206\u7c7b\u65f6\u66f4\u65b0 category_id\/name\/slug&#xff1b;\u6539\u540d\u5206\u7c7b\u65f6\u9700\u8981\u540c\u6b65\u88ab\u5f15\u7528\u6587\u7ae0\u6295\u5f71\u3002\u771f\u5b9e\u5916\u952e\u5173\u7cfb\u548c\u5197\u4f59\u6587\u672c\u627f\u62c5\u4e0d\u540c\u4efb\u52a1&#xff0c;\u4e0d\u80fd\u628a\u4e00\u4e2a\u5f53\u4f5c\u53e6\u4e00\u4e2a\u7684\u552f\u4e00\u6765\u6e90\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144104-6ae5fe003a075.png\" alt=\"\u6295\u5f71\" \/><\/p>\n<h3>\u516d\u3001\u6392\u5e8f\u548c\u7b5b\u9009\u4ecd\u7136\u8981\u9762\u5bf9\u6570\u636e\u5e93\u5dee\u5f02<\/h3>\n<p>\u5173\u952e\u8bcd\u4f7f\u7528 LOWER \u4e0e LIKE \u7ec4\u5408&#xff0c;\u6392\u5e8f\u4f7f\u7528 COALESCE \u7b49\u8868\u8fbe\u5f0f&#xff0c;\u6570\u636e\u5e93 collation \u548c\u5b57\u7b26\u4e32\u51fd\u6570\u53ef\u80fd\u6709\u65b9\u8a00\u5dee\u5f02\u3002PostgreSQL\u3001MySQL\u3001SQLite \u5b9e\u6d4b\u7ed3\u679c\u8981\u4fdd\u6301\u65e2\u5b9a\u8bed\u4e49&#xff1b;\u4ec5\u4f9d\u8d56 ORM \u80fd\u7f16\u8bd1\u4e0d\u4ee3\u8868\u4e2d\u6587\u5927\u5c0f\u5199\u3001\u5b57\u7b26\u6bd4\u8f83\u548c\u7d22\u5f15\u8ba1\u5212\u90fd\u4e00\u81f4\u3002<\/p>\n<p>\u5206\u9875\u67e5\u8be2\u8981\u68c0\u67e5\u6267\u884c\u8ba1\u5212\u3002\u5173\u952e\u8bcd count \u6709\u754c\u51cf\u5c11\u6210\u672c&#xff0c;\u4f46\u5217\u8868\u672c\u8eab\u4ecd\u53ef\u80fd\u626b\u63cf&#xff1b;\u6807\u7b7e EXISTS\u3001\u5206\u7c7b IN \u548c sort \u7ec4\u5408\u53ef\u80fd\u9700\u8981\u5339\u914d\u7d22\u5f15\u3002\u4e0d\u8981\u5148\u51ed\u611f\u89c9\u6dfb\u52a0\u5341\u4e2a\u7d22\u5f15&#xff0c;\u5e94\u8be5\u4ece\u771f\u5b9e\u6570\u636e\u89c4\u6a21\u548c explain \u7ed3\u679c\u5224\u65ad\u3002\u7d22\u5f15\u5199\u5165\u6210\u672c\u3001\u8fc1\u79fb\u98ce\u9669\u4e5f\u8981\u8003\u8651\u3002<\/p>\n<p>\u67e5\u8be2\u6027\u80fd\u8bc1\u636e\u9700\u8bf4\u660e fixture \u6570\u636e\u89c4\u6a21\u3001\u6570\u636e\u5e93\u7c7b\u578b\u3001\u7b5b\u9009\u7ec4\u5408\u3001\u7f13\u5b58\u72b6\u6001\u548c\u6d4b\u91cf\u8303\u56f4\u3002\u5355\u5143\u6d4b\u8bd5\u901a\u8fc7\u53ea\u80fd\u8bc1\u660e\u903b\u8f91\u7ed3\u679c&#xff0c;\u4e0d\u80fd\u8bf4\u660e\u771f\u5b9e\u6570\u636e\u91cf\u4e0b\u7684\u67e5\u8be2\u5ef6\u8fdf\u3002<\/p>\n<h3>\u4e03\u3001\u5982\u4f55\u6d4b\u8bd5\u67e5\u8be2\u6295\u5f71<\/h3>\n<p>\u5efa\u7acb\u80fd\u533a\u5206\u8fb9\u754c\u7684\u6587\u7ae0\u96c6&#xff1a;\u516c\u5f00\u5df2\u53d1\u5e03\u3001\u79c1\u6709\u8349\u7a3f\u3001\u5df2\u5220\u9664\u6587\u7ae0\u3001\u540c\u53d1\u5e03\u65f6\u95f4\u6587\u7ae0\u3001\u591a\u5206\u7c7b\u540e\u4ee3\u3001\u5e26\u591a\u4e2a\u6807\u7b7e\u3001\u957f\u6b63\u6587\u3001\u7a7a\u6458\u8981\u3002\u5206\u522b\u68c0\u67e5&#xff1a;<\/p>\n<ul>\n<li>\u533f\u540d\u5217\u8868\/\u641c\u7d22\u4e0d\u51fa\u73b0\u8349\u7a3f\u548c\u8f6f\u5220\u9664\u8bb0\u5f55&#xff1b;<\/li>\n<li>\u4f5c\u8005\u7ba1\u7406\u67e5\u8be2\u53ef\u6309\u5951\u7ea6\u770b\u5230\u81ea\u5df1\u7684\u5f85\u5ba1\u6587\u7ae0&#xff1b;<\/li>\n<li>\u6807\u7b7e\u8fc7\u6ee4\u4e0d\u91cd\u590d\u6587\u7ae0&#xff1b;<\/li>\n<li>\u5206\u7c7b\u67e5\u8be2\u53ea\u5305\u542b\u6307\u5b9a\u5b50\u6811\u8303\u56f4&#xff1b;<\/li>\n<li>\u672a\u77e5\u6392\u5e8f\u9879\u5b89\u5168\u56de\u9000\u4e14\u987a\u5e8f\u7a33\u5b9a&#xff1b;<\/li>\n<li>\u7ffb\u9875\u8fb9\u754c\u6ca1\u6709\u7531\u6392\u5e8f\u540c\u503c\u9020\u6210\u7684\u91cd\u590d&#xff1b;<\/li>\n<li>Summary \u4e0d\u542b content \u6b63\u6587&#xff1b;<\/li>\n<li>keyword count \u4e0e\u9650\u5236\u89c4\u5219\u4e00\u81f4&#xff1b;<\/li>\n<li>\u8f6f\u5220\u9664\u6587\u7ae0\u4e0d\u518d\u53c2\u4e0e\u516c\u5f00\u8ba1\u6570\u548c\u76f8\u5173\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u518d\u5728\u4e09\u79cd\u6570\u636e\u5e93\u4e0a\u6267\u884c\u5173\u952e\u7b5b\u9009&#xff0c;\u68c0\u67e5\u6570\u636e\u6392\u5e8f\u4e0e\u6267\u884c\u5931\u8d25\u3002\u6570\u636e\u5e93\u65b9\u8a00\u6d4b\u8bd5\u5728 M6-16 \u5c55\u5f00&#xff0c;\u5dee\u5206\u5951\u7ea6\u9a8c\u8bc1\u5728 M6-09 \u5c55\u5f00\u3002<\/p>\n<p>\u6d4b\u8bd5\u62a5\u544a\u91cc\u6700\u597d\u4fdd\u7559\u4e24\u7ec4\u5bf9\u7167&#xff1a;\u4e00\u7ec4\u662f\u540c\u4e00\u6761\u4ef6\u5728 count \u4e0e list \u4e0a\u7684\u7ed3\u679c&#xff0c;\u7528\u6765\u8bc1\u660e\u7b5b\u9009\u4e00\u81f4&#xff1b;\u53e6\u4e00\u7ec4\u662f\u540c\u6392\u5e8f\u503c\u7ffb\u9875\u7684\u76f8\u90bb\u9875\u7801&#xff0c;\u7528\u6765\u8bc1\u660e\u6b21\u5e8f\u7a33\u5b9a\u3002\u8fd9\u6837\u5373\u4fbf\u4ee5\u540e\u6709\u4eba\u6539\u4e86\u7b5b\u9009\u5206\u652f&#xff0c;\u4e5f\u80fd\u4ece\u6d4b\u8bd5\u76f4\u63a5\u770b\u51fa\u662f\u53ef\u89c1\u6027\u3001\u8ba1\u6570\u8fd8\u662f\u5206\u9875\u51fa\u4e86\u95ee\u9898&#xff0c;\u800c\u4e0d\u662f\u53ea\u770b\u5230\u4e00\u4e2a\u603b\u6570\u5bf9\u4e0d\u4e0a\u3002<\/p>\n<h3>\u5c0f\u7ed3&#xff1a;\u67e5\u8be2\u8d28\u91cf\u662f\u53ef\u89c1\u6027\u3001\u6295\u5f71\u548c\u6210\u672c\u7684\u7ec4\u5408<\/h3>\n<p>M6 \u4f7f\u7528\u89c4\u8303\u5316\u5206\u9875\u3001\u56fa\u5b9a\u6392\u5e8f\u767d\u540d\u5355\u3001\u7a33\u5b9a ID \u6b21\u5e8f\u3001\u663e\u5f0f ActiveArticles \u8fc7\u6ee4\u548c SummaryColumns \u6295\u5f71\u3002\u5206\u7c7b\u3001\u6807\u7b7e\u4e0e\u5173\u952e\u8bcd\u6761\u4ef6\u6309\u5951\u7ea6\u8303\u56f4\u7ec4\u5408&#xff1b;\u5173\u952e\u8bcd\u8ba1\u6570\u6709 2000 \u6761\u62a4\u680f\u3002\u67e5\u8be2\u5c42\u4e0d\u8ba9\u5ba2\u6237\u7aef\u51b3\u5b9a SQL \u7ed3\u6784&#xff0c;\u4e5f\u4e0d\u628a\u5b8c\u6574\u6587\u7ae0\u6b63\u6587\u9ed8\u8ba4\u5e26\u5165\u5217\u8868\u3002<\/p>\n<p>\u9605\u8bfb ORM query chain \u65f6&#xff0c;\u4ecd\u5e94\u8ffd\u95ee\u6700\u7ec8 SQL&#xff1a;\u7b5b\u9009\u662f\u5426\u5b8c\u6574&#xff0c;\u8f6f\u5220\u9664\u662f\u5426\u8986\u76d6 join&#xff0c;\u5206\u9875\u662f\u5426\u7a33\u5b9a&#xff0c;\u6295\u5f71\u6709\u6ca1\u6709\u5168\u6587&#xff0c;count \u662f\u5426\u4e00\u81f4&#xff0c;\u771f\u5b9e\u6570\u636e\u5e93\u8ba1\u5212\u80fd\u5426\u627f\u53d7\u3002\u6e05\u695a\u7684\u67e5\u8be2\u4e0d\u662f\u94fe\u5f0f API \u5199\u5f97\u77ed&#xff0c;\u800c\u662f\u884c\u4e3a\u53ef\u63a8\u7406\u3002<\/p>\n<h3>\u5ef6\u4f38\u9605\u8bfb<\/h3>\n<ul>\n<li>\u6587\u7ae0\u5217\u8868&#xff1a;\u670d\u52a1\u7aef\u9996\u5c4f\u4e0e\u5ba2\u6237\u7aef\u6301\u7eed\u52a0\u8f7d\u600e\u6837\u534f\u4f5c<\/li>\n<li>\u5217\u8868\u63a5\u53e3\u4e09\u4ef6\u5957&#xff1a;\u5206\u9875\u3001\u7b5b\u9009\u3001\u6392\u5e8f<\/li>\n<li>\u6587\u7ae0 CRUD \u4e0e\u6295\u7a3f\u72b6\u6001\u673a<\/li>\n<li>\u9605\u8bfb\u91cf\u9632\u5237&#xff1a;\u53bb\u91cd\u51b7\u5374\u4e0e\u8ba1\u6570\u5199\u5206\u79bb<\/li>\n<\/ul>\n<hr \/>\n<p>\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u8ba2\u9605\u6211\u7684 CSDN \u4e13\u680f \u300c\u6210\u4e3a\u5168\u6808\u300d&#xff1a;<\/p>\n<p>&#x1f517; \u4e13\u680f\u5730\u5740&#xff1a;https:\/\/blog.csdn.net\/fungleo\/category_13204651.html<\/p>\n<p>&#x1f4e6; \u672c\u7cfb\u5217\u914d\u5957\u4ee3\u7801\u4ed3\u5e93&#xff1a;https:\/\/github.com\/fengcms\/become-a-full-stack-developer<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144105-6ae5fe012294c.png\" alt=\"\u6210\u4e3a\u5168\u6808\u4e13\u680f\u8ba2\u9605\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71&#xff1a;\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 \u5217\u8868\u63a5\u53e3\u4e0d\u53ea\u662f\u62fc\u51e0\u4e2a WHERE \u6761\u4ef6\u3002\u5b83\u8fd8\u8981\u9694\u79bb\u672a\u53d1\u5e03\u5185\u5bb9\u3001\u7a33\u5b9a\u6392\u5e8f\u3001\u9650\u5236\u91cd\u67e5\u8be2\u6210\u672c&#xff0c;\u5e76\u786e\u4fdd\u6458\u8981\u67e5\u8be2\u4e0d\u4f1a\u987a\u624b\u52a0\u8f7d\u6574\u7bc7\u6b63\u6587\u3002 \u672c\u6587\u4ee3\u7801\u5feb\u7167&#xff1a;\u63d0\u4ea4 ceac4e0\u3002\u67e5\u8be2\u884c\u4e3a\u4ee5\u5951\u7ea6\u548c internal\/article\/query.go\u3001internal\/article\/view.go \u4e3a\u51c6\u3002 \u524d\u8a00&#xff1a;\u4e00\u6761\u5217\u8868 SQL \u5bf9\u5e94\u591a\u6761\u4ea7\u54c1\u89c4\u5219<br \/>\n\u6587\u7ae0\u5217\u8868\u7684\u67e5\u8be2\u6761\u4ef6&amp;#<\/p>\n","protected":false},"author":2,"featured_media":120535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13747,13206,11686,745,746],"topic":[],"class_list":["post-120539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-gorm","tag-13206","tag-11686","tag-go","tag-golang"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/120539.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71&#xff1a;\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 \u5217\u8868\u63a5\u53e3\u4e0d\u53ea\u662f\u62fc\u51e0\u4e2a WHERE \u6761\u4ef6\u3002\u5b83\u8fd8\u8981\u9694\u79bb\u672a\u53d1\u5e03\u5185\u5bb9\u3001\u7a33\u5b9a\u6392\u5e8f\u3001\u9650\u5236\u91cd\u67e5\u8be2\u6210\u672c&#xff0c;\u5e76\u786e\u4fdd\u6458\u8981\u67e5\u8be2\u4e0d\u4f1a\u987a\u624b\u52a0\u8f7d\u6574\u7bc7\u6b63\u6587\u3002 \u672c\u6587\u4ee3\u7801\u5feb\u7167&#xff1a;\u63d0\u4ea4 ceac4e0\u3002\u67e5\u8be2\u884c\u4e3a\u4ee5\u5951\u7ea6\u548c internal\/article\/query.go\u3001internal\/article\/view.go \u4e3a\u51c6\u3002 \u524d\u8a00&#xff1a;\u4e00\u6761\u5217\u8868 SQL \u5bf9\u5e94\u591a\u6761\u4ea7\u54c1\u89c4\u5219 \u6587\u7ae0\u5217\u8868\u7684\u67e5\u8be2\u6761\u4ef6&amp;#\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/120539.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-31T14:41:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144101-6ae5fdfdec353.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120539.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/120539.html\",\"name\":\"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-31T14:41:07+00:00\",\"dateModified\":\"2026-10-31T14:41:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/120539.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/120539.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120539.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/120539.html","og_locale":"zh_CN","og_type":"article","og_title":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71&#xff1a;\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 \u5217\u8868\u63a5\u53e3\u4e0d\u53ea\u662f\u62fc\u51e0\u4e2a WHERE \u6761\u4ef6\u3002\u5b83\u8fd8\u8981\u9694\u79bb\u672a\u53d1\u5e03\u5185\u5bb9\u3001\u7a33\u5b9a\u6392\u5e8f\u3001\u9650\u5236\u91cd\u67e5\u8be2\u6210\u672c&#xff0c;\u5e76\u786e\u4fdd\u6458\u8981\u67e5\u8be2\u4e0d\u4f1a\u987a\u624b\u52a0\u8f7d\u6574\u7bc7\u6b63\u6587\u3002 \u672c\u6587\u4ee3\u7801\u5feb\u7167&#xff1a;\u63d0\u4ea4 ceac4e0\u3002\u67e5\u8be2\u884c\u4e3a\u4ee5\u5951\u7ea6\u548c internal\/article\/query.go\u3001internal\/article\/view.go \u4e3a\u51c6\u3002 \u524d\u8a00&#xff1a;\u4e00\u6761\u5217\u8868 SQL \u5bf9\u5e94\u591a\u6761\u4ea7\u54c1\u89c4\u5219 \u6587\u7ae0\u5217\u8868\u7684\u67e5\u8be2\u6761\u4ef6&amp;#","og_url":"https:\/\/www.wsisp.com\/helps\/120539.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-31T14:41:07+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261031144101-6ae5fdfdec353.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/120539.html","url":"https:\/\/www.wsisp.com\/helps\/120539.html","name":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-31T14:41:07+00:00","dateModified":"2026-10-31T14:41:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/120539.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/120539.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/120539.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u6210\u4e3a\u5168\u6808\u00b7Go \u540e\u7aef\u7bc7\u00b7\u6587\u7ae0\u67e5\u8be2\u4e0e\u5c55\u793a\u6295\u5f71\uff1a\u5b89\u5168\u5206\u9875\u3001\u6392\u5e8f\u548c\u8f6f\u5220\u9664"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=120539"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120539\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/120535"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=120539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=120539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=120539"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=120539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}