{"id":208183,"date":"2025-07-08T14:07:34","date_gmt":"2025-07-08T06:07:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208183\/"},"modified":"2025-07-08T14:07:34","modified_gmt":"2025-07-08T06:07:34","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e4%b8%ad%e6%a8%a1%e6%8b%9fsync-map%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208183\/","title":{"rendered":"\u5982\u4f55\u5728Go\u4e2d\u6a21\u62dfsync.Map\uff1f"},"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>\u5982\u4f55\u5728Go\u4e2d\u6a21\u62dfsync.Map\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 23:18:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728Go\u4e2d\u6a21\u62dfsync.Map\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>var pageCache sync.Map\n\nfunc getCache(){\n    appCache, ok := pageCache.Load(key)\n}<\/pre>\n<p>\u6211\u5c1d\u8bd5\u901a\u8fc7 gomonkey \u8fdb\u884c\u6a21\u62df\u3002<\/p>\n<p><code>patches := gomonkey.applymethodseq(reflect.typeof(pagecache), \"\u52a0\u8f7d\", \u8f93\u51fa)<\/code><\/p>\n<p>goland \u5728 <code>pagecache<\/code> \u4e0a\u51fa\u73b0\u4ee5\u4e0b\u8b66\u544a\uff1a<\/p>\n<p>\u8c03\u7528\u201creflect.typeof\u201d\u4f1a\u590d\u5236\u9501\u5b9a\u503c\uff1a\u201csync.map\u201d\u7c7b\u578b\u5305\u542b\u201csync.mutex\u201d\uff0c\u5373\u201csync.locker\u201d<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4e5f\u8bb8\u8fd9\u9002\u5408\u60a8\u7684\u7528\u4f8b\uff1f<\/p>\n<pre>type mockedmap struct {\n    sync.map\n}\n\nfunc (m *mockedmap) load(key interface{}) (value interface{}, ok bool) {\n    \/\/ do something with the call unique to this mock\n    return m.map.load(key)\n}<\/pre>\n<p>\u60a8\u7684\u5b9e\u73b0\u53ea\u9700\u8981\u63a5\u53d7\u5730\u56fe\u63a5\u53e3\u800c\u4e0d\u662f <code>*sync.map<\/code> \uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u5904\u7406\u6a21\u62df\u548c\u771f\u5b9e\u7684\u4e8b\u7269\u3002<\/p>\n<pre>type Loadable interface {\n    Load(interface{}) (interface{}, bool)\n}<\/pre>\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-208183","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208183","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=208183"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208183\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}