{"id":184290,"date":"2024-05-08T22:05:08","date_gmt":"2024-05-08T14:05:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/184290.html"},"modified":"2024-05-08T22:05:14","modified_gmt":"2024-05-08T14:05:14","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88go%e8%af%ad%e8%a8%80%e7%9a%84%e5%ad%97%e5%85%b8%e6%80%a7%e8%83%bd%e4%b8%8d%e5%a6%82c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/184290.html","title":{"rendered":"\u4e3a\u4ec0\u4e48GO\u8bed\u8a00\u7684\u5b57\u5178\u6027\u80fd\u4e0d\u5982C#"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27075349\/a4f0c1d6-8fb2-43ab-b5d1-c118013ed059.webp\" alt=\"\u4e3a\u4ec0\u4e48GO\u8bed\u8a00\u7684\u5b57\u5178\u6027\u80fd\u4e0d\u5982C#\" \/><\/p>\n<p><p>Go\u8bed\u8a00\u7684\u5b57\u5178\uff08map\uff09\u5728\u6027\u80fd\u4e0a\u901a\u5e38\u4e0d\u5982C#\u7684\u5b57\u5178\uff08Dictionary\uff09\uff0c\u4e3b\u8981\u56e0\u4e3a\u51e0\u4e2a\u6838\u5fc3\u56e0\u7d20\uff1a<strong>\u5185\u5b58\u5206\u914d\u7b56\u7565\u3001\u5e76\u53d1\u5904\u7406\u673a\u5236\u3001\u54c8\u5e0c\u51fd\u6570\u7684\u8bbe\u8ba1\u4ee5\u53ca\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u7ed3\u6784<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5e76\u53d1\u5904\u7406\u673a\u5236<\/strong>\u7684\u5dee\u5f02\u5bf9\u6027\u80fd\u5f71\u54cd\u5c24\u5176\u663e\u8457\u3002<\/p>\n<\/p>\n<p><p><strong>C#\u7684Dictionary\u5728\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u7ebf\u7a0b\u5b89\u5168\u7b56\u7565\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982ConcurrentDictionary\uff0c\u4f18\u5316\u4e86\u591a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u6027\u80fd\uff0c\u51cf\u5c11\u4e86\u9501\u7684\u7ade\u4e89<\/strong>\u3002\u800cGo\u7684map\u6ca1\u6709\u5185\u7f6e\u7684\u5e76\u53d1\u5b89\u5168\u4fdd\u969c\uff0c\u76f4\u63a5\u4f7f\u7528\u65f6\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u9700\u8981\u5916\u90e8\u540c\u6b65\u673a\u5236\uff08\u5982sync.Mutex\uff09\u6765\u4fdd\u62a4\uff0c\u8fd9\u589e\u52a0\u4e86\u5f00\u9500\u5e76\u5f71\u54cd\u4e86\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5185\u5b58\u5206\u914d\u7b56\u7565<\/h3>\n<\/p>\n<p><p>Go\u8bed\u8a00\u7684map\u548cC#\u7684Dictionary\u5728\u5185\u5b58\u5206\u914d\u7b56\u7565\u4e0a\u6709\u6240\u5dee\u5f02\u3002Go\u7684map\u5728\u521d\u59cb\u5316\u65f6\u5e76\u4e0d\u7acb\u5373\u5206\u914d\u5185\u5b58\uff0c\u800c\u662f\u5728\u6dfb\u52a0\u7b2c\u4e00\u4e2a\u5143\u7d20\u65f6\u624d\u8fdb\u884c\u5185\u5b58\u5206\u914d\uff0c\u8fd9\u79cd\u60f0\u6027\u5206\u914d\u7b56\u7565\u867d\u7136\u8282\u7ea6\u4e86\u672a\u4f7f\u7528map\u65f6\u7684\u5185\u5b58\uff0c\u4f46\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u9891\u7e41\u5730\u589e\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u5bfc\u81f4\u9891\u7e41\u7684\u5185\u5b58\u91cd\u65b0\u5206\u914d\u548c\u590d\u5236\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>C#\u7684Dictionary\u5219\u5728\u521d\u59cb\u5316\u65f6\u5c31\u6839\u636e\u6307\u5b9a\u7684\u5bb9\u91cf\u9884\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u51cf\u5c11\u4e86\u968f\u7740\u5143\u7d20\u6dfb\u52a0\u65f6\u7684\u5185\u5b58\u91cd\u5206\u914d\u6b21\u6570\uff0c\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u64cd\u4f5c\u6709\u8f83\u597d\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e76\u53d1\u5904\u7406\u673a\u5236<\/h3>\n<\/p>\n<p><p>Go\u8bed\u8a00\u7684map\u5728\u8bbe\u8ba1\u65f6\u6ca1\u6709\u8003\u8651\u5230\u7ebf\u7a0b\u6216\u534f\u7a0b\u7684\u5e76\u53d1\u5b89\u5168\uff0c\u8fd9\u610f\u5473\u7740\u5728\u5e76\u53d1\u8bbf\u95eemap\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u6070\u5f53\u7684\u540c\u6b65\u673a\u5236\uff0c\u5f88\u5bb9\u6613\u53d1\u751f\u7ade\u6001\u6761\u4ef6\u3002\u5f00\u53d1\u8005\u901a\u5e38\u9700\u8981\u4f7f\u7528sync\u5305\u4e2d\u7684Mutex\u6216\u8005RWMutex\u6765\u4fdd\u62a4map\uff0c\u8fd9\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4f1a\u9020\u6210\u663e\u8457\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e4b\u4e0b\uff0cC#\u63d0\u4f9b\u4e86\u4e13\u4e3a\u5e76\u53d1\u573a\u666f\u8bbe\u8ba1\u7684ConcurrentDictionary\uff0c\u5b83\u901a\u8fc7\u7ec6\u7c92\u5ea6\u9501\u7684\u7b56\u7565\u548c\u9ad8\u6548\u7684\u5e76\u53d1\u7b97\u6cd5\uff0c\u5927\u5927\u51cf\u5c11\u4e86\u9501\u7684\u4e89\u7528\uff0c\u63d0\u5347\u4e86\u6027\u80fd\u3002\u5c24\u5176\u662f\u5728\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\u4e0b\uff0cConcurrentDictionary\u7684\u8868\u73b0\u8981\u8fdc\u8fdc\u597d\u4e8e\u52a0\u9501\u4fdd\u62a4\u7684Go map\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u54c8\u5e0c\u51fd\u6570\u7684\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u54c8\u5e0c\u51fd\u6570\u7684\u8bbe\u8ba1\u76f4\u63a5\u5f71\u54cd\u5230\u5b57\u5178\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\u65f6\u7684\u6548\u7387\u3002Go\u8bed\u8a00\u7684map\u4f7f\u7528\u4e00\u79cd\u8f83\u4e3a\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u5e76\u901a\u8fc7\u94fe\u8868\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u826f\u597d\uff0c\u4f46\u5f53\u6570\u636e\u89c4\u6a21\u589e\u5927\u6216\u51b2\u7a81\u8f83\u591a\u65f6\uff0c\u6027\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>C#\u7684Dictionary\u5219\u91c7\u7528\u4e86\u66f4\u4e3a\u590d\u6742\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7ed3\u5408\u5f00\u653e\u5bfb\u5740\uff08Open Addressing\uff09\u7b49\u6280\u672f\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\uff0c\u5373\u4f7f\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u4e0a\u4e5f\u80fd\u4fdd\u6301\u8f83\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>Go\u8bed\u8a00\u7684map\u5728\u5b58\u50a8\u7ed3\u6784\u4e0a\u91c7\u7528\u4e86\u54c8\u5e0c\u8868+\u94fe\u8868\u7684\u7ec4\u5408\uff0c\u5373\u6bcf\u4e2a\u54c8\u5e0c\u6876\uff08bucket\uff09\u901a\u8fc7\u94fe\u8868\u8fde\u63a5\u8d77\u6765\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u7ed3\u6784\u5728\u5904\u7406\u51b2\u7a81\u65f6\u6bd4\u8f83\u7075\u6d3b\uff0c\u4f46\u662f\u8bbf\u95ee\u6548\u7387\u53d7\u5230\u94fe\u8868\u6027\u80fd\u7684\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p>C#\u7684Dictionary\u5728\u5185\u90e8\u5219\u91c7\u7528\u4e86\u6570\u7ec4\u52a0\u94fe\u8868\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u4e00\u4e2a\u8f83\u5927\u7684\u6570\u7ec4\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\uff0c\u51b2\u7a81\u53d1\u751f\u65f6\u518d\u4f7f\u7528\u94fe\u8868\u8fde\u63a5\u51b2\u7a81\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6570\u7ec4\u7d22\u5f15\u5feb\u901f\u8bbf\u95ee\u5230\u5143\u7d20\uff0c\u4ece\u800c\u62e5\u6709\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cGo\u8bed\u8a00\u7684map\u5728\u8bbe\u8ba1\u4e0a\u66f4\u6ce8\u91cd\u7b80\u5355\u548c\u901a\u7528\uff0c\u800c\u6ca1\u6709\u9488\u5bf9\u7279\u5b9a\u573a\u666f\u8fdb\u884c\u4f18\u5316\uff0c\u8fd9\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u5408\u7406\u7684\u9009\u62e9\u3002\u4f46\u662f\uff0c\u5f53\u6d89\u53ca\u5230\u9700\u8981\u9ad8\u6027\u80fd\u5904\u7406\u7684\u7279\u5b9a\u573a\u666f\u65f6\uff0c\u5982\u9ad8\u5e76\u53d1\u8bbf\u95ee\uff0cC#\u901a\u8fc7\u63d0\u4f9b\u66f4\u7ec6\u81f4\u7684\u5185\u5b58\u5206\u914d\u7b56\u7565\u3001\u9ad8\u6548\u7684\u5e76\u53d1\u5b89\u5168\u673a\u5236\u3001\u4f18\u5316\u7684\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u548c\u5b58\u50a8\u7ed3\u6784\uff0c\u5c55\u73b0\u51fa\u4e86\u66f4\u4f18\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. GO\u8bed\u8a00\u7684\u5b57\u5178\u6027\u80fd\u76f8\u5bf9\u8f83\u4f4e\u7684\u539f\u56e0\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>GO\u8bed\u8a00\u7684\u5b57\u5178\u6027\u80fd\u76f8\u5bf9\u8f83\u4f4e\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5176\u5b57\u5178\u5e95\u5c42\u4f7f\u7528\u4e86\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002\u867d\u7136\u54c8\u5e0c\u8868\u5728\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u4e0a\u5177\u6709\u8f83\u9ad8\u7684\u6548\u7387\uff0c\u4f46\u7531\u4e8eGO\u8bed\u8a00\u7684\u5b57\u5178\u5e95\u5c42\u5b9e\u73b0\u91c7\u7528\u4e86\u5f00\u653e\u5bfb\u5740\u6cd5\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u8fd9\u5bfc\u81f4\u5b57\u5178\u7684\u88c5\u8f7d\u56e0\u5b50\u8f83\u9ad8\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u4f1a\u6709\u6240\u4e0b\u964d\u3002<\/p>\n<p><strong>2. \u4e3a\u4ec0\u4e48C#\u7684\u5b57\u5178\u6027\u80fd\u76f8\u5bf9\u8f83\u9ad8\uff1f<\/strong><\/p>\n<p>C#\u8bed\u8a00\u7684\u5b57\u5178\u6027\u80fd\u76f8\u5bf9\u8f83\u9ad8\u4e3b\u8981\u662f\u56e0\u4e3a\u5176\u5b57\u5178\u5e95\u5c42\u4f7f\u7528\u4e86\u5e73\u8861\u4e8c\u53c9\u6811\uff08\u7ea2\u9ed1\u6811\uff09\u6765\u5b9e\u73b0\u3002\u5e73\u8861\u4e8c\u53c9\u6811\u5728\u589e\u52a0\u3001\u5220\u9664\u548c\u67e5\u627e\u5143\u7d20\u65f6\u90fd\u5177\u6709O(log n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u76f8\u5bf9\u4e8e\u54c8\u5e0c\u8868\u7684\u5e38\u6570\u65f6\u95f4\u590d\u6742\u5ea6O(1)\uff0c\u867d\u7136\u66f4\u9ad8\uff0c\u4f46\u5728\u5927\u89c4\u6a21\u6570\u636e\u91cf\u7684\u60c5\u51b5\u4e0b\uff0c\u5e73\u8861\u4e8c\u53c9\u6811\u7684\u6027\u80fd\u66f4\u52a0\u7a33\u5b9a\u3002<\/p>\n<p><strong>3. \u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u6765\u63d0\u9ad8GO\u8bed\u8a00\u5b57\u5178\u7684\u6027\u80fd\u5462\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0c\u901a\u8fc7\u4f18\u5316\u53ef\u4ee5\u63d0\u9ad8GO\u8bed\u8a00\u5b57\u5178\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u8003\u8651\u8c03\u6574\u5b57\u5178\u7684\u88c5\u8f7d\u56e0\u5b50\uff0c\u5c06\u5176\u63a7\u5236\u5728\u4e00\u4e2a\u76f8\u5bf9\u8f83\u4f4e\u7684\u8303\u56f4\uff0c\u8fd9\u6837\u53ef\u4ee5\u964d\u4f4e\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\uff0c\u63d0\u9ad8\u5b57\u5178\u7684\u8bbf\u95ee\u6548\u7387\u3002\u53e6\u5916\uff0cGO\u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86sync.Map\u7c7b\u578b\uff0c\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5b57\u5178\uff0c\u901a\u8fc7\u5229\u7528\u8bfb\u5199\u9501\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5e76\u53d1\u5b89\u5168\uff0c\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u63d0\u9ad8\u5b57\u5178\u7684\u6027\u80fd\u3002\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684\u9ad8\u6027\u80fd\u5b57\u5178\u5e93\uff0c\u4f8b\u5982Golang-DataStructures\u7b49\uff0c\u6765\u66ff\u4ee3GO\u8bed\u8a00\u539f\u751f\u7684\u5b57\u5178\u5b9e\u73b0\uff0c\u4ee5\u63d0\u5347\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Go\u8bed\u8a00\u7684\u5b57\u5178\uff08map\uff09\u5728\u6027\u80fd\u4e0a\u901a\u5e38\u4e0d\u5982C#\u7684\u5b57\u5178\uff08Dictionary\uff09\uff0c\u4e3b\u8981\u56e0\u4e3a\u51e0\u4e2a\u6838\u5fc3\u56e0\u7d20\uff1a\u5185\u5b58\u5206\u914d\u7b56\u7565 [&hellip;]","protected":false},"author":3,"featured_media":184294,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184290"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=184290"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184290\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/184294"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=184290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=184290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=184290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}