{"id":206341,"date":"2025-06-12T11:04:37","date_gmt":"2025-06-12T03:04:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206341\/"},"modified":"2025-06-12T11:04:37","modified_gmt":"2025-06-12T03:04:37","slug":"go-%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bd%ac%e4%b9%89-mysql-%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2%e7%9a%84%e7%89%b9%e6%ae%8a%e5%ad%97%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206341\/","title":{"rendered":"Go \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f"},"content":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/173120674767301e5b932ec.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 go \u4e2d\u6a21\u7cca\u67e5\u8be2\u5305\u542b\u767e\u5206\u53f7\u7684\u5185\u5bb9<\/strong>,\u5728 mysql \u4e2d\u8fdb\u884c like \u6a21\u7cca\u67e5\u8be2\u65f6\uff0c\u767e\u5206\u53f7 (%) \u5b57\u7b26\u9700\u8981\u8f6c\u4e49\uff0c\u5426\u5219\u67e5\u8be2\u5c06\u5339\u914d\u6240\u6709\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 go \u8bed\u8a00\u4e2d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u51fd\u6570\u8fdb\u884c\u8f6c\u4e49\u3002,<br \/>\n<strong>\u95ee\u9898\u91cd\u70b9<\/strong>,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6848<\/strong>,\u5728 go \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 strings.builder \u548c strings.replace \u51fd\u6570\u6765\u5bf9\u6a21\u7cca\u67e5\u8be2\u8fdb\u884c\u8f6c\u4e49\u3002\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442s\uff0c\u5982\u4f55\u8f6c\u4e49 % \u5b57\u7b26\uff1a,\u4f7f\u7528\u6b64\u51fd\u6570\u65f6\uff0c\u60a8\u53ef\u4ee5\u5c06\u6a21\u7cca\u67e5\u8be2\u4e2d\u7684\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a,<br \/>\n<strong>mysql \u4e2d\u9700\u8981\u8f6c\u4e49\u7684\u7279\u6b8a\u5b57\u7b26<\/strong>,\u9664\u4e86\u767e\u5206\u53f7\u4e4b\u5916\uff0c\u5728 mysql \u4e2d\u8fd8\u6709\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u9700\u8981\u5728\u6a21\u7cca\u67e5\u8be2\u4e2d\u8f6c\u4e49\uff1a,<br \/>\n<strong>\u5b98\u65b9\u53c2\u8003\u6587\u6863<\/strong>,\u6709\u5173 mysql \u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49\u7684\u5b98\u65b9\u6587\u6863\uff0c\u8bf7\u53c2\u9605 mysql \u7684\u5b98\u65b9\u624b\u518c\uff1a,\u4eca\u5929\u5173\u4e8e\u300aGo \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\uff08\u53cd\u659c\u6760\uff09,%\uff08\u767e\u5206\u53f7\uff09,_\uff08\u4e0b\u5212\u7ebf\uff09,mysql \u4e2d\u9700\u8981\u8f6c\u4e49\u7684\u7279\u6b8a\u5b57\u7b26\u6709\u54ea\u4e9b\uff1f,\u5728 go \u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u8f6c\u4e49\u6a21\u7cca\u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff08\u5982 %\uff09\uff1f,\u662f\u5426\u6709\u5b98\u65b9\u6587\u6863\u53ef\u4f9b\u53c2\u8003\uff1f,\u5982\u4f55\u5728 go \u4e2d\u6a21\u7cca\u67e5\u8be2\u5305\u542b\u767e\u5206\u53f7\u7684\u5185\u5bb9,\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<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>Go \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f<\/span><\/p>\n<h1>Go \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f<\/h1>\n<p><span>2024-11-10 10:46:03<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/173120674767301e5b932ec.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 go \u4e2d\u6a21\u7cca\u67e5\u8be2\u5305\u542b\u767e\u5206\u53f7\u7684\u5185\u5bb9<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\u8fdb\u884c like \u6a21\u7cca\u67e5\u8be2\u65f6\uff0c\u767e\u5206\u53f7 (%) \u5b57\u7b26\u9700\u8981\u8f6c\u4e49\uff0c\u5426\u5219\u67e5\u8be2\u5c06\u5339\u914d\u6240\u6709\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 go \u8bed\u8a00\u4e2d\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u51fd\u6570\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<p><strong>\u95ee\u9898\u91cd\u70b9<\/strong><\/p>\n<ul>\n<li>\u5728 go \u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u8f6c\u4e49\u6a21\u7cca\u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff08\u5982 %\uff09\uff1f<\/li>\n<li>mysql \u4e2d\u9700\u8981\u8f6c\u4e49\u7684\u7279\u6b8a\u5b57\u7b26\u6709\u54ea\u4e9b\uff1f<\/li>\n<li>\u662f\u5426\u6709\u5b98\u65b9\u6587\u6863\u53ef\u4f9b\u53c2\u8003\uff1f<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5728 go \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 strings.builder \u548c strings.replace \u51fd\u6570\u6765\u5bf9\u6a21\u7cca\u67e5\u8be2\u8fdb\u884c\u8f6c\u4e49\u3002\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442s\uff0c\u5982\u4f55\u8f6c\u4e49 % \u5b57\u7b26\uff1a<\/p>\n<pre>import (\n    \"strings\"\n)\n\nfunc escapelike(left, right, word string) string {\n    var n int\n    for i := range word {\n        if c := word[i]; c == '%' || c == '_' || c == '\\\\' {\n            n++\n        }\n    }\n    \/\/ no characters to escape.\n    if n == 0 {\n        return left + word + right\n    }\n    var b strings.builder\n    b.grow(len(word) + n)\n    for _, c := range word {\n        if c == '%' || c == '_' || c == '\\\\' {\n            b.writebyte('\\\\')\n        }\n        b.writerune(c)\n    }\n    return left + b.string() + right\n}<\/pre>\n<p>\u4f7f\u7528\u6b64\u51fd\u6570\u65f6\uff0c\u60a8\u53ef\u4ee5\u5c06\u6a21\u7cca\u67e5\u8be2\u4e2d\u7684\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff1a<\/p>\n<pre>query := escapeLike(&amp;quot;%&amp;quot;, &amp;quot;%&amp;quot;, &amp;quot;%&amp;quot;)\n...<\/pre>\n<p><strong>mysql \u4e2d\u9700\u8981\u8f6c\u4e49\u7684\u7279\u6b8a\u5b57\u7b26<\/strong><\/p>\n<p>\u9664\u4e86\u767e\u5206\u53f7\u4e4b\u5916\uff0c\u5728 mysql \u4e2d\u8fd8\u6709\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u9700\u8981\u5728\u6a21\u7cca\u67e5\u8be2\u4e2d\u8f6c\u4e49\uff1a<\/p>\n<ul>\n<li>_\uff08\u4e0b\u5212\u7ebf\uff09<\/li>\n<li>%\uff08\u767e\u5206\u53f7\uff09<\/li>\n<li>\uff08\u53cd\u659c\u6760\uff09<\/li>\n<\/ul>\n<p><strong>\u5b98\u65b9\u53c2\u8003\u6587\u6863<\/strong><\/p>\n<p>\u6709\u5173 mysql \u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49\u7684\u5b98\u65b9\u6587\u6863\uff0c\u8bf7\u53c2\u9605 mysql \u7684\u5b98\u65b9\u624b\u518c\uff1a<\/p>\n<ul>\n<li>[\u8f6c\u4e49\u5b57\u7b26\u8bed\u6cd5](https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/regexp-syntax.html#regexp-syntax-escapes)<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGo \u4e2d\u5982\u4f55\u8f6c\u4e49 MySQL \u6a21\u7cca\u67e5\u8be2\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Flex \u5e03\u5c40\u4e2d overflow \u5931\u6548\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6ca1\u6709&#8220;\u6807\u7b7e\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u5b9e\u73b0\u9875\u9762\u4e2d\u4f4d\u7f6e\u8df3\u8f6c\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211&#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-206341","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206341","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=206341"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206341\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}