{"id":207597,"date":"2025-07-08T12:50:20","date_gmt":"2025-07-08T04:50:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207597\/"},"modified":"2025-07-08T12:50:20","modified_gmt":"2025-07-08T04:50:20","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8golang%e4%b8%ad%e4%bd%bf%e7%94%a82%e4%b8%aa%e5%8d%95%e7%8b%ac%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%a4%84%e7%90%862%e4%b8%aa%e4%ba%8b%e4%bb%b6%e6%ba%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207597\/","title":{"rendered":"\u5982\u4f55\u5728golang\u4e2d\u4f7f\u75282\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u5904\u74062\u4e2a\u4e8b\u4ef6\u6e90"},"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\u5728golang\u4e2d\u4f7f\u75282\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u5904\u74062\u4e2a\u4e8b\u4ef6\u6e90<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 20:54:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5728golang\u4e2d\u4f7f\u75282\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u5904\u74062\u4e2a\u4e8b\u4ef6\u6e90\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4e0e\u8bbe\u8ba1\u76f8\u5173\u7684\u95ee\u9898\u3002\u6211\u9047\u5230\u7684\u60c5\u51b5\u662f\uff0c\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u4ece\u5176\u6ce8\u518c\u7684\u4e24\u4e2a\u4e0d\u540c\u6e90\u63a5\u6536\u4e8b\u4ef6\uff0c\u5e76\u4e14\u5e94\u7528\u7a0b\u5e8f\u5e94\u8be5\u5e76\u884c\u5904\u7406\u6765\u81ea\u8fd9\u4e24\u4e2a\u6e90\u7684\u4e8b\u4ef6\u3002\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u4f7f\u7528\u7f13\u51b2\u901a\u9053\u5904\u7406\u6765\u81ea\u4e00\u4e2a\u6e90\u7684\u4e8b\u4ef6\uff08\u5176\u4e2d\u4e8b\u4ef6\u6392\u961f\u5e76\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u5904\u7406\uff09\u3002\u73b0\u5728\uff0c\u6211\u9762\u4e34\u7684\u60c5\u51b5\u662f\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u6765\u81ea\u4e0d\u540c\u6e90\u7684\u4e8b\u4ef6\uff0c\u5e76\u4e14\u6211\u65e0\u6cd5\u5728\u6b64\u5904\u4f7f\u7528\u76f8\u540c\u7684\u901a\u9053\uff0c\u56e0\u4e3a\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u5fc5\u987b\u5e76\u884c\u5904\u7406\u6765\u81ea\u8fd9\u4e24\u4e2a\u6e90\u7684\u4e8b\u4ef6\u3002\u6211\u6b63\u5728\u8003\u8651\u4f7f\u7528\u53e6\u4e00\u4e2a\u7f13\u51b2\u901a\u9053\u6765\u5904\u7406\u6765\u81ea\u7b2c\u4e8c\u4e2a\u4e8b\u4ef6\u6e90\u7684\u4e8b\u4ef6\u3002\u4f46\u6211\u62c5\u5fc3\u76f8\u540c\u7684\u8d44\u6e90\u88ab\u7528\u6765\u5e76\u884c\u5904\u7406 2 \u4e2a\u4e8b\u4ef6\u3002\u5373\u4f7f\u6211\u4eec\u4f7f\u7528\u901a\u9053\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u5728\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u65f6\u518d\u6b21\u5e94\u7528\u540c\u6b65\u3002<\/p>\n<p>\u60a8\u80fd\u5426\u5efa\u8bae\u6211\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u6cd5\u3001\u6211\u53ef\u4ee5\u4f7f\u7528\u7684\u4efb\u4f55\u6a21\u5f0f\u6216\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u7684\u8bbe\u8ba1\uff1f<\/p>\n<p>\u8fd9\u662f\u6211\u73b0\u5728\u5904\u7406\u6765\u81ea\u4e00\u4e2a\u6e90\u7684\u4e8b\u4ef6\u7684\u4ee3\u7801<\/p>\n<pre>for event := range thisObj.channel {\n\n    log.Printf(\"Found a new event '%s' to process at the state %s\", event, thisObj.currentState)\n\n    stateins := thisObj.statesMap[thisObj.currentState]\n    \/\/ This is separate go routine. Hence acuire the lock before calling a state to process the event.\n    thisObj.mu.Lock()\n    stateins.ProcessState(event.EventType, event.EventData)\n    thisObj.mu.Unlock()\n\n}<\/pre>\n<p>\u8fd9\u91cc thisobj.channel \u662f\u5728\u542f\u52a8\u65f6\u521b\u5efa\u7684\uff0c\u5e76\u4e14\u4e8b\u4ef6\u662f\u5728\u5355\u72ec\u7684\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u7684\u3002\u76ee\u524d\u8be5\u65b9\u6cd5\u6b63\u5728\u4ece\u901a\u9053\u8bfb\u53d6\u4e8b\u4ef6\u5e76\u5904\u7406\u4e8b\u4ef6\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 \u6a21\u5f0f\u4ece\u5355\u4e2a\u901a\u9053\u8bfb\u53d6\u5904\u7406\u4e8b\u4ef6\u5e76\u5e76\u884c\u5904\u7406\u5b83\u4eec\uff1a<\/p>\n<pre>var events *EventsChannel\n\nfor {\n        select {\n        case eventA = &lt;-events:\n        \/\/ handle A\n        case eventB = &lt;-events:\n        \/\/ handle B\n        default:\n            \/\/ unknown type, error case\n\n        }\n    }\n<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u5728golang\u4e2d\u4f7f\u75282\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u5904\u74062\u4e2a\u4e8b\u4ef6\u6e90\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\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-207597","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207597","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=207597"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207597\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}