{"id":200505,"date":"2025-05-06T12:56:34","date_gmt":"2025-05-06T04:56:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200505\/"},"modified":"2025-05-06T12:56:34","modified_gmt":"2025-05-06T04:56:34","slug":"%e5%9c%a8-go-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%ae%89%e5%85%a8%e8%bd%ac%e4%b9%89-mysql-like-%e6%9f%a5%e8%af%a2%e4%b8%ad%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\/200505\/","title":{"rendered":"\u5728 Go \u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u8f6c\u4e49 MySQL Like \u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728 Go \u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u8f6c\u4e49 MySQL Like \u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5728 Go \u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u8f6c\u4e49 MySQL Like \u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730905250672b84a27d434.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 go \u4e2d\u8fdb\u884c mysql like % \u6a21\u7cca\u67e5\u8be2\u7684\u8f6c\u4e49<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u67e5\u8be2\u5185\u5bb9\u5305\u542b\u767e\u5206\u53f7 (%)\uff0c\u5219\u9700\u8981\u5bf9 % \u8fdb\u884c\u8f6c\u4e49\uff0c\u5426\u5219\u5c06\u5339\u914d\u6240\u6709\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 go \u4e2d\u65b9\u4fbf\u5730\u5bf9\u6a21\u7cca\u67e5\u8be2\u5185\u5bb9\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<p>go \u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86 strings.builder \u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528\u6b64\u7c7b\u578b\u6765\u624b\u52a8\u8f6c\u4e49\u67e5\u8be2\u5185\u5bb9\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u51fd\u6570 escapelike\uff0c\u5b83\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>left: \u7528\u4e8e\u8fde\u63a5\u67e5\u8be2\u5185\u5bb9\u5de6\u4fa7\u7684\u5b57\u7b26\uff08\u4f8b\u5982\u5de6\u5355\u5f15\u53f7\uff09<\/li>\n<li>right: \u7528\u4e8e\u8fde\u63a5\u67e5\u8be2\u5185\u5bb9\u53f3\u4fa7\u7684\u5b57\u7b26\uff08\u4f8b\u5982\u53f3\u5355\u5f15\u53f7\uff09<\/li>\n<li>word: \u8981\u8f6c\u4e49\u7684\u67e5\u8be2\u5185\u5bb9<\/li>\n<\/ul>\n<p>\u8be5\u51fd\u6570\u5c06\u904d\u5386\u67e5\u8be2\u5185\u5bb9\uff0c\u68c0\u67e5\u662f\u5426\u5b58\u5728\u9700\u8981\u8f6c\u4e49\u7684\u5b57\u7b26\uff08%\uff0c_\uff0c\uff09\u3002\u5982\u679c\u627e\u5230\u8fd9\u6837\u7684\u5b57\u7b26\uff0c\u5b83\u5c06\u5728\u5b57\u7b26\u524d\u9762\u6dfb\u52a0\u53cd\u659c\u6760\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 escapelike \u51fd\u6570\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>\u8c03\u7528 escapelike \u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u5de6\u5355\u5f15\u53f7\u548c\u53f3\u5355\u5f15\u53f7\u4f5c\u4e3a left \u548c right \u53c2\u6570\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9664\u4e86 %\uff0c_\uff0c \u4e4b\u5916\uff0cmysql \u4e2d\u9700\u8981\u88ab\u8f6c\u4e49\u7684\u5b57\u7b26\u8fd8\u6709\uff1a<\/p>\n<pre>[ ] ( ) { } | ^ \\ \/ $<\/pre>\n<p>\u53ef\u4ee5\u5728 mysql \u5b98\u65b9\u6587\u6863\u4e2d\u627e\u5230\u6709\u5173\u6a21\u7cca\u67e5\u8be2\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728 Go \u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u8f6c\u4e49 MySQL Like \u67e5\u8be2\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0c\u5982\u4f55\u5b89\u5168\u8f6c\u4e49 My&#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":[101],"tags":[],"class_list":["post-200505","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200505","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=200505"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200505\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}