{"id":207957,"date":"2025-07-08T08:52:33","date_gmt":"2025-07-08T00:52:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207957\/"},"modified":"2025-07-08T08:52:33","modified_gmt":"2025-07-08T00:52:33","slug":"golang-%e6%b3%9b%e5%9e%8b%e5%9c%a8%e7%94%a8%e4%bd%9c%e5%9c%b0%e5%9b%be%e5%85%83%e7%b4%a0%e6%97%b6%e4%b8%8d%e8%b5%b7%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207957\/","title":{"rendered":"Golang \u6cdb\u578b\u5728\u7528\u4f5c\u5730\u56fe\u5143\u7d20\u65f6\u4e0d\u8d77\u4f5c\u7528"},"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>Golang \u6cdb\u578b\u5728\u7528\u4f5c\u5730\u56fe\u5143\u7d20\u65f6\u4e0d\u8d77\u4f5c\u7528<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 12:12:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aGolang \u6cdb\u578b\u5728\u7528\u4f5c\u5730\u56fe\u5143\u7d20\u65f6\u4e0d\u8d77\u4f5c\u7528\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u521b\u5efa\u4e86\u4e00\u4e2amapkeys\u6cdb\u578b\u548clist\u6cdb\u578b\uff0c\u4f46\u662f\u5f53\u5c06list\u6cdb\u578b\u4e0e\u666e\u901amap[string]list[int]\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u6211\u65e0\u6cd5\u8c03\u7528\u6cdb\u578b\u7684\u65b9\u6cd5\uff0c\u6211\u9519\u4e86\u4ec0\u4e48\uff1f\u4efb\u4f55\u60f3\u6cd5\u5c06\u4e0d\u80dc\u611f\u6fc0\uff01<\/p>\n<p>\u50cf\u8fd9\u6837\u7684\u901a\u7528\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc mapkeys[k comparable, v any](m map[k]v) []k {\n    r := make([]k, 0, len(m))\n    for k := range m {\n        r = append(r, k)\n    }\n    return r\n}\n\ntype list[t any] struct {\n    head, tail *element[t]\n}\n\ntype element[t any] struct {\n    next *element[t]\n    val  t\n}\n\nfunc (lst *list[t]) push(v t) {\n    if lst.tail == nil {\n        lst.head = &amp;element[t]{val: v}\n        lst.tail = lst.head\n    } else {\n        lst.tail.next = &amp;element[t]{val: v}\n        lst.tail = lst.tail.next\n    }\n}\n\nfunc (lst *list[t]) getall() []t {\n    var elems []t\n    for e := lst.head; e != nil; e = e.next {\n        elems = append(elems, e.val)\n    }\n    return elems\n}\n\nfunc main() {\n    var m = map[int]string{1: \"2\", 2: \"4\", 4: \"8\"}\n\n    fmt.println(\"keys m:\", mapkeys(m))\n\n    _ = mapkeys[int, string](m)\n\n    var yx = make(map[string]list[int])\n    yx[\"one\"] = list[int]{}\n    yx[\"one\"].push(10)\n\n    lst := list[int]{}\n    lst.push(10)\n    lst.push(13)\n    lst.push(23)\n    fmt.println(\"list:\", lst.getall())\n}<\/pre>\n<p>\u8c03\u7528\u6b64\u884c\uff1a<\/p>\n<pre>yx[\"one\"].Push(10)<\/pre>\n<h2>\u6536\u5230\u9519\u8bef\u6d88\u606f\uff1a<\/h2>\n<p>.\/prog.go:49:12: \u65e0\u6cd5\u8c03\u7528 list[int] \u4e0a\u7684\u6307\u9488\u65b9\u6cd5 push<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u6b64\u5904\u64ad\u653e\u4ee3\u7801<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p><code>list.push()<\/code> \u6709\u6307\u9488\u63a5\u6536\u5668\uff0c\u4f46\u60a8\u5728\u6620\u5c04\u4e2d\u5b58\u50a8\u975e\u6307\u9488\uff0c\u5e76\u4e14\u7d22\u5f15\u6620\u5c04\u4e0d\u662f \u3002\u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 \u3002<\/p>\n<p>\u5c06\u6307\u9488\u5b58\u50a8\u5728\u5730\u56fe\u4e2d\uff0c\u7136\u540e\u60a8\u53ef\u4ee5\u5728\u5730\u56fe\u7d22\u5f15\u7ed3\u679c\u4e0a\u8c03\u7528\u5e26\u6709\u6307\u9488\u63a5\u6536\u5668\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>var yx = make(map[string]*list[int])\nyx[\"one\"] = &amp;list[int]{}\nyx[\"one\"].push(10)<\/pre>\n<p>\u5728 \u4e0a\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u503c\u662f\u53ef\u5bfb\u5740\u7684\uff0c\u4f8b\u5982\u53d8\u91cf\uff0c\u5219\u53ef\u4ee5\u5728\u975e\u6307\u9488\u503c\u4e0a\u8c03\u7528\u5e26\u6709\u6307\u9488\u63a5\u6536\u5668\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>lst := List[int]{}\nlst.Push(10)<\/pre>\n<p>\u4e0a\u9762\u7684<code>lst.push(10)<\/code>\u662f<code>(&amp;lst).push(10)<\/code>\u7684\u7b80\u5199\u3002\u8be6\u60c5\u8bf7\u89c1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGolang \u6cdb\u578b\u5728\u7528\u4f5c\u5730\u56fe\u5143\u7d20\u65f6\u4e0d\u8d77\u4f5c\u7528\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/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-207957","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207957","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=207957"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207957\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}