{"id":668,"date":"2025-01-02T22:27:15","date_gmt":"2025-01-02T14:27:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=668"},"modified":"2025-01-02T22:27:15","modified_gmt":"2025-01-02T14:27:15","slug":"%e5%9c%a8-go-map%e4%b8%ad-%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%ae%9a%e4%b9%89%e9%94%ae%e7%9b%b8%e7%ad%89%e9%80%bb%e8%be%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/02\/%e5%9c%a8-go-map%e4%b8%ad-%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%ae%9a%e4%b9%89%e9%94%ae%e7%9b%b8%e7%ad%89%e9%80%bb%e8%be%91%ef%bc%9f\/","title":{"rendered":"\u5728 Go map\u4e2d\u00a0\u600e\u6837\u5b9e\u73b0\u81ea\u5b9a\u4e49\u952e\u76f8\u7b49\u903b\u8f91\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c<code>map<\/code> \u7684\u952e\u5fc5\u987b\u662f\u53ef\u4ee5\u6bd4\u8f83\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u6216\u7531\u8fd9\u4e9b\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u5982\u679c\u4f60\u9700\u8981\u81ea\u5b9a\u4e49\u952e\u76f8\u7b49\u903b\u8f91\uff08\u4f8b\u5982\u5728\u952e\u4e2d\u6bd4\u8f83\u67d0\u4e9b\u5b57\u6bb5\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u578b\u548c <code>struct<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7684 <code>struct<\/code> \u7c7b\u578b\u53ef\u4ee5\u4f5c\u4e3a <code>map<\/code> \u7684\u952e\uff0c\u4f46\u6240\u6709\u5b57\u6bb5\u5fc5\u987b\u662f\u53ef\u6bd4\u8f83\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u7279\u5b9a\u7684\u76f8\u7b49\u903b\u8f91\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 <code>struct<\/code> \u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4ee5\u4e24\u4e2a\u5b57\u6bb5\u76f8\u7b49\u4e3a\u51c6<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u7ed3\u6784\u4f53\u4f5c\u4e3a map \u7684\u952e\ntype Key struct {\n    Field1 string\n    Field2 int\n}\n\nfunc main() {\n    m := make(map&#91;Key]string)\n\n    \/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u952e\n    key1 := Key{\"Alice\", 42}\n    key2 := Key{\"Bob\", 30}\n\n    m&#91;key1] = \"Engineer\"\n    m&#91;key2] = \"Doctor\"\n\n    fmt.Println(m&#91;key1]) \/\/ \u8f93\u51fa: Engineer\n    fmt.Println(m&#91;key2]) \/\/ \u8f93\u51fa: Doctor\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5bf9\u4e8e <code>struct<\/code> \u7c7b\u578b\uff0c\u6240\u6709\u5b57\u6bb5\u90fd\u4f1a\u9ed8\u8ba4\u53c2\u4e0e\u6bd4\u8f83\u3002\u5982\u679c\u9700\u8981\u63a7\u5236\u54ea\u4e9b\u5b57\u6bb5\u53c2\u4e0e\u6bd4\u8f83\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5\u7684\u503c\u552f\u4e00\u6027\u4fdd\u8bc1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528\u6307\u9488\u6216\u5f15\u7528\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u952e\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u6307\u9488\u6216\u5207\u7247\u7b49\u4e0d\u53ef\u76f4\u63a5\u6bd4\u8f83\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u54c8\u5e0c\u503c\u6216\u5176\u4ed6\u552f\u4e00\u6807\u8bc6\u6765\u4f5c\u4e3a\u952e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528\u54c8\u5e0c\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"hash\/fnv\"\n)\n\n\/\/ \u751f\u6210\u5b57\u7b26\u4e32\u7684\u552f\u4e00\u54c8\u5e0c\u503c\nfunc hash(s string) uint64 {\n    h := fnv.New64a()\n    h.Write(&#91;]byte(s))\n    return h.Sum64()\n}\n\nfunc main() {\n    m := make(map&#91;uint64]string)\n\n    key1 := \"Alice\"\n    key2 := \"Bob\"\n\n    m&#91;hash(key1)] = \"Engineer\"\n    m&#91;hash(key2)] = \"Doctor\"\n\n    fmt.Println(m&#91;hash(key1)]) \/\/ \u8f93\u51fa: Engineer\n    fmt.Println(m&#91;hash(key2)]) \/\/ \u8f93\u51fa: Doctor\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 3\uff1a\u901a\u8fc7\u5c01\u88c5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u903b\u8f91<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e00\u4e2a\u5305\u88c5\u7c7b\u578b\uff0c\u63d0\u4f9b <code>Equal<\/code> \u65b9\u6cd5\uff0c\u4f7f\u7528 <code>map[string]T<\/code> \u6a21\u62df\u81ea\u5b9a\u4e49\u952e\u7684\u76f8\u7b49\u903b\u8f91\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5b9e\u73b0\u81ea\u5b9a\u4e49\u903b\u8f91<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"strconv\"\n)\n\n\/\/ \u81ea\u5b9a\u4e49\u952e\ntype CustomKey struct {\n    Field1 string\n    Field2 int\n}\n\n\/\/ \u751f\u6210\u552f\u4e00\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\nfunc (k CustomKey) Key() string {\n    return k.Field1 + \":\" + strconv.Itoa(k.Field2)\n}\n\nfunc main() {\n    m := make(map&#91;string]string)\n\n    key1 := CustomKey{\"Alice\", 42}\n    key2 := CustomKey{\"Bob\", 30}\n\n    m&#91;key1.Key()] = \"Engineer\"\n    m&#91;key2.Key()] = \"Doctor\"\n\n    fmt.Println(m&#91;key1.Key()]) \/\/ \u8f93\u51fa: Engineer\n    fmt.Println(m&#91;key2.Key()]) \/\/ \u8f93\u51fa: Doctor\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 4\uff1a\u5c01\u88c5\u903b\u8f91\u5728\u64cd\u4f5c\u51fd\u6570\u4e2d\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5c01\u88c5\u7684 <code>Map<\/code> \u7c7b\u578b\uff0c\u5e76\u5728\u64cd\u4f5c\u51fd\u6570\u4e2d\u5904\u7406\u952e\u76f8\u7b49\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5c01\u88c5 Map<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u81ea\u5b9a\u4e49\u952e\u7c7b\u578b\ntype CustomKey struct {\n    Field1 string\n    Field2 int\n}\n\n\/\/ \u81ea\u5b9a\u4e49 Map\ntype CustomMap struct {\n    data map&#91;string]string\n}\n\n\/\/ \u6dfb\u52a0\u952e\u503c\u5bf9\nfunc (cm *CustomMap) Set(key CustomKey, value string) {\n    compositeKey := fmt.Sprintf(\"%s:%d\", key.Field1, key.Field2)\n    cm.data&#91;compositeKey] = value\n}\n\n\/\/ \u83b7\u53d6\u503c\nfunc (cm *CustomMap) Get(key CustomKey) (string, bool) {\n    compositeKey := fmt.Sprintf(\"%s:%d\", key.Field1, key.Field2)\n    value, ok := cm.data&#91;compositeKey]\n    return value, ok\n}\n\nfunc main() {\n    m := &amp;CustomMap{data: make(map&#91;string]string)}\n\n    key1 := CustomKey{\"Alice\", 42}\n    key2 := CustomKey{\"Bob\", 30}\n\n    m.Set(key1, \"Engineer\")\n    m.Set(key2, \"Doctor\")\n\n    value, ok := m.Get(key1)\n    if ok {\n        fmt.Println(value) \/\/ \u8f93\u51fa: Engineer\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5982\u679c\u952e\u7684\u7c7b\u578b\u53ef\u6bd4\u8f83\uff0c\u76f4\u63a5\u4f7f\u7528 <code>struct<\/code> \u4f5c\u4e3a\u952e\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u952e\u4e0d\u53ef\u6bd4\u8f83\uff08\u5982\u5207\u7247\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u552f\u4e00\u6807\u8bc6\u6216\u5c01\u88c5\u903b\u8f91\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u903b\u8f91\u590d\u6742\uff0c\u5c01\u88c5\u4e00\u4e2a\u5de5\u5177\u7c7b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u4e1a\u52a1\u9700\u6c42\u548c\u952e\u7684\u590d\u6742\u7a0b\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0cmap \u7684\u952e\u5fc5\u987b\u662f\u53ef\u4ee5\u6bd4\u8f83\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u6216\u7531\u8fd9\u4e9b\u7c7b\u578b\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u5982\u679c\u4f60 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/668\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}