{"id":57696,"date":"2024-10-10T20:26:21","date_gmt":"2024-10-10T12:26:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57696\/"},"modified":"2024-10-10T20:26:22","modified_gmt":"2024-10-10T12:26:22","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-map%ef%bc%88go-%e8%aa%9e%e8%a8%80%e6%98%a0%e5%b0%84%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57696\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00 map\uff08Go \u8a9e\u8a00\u6620\u5c04\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-map%ef%bc%88go-%e8%aa%9e%e8%a8%80%e6%98%a0%e5%b0%84%ef%bc%89-oeMqWYkMur\">GO\u6559\u7a0b: Go \u8a9e\u8a00 map\uff08Go \u8a9e\u8a00\u6620\u5c04\uff09<\/h1>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0cmap \u662f\u4e00\u7a2e\u5167\u5efa\u7684\u6578\u64da\u7d50\u69cb\uff0c\u7528\u65bc\u5b58\u5132\u9375\u503c\u5c0d\uff08key-value pairs\uff09\u3002\u5b83\u985e\u4f3c\u65bc\u5176\u4ed6\u7de8\u7a0b\u8a9e\u8a00\u4e2d\u7684\u5b57\u5178\u6216\u54c8\u5e0c\u8868\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u4e2d\u7684 map\uff0c\u5305\u62ec\u5176\u57fa\u672c\u7528\u6cd5\u3001\u7279\u6027\u4ee5\u53ca\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"1-map-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-oeMqWYkMur\">1. map \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>map \u662f\u4e00\u7a2e\u7121\u5e8f\u7684\u96c6\u5408\uff0c\u901a\u904e\u9375\u4f86\u8a2a\u554f\u5c0d\u61c9\u7684\u503c\u3002\u6bcf\u500b\u9375\u5728 map \u4e2d\u662f\u552f\u4e00\u7684\uff0c\u9019\u610f\u5473\u8457\u4e0d\u80fd\u6709\u91cd\u8907\u7684\u9375\u3002Go \u8a9e\u8a00\u4e2d\u7684 map \u662f\u5f15\u7528\u985e\u578b\uff0c\u9019\u610f\u5473\u8457\u7576\u4f60\u5c07\u4e00\u500b map \u8ce6\u503c\u7d66\u53e6\u4e00\u500b\u8b8a\u91cf\u6642\uff0c\u5169\u8005\u5be6\u969b\u4e0a\u6307\u5411\u540c\u4e00\u500b\u5e95\u5c64\u6578\u64da\u7d50\u69cb\u3002<\/p>\n<h2 id=\"2-%e5%89%b5%e5%bb%ba-map-oeMqWYkMur\">2. \u5275\u5efa map<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5167\u5efa\u7684 make \u51fd\u6578\u4f86\u5275\u5efa map\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684 map \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ \u5275\u5efa\u4e00\u500b map\uff0c\u9375\u70ba string \u985e\u578b\uff0c\u503c\u70ba int \u985e\u578b\n    ages := make(map[string]int)\n\n    \/\/ \u6dfb\u52a0\u6578\u64da\n    ages[\"Alice\"] = 25\n    ages[\"Bob\"] = 30\n\n    fmt.Println(ages) \/\/ \u8f38\u51fa: map[Alice:25 Bob:30]\n}\n<\/code><\/pre>\n<h2 id=\"3-%e8%a8%aa%e5%95%8f%e5%92%8c%e4%bf%ae%e6%94%b9-map-%e7%9a%84%e5%80%bc-oeMqWYkMur\">3. \u8a2a\u554f\u548c\u4fee\u6539 map \u7684\u503c<\/h2>\n<p>\u53ef\u4ee5\u901a\u904e\u9375\u4f86\u8a2a\u554f map \u4e2d\u7684\u503c\u3002\u5982\u679c\u9375\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9\u7684\u503c\uff1b\u5982\u679c\u9375\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u8a72\u503c\u985e\u578b\u7684\u96f6\u503c\u3002\u4ee5\u4e0b\u662f\u8a2a\u554f\u548c\u4fee\u6539 map \u503c\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ages := map[string]int{\"Alice\": 25, \"Bob\": 30}\n\n    \/\/ \u8a2a\u554f\u503c\n    fmt.Println(ages[\"Alice\"]) \/\/ \u8f38\u51fa: 25\n\n    \/\/ \u4fee\u6539\u503c\n    ages[\"Alice\"] = 26\n    fmt.Println(ages[\"Alice\"]) \/\/ \u8f38\u51fa: 26\n\n    \/\/ \u8a2a\u554f\u4e0d\u5b58\u5728\u7684\u9375\n    fmt.Println(ages[\"Charlie\"]) \/\/ \u8f38\u51fa: 0\n}\n<\/code><\/pre>\n<h2 id=\"4-%e5%88%aa%e9%99%a4-map-%e4%b8%ad%e7%9a%84%e9%8d%b5%e5%80%bc%e5%b0%8d-oeMqWYkMur\">4. \u522a\u9664 map \u4e2d\u7684\u9375\u503c\u5c0d<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5167\u5efa\u7684 delete \u51fd\u6578\u4f86\u522a\u9664 map \u4e2d\u7684\u9375\u503c\u5c0d\u3002\u4ee5\u4e0b\u662f\u522a\u9664\u64cd\u4f5c\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ages := map[string]int{\"Alice\": 25, \"Bob\": 30}\n\n    \/\/ \u522a\u9664\u9375\u70ba \"Bob\" \u7684\u9375\u503c\u5c0d\n    delete(ages, \"Bob\")\n\n    fmt.Println(ages) \/\/ \u8f38\u51fa: map[Alice:25]\n}\n<\/code><\/pre>\n<h2 id=\"5-%e9%81%8d%e6%ad%b7-map-oeMqWYkMur\">5. \u904d\u6b77 map<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u74b0\u4f86\u904d\u6b77 map \u4e2d\u7684\u6240\u6709\u9375\u503c\u5c0d\u3002\u4ee5\u4e0b\u662f\u904d\u6b77 map \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ages := map[string]int{\"Alice\": 25, \"Bob\": 30}\n\n    \/\/ \u904d\u6b77 map\n    for name, age := range ages {\n        fmt.Printf(\"%s is %d years oldn\", name, age)\n    }\n}\n<\/code><\/pre>\n<h2 id=\"6-map-%e7%9a%84%e7%89%b9%e6%80%a7-oeMqWYkMur\">6. map \u7684\u7279\u6027<\/h2>\n<ul>\n<li>\u7121\u5e8f\uff1amap \u4e2d\u7684\u9375\u503c\u5c0d\u662f\u7121\u5e8f\u7684\uff0c\u7121\u6cd5\u4fdd\u8b49\u904d\u6b77\u7684\u9806\u5e8f\u3002<\/li>\n<li>\u52d5\u614b\u5927\u5c0f\uff1amap \u7684\u5927\u5c0f\u53ef\u4ee5\u96a8\u8457\u6dfb\u52a0\u6216\u522a\u9664\u9375\u503c\u5c0d\u800c\u52d5\u614b\u6539\u8b8a\u3002<\/li>\n<li>\u9375\u7684\u985e\u578b\uff1amap \u7684\u9375\u53ef\u4ee5\u662f\u4efb\u4f55\u53ef\u6bd4\u8f03\u7684\u985e\u578b\uff0c\u5982\u6574\u6578\u3001\u6d6e\u9ede\u6578\u3001\u5b57\u7b26\u4e32\u7b49\uff0c\u4f46\u4e0d\u80fd\u662f\u5207\u7247\u3001\u51fd\u6578\u6216\u5176\u4ed6 map\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oeMqWYkMur\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u4e2d\u7684 map \u662f\u4e00\u7a2e\u5f37\u5927\u4e14\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0c\u9069\u5408\u7528\u65bc\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u5b58\u5132\u9375\u503c\u5c0d\u7684\u5834\u666f\u3002\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u76f8\u4fe1\u8b80\u8005\u5c0d Go \u8a9e\u8a00\u7684 map \u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684 map\uff08\u6620\u5c04\uff09\u529f\u80fd\uff0c\u5b78\u7fd2\u5982\u4f55\u5275\u5efa\u3001\u64cd\u4f5c\u548c\u61c9\u7528\u9019\u4e00\u5f37\u5927\u6578\u64da\u7d50\u69cb\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57696","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57696","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=57696"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57696\/revisions"}],"predecessor-version":[{"id":57697,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57696\/revisions\/57697"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}