{"id":1240,"date":"2022-11-24T10:34:51","date_gmt":"2022-11-24T02:34:51","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1240"},"modified":"2022-11-24T10:36:24","modified_gmt":"2022-11-24T02:36:24","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a03-%e6%98%a0%e5%b0%84map","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1240","title":{"rendered":"Go\u6307\u5357\u7ec3\u4e603-\u6620\u5c04Map"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/go.dev\/tour\/list\" target=\"_blank\">A Tour of Go<\/a>\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u4e09\u4e2a\u7ec3\u4e60\uff0c<a href=\"https:\/\/tour.go-zh.org\/moretypes\/23\" target=\"_blank\" rel=\"noreferrer noopener\">\u539f\u9898<\/a>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5b9e\u73b0&nbsp;<code>WordCount<\/code>\u3002\u5b83\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u6620\u5c04\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\u4e2d\u6bcf\u4e2a\u201c\u5355\u8bcd\u201d\u7684\u4e2a\u6570\u3002\u51fd\u6570&nbsp;<code>wc.Test<\/code>&nbsp;\u4f1a\u5bf9\u6b64\u51fd\u6570\u6267\u884c\u4e00\u7cfb\u5217\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5e76\u8f93\u51fa\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002<\/p>\n\n\n\n<p>\u4f60\u4f1a\u53d1\u73b0&nbsp;<a href=\"https:\/\/go-zh.org\/pkg\/strings\/#Fields\" target=\"_blank\" rel=\"noreferrer noopener\">strings.Fields<\/a>&nbsp;\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u6253\u5f00\u4e0a\u9762\u5bf9\u5e94\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u770b\u5230<code>strings.Fields<\/code>\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u6bb5\u8bdd\u4e2d\u7684\u5355\u8bcd\u63d0\u53d6\u51fa\u6765\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5207\u7247<code>[]string<\/code>\u3002\u6211\u4eec\u53ea\u9700\u8981\u7528\u5faa\u73af\u7edf\u8ba1\u4e00\u4e0b\u8fd9\u4e2a\u5207\u7247\u91cc\u9762\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\u5c31\u597d\u4e86\u3002\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"golang.org\/x\/tour\/wc\"\n\t\"strings\"\n)\n\nfunc WordCount(s string) map&#91;string]int {\n\tm := make(map&#91;string]int)\n\t\n\tfor _, w := range strings.Fields(s) {\n\t\tm&#91;w]++\n\t}\n\treturn m\n}\n\nfunc main() {\n\twc.Test(WordCount)\n}\n<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PASS\n f(\"I am learning Go!\") = \n  map&#91;string]int{\"Go!\":1, \"I\":1, \"am\":1, \"learning\":1}\nPASS\n f(\"The quick brown fox jumped over the lazy dog.\") = \n  map&#91;string]int{\"The\":1, \"brown\":1, \"dog.\":1, \"fox\":1, \"jumped\":1, \"lazy\":1, \"over\":1, \"quick\":1, \"the\":1}\nPASS\n f(\"I ate a donut. Then I ate another donut.\") = \n  map&#91;string]int{\"I\":2, \"Then\":1, \"a\":1, \"another\":1, \"ate\":2, \"donut.\":2}\nPASS\n f(\"A man a plan a canal panama.\") = \n  map&#91;string]int{\"A\":1, \"a\":2, \"canal\":1, \"man\":1, \"panama.\":1, \"plan\":1}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u4e09\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u5b9e\u73b0&nbsp;WordCount\u3002\u5b83\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u6620\u5c04\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u7b26\u4e32&nbsp;s&nbsp;\u4e2d\u6bcf\u4e2a\u201c\u5355\u8bcd\u201d\u7684\u4e2a\u6570\u3002\u51fd\u6570&nbsp;wc.Test&nbsp;\u4f1a\u5bf9\u6b64\u51fd\u6570\u6267\u884c\u4e00\u7cfb\u5217\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5e76\u8f93\u51fa\u6210\u529f\u8fd8\u662f\u5931\u8d25\u3002 \u4f60\u4f1a\u53d1\u73b0&nbsp;strings.Fields&nbsp;\u5f88\u6709\u5e2e\u52a9\u3002 \u6253\u5f00\u4e0a\u9762\u5bf9\u5e94\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u770b\u5230strings.Fields\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u6bb5\u8bdd\u4e2d\u7684\u5355\u8bcd\u63d0\u53d6\u51fa\u6765\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5207\u7247[]string\u3002\u6211\u4eec\u53ea\u9700\u8981\u7528\u5faa\u73af\u7edf\u8ba1\u4e00\u4e0b\u8fd9\u4e2a\u5207\u7247\u91cc\u9762\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\u5c31\u597d\u4e86\u3002\u5b9e\u73b0\u5982\u4e0b\uff1a \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":323,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1240"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}