{"id":207545,"date":"2025-07-08T08:09:05","date_gmt":"2025-07-08T00:09:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207545\/"},"modified":"2025-07-08T08:09:05","modified_gmt":"2025-07-08T00:09:05","slug":"%e9%98%b2%e6%ad%a2%e4%bf%ae%e6%94%b9%e7%bb%93%e6%9e%84%e6%95%b0%e6%8d%ae%e6%88%90%e5%91%98%e7%9a%84%e6%83%af%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207545\/","title":{"rendered":"\u9632\u6b62\u4fee\u6539\u7ed3\u6784\u6570\u636e\u6210\u5458\u7684\u60ef\u7528\u65b9\u6cd5"},"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>\u9632\u6b62\u4fee\u6539\u7ed3\u6784\u6570\u636e\u6210\u5458\u7684\u60ef\u7528\u65b9\u6cd5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 15:18:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u9632\u6b62\u4fee\u6539\u7ed3\u6784\u6570\u636e\u6210\u5458\u7684\u60ef\u7528\u65b9\u6cd5\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4e00\u4e9b\u8c37\u6b4c\u641c\u7d22\u7684\u7ed3\u8bba\u662f\uff1a 1\uff09go\u4e0d\u652f\u6301\u4e0d\u53ef\u53d8\/\u5e38\u91cf\u6570\u636e\u6210\u5458\u3002 2\uff09go\u4e0d\u652f\u6301\u79c1\u6709\u6570\u636e\u6210\u5458\u3002\u76f8\u53cd\uff0c\u4f7f\u7528\u5305\u6765\u9694\u79bb\u6570\u636e\u662f\u60ef\u7528\u7684\u505a\u6cd5\u3002<\/p>\n<p>\u90a3\u4e48\u9632\u6b62\u5bf9\u7ed3\u6784\u4f53\u6570\u636e\u6210\u5458\u8fdb\u884c\u540e\u4fee\u6539\u7684\u60ef\u7528\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u60f3\u58f0\u660e\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5e76\u4e00\u6b21\u6027\u786e\u5b9a\u5176\u5927\u5c0f\u3002 <\/p>\n<pre>type ThreadPool struct {\n    tp_size int  \n}\n\nfunc (tp* ThreadPool) Init(size int) {\n  tp.tp_size = size;\n  \/\/ Allocate space...\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5c06\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u79c1\u6709\uff0c\u56e0\u6b64\u65e0\u6cd5\u4ece\u5916\u90e8\u5305\u8bbf\u95ee\u5b83\u3002<\/p>\n<p>\u5bf9\u4e8e\u76f8\u540c\u7684\u5305\u8bbf\u95ee\uff0c<strong>\u60a8\u4e0d\u80fd<\/strong>\u3002 golang\u7684\u54f2\u5b66\u662f\uff1a\u4f60\u662f\u4ee3\u7801\u7684\u6240\u6709\u8005\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u505a\u4efb\u4f55\u4f60\u60f3\u505a\u7684\u4e8b\u60c5\u3002 <\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u60a8\u60f3\u8ba9\u5b57\u6bb5\u4e0d\u53ef\u53d8\uff0c\u60a8\u53ef\u4ee5\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u518d\u6b21\u5305\u88c5\u5728\u540d\u4e3a <code>immutablesomething<\/code> \u7684\u7ed3\u6784\u4e2d\uff0c\u5e76\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u5305\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>package util\n\ntype immutableint struct {\n    val int\n}\n\nfunc newimmutableint(val int) immutableint {\n    return immutableint{val: val}\n}\n\nfunc (i immutableint) get() int {\n    return i.val\n}\n\nfunc (i immutableint) set(val int) immutableint {\n    return immutableint{val: val}\n}<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>package app\n\nimport \"util\"\n\ntype ThreadPool struct {\n    size util.ImmutableInt\n}\n\nfunc NewThreadPool(size int) ThreadPool {\n    return ThreadPool{size: util.NewImmutableInt(size)}\n}\n\nfunc test() {\n    pool := NewThreadPool(10)\n\n    \/\/ you cannot do this because ImmutableInt is in another package\n    pool.size.val = 3\n\n    \/\/ this  won't work\n    pool.size.Set(3)\n\n    \/\/ but you can do this. which is weird. \n    \/\/ and praying when someone does this, they know something not right\n    pool.size = util.NewImmutableInt(3)\n}<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u9632\u6b62\u4fee\u6539\u7ed3\u6784\u6570\u636e\u6210\u5458\u7684\u60ef\u7528\u65b9\u6cd5\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/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-207545","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207545","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=207545"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207545\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}