{"id":207357,"date":"2025-07-08T12:26:44","date_gmt":"2025-07-08T04:26:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207357\/"},"modified":"2025-07-08T12:26:44","modified_gmt":"2025-07-08T04:26:44","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e6%96%87%e6%9c%ac%e6%98%af%e5%90%a6%e5%8c%85%e5%90%ab-golang-%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e4%bb%bb%e4%bd%95%e5%8d%95%e8%af%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207357\/","title":{"rendered":"\u5982\u4f55\u68c0\u67e5\u6587\u672c\u662f\u5426\u5305\u542b Golang \u5217\u8868\u4e2d\u7684\u4efb\u4f55\u5355\u8bcd\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u68c0\u67e5\u6587\u672c\u662f\u5426\u5305\u542b Golang \u5217\u8868\u4e2d\u7684\u4efb\u4f55\u5355\u8bcd\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 22:09:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u68c0\u67e5\u6587\u672c\u662f\u5426\u5305\u542b Golang \u5217\u8868\u4e2d\u7684\u4efb\u4f55\u5355\u8bcd\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u68c0\u67e5\u7528\u6237\u63d0\u4ea4\u7684\u6587\u672c\u4e2d\u662f\u5426\u6709\u5783\u573e\u90ae\u4ef6\u5355\u8bcd\u3002\u8fd9\u662f\u6211\u7f16\u5199\u7684\u51fd\u6570\uff1a<\/p>\n<pre>func TextHasSpamWords(text string, spamWords []string) bool {\n    removePunctuation := func(r rune) rune {\n        if strings.ContainsRune(\".,:;\", r) {\n            return -1\n        } else {\n            return r\n        }\n    }\n    text = strings.Map(removePunctuation, text)\n    sort.Strings(spamWords)\n    for _, word := range text {\n        for _, sapmWord := range spamWords {\n            if word == sapmWord {\n                return true\n            }\n        }\n    }\n}<\/pre>\n<p>\u4f46\u662f\u6211\u5728 <code>word == sapmword<\/code> \u5904\u5f97\u5230 <code>mismatched type rune \u548c string<\/code>\u3002 \u6211\u77e5\u9053\u8fd9\u662f\u56e0\u4e3a <code>removepunctuation<\/code> \u8fd4\u56de\u7b26\u6587\uff08\u53c8\u540d int32\uff09\uff0c\u5b83\u65e0\u6cd5\u4e0e\u5b57\u7b26\u4e32\uff08unit8\uff09\u8fdb\u884c\u6bd4\u8f83\u3002\u4f46\u662f\u5220\u9664\u6807\u70b9\u7b26\u53f7\u5bf9\u4e8e\u8be5\u51fd\u6570\u7684\u5de5\u4f5c\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u6240\u4ee5\u6211\u60f3\u77e5\u9053\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f\u6216\u8005\u4e5f\u8bb8\u6709\u66f4\u60ef\u7528\u7684\u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u6b63\u5728\u5c1d\u8bd5\u6bd4\u8f83\u5b57\u6bcd\u548c\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5f53\u60a8\u6267\u884c <code>for _, word := range text<\/code> \u65f6\uff0c\u5b83\u4e0d\u662f\u5355\u8bcd\uff0c\u800c\u662f <code>letter<\/code>\u3002\u5982\u679c\u60a8\u60f3\u8981\u5355\u8bcd\uff0c\u5219\u5fc5\u987b\u4f7f\u7528 <code>textarr := strings.split(text, \" \")<\/code> \uff08\u6309\u7a7a\u683c\u5206\u5272\uff09\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5355\u8bcd\u5207\u7247<\/p>\n<pre>func texthasspamwords(text string, spamwords []string) bool {\n    removepunctuation := func(r rune) rune {\n        if strings.containsrune(\".,:;\", r) {\n            return -1\n        } else {\n            return r\n        }\n    }\n    text = strings.map(removepunctuation, text)\n    textarr := strings.split(text, ` `)\n    sort.strings(spamwords)\n    for _, word := range textarr {\n        for _, sapmword := range spamwords {\n            if word == sapmword {\n                return true\n            }\n        }\n    }\n    return false\n}\n<\/pre>\n<p>\u8003\u8651\u4f7f\u7528\u5730\u56fe\u8fdb\u884c\u67e5\u627e<\/p>\n<pre>lookupspam := make(map[string]bool)\nfor _, v := range spamwords {\n    lookupspam[v] = true\n}<\/pre>\n<p>\u7136\u540e\u4ee3\u66ff<\/p>\n<pre>for _, word := range textarr {\n    for _, sapmword := range spamwords {\n        if word == sapmword {\n            return true\n        }\n    }\n}<\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u4e1c\u897f<\/p>\n<pre>for _, word := range textArr {\n    _,found:=lookupSpam[word]\n    if found {\n       return true\n    }\n}<\/pre>\n<p>\u5982\u679c\u60a8\u91cd\u590d\u68c0\u67e5\u540c\u4e00\u7ec4\u5355\u8bcd\uff0c\u5219\u8bbe\u7f6e\u4e00\u6b21\u5730\u56fe\u5e76\u91cd\u590d\u4f7f\u7528<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u68c0\u67e5\u6587\u672c\u662f\u5426\u5305\u542b Golang \u5217\u8868\u4e2d\u7684\u4efb\u4f55\u5355\u8bcd\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207357","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207357","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=207357"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207357\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}