{"id":57844,"date":"2024-10-10T21:37:17","date_gmt":"2024-10-10T13:37:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57844\/"},"modified":"2024-10-10T21:37:17","modified_gmt":"2024-10-10T13:37:17","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-sync-map%ef%bc%88%e5%9c%a8%e4%b8%a6%e7%99%bc%e7%92%b0%e5%a2%83%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%9a%84-map%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57844\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00 sync.Map\uff08\u5728\u4e26\u767c\u74b0\u5883\u4e2d\u4f7f\u7528\u7684 map\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-sync-map%ef%bc%88%e5%9c%a8%e4%b8%a6%e7%99%bc%e7%92%b0%e5%a2%83%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%9a%84-map%ef%bc%89-CyMUrxJQre\">GO\u6559\u7a0b: Go \u8a9e\u8a00 sync.Map\uff08\u5728\u4e26\u767c\u74b0\u5883\u4e2d\u4f7f\u7528\u7684 map\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u4e26\u767c\u7de8\u7a0b\u5df2\u6210\u70ba\u4e00\u500b\u91cd\u8981\u7684\u4e3b\u984c\u3002Go \u8a9e\u8a00\u4f5c\u70ba\u4e00\u7a2e\u8a2d\u8a08\u7528\u65bc\u4e26\u767c\u7684\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u4f86\u7c21\u5316\u9019\u4e00\u904e\u7a0b\u3002\u5176\u4e2d\uff0c<code>sync.Map<\/code> \u662f\u4e00\u500b\u5c08\u9580\u70ba\u4e26\u767c\u74b0\u5883\u8a2d\u8a08\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u80fd\u5920\u6709\u6548\u5730\u8655\u7406\u591a\u500b goroutine \u540c\u6642\u8a2a\u554f\u7684\u60c5\u6cc1\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-sync-map%ef%bc%9f-CyMUrxJQre\">\u4ec0\u9ebc\u662f sync.Map\uff1f<\/h2>\n<p><code>sync.Map<\/code> \u662f Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u4e2d\u7684\u4e00\u500b\u985e\u578b\uff0c\u4f4d\u65bc <code>sync<\/code> \u5305\u4e2d\u3002\u5b83\u7684\u8a2d\u8a08\u76ee\u7684\u662f\u70ba\u4e86\u5728\u591a\u500b goroutine \u4e2d\u5b89\u5168\u5730\u8b80\u53d6\u548c\u5beb\u5165\u8cc7\u6599\u3002\u8207\u50b3\u7d71\u7684 <code>map<\/code> \u4e0d\u540c\uff0c<code>sync.Map<\/code> \u5167\u90e8\u4f7f\u7528\u4e86\u9396\u548c\u5176\u4ed6\u540c\u6b65\u6a5f\u5236\u4f86\u78ba\u4fdd\u8cc7\u6599\u7684\u4e00\u81f4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"sync-map-%e7%9a%84%e7%89%b9%e6%80%a7-CyMUrxJQre\">sync.Map \u7684\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u7684\u8b80\u53d6\u64cd\u4f5c\uff1a<\/strong> <code>sync.Map<\/code> \u5728\u8b80\u53d6\u64cd\u4f5c\u4e0a\u9032\u884c\u4e86\u512a\u5316\uff0c\u7279\u5225\u662f\u5728\u8b80\u53d6\u9060\u591a\u65bc\u5beb\u5165\u7684\u60c5\u6cc1\u4e0b\uff0c\u6027\u80fd\u8868\u73fe\u512a\u7570\u3002<\/li>\n<li><strong>\u81ea\u52d5\u8655\u7406\u9396\uff1a<\/strong> \u4f7f\u7528 <code>sync.Map<\/code> \u6642\uff0c\u958b\u767c\u8005\u4e0d\u9700\u8981\u624b\u52d5\u52a0\u9396\uff0c\u9019\u964d\u4f4e\u4e86\u51fa\u932f\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<li><strong>\u652f\u6301\u4e26\u767c\u5b89\u5168\uff1a<\/strong> \u591a\u500b goroutine \u53ef\u4ee5\u540c\u6642\u8b80\u53d6\u548c\u5beb\u5165\uff0c\u800c\u4e0d\u6703\u5c0e\u81f4\u8cc7\u6599\u7af6\u722d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-sync-map-CyMUrxJQre\">\u5982\u4f55\u4f7f\u7528 sync.Map<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>sync.Map<\/code> \u7684\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var m sync.Map\n\n    \/\/ \u5beb\u5165\u8cc7\u6599\n    m.Store(\"key1\", \"value1\")\n    m.Store(\"key2\", \"value2\")\n\n    \/\/ \u8b80\u53d6\u8cc7\u6599\n    if value, ok := m.Load(\"key1\"); ok {\n        fmt.Println(\"key1:\", value)\n    }\n\n    \/\/ \u522a\u9664\u8cc7\u6599\n    m.Delete(\"key2\")\n\n    \/\/ \u904d\u6b77\u8cc7\u6599\n    m.Range(func(key, value interface{}) bool {\n        fmt.Println(key, value)\n        return true \/\/ \u8fd4\u56de true \u7e7c\u7e8c\u904d\u6b77\n    })\n}\n<\/code><\/pre>\n<h3 id=\"%e7%af%84%e4%be%8b%e8%a7%a3%e6%9e%90-CyMUrxJQre\">\u7bc4\u4f8b\u89e3\u6790<\/h3>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b <code>sync.Map<\/code> \u7684\u5be6\u4f8b\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528 <code>Store<\/code> \u65b9\u6cd5\u5c07\u8cc7\u6599\u5beb\u5165 map \u4e2d\u3002\u4f7f\u7528 <code>Load<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u8b80\u53d6\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u6aa2\u67e5\u8a72\u9375\u662f\u5426\u5b58\u5728\u3002\u4f7f\u7528 <code>Delete<\/code> \u65b9\u6cd5\u53ef\u4ee5\u522a\u9664\u6307\u5b9a\u7684\u9375\u3002\u6700\u5f8c\uff0c<code>Range<\/code> \u65b9\u6cd5\u5141\u8a31\u6211\u5011\u904d\u6b77\u6240\u6709\u7684\u9375\u503c\u5c0d\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%a0%b4%e6%99%af-CyMUrxJQre\">\u4f7f\u7528\u5834\u666f<\/h2>\n<p><code>sync.Map<\/code> \u7279\u5225\u9069\u5408\u65bc\u4ee5\u4e0b\u5834\u666f\uff1a<\/p>\n<ul>\n<li>\u9700\u8981\u983b\u7e41\u8b80\u53d6\u800c\u4e0d\u5e38\u5beb\u5165\u7684\u60c5\u6cc1\uff0c\u4f8b\u5982\u5feb\u53d6\u7cfb\u7d71\u3002<\/li>\n<li>\u591a\u500b goroutine \u9700\u8981\u5171\u4eab\u8cc7\u6599\u7684\u60c5\u6cc1\uff0c\u4f8b\u5982\u5728 Web \u4f3a\u670d\u5668\u4e2d\u5b58\u5132\u7528\u6236\u6703\u8a71\u3002<\/li>\n<li>\u9700\u8981\u5728\u9ad8\u4f75\u767c\u74b0\u5883\u4e2d\u5b89\u5168\u5730\u7ba1\u7406\u8cc7\u6599\u7684\u60c5\u6cc1\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CyMUrxJQre\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c<code>sync.Map<\/code> \u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u4e26\u767c\u74b0\u5883\u4e2d\u7684\u8cc7\u6599\u5b58\u53d6\u554f\u984c\u3002\u5b83\u7684\u8a2d\u8a08\u8003\u616e\u5230\u4e86\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\uff0c\u800c\u4e0d\u5fc5\u64d4\u5fc3\u8cc7\u6599\u7af6\u722d\u7684\u554f\u984c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u4e26\u767c\u8655\u7406\u7684\u61c9\u7528\uff0c<code>sync.Map<\/code> \u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684 sync.Map\uff0c\u4e86\u89e3\u5982\u4f55\u5728\u4e26\u767c\u74b0\u5883\u4e2d\u5b89\u5168\u9ad8\u6548\u5730\u4f7f\u7528 map\uff0c\u63d0\u5347\u7a0b\u5f0f\u6027\u80fd\u8207\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57844","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57844","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=57844"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57844\/revisions"}],"predecessor-version":[{"id":57845,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57844\/revisions\/57845"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}