{"id":155352,"date":"2020-08-29T22:32:34","date_gmt":"2020-08-29T14:32:34","guid":{"rendered":"http:\/\/4563.org\/?p=155352"},"modified":"2020-08-29T22:32:34","modified_gmt":"2020-08-29T14:32:34","slug":"golang-%e4%b8%ad-map-%e7%9a%84%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=155352","title":{"rendered":"golang \u4e2d map \u7684\u5e76\u53d1\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang \u4e2d map \u7684\u5e76\u53d1\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ksedz <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        golang \u4e2d\u7684 map \u5e76\u53d1\u8bbf\u95ee\u4f1a\u62a5\u9519\uff0c\u4e00\u822c\u5efa\u8bae\u662f\u4f7f\u7528\u8bfb\u5199\u9501\u3002<\/p>\n<p>\u6211\u6709\u4e24\u4e2a\u573a\u666f\u4e0d\u592a\u9002\u5408\u4f7f\u7528\u8bfb\u5199\u9501\uff0c\u6c42\u95ee\u6709\u4ec0\u4e48\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>1. \u5e76\u53d1\u5199\u5165\u4e0d\u540c\u7684 key\uff0c\u8bfb\u53d6\u548c\u5199\u5165\u80af\u5b9a\u4e0d\u4f1a\u540c\u65f6\u8fdb\u884c<br \/>\u5b9e\u9645\u573a\u666f\u662f\u6bcf\u4e2a\u534f\u7a0b\u7ef4\u62a4\u4e00\u4e2a\u4f1a\u8bdd\u4fe1\u606f\uff0c\u4f1a\u8bdd\u4e4b\u95f4\u80af\u5b9a\u4e0d\u4f1a\u51b2\u7a81\uff0c\u4f1a\u8bdd\u5185\u4e5f\u4e0d\u4f1a\u5e76\u53d1\u8bfb\u5199\u3002\u534f\u7a0b\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f46\u6211\u60f3\u6c47\u603b\u534f\u7a0b\u5185\u7684\u4fe1\u606f\uff0c\u5c31\u9700\u8981\u4f7f\u7528 map \u4e86\u3002<\/p>\n<p>2. \u5927\u91cf\u7684\u5e76\u53d1\u5199\u5165\uff0c\u5c11\u91cf\u7684\u8bfb\u53d6<br \/>\u8fd9\u4e2a\u548c\u4e0a\u9762\u662f\u540c\u4e00\u4e2a\u4e1a\u52a1\uff0c\u8981\u5bf9\u4e0d\u540c\u4f1a\u8bdd\u5185\u7684\u4e0d\u540c\u7c7b\u578b\u4e8b\u4ef6\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\uff0c\u7136\u540e\u5b9a\u65f6\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u8bf4\u8fd9\u662f\u5c11\u91cf\u7684\u8bfb\u53d6\u4e5f\u4e0d\u592a\u5408\u9002\uff0c\u56e0\u4e3a\u5199\u5165\u524d\u8fd8\u8981\u67e5\u8be2\u539f\u503c\uff0c\u4f46\u4e5f\u4e0d\u662f\u8bfb\u5199\u9501\u66f4\u9002\u5408\u7684\u8bfb\u591a\u5199\u5c11\u7684\u60c5\u51b5\u3002\u8fd9\u91cc\u53ef\u80fd\u662f map \u5b9e\u73b0\u672c\u8eab\u7684\u95ee\u9898\u4e86\uff0c\u6c42\u95ee\u6709\u4ec0\u4e48\u66f4\u4f18\u7684\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u5904\u7406\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u505a\u5230\u9ad8\u6548\u7684\u539f\u5b50\u6027\u7684 increase 1 \uff1f      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3286188\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abbycin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6362 tbb                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286189\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ksedz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @abbycin \u8c22\u8c22\u63d0\u793a\u3002\u641c\u4e86\u4e0b\uff0c\u662f C++\u7684\u65b9\u6848\uff1f\u6539\u52a8\u6709\u70b9\u5927\uff0c\u6211\u5148\u627e\u627e\u5176\u4ed6\u89e3\u51b3\u65b9\u6848\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286190\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jingniao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             map \u7684 value \u5f04\u6210\u7ed3\u6784\u4f53\u6307\u9488\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286191\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : qianlv7 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5206\u6876\uff0c\u6bcf\u4e2a\u6876\u5355\u72ec\u52a0\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286192\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teawithlife <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u6362\u81ea\u5e26\u7684 sync.Map<br \/>\u552f\u4e00\u5751\u7684\u5730\u65b9\u662f\u6ca1\u6709 Len()\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6 Map \u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u5f97\u81ea\u5df1\u904d\u5386\u4e00\u904d\u6570\u4e2a\u6570\u3002\u4e0d\u8fc7\u770b\u4f60\u7684\u4f7f\u7528\u573a\u666f\uff0c\u5e94\u8be5\u4e0d\u9700\u8981 Len()\u65b9\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286193\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : foam <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c 2 \u4e2a\u573a\u666f\u4e0d\u8981\u52a0\u9501\u4e86\uff0c\u592a\u91cd\u3002\u7528 atomic \u5305\u7684 cas\uff0c\u5176\u5b9e\u5c31\u662f\u4e50\u89c2\u9501\u3002\u52a0\u4e0a for \u91cd\u8bd5\uff0c\u4fdd\u8bc1\u5199\u5165\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286194\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fiypig <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sync.Map                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286195\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fighterlyt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u6765\u4e0a\u8fb9\u7684\u4eba\u90fd\u6ca1\u627e\u5230\u7684\u5177\u4f53\u7684\u95ee\u9898\uff0cmap \u4e0d\u652f\u6301\u5e76\u53d1\u8bfb\u5199\u7684\u6839\u672c\u539f\u56e0\u662f** \u4f7f\u7528\u4e00\u4e2a\u6709\u7a7a\u95f4\u5927\u5c0f\u7684\u7ed3\u6784\u8868\u793a\u4e00\u4e2a\u65e0\u9650\u5927\u5c0f\u7684\u7ed3\u6784**\u3002\u8fd9\u6837\u5fc5\u7136\u4f34\u968f\u7740\u5185\u5b58\u7684\u91cd\u65b0\u5206\u914d\u548c\u5bf9\u5e94\u7684\u590d\u5236\u95ee\u9898\u3002\u6240\u4ee5\u4e0d\u7ba1\u8bfb\u5199\u662f\u5426\u4e3a\u540c\u4e00\u4e2a key\uff0c\u603b\u662f\u9700\u8981\u52a0\u9501\u3002\u9488\u5bf9\u7b2c\u4e8c\u4e2a\u95ee\u9898\uff0c\u4e24\u4e2a map \u5c31 ok \u4e86\uff0c\u53ef\u4ee5\u7528\u540c\u4e00\u628a\u9501,1 \u4e2a map[string]***\uff0c1 \u4e2a map[string]int64                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286196\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u7740 java concurrenthashmap \u5b9e\u73b0\u4e00\u904d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286197\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dongisking <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             golang \u5e76\u53d1\u8bbf\u95ee\u4f1a\u62a5\u9519\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286198\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhs227 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dongisking \u51c6\u786e\u7684\u8bf4\u662f\u4f1a panic\uff0c\u7a0b\u5e8f\u76f4\u63a5\u9000\u51fa\u3002 \u4e00\u822c\u7528 sync.map \u6216\u8005\u662f\u52a0\u9501\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286199\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sync.Map \u5c31\u662f\u4e3a\u8fd9\u79cd\u573a\u666f\u5b9a\u5236\u7684<\/p>\n<p> The Map type is specialized. Most code should use a plain Go map instead,<br \/> with separate locking or coordination, for better type safety and to make it<br \/> easier to maintain other invariants along with the map content.<\/p>\n<p> The Map type is optimized for two common use cases: (1) when the entry for a<br \/> given key is only ever written once but read many times, as in caches that<br \/> only grow, or (2) when multiple goroutines read, write, and overwrite<br \/> entries for disjoint sets of keys. In these two cases, use of a Map may<br \/> significantly reduce lock contention compared to a Go map paired with a<br \/> separate Mutex or RWMutex.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286200\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u7528 sync.Map<br \/>2. \u7528\u7b2c\u4e09\u65b9\u5c01\u88c5\u8fc7\u7684 map\uff0c\u5206 bucket \u7684\uff0c\u4f46\u8fd8\u662f\u52a0\u9501\u7684\u601d\u8def<br \/>3. \u81ea\u5df1\u5c01\u88c5\u4e00\u5c42\uff0c\u901a\u8fc7 channel \u7684\u53bb\u9650\u5236\u5e76\u53d1\u8bfb\u5199 map                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3286201\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dafsic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7ed9\u4f60\u770b\u770b\u6211\u7528\u7684\u65b9\u6cd5\uff0c\u4e0d\u8bf4\u591a\u597d\uff0c\u53cd\u6b63\u6211\u4e00\u76f4\u8fd9\u6837\u7528 https:\/\/www.v2ex.com\/t\/596606#reply18                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u4e2d map \u7684\u5e76\u53d1\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155352"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155352"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155352\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155352"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}