{"id":208077,"date":"2025-07-08T11:33:11","date_gmt":"2025-07-08T03:33:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208077\/"},"modified":"2025-07-08T11:33:11","modified_gmt":"2025-07-08T03:33:11","slug":"%e6%9b%b4%e6%94%b9%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e5%85%a8%e5%b1%80%e5%88%87%e7%89%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208077\/","title":{"rendered":"\u66f4\u6539\u51fd\u6570\u4e2d\u7684\u5168\u5c40\u5207\u7247"},"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>\u66f4\u6539\u51fd\u6570\u4e2d\u7684\u5168\u5c40\u5207\u7247<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 17:00:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u66f4\u6539\u51fd\u6570\u4e2d\u7684\u5168\u5c40\u5207\u7247\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4ece\u672a\u4f7f\u7528\u8fc7 c\/c++\uff0c\u6240\u4ee5\u6211\u5bf9 go \u4e2d\u7684\u6307\u9488\u6709\u70b9\u56f0\u60d1\u3002\u95ee\u9898\u662f\uff1a\u6211\u6bcf\u4e2a\u201c\u623f\u95f4\u201d\u90fd\u6709\u4e00\u4e2a <code>map[string][]interactiveitems<\/code> \uff0c\u6211\u60f3\u5728\u51fd\u6570\u4e2d\u66f4\u6539\u5b83\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u662f\uff1a<\/p>\n<pre>func (r *room) getItem(arg string) InteractiveItem {\nfor i, val := range r.interactiveItems {\n    for _, item := range val {\n        if item.getName() == arg {\n            var idxToDelete int\n            for idx := range val {\n                if val[idx].getName() == arg {\n                    idxToDelete = idx\n                    break\n                }\n                if len(val) == 0 {\n                    delete(r.interactiveItems, i)\n                }\n            }\n            val = append(val[:idxToDelete], val[idxToDelete+1:]...)\n            return item\n        }\n    }\n}\nreturn nil<\/pre>\n<p>\u663e\u7136\uff0c<code>val<\/code> \u5728\u51fd\u6570\u5185\u90e8\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u623f\u95f4\u7684\u5730\u56fe\u6ca1\u6709\u53d8\u5316\u3002\u6211\u5e94\u8be5\u5982\u4f55\u5904\u7406\u5220\u9664\u5207\u7247\u5143\u7d20\u7684\u6307\u9488\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a go \u4e2d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u662f\u6309\u503c\u4f20\u9012\u7684\u3002<\/p>\n<p>\u8fd9\u5bf9\u4e8e\u60a8\u7684\u60c5\u51b5\u610f\u5473\u7740\uff0c\u5f53\u60a8\u8fed\u4ee3\u5730\u56fe\u65f6\uff0c\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u90fd\u4f1a\u4e3a <code>val<\/code> \u53d8\u91cf\u5206\u914d\u76f8\u5e94\u5207\u7247\u503c\u7684<em>\u526f\u672c<\/em>\u3002\u56e0\u6b64\uff0c\u5728\u5faa\u73af\u5185\u91cd\u65b0\u5207\u7247 <code>val<\/code> \u503c\u5bf9\u5730\u56fe\u5185\u7684\u539f\u59cb\u5207\u7247\u6ca1\u6709\u5f71\u54cd\uff0c\u56e0\u4e3a\u4e00\u4e2a\u662f\u53e6\u4e00\u4e2a\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u5bf9\u53e6\u4e00\u4e2a\u7684\u5f15\u7528\u3002<\/p>\n<p>\u8981\u66f4\u6539\u5730\u56fe\u5185\u7684\u5207\u7247\uff0c\u60a8\u53ef\u4ee5\u5c06 <code>append<\/code> \u64cd\u4f5c\u7684\u7ed3\u679c\u91cd\u65b0\u5206\u914d\u7ed9\u76f8\u5e94\u7684\u5730\u56fe\u952e\u3002<\/p>\n<pre>r.interactiveItems[i] = append(val[:idxToDelete], val[idxToDelete+1:]...)<\/pre>\n<p>\u8bf7\u8bb0\u4f4f\uff0c\u590d\u5236\u5207\u7247\u65f6\uff0c\u5b83\u4e0d\u4f1a\u590d\u5236\u5207\u7247\u6307\u5411\u7684\u6570\u636e\uff0c\u800c\u53ea\u4f1a\u590d\u5236\u5207\u7247\u7684\u201c\u63cf\u8ff0\u7b26\u201d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-208077","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208077","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=208077"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208077\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}