{"id":207405,"date":"2025-07-08T12:07:51","date_gmt":"2025-07-08T04:07:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207405\/"},"modified":"2025-07-08T12:07:51","modified_gmt":"2025-07-08T04:07:51","slug":"%e5%9c%a8-go-%e4%b8%ad%e7%bb%84%e5%90%88%e5%ad%98%e5%82%a8%e5%9c%a8%e9%80%9a%e9%81%93%e4%b8%8a%e7%9a%84%e5%a4%9a%e4%b8%aa%e6%98%a0%e5%b0%84%ef%bc%88%e7%9b%b8%e5%90%8c%e9%94%ae%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207405\/","title":{"rendered":"\u5728 Go \u4e2d\u7ec4\u5408\u5b58\u50a8\u5728\u901a\u9053\u4e0a\u7684\u591a\u4e2a\u6620\u5c04\uff08\u76f8\u540c\u952e\u7684\u503c\u6c42\u548c\u3002\uff09"},"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>\u5728 Go \u4e2d\u7ec4\u5408\u5b58\u50a8\u5728\u901a\u9053\u4e0a\u7684\u591a\u4e2a\u6620\u5c04\uff08\u76f8\u540c\u952e\u7684\u503c\u6c42\u548c\u3002\uff09<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 12:30:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Go \u4e2d\u7ec4\u5408\u5b58\u50a8\u5728\u901a\u9053\u4e0a\u7684\u591a\u4e2a\u6620\u5c04\uff08\u76f8\u540c\u952e\u7684\u503c\u6c42\u548c\u3002\uff09\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7684\u76ee\u6807\u662f\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4ee5\u5e76\u884c\u65b9\u5f0f\u8ba1\u7b97\u6587\u672c\u6587\u4ef6\u4e2d\u6bcf\u4e2a\u552f\u4e00\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u6240\u6709\u51fa\u73b0\u7684\u6b21\u6570\u90fd\u5fc5\u987b\u5728\u5355\u4e2a\u5730\u56fe\u4e2d\u5448\u73b0\u3002<\/p>\n<p>\u6211\u5728\u8fd9\u91cc\u6240\u505a\u7684\u662f\u5c06\u6587\u672c\u6587\u4ef6\u5212\u5206\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5212\u5206\u4e3a\u6570\u7ec4\u3002\u7136\u540e\u5c06\u8be5\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u957f\u5ea6\u76f8\u7b49\u7684\u5207\u7247\uff0c\u5e76\u540c\u65f6\u9988\u9001\u5230\u6620\u5c04\u5668\u51fd\u6570\u3002 <\/p>\n<pre>   func wordcount(text string)  (map[string]int) {\n    wg := new(sync.waitgroup)\n    s := strings.fields(newtext)\n\n    freq := make(map[string]int,len(s))\n    channel := make(chan map[string]int,2)\n\n    wg.add(1)\n    go mappers(s[0:(len(s)\/2)], freq, channel,wg)\n    wg.add(1)\n    go mappers(s[(len(s)\/2):], freq, channel,wg)\n    wg.wait()\n\n    actualmap := &lt;-channel\n\n\n    return actualmap\n<\/pre>\n<pre>func mappers(slice []string, occurrences map[string]int, ch chan map[string]int, wg *sync.waitgroup)  {\n    var l = sync.mutex{}\n    for _, word := range slice {\n        l.lock()\n        occurrences[word]++\n        l.unlock()\n\n    }\n    ch &lt;- occurrences\n    wg.done()\n}\n<\/pre>\n<p>\u6700\u91cd\u8981\u7684\u662f\uff0c\u6211\u6536\u5230\u4e86\u4e00\u4e2a\u4ee5 <\/p>\n<p> \u5f00\u5934\u7684\u5de8\u5927\u591a\u884c\u9519\u8bef <\/p>\n<p> \u81f4\u547d\u9519\u8bef\uff1a\u5e76\u53d1\u6620\u5c04\u5199\u5165 <\/p>\n<p>\u5f53\u6211\u8fd0\u884c\u4ee3\u7801\u65f6\u3002\u6211\u4ee5\u4e3a\u6211\u662f\u901a\u8fc7\u4e92\u65a5\u6765\u5b88\u62a4\u7684<\/p>\n<pre>        l.Lock()\n        occurrences[word]++\n        l.Unlock()\n<\/pre>\n<p>\u6211\u5728\u8fd9\u91cc\u505a\u9519\u4e86\u4ec0\u4e48\uff1f\u800c\u4e14\u3002\u5982\u4f55\u5c06\u6240\u6709\u5730\u56fe\u5408\u5e76\u5230\u4e00\u4e2a\u9891\u9053\u4e2d\uff1f\u5bf9\u4e8e\u7ec4\u5408\uff0c\u6211\u7684\u610f\u601d\u662f\u76f8\u540c\u952e\u7684\u503c\u5728\u65b0\u6620\u5c04\u4e2d\u6c42\u548c\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4e3b\u8981\u95ee\u9898\u662f\u5728\u6bcf\u4e2a goroutine \u4e2d\u4f7f\u7528\u5355\u72ec\u7684\u9501\u3002\u8fd9\u5bf9\u4e8e\u5e8f\u5217\u5316\u5bf9\u5730\u56fe\u7684\u8bbf\u95ee\u6ca1\u6709\u4efb\u4f55\u5e2e\u52a9\u3002\u6bcf\u4e2a goroutine \u5fc5\u987b\u4f7f\u7528\u76f8\u540c\u7684\u9501\u3002<\/p>\n<p>\u7531\u4e8e\u60a8\u5728\u6bcf\u4e2a goroutine \u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u6620\u5c04\uff0c\u56e0\u6b64\u60a8\u4e0d\u5fc5\u5408\u5e76\u5b83\u4eec\uff0c\u4e5f\u4e0d\u9700\u8981\u901a\u9053\u6765\u4f20\u9012\u7ed3\u679c\u3002<\/p>\n<p>\u5373\u4f7f\u60a8\u5728\u6bcf\u4e2a goroutine \u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u4e92\u65a5\u9501\uff0c\u7531\u4e8e\u60a8\u4f7f\u7528\u5355\u4e2a\u6620\u5c04\uff0c\u8fd9\u53ef\u80fd\u5bf9\u6027\u80fd\u6ca1\u6709\u5e2e\u52a9\uff0cgoroutine \u5c06\u4e0d\u5f97\u4e0d\u76f8\u4e92\u7ade\u4e89\u6620\u5c04\u7684\u9501\u3002<\/p>\n<p>\u60a8\u5e94\u8be5\u5728\u6bcf\u4e2a goroutine \u4e2d\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u6620\u5c04\uff0c\u4f7f\u7528\u5b83\u5728\u672c\u5730\u8fdb\u884c\u8ba1\u6570\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u6620\u5c04\u4f20\u9012\u5230\u901a\u9053\u4e0a\u3002\u8fd9\u53ef\u80fd\u4f1a\u7ed9\u60a8\u5e26\u6765\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<p>\u4f46\u662f\u4f60\u4e0d\u9700\u8981\u9501\uff0c\u56e0\u4e3a\u6bcf\u4e2a goroutine \u90fd\u6709\u81ea\u5df1\u7684\u6620\u5c04\uff0c\u5b83\u53ef\u4ee5\u5728\u6ca1\u6709\u4e92\u65a5\u4f53\u7684\u60c5\u51b5\u4e0b\u8bfb\/\u5199\u3002<\/p>\n<p>\u4f46\u662f\u60a8\u5fc5\u987b\u5728\u901a\u9053\u4e0a\u4f20\u9012\u7ed3\u679c\uff0c\u7136\u540e\u5408\u5e76\u5b83\u3002<\/p>\n<p>\u7531\u4e8e goroutine \u5728\u901a\u9053\u4e0a\u4f20\u9012\u7ed3\u679c\uff0c\u56e0\u6b64\u7b49\u5f85\u7ec4\u53d8\u5f97\u4e0d\u5fc5\u8981\u3002<\/p>\n<pre>func wordcount(text string) map[string]int {\n    s := strings.fields(text)\n\n    channel := make(chan map[string]int, 2)\n\n    go mappers(s[0:(len(s)\/2)], channel)\n    go mappers(s[(len(s)\/2):], channel)\n\n    total := map[string]int{}\n    for i := 0; i &lt; 2; i++ {\n        m := &lt;-channel\n        for k, v := range m {\n            total[k] += v\n        }\n    }\n\n    return total\n}\n\nfunc mappers(slice []string, ch chan map[string]int) {\n    occurrences := map[string]int{}\n    for _, word := range slice {\n        occurrences[word]++\n\n    }\n    ch &lt;- occurrences\n}<\/pre>\n<p>\u6d4b\u8bd5\u793a\u4f8b\uff1a<\/p>\n<pre>fmt.println(wordcount(\"aa ab cd cd de ef a x cd aa\"))<\/pre>\n<p>\u8f93\u51fa\uff08\u5728 \u4e0a\u5c1d\u8bd5\uff09\uff1a<\/p>\n<pre>map[a:1 aa:2 ab:1 cd:3 de:1 ef:1 x:1]<\/pre>\n<p>\u53e6\u8bf7\u6ce8\u610f\uff0c\u7406\u8bba\u4e0a\u8fd9\u770b\u8d77\u6765\u201c\u4e0d\u9519\u201d\uff0c\u4f46\u5b9e\u9645\u4e0a\u60a8\u53ef\u80fd\u4ecd\u7136\u65e0\u6cd5\u5b9e\u73b0\u4efb\u4f55\u6027\u80fd\u63d0\u5347\uff0c\u56e0\u4e3a goroutine \u6240\u505a\u7684\u5de5\u4f5c\u592a\u201c\u5c11\u201d\uff0c\u5e76\u4e14\u542f\u52a8\u5b83\u4eec\u5e76\u5408\u5e76\u7ed3\u679c\u9700\u8981\u4ed8\u51fa\u52aa\u529b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8d85\u8fc7\u6536\u76ca.<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207405","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207405","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=207405"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207405\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}