{"id":1730,"date":"2023-05-12T06:56:00","date_gmt":"2023-05-12T06:56:00","guid":{"rendered":"https:\/\/www.liutianfeng.com\/?p=1730"},"modified":"2023-05-12T06:56:00","modified_gmt":"2023-05-12T06:56:00","slug":"%e6%98%a0%e5%b0%84-map","status":"publish","type":"post","link":"https:\/\/www.liutianfeng.com\/?p=1730","title":{"rendered":"\u6620\u5c04 &#8211; Map"},"content":{"rendered":"\n<p><\/p>\n\n\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u6620\u5c04\u662f\u5b58\u50a8\u4e00\u7cfb\u5217\u65e0\u5e8f\u7684key\/value\u5bf9\uff0c\u901a\u8fc7key\u6765\u5bf9value\u8fdb\u884c\u64cd\u4f5c\uff08\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\uff09\u3002<br \/>Map\u662f\u65e0\u5e8f\u7684\u3002<\/p>\n<p><strong>key\/value\u89c4\u5219\uff1a<\/strong><\/p>\n<p>\u6620\u5c04\u7684key\u53ea\u80fd\u4e3a\u53ef\u4f7f\u7528==\u8fd0\u7b97\u7b26\u7684\u503c\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u3001\u6570\u7ec4\uff09\uff0cvalue\u53ef\u4ee5\u662f\u4efb\u610f\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5b9a\u4e49\u3001\u8d4b\u503c\uff1a<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">scores := make(map[string]int, 0)  \/\/ make\u6765\u5b9a\u4e49\nscores[\"Alice\"] = 12\nfmt.Println(scores) \/\/ map[Alice:12]\nscores := map[string]int{\"Liemer\": 1, \"Lius\": 100} \/\/ \u5b57\u9762\u91cf\u6765\u5b9a\u4e49, map[Liemer:1 Lius:100]<\/code><\/pre>\n<p><strong>\u64cd\u4f5c\uff1a<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u589e\nscores[\"hello\"] = 12 \/\/ \u5982\u679c\u6ca1\u6709\u5143\u7d20\u5c31\u65b0\u589e\n\n\/\/ \u5220\ndelete(scores, \"Lius\")\n\n\/\/ \u6539\nscores[\"Lius\"] = 3 \/\/ map[Liemer:1 Lius:3]\n\n\/\/ \u67e5\uff1a\u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u9ed8\u8ba4\u53d6\u96f6\u503c\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u5224\u5b9a\u4e00\u4e0b\nscores := map[string]int{\"Liemer\": 1, \"Lius\": 100}\nfmt.Println(scores[\"Lius\"]) \/\/ 100\nfmt.Println(scores[\"Tom\"])  \/\/ 0\uff0c \u4e0d\u5b58\u5728\uff0c\u53d6\u96f6\u503c\nif v, ok := scores[\"Tom\"]; ok {\n\tfmt.Println(v)\n} else {\n    fmt.Println(ok) \/\/ false\n\tfmt.Println(\"Tom does not exist...\") \/\/ Tom does not exist...\n}<\/code><\/pre>\n<p><strong>\u957f\u5ea6\uff1alen<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">fmt.Println(len(scores))<\/code><\/pre>\n<p><strong>for range:<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">for k, _ := range scores {\n\tfmt.Printf(\"Key: %v; Value: %v\\n\", k, scores[k])\n}\nKey: Lius; Value: 100\nKey: Liemer; Value: 1<\/code><\/pre>\n<p><strong>\u6295\u7968\uff1aExp1<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">ticket := []string{\"Tom\", \"Jerry\", \"Alice\", \"Jerry\", \"Tom\"}\nscores := map[string]int{}\nfor _, n := range ticket {\n\tscores[n] += 1\n}\nfmt.Println(scores) \/\/ map[Alice:1 Jerry:2 Tom:2]<\/code><\/pre>\n<p><strong>\u5927\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7edf\u8ba1\u3010\u52a0\u6392\u5e8f\u3011\uff1aExp2<\/strong><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">func main() {\n\tart_map := map[rune]int{}\n\tarticle := `Package os provides a platform-independent interface to operating system functionality.\nThe design is Unix-like, although the error handling is Go-like; failing calls return values\nof type error rather than error numbers. Often, more information is available within\nthe error. For example, if a call that takes a file name fails, such as Open or Stat,\nthe error will include the failing file name when printed and will be of type *PathError,\nwhich may be unpacked for more information.`\n\tart_rune := []rune(article)\n\tfor _, v := range art_rune {\n\t\tif v &gt;= 'A' &amp;&amp; v &lt;= 'Z' || v &gt;= 'a' &amp;&amp; v &lt;= 'z' {\n\t\t\tart_map[v] += 1\n\t\t}\n\t}\n\n\tidx_slice := []int{}\n\tfor k, _ := range art_map {\n\t\tidx_slice = append(idx_slice, int(k))\n\t}\n\tsort.Ints(idx_slice) \/\/ \u6392\u5e8f\u4e00\u4e0b\n\n\tfor _, v := range idx_slice {\n\n\t\tfmt.Printf(\"%c: %v; \", rune(v), art_map[rune(v)])\n\t}\n\tfmt.Println()\n}\n\n# go run main.go\nE: 1; F: 1; G: 1; O: 2; P: 2; S: 1; T: 1; U: 1; a: 35; b: 4; c: 9; d: 9; e: 46; f: 15; g: 7; h: 17; i: 34; k: 5; l: 24; m: 11; n: 30; o: 25; p: 10; r: 35; s: 15; t: 29; u: 8; v: 3; w: 5; x: 2; y: 5;<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p><p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.liutianfeng.com\">liutianfeng.com<\/a> &raquo; <a href=\"https:\/\/www.liutianfeng.com\/?p=1730\">\u6620\u5c04 &#8211; Map<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49\uff1a\u6620\u5c04\u662f\u5b58\u50a8\u4e00\u7cfb\u5217\u65e0\u5e8f\u7684key\/value\u5bf9\uff0c\u901a\u8fc7key\u6765\u5bf9value\u8fdb\u884c\u64cd\u4f5c\uff08\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\uff09\u3002Map\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1730"}],"collection":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1730"}],"version-history":[{"count":1,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions"}],"predecessor-version":[{"id":1731,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1730\/revisions\/1731"}],"wp:attachment":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}