{"id":187293,"date":"2020-11-02T09:08:01","date_gmt":"2020-11-02T01:08:01","guid":{"rendered":"http:\/\/4563.org\/?p=187293"},"modified":"2020-11-09T14:36:23","modified_gmt":"2020-11-09T06:36:23","slug":"%e5%b9%b6%e5%8f%91%e7%bb%84%e4%bb%b6-go-%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=187293","title":{"rendered":"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u516c\u4f17\u53f7\u83b7\u53d6\u66f4\u591a\u539f\u521b\u6280\u672f\u6587\u7ae0:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/033f853ef3834b8ab6c626c3a4564acdtplv-k3u1fbpfcp-zoom-1.image_.png\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/><\/p>\n<blockquote>\n<p>\u55ef\uff0cGo \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\u7cfb\u5217\uff0c\u4e00\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u4e1a\u52a1\u771f\u5b9e\u4f7f\u7528\u7684 golang \u7cfb\u5217\u3002<\/p>\n<\/blockquote>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201109_5fa8e34b46217.png\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/> <\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u672c\u7cfb\u5217\u4e3b\u8981\u5206\u4eab\uff0c\u5982\u4f55\u5728\u6211\u4eec\u7684\u771f\u5b9e\u4e1a\u52a1\u573a\u666f\u4e2d\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n<p>\u672c\u7cfb\u5217\u6587\u7ae0\u4e3b\u8981\u91c7\u7528\u5982\u4e0b\u7ed3\u6784\uff1a<\/p>\n<ul>\n<li>\u4ec0\u4e48\u662f\u300c XX \u8bbe\u8ba1\u6a21\u5f0f\u300d\uff1f<\/li>\n<li>\u4ec0\u4e48\u771f\u5b9e\u4e1a\u52a1\u573a\u666f\u53ef\u4ee5\u4f7f\u7528\u300c XX \u8bbe\u8ba1\u6a21\u5f0f\u300d\uff1f<\/li>\n<li>\u600e\u4e48\u7528\u300c XX \u8bbe\u8ba1\u6a21\u5f0f\u300d\uff1f<\/li>\n<\/ul>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7ed3\u5408 Go \u8bed\u8a00\u5929\u751f\u7684\u5e76\u53d1\u7279\u6027\uff0c\u5982\u4f55\u5728\u771f\u5b9e\u4e1a\u52a1\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4e4b\u524d\u6587\u7ae0\u300a\u4ee3\u7801\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\u300b\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5728\u4e1a\u52a1\u4e2d\u7684\u4f7f\u7528\u3002\u4eca\u5929\u6211\u4eec\u7ed3\u5408 Go \u8bed\u8a00\u5929\u751f\u7684\u5e76\u53d1\u7279\u6027\uff0c\u5347\u7ea7\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u4e3a\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u3002<\/p>\n<p>\u6211\u4eec\u5148\u6765\u7b80\u5355\u56de\u987e\u4e0b\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u77e5\u8bc6\uff0c\u8be6\u7ec6\u53ef\u4ee5\u67e5\u770b\u4e0a\u7bc7\u6587\u7ae0\u300a\u4ee3\u7801\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\u300b<\/p>\n<h2>\u4ec0\u4e48\u662f\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\uff1f<\/h2>\n<p>\u7ec4\u5408\u6a21\u5f0f\u7684\u6982\u5ff5\uff1a<\/p>\n<blockquote>\n<p>\u4e00\u4e2a\u5177\u6709\u5c42\u7ea7\u5173\u7cfb\u7684\u5bf9\u8c61\u7531\u4e00\u7cfb\u5217\u62e5\u6709\u7236\u5b50\u5173\u7cfb\u7684\u5bf9\u8c61\u901a\u8fc7\u6811\u5f62\u7ed3\u6784\u7ec4\u6210\u3002<\/p>\n<\/blockquote>\n<p>\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u7684\u6982\u5ff5\uff1a<\/p>\n<blockquote>\n<p>\u4e00\u4e2a\u5177\u6709\u5c42\u7ea7\u5173\u7cfb\u7684\u5bf9\u8c61\u7531\u4e00\u7cfb\u5217\u62e5\u6709\u7236\u5b50\u5173\u7cfb\u7684\u5bf9\u8c61\u901a\u8fc7\u6811\u5f62\u7ed3\u6784\u7ec4\u6210\uff0c\u5b50\u5bf9\u8c61\u5373\u53ef\u88ab\u4e32\u884c\u6267\u884c\uff0c\u4e5f\u53ef\u88ab\u5e76\u53d1\u6267\u884c<\/p>\n<\/blockquote>\n<p>\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u7684\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>\u539f\u672c\u4e32\u884c\u7684\u4e1a\u52a1(\u5b58\u5728\u963b\u585e\u7684\u90e8\u5206\uff0c\u6bd4\u5982\u7f51\u7edc IO \u7b49)\u53ef\u4ee5\u88ab\u5e76\u53d1\u6267\u884c\uff0c\u5229\u7528\u591a\u6838\u4f18\u52bf\u63d0\u5347\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2>\u4ec0\u4e48\u771f\u5b9e\u4e1a\u52a1\u573a\u666f\u53ef\u4ee5\u7528\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\uff1f<\/h2>\n<p>\u6211\u4eec\u8fd8\u662f\u4ee5\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u4e2d\u7684\u201c\u8ba2\u5355\u7ed3\u7b97\u9875\u9762\u201d\u4e3a\u4f8b\uff0c\u7ee7\u7eed\u6765\u770b\u770b\u67d0\u4e1c\u7684\u8ba2\u5355\u7ed3\u7b97\u9875\u9762\uff1a<\/p>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201109_5fa8e34f3b43e.jpg\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/> <\/p>\n<p>\u4ece\u9875\u9762\u7684\u5c55\u793a\u5f62\u5f0f\u4e0a\uff0c\u53ef\u4ee5\u770b\u51fa\uff1a<\/p>\n<ul>\n<li>\u9875\u9762\u7531\u591a\u4e2a\u6a21\u5757\u6784\u6210\uff0c\u6bd4\u5982\uff1a\n<ul>\n<li>\u5730\u5740\u6a21\u5757\uff1a\u83b7\u53d6\u7528\u6237\u5730\u5740\u6570\u636e<\/li>\n<li>\u652f\u4ed8\u65b9\u5f0f\u6a21\u5757\uff1a\u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f\u5217\u8868<\/li>\n<li>\u5e97\u94fa\u6a21\u5757\uff1a\u83b7\u53d6\u5e97\u94fa\u3001\u8d2d\u7269\u8f66\u9009\u4e2d\u5546\u54c1\u7b49\u4fe1\u606f<\/li>\n<li>\u53d1\u7968\u6a21\u5757\uff1a\u83b7\u53d6\u53d1\u7968\u7c7b\u578b\u5217\u8868<\/li>\n<li>\u4f18\u60e0\u5238\u6a21\u5757\uff1a\u83b7\u53d6\u7528\u6237\u4f18\u60e0\u5238\u5217\u8868<\/li>\n<li>\u67d0\u8c46\u6a21\u5757\uff1a\u83b7\u53d6\u7528\u6237\u79ef\u5206\u4fe1\u606f<\/li>\n<li>\u793c\u54c1\u5361\u6a21\u5757\uff1a\u83b7\u53d6\u793c\u54c1\u5361\u5217\u8868\u5217\u8868<\/li>\n<li>\u8ba2\u5355\u8be6\u7ec6\u91d1\u989d\u6a21\u5757\uff1a\u83b7\u53d6\u8ba2\u5355\u91d1\u989d\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<li>\u5355\u4e2a\u6a21\u5757\u53ef\u4ee5\u7531\u591a\u4e2a\u5b50\u6a21\u5757\u6784\u6210\n<ul>\n<li>\u5e97\u94fa\u6a21\u5757\uff0c\u53c8\u7531\u5982\u4e0b\u6a21\u5757\u6784\u6210\uff1a\n<ul>\n<li>\u5546\u54c1\u6a21\u5757\uff1a\u83b7\u53d6\u8d2d\u7269\u8f66\u9009\u4e2d\u5546\u54c1\u4fe1\u606f<\/li>\n<li>\u552e\u540e\u6a21\u5757\uff1a\u83b7\u53d6\u5546\u54c1\u552e\u540e\u4fe1\u606f<\/li>\n<li>\u4f18\u60e0\u6a21\u5757\uff1a\u83b7\u53d6\u5546\u54c1\u53c2\u4e0e\u7684\u4f18\u60e0\u6d3b\u52a8\u4fe1\u606f<\/li>\n<li>\u7269\u6d41\u6a21\u5757\uff1a\u83b7\u53d6\u5546\u54c1\u652f\u6301\u7684\u914d\u9001\u65b9\u5f0f\u5217\u8868<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6309\u7167\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u4e1a\u52a1\u903b\u8f91\u6267\u884c\u6d41\u7a0b\uff1a<\/p>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201109_5fa8e353b05a0.png\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/> <\/p>\n<p>\u4f46\u662f\uff0c\u6211\u4eec\u5f88\u6e05\u695a\u6709\u4e9b\u6a21\u5757\u4e4b\u95f4\u5e76\u6ca1\u6709\u4f9d\u8d56\uff0c<strong>\u4e14\u8be5\u6a21\u5757\u6d89\u53ca\u670d\u52a1\u8fdc\u7a0b\u8c03\u7528\u7b49\u963b\u585e\u64cd\u4f5c<\/strong>\uff0c\u6bd4\u5982\uff1a<\/p>\n<ul>\n<li>\u5730\u5740\u6a21\u5757\u8c03\u7528\u5730\u5740\u670d\u52a1\u83b7\u53d6\u7528\u6237\u5730\u5740\u6570\u636e\u65f6\u3002<\/li>\n<li>\u652f\u4ed8\u65b9\u5f0f\u6a21\u5757\u4e5f\u53ef\u4ee5\u540c\u65f6\u53bb\u8bfb redis \u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f\u5217\u8868\u6570\u636e\u7b49\u7b49\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5:<strong>\u6709\u7684\u6a21\u5757\u5176\u5b9e\u53ef\u4ee5\u88ab\u5e76\u53d1\u7684\u6267\u884c<\/strong>\u3002<\/p>\n<p>\u5982\u679c\u628a\u4e0a\u9762\u4e0d\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\u7684\u6a21\u5757\u4fee\u6539\u4e3a\u5e76\u53d1\u7684\u6267\u884c\uff0c\u5219\u6211\u4eec\u5f97\u5230\u5982\u4e0b\u7684\u6267\u884c\u6d41\u7a0b\uff1a<\/p>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201109_5fa8e358d71e3.png\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/> <\/p>\n<h2>\u600e\u4e48\u7528\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\uff1f<\/h2>\n<p>\u5173\u4e8e\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u5efa\u6a21\u8fc7\u7a0b\u5b8c\u5168\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u6587\u7ae0\u300a\u4ee3\u7801\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\u300b\uff0c\u6211\u4eec\u8fd9\u91cc\u53ea\u8bf4\u8bf4\u9700\u8981\u7740\u91cd\u6ce8\u610f\u7684\u5730\u65b9\u3002<\/p>\n<p>\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u6838\u5fc3\u8fd8\u662f<code>Component<\/code>\u7ec4\u4ef6\u63a5\u53e3\uff0c\u6211\u4eec\u5148\u770b\u770b\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7684<code>Component<\/code>\u7ec4\u4ef6\u63a5\u53e3\u5982\u4e0b(\u518d\u4e4b\u524d\u7684\u6587\u7ae0\u4e0a\u505a\u4e86\u4f18\u5316\uff0c\u8fdb\u4e00\u6b65\u5c01\u88c5\u63d0\u53d6\u4e86<code>BusinessLogicDo<\/code>\u65b9\u6cd5)\uff1a<\/p>\n<pre><code>\/\/ Component \u7ec4\u4ef6\u63a5\u53e3 type Component interface {  \/\/ \u6dfb\u52a0\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Mount(c Component, components ...Component) error  \/\/ \u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Remove(c Component) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u548c\u6267\u884c\u5b50\u7ec4\u4ef6  \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587  \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6  Do(ctx *Context, currentConponent Component) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u4e1a\u52a1\u903b\u8f91  BusinessLogicDo(ctx *Context) error  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  ChildsDo(ctx *Context) error } <\/code><\/pre>\n<p>\u518d\u6765\u770b\u770b\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u7684 Component`\u7ec4\u4ef6\u63a5\u53e3\uff0c\u5982\u4e0b(\u91cd\u70b9\u770b\u548c\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u7684\u533a\u522b)\uff1a<\/p>\n<pre><code>\/\/ Component \u7ec4\u4ef6\u63a5\u53e3 type Component interface {  \/\/ \u6dfb\u52a0\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Mount(c Component, components ...Component) error  \/\/ \u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Remove(c Component) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1:`BusinessLogicDo`\u548c\u6267\u884c\u5b50\u7ec4\u4ef6:`ChildsDo`  \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587  \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6  \/\/ wg \u7236\u7ec4\u4ef6\u7684 WaitGroup \u5bf9\u8c61  \/\/ \u533a\u522b 1\uff1a\u589e\u52a0\u4e86 WaitGroup \u5bf9\u8c61\u53c2\u6570\uff0c\u76ee\u7684\u662f\u7b49\u5f85\u5e76\u53d1\u5b50\u7ec4\u4ef6\u7684\u6267\u884c\u5b8c\u6210\u3002  Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u903b\u8f91  \/\/ resChan \u56de\u5199\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u6267\u884c\u7ed3\u679c\u7684 channel  \/\/ \u533a\u522b 2\uff1a\u589e\u52a0\u4e86\u4e00\u4e2a channel \u53c2\u6570\uff0c\u76ee\u7684\u662f\u5e76\u53d1\u7ec4\u4ef6\u6267\u884c\u903b\u8f91\u65f6\u5f15\u5165\u4e86\u8d85\u65f6\u673a\u5236\uff0c\u9700\u8981\u4e00\u4e2a channel \u63a5\u53d7\u7ec4\u4ef6\u7684\u6267\u884c\u7ed3\u679c  BusinessLogicDo(resChan chan interface{}) error  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  ChildsDo(ctx *Context) error } <\/code><\/pre>\n<p>\u6211\u4eec\u8be6\u7ec6\u518d\u6765\u770b\uff0c\u76f8\u5bf9\u4e8e\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\uff0c\u5f15\u5165\u5e76\u53d1\u4e4b\u540e\u9700\u8981\u7740\u91cd\u5173\u6ce8\u5982\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u5e76\u53d1\u5b50\u7ec4\u4ef6\u9700\u8981\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1a\u9632\u6b62\u5b50\u7ec4\u4ef6\u6267\u884c\u65f6\u95f4\u8fc7\u957f\uff0c\u89e3\u51b3\u65b9\u6848\u5173\u952e\u5b57<code>context.WithTimeout<\/code><\/li>\n<li>\u533a\u5206\u666e\u901a\u7ec4\u4ef6\u548c\u5e76\u53d1\u7ec4\u4ef6\uff1a\u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6\uff0c\u5c01\u88c5\u4e3a\u5e76\u53d1\u57fa\u7840\u7ec4\u4ef6<\/li>\n<li>\u62e5\u6709\u5e76\u53d1\u5b50\u7ec4\u4ef6\u7684\u7236\u7ec4\u4ef6\u9700\u8981\u7b49\u5f85\u5e76\u53d1\u5b50\u7ec4\u4ef6\u6267\u884c\u5b8c\u6bd5(\u5305\u542b\u8d85\u65f6)\uff0c\u89e3\u51b3\u65b9\u6848\u5173\u952e\u5b57<code>sync.WaitGroup<\/code><\/li>\n<li>\u5e76\u53d1\u5b50\u7ec4\u4ef6\u6267\u884c\u81ea\u8eab\u4e1a\u52a1\u903b\u8f91\u662f\u9700\u68c0\u6d4b\u8d85\u65f6\uff1a\u9632\u6b62\u5b50\u7ec4\u4ef6\u5185\u90e8\u6267\u884c\u4e1a\u52a1\u903b\u8f91\u65f6\u95f4\u8fc7\u957f\uff0c\u89e3\u51b3\u65b9\u6848\u5173\u952e\u5b57<code>select<\/code>\u548c<code>&lt;-ctx.Done()<\/code><\/li>\n<\/ul>\n<h3>\u7b2c\u4e00\u70b9\uff1a\u5e76\u53d1\u5b50\u7ec4\u4ef6\u9700\u8981\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/h3>\n<pre><code>\/\/ Context \u4e1a\u52a1\u4e0a\u4e0b\u6587 type Context struct {  \/\/ context.WithTimeout \u6d3e\u751f\u7684\u5b50\u4e0a\u4e0b\u6587  TimeoutCtx context.Context  \/\/ \u8d85\u65f6\u51fd\u6570  context.CancelFunc } <\/code><\/pre>\n<h3>\u7b2c\u4e8c\u70b9\uff1a\u533a\u5206\u666e\u901a\u7ec4\u4ef6\u548c\u5e76\u53d1\u7ec4\u4ef6<\/h3>\n<p>\u589e\u52a0\u65b0\u7684\u5e76\u53d1\u57fa\u7840\u7ec4\u4ef6\u7ed3\u6784\u4f53<code>BaseConcurrencyComponent<\/code>\uff0c\u5e76\u5408\u6210\u590d\u7528\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u4e2d\u7684\u57fa\u7840\u7ec4\u4ef6<code>BaseComponent<\/code>\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ BaseConcurrencyComponent \u5e76\u53d1\u57fa\u7840\u7ec4\u4ef6 type BaseConcurrencyComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent  \/\/ \u5f53\u524d\u7ec4\u4ef6\u662f\u5426\u6709\u5e76\u53d1\u5b50\u7ec4\u4ef6  HasChildConcurrencyComponents bool  \/\/ \u5e76\u53d1\u5b50\u7ec4\u4ef6\u5217\u8868  ChildConcurrencyComponents []Component  \/\/ wg \u5bf9\u8c61  *sync.WaitGroup  \/\/ \u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u6267\u884c\u7ed3\u679c channel  logicResChan chan interface{}  \/\/ \u5f53\u524d\u7ec4\u4ef6\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u4fe1\u606f  Err error } <\/code><\/pre>\n<h3>\u7b2c\u4e09\u70b9\uff1a\u62e5\u6709\u5e76\u53d1\u5b50\u7ec4\u4ef6\u7684\u7236\u7ec4\u4ef6\u9700\u8981\u7b49\u5f85\u5e76\u53d1\u5b50\u7ec4\u4ef6\u6267\u884c\u5b8c\u6bd5(\u5305\u542b\u8d85\u65f6)<\/h3>\n<p>\u4fee\u6539\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u4e2d\u7684<code>ChildsDo<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u652f\u6301\u5e76\u53d1\u6267\u884c\u5b50\u7ec4\u4ef6\uff0c\u4e3b\u8981\u4fee\u6539\u548c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7<code>go<\/code>\u5173\u952e\u5b57\u6267\u884c\u5b50\u7ec4\u4ef6<\/li>\n<li>\u901a\u8fc7<code>*WaitGroup.Wait()<\/code>\u7b49\u5f85\u5b50\u7ec4\u4ef6\u6267\u884c\u7ed3\u679c<\/li>\n<\/ul>\n<pre><code>\/\/ ChildsDo \u6267\u884c\u5b50\u7ec4\u4ef6 func (bc *BaseConcurrencyComponent) ChildsDo(ctx *Context) (err error) {  if bc.WaitGroup == nil {   bc.WaitGroup = &amp;sync.WaitGroup{}  }  \/\/ \u6267\u884c\u5e76\u53d1\u5b50\u7ec4\u4ef6  for _, childComponent := range bc.ChildConcurrencyComponents {   bc.WaitGroup.Add(1)   go childComponent.Do(ctx, childComponent, bc.WaitGroup)  }  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  for _, childComponent := range bc.ChildComponents {   if err = childComponent.Do(ctx, childComponent, nil); err != nil {    return err   }  }  if bc.HasChildConcurrencyComponents {   \/\/ \u7b49\u5f85\u5e76\u53d1\u7ec4\u4ef6\u6267\u884c\u7ed3\u679c   bc.WaitGroup.Wait()  }  return } <\/code><\/pre>\n<h3>\u7b2c\u56db\u70b9\uff1a\u5e76\u53d1\u5b50\u7ec4\u4ef6\u6267\u884c\u81ea\u8eab\u4e1a\u52a1\u903b\u8f91\u662f\u9700\u68c0\u6d4b\u8d85\u65f6<\/h3>\n<p><code>select<\/code>\u5173\u952e\u5b57 context.WithTimeout()\u6d3e\u751f\u7684\u5b50\u4e0a\u4e0b\u6587 Done()\u65b9\u6848\u8fd4\u56de\u7684 channel\uff0c\u53d1\u751f\u8d85\u65f6\u8be5 channel \u4f1a\u88ab\u5173\u95ed\u3002\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ Do \u6267\u884c\u5b50\u7ec4\u4ef6 \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587 \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6 \/\/ wg \u7236\u7ec4\u4ef6\u7684 waitgroup \u5bf9\u8c61 func (bc *BaseConcurrencyComponent) Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) (err error) {  defer wg.Done()  \/\/ \u521d\u59cb\u5316\u5e76\u53d1\u5b50\u7ec4\u4ef6 channel  if bc.logicResChan == nil {   bc.logicResChan = make(chan interface{}, 1)  }   go currentConponent.BusinessLogicDo(bc.logicResChan)   select {  \/\/ \u7b49\u5f85\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  case &lt;-bc.logicResChan:   \/\/ \u4e1a\u52a1\u6267\u884c\u7ed3\u679c   fmt.Println(runFuncName(), \"bc.BusinessLogicDo wait.done...\")   break  \/\/ \u8d85\u65f6\u7b49\u5f85  case &lt;-ctx.TimeoutCtx.Done():   \/\/ \u8d85\u65f6\u9000\u51fa   fmt.Println(runFuncName(), \"bc.BusinessLogicDo timeout...\")   bc.Err = ErrConcurrencyComponentTimeout   break  }  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  err = currentConponent.ChildsDo(ctx)  return } <\/code><\/pre>\n<h4>\u4ee3\u7801 demo<\/h4>\n<pre><code>var (  \/\/ ErrConcurrencyComponentTimeout \u5e76\u53d1\u7ec4\u4ef6\u4e1a\u52a1\u8d85\u65f6  ErrConcurrencyComponentTimeout = errors.New(\"Concurrency Component Timeout\") )  \/\/ Context \u4e1a\u52a1\u4e0a\u4e0b\u6587 type Context struct {  \/\/ context.WithTimeout \u6d3e\u751f\u7684\u5b50\u4e0a\u4e0b\u6587  TimeoutCtx context.Context  \/\/ \u8d85\u65f6\u51fd\u6570  context.CancelFunc }  \/\/ GetContext \u83b7\u53d6\u4e1a\u52a1\u4e0a\u4e0b\u6587\u5b9e\u4f8b \/\/ d \u8d85\u65f6\u65f6\u95f4 func GetContext(d time.Duration) *Context {  c := &amp;Context{}  c.TimeoutCtx, c.CancelFunc = context.WithTimeout(context.Background(), d)  return c }  \/\/ Component \u7ec4\u4ef6\u63a5\u53e3 type Component interface {  \/\/ \u6dfb\u52a0\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Mount(c Component, components ...Component) error  \/\/ \u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6  Remove(c Component) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1:`BusinessLogicDo`\u548c\u6267\u884c\u5b50\u7ec4\u4ef6:`ChildsDo`  \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587  \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6  \/\/ wg \u7236\u7ec4\u4ef6\u7684 waitgroup \u5bf9\u8c61  Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) error  \/\/ \u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u903b\u8f91  \/\/ resChan \u56de\u5199\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u6267\u884c\u7ed3\u679c\u7684 channel  BusinessLogicDo(resChan chan interface{}) error  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  ChildsDo(ctx *Context) error }  \/\/ BaseComponent \u57fa\u7840\u7ec4\u4ef6 \/\/ \u5b9e\u73b0 Add:\u6dfb\u52a0\u4e00\u4e2a\u5b50\u7ec4\u4ef6 \/\/ \u5b9e\u73b0 Remove:\u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6 type BaseComponent struct {  \/\/ \u5b50\u7ec4\u4ef6\u5217\u8868  ChildComponents []Component }  \/\/ Mount \u6302\u8f7d\u4e00\u4e2a\u5b50\u7ec4\u4ef6 func (bc *BaseComponent) Mount(c Component, components ...Component) (err error) {  bc.ChildComponents = append(bc.ChildComponents, c)  if len(components) == 0 {   return  }  bc.ChildComponents = append(bc.ChildComponents, components...)  return }  \/\/ Remove \u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6 func (bc *BaseComponent) Remove(c Component) (err error) {  if len(bc.ChildComponents) == 0 {   return  }  for k, childComponent := range bc.ChildComponents {   if c == childComponent {    fmt.Println(runFuncName(), \"\u79fb\u9664:\", reflect.TypeOf(childComponent))    bc.ChildComponents = append(bc.ChildComponents[:k], bc.ChildComponents[k+1:]...)   }  }  return }  \/\/ Do \u6267\u884c\u5b50\u7ec4\u4ef6 \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587 \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6 \/\/ wg \u7236\u7ec4\u4ef6\u7684 waitgroup \u5bf9\u8c61 func (bc *BaseComponent) Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) (err error) {  \/\/\u6267\u884c\u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u4ee3\u7801  err = currentConponent.BusinessLogicDo(nil)  if err != nil {   return err  }  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  return currentConponent.ChildsDo(ctx) }  \/\/ BusinessLogicDo \u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u586b\u5145\u5904 func (bc *BaseComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ do nothing  return }  \/\/ ChildsDo \u6267\u884c\u5b50\u7ec4\u4ef6 func (bc *BaseComponent) ChildsDo(ctx *Context) (err error) {  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  for _, childComponent := range bc.ChildComponents {   if err = childComponent.Do(ctx, childComponent, nil); err != nil {    return err   }  }  return }  \/\/ BaseConcurrencyComponent \u5e76\u53d1\u57fa\u7840\u7ec4\u4ef6 type BaseConcurrencyComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent  \/\/ \u5f53\u524d\u7ec4\u4ef6\u662f\u5426\u6709\u5e76\u53d1\u5b50\u7ec4\u4ef6  HasChildConcurrencyComponents bool  \/\/ \u5e76\u53d1\u5b50\u7ec4\u4ef6\u5217\u8868  ChildConcurrencyComponents []Component  \/\/ wg \u5bf9\u8c61  *sync.WaitGroup  \/\/ \u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u6267\u884c\u7ed3\u679c channel  logicResChan chan interface{}  \/\/ \u5f53\u524d\u7ec4\u4ef6\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u4fe1\u606f  Err error }  \/\/ Remove \u79fb\u9664\u4e00\u4e2a\u5b50\u7ec4\u4ef6 func (bc *BaseConcurrencyComponent) Remove(c Component) (err error) {  if len(bc.ChildComponents) == 0 {   return  }  for k, childComponent := range bc.ChildComponents {   if c == childComponent {    fmt.Println(runFuncName(), \"\u79fb\u9664:\", reflect.TypeOf(childComponent))    bc.ChildComponents = append(bc.ChildComponents[:k], bc.ChildComponents[k+1:]...)   }  }  for k, childComponent := range bc.ChildConcurrencyComponents {   if c == childComponent {    fmt.Println(runFuncName(), \"\u79fb\u9664:\", reflect.TypeOf(childComponent))    bc.ChildConcurrencyComponents = append(bc.ChildComponents[:k], bc.ChildComponents[k+1:]...)   }  }  return }  \/\/ MountConcurrency \u6302\u8f7d\u4e00\u4e2a\u5e76\u53d1\u5b50\u7ec4\u4ef6 func (bc *BaseConcurrencyComponent) MountConcurrency(c Component, components ...Component) (err error) {  bc.HasChildConcurrencyComponents = true  bc.ChildConcurrencyComponents = append(bc.ChildConcurrencyComponents, c)  if len(components) == 0 {   return  }  bc.ChildConcurrencyComponents = append(bc.ChildConcurrencyComponents, components...)  return }  \/\/ ChildsDo \u6267\u884c\u5b50\u7ec4\u4ef6 func (bc *BaseConcurrencyComponent) ChildsDo(ctx *Context) (err error) {  if bc.WaitGroup == nil {   bc.WaitGroup = &amp;sync.WaitGroup{}  }  \/\/ \u6267\u884c\u5e76\u53d1\u5b50\u7ec4\u4ef6  for _, childComponent := range bc.ChildConcurrencyComponents {   bc.WaitGroup.Add(1)   go childComponent.Do(ctx, childComponent, bc.WaitGroup)  }  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  for _, childComponent := range bc.ChildComponents {   if err = childComponent.Do(ctx, childComponent, nil); err != nil {    return err   }  }  if bc.HasChildConcurrencyComponents {   \/\/ \u7b49\u5f85\u5e76\u53d1\u7ec4\u4ef6\u6267\u884c\u7ed3\u679c   bc.WaitGroup.Wait()  }  return }  \/\/ Do \u6267\u884c\u5b50\u7ec4\u4ef6 \/\/ ctx \u4e1a\u52a1\u4e0a\u4e0b\u6587 \/\/ currentConponent \u5f53\u524d\u7ec4\u4ef6 \/\/ wg \u7236\u7ec4\u4ef6\u7684 waitgroup \u5bf9\u8c61 func (bc *BaseConcurrencyComponent) Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) (err error) {  defer wg.Done()  \/\/ \u521d\u59cb\u5316\u5e76\u53d1\u5b50\u7ec4\u4ef6 channel  if bc.logicResChan == nil {   bc.logicResChan = make(chan interface{}, 1)  }   go currentConponent.BusinessLogicDo(bc.logicResChan)   select {  \/\/ \u7b49\u5f85\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  case &lt;-bc.logicResChan:   \/\/ \u4e1a\u52a1\u6267\u884c\u7ed3\u679c   fmt.Println(runFuncName(), \"bc.BusinessLogicDo wait.done...\")   break  \/\/ \u8d85\u65f6\u7b49\u5f85  case &lt;-ctx.TimeoutCtx.Done():   \/\/ \u8d85\u65f6\u9000\u51fa   fmt.Println(runFuncName(), \"bc.BusinessLogicDo timeout...\")   bc.Err = ErrConcurrencyComponentTimeout   break  }  \/\/ \u6267\u884c\u5b50\u7ec4\u4ef6  err = currentConponent.ChildsDo(ctx)  return }  \/\/ CheckoutPageComponent \u8ba2\u5355\u7ed3\u7b97\u9875\u9762\u7ec4\u4ef6 type CheckoutPageComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u586b\u5145\u5904 func (bc *CheckoutPageComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u8ba2\u5355\u7ed3\u7b97\u9875\u9762\u7ec4\u4ef6...\")  return }  \/\/ AddressComponent \u5730\u5740\u7ec4\u4ef6 type AddressComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *AddressComponent) BusinessLogicDo(resChan chan interface{}) error {  fmt.Println(runFuncName(), \"\u5730\u5740\u7ec4\u4ef6...\")  fmt.Println(runFuncName(), \"\u83b7\u53d6\u5730\u5740\u4fe1\u606f ing...\")   \/\/ \u6a21\u62df\u8fdc\u7a0b\u8c03\u7528\u5730\u5740\u670d\u52a1  http.Get(\"http:\/\/example.com\/\")   resChan &lt;- struct{}{} \/\/ \u5199\u5165\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  fmt.Println(runFuncName(), \"\u83b7\u53d6\u5730\u5740\u4fe1\u606f done...\")  return nil }  \/\/ PayMethodComponent \u652f\u4ed8\u65b9\u5f0f\u7ec4\u4ef6 type PayMethodComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *PayMethodComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u652f\u4ed8\u65b9\u5f0f\u7ec4\u4ef6...\")  fmt.Println(runFuncName(), \"\u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f ing...\")  \/\/ \u6a21\u62df\u8fdc\u7a0b\u8c03\u7528\u5730\u5740\u670d\u52a1 \u7565  resChan &lt;- struct{}{}  fmt.Println(runFuncName(), \"\u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f done...\")  return nil }  \/\/ StoreComponent \u5e97\u94fa\u7ec4\u4ef6 type StoreComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *StoreComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u5e97\u94fa\u7ec4\u4ef6...\")  return }  \/\/ SkuComponent \u5546\u54c1\u7ec4\u4ef6 type SkuComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *SkuComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u5546\u54c1\u7ec4\u4ef6...\")  return }  \/\/ PromotionComponent \u4f18\u60e0\u4fe1\u606f\u7ec4\u4ef6 type PromotionComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *PromotionComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u4f18\u60e0\u4fe1\u606f\u7ec4\u4ef6...\")  return }  \/\/ ExpressComponent \u7269\u6d41\u7ec4\u4ef6 type ExpressComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *ExpressComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u7269\u6d41\u7ec4\u4ef6...\")  return }  \/\/ AftersaleComponent \u552e\u540e\u7ec4\u4ef6 type AftersaleComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *AftersaleComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u552e\u540e\u7ec4\u4ef6...\")  return }  \/\/ InvoiceComponent \u53d1\u7968\u7ec4\u4ef6 type InvoiceComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *InvoiceComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u53d1\u7968\u7ec4\u4ef6...\")  fmt.Println(runFuncName(), \"\u83b7\u53d6\u53d1\u7968\u4fe1\u606f ing...\")  \/\/ \u6a21\u62df\u8fdc\u7a0b\u8c03\u7528\u5730\u5740\u670d\u52a1 \u7565  resChan &lt;- struct{}{} \/\/ \u5199\u5165\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  fmt.Println(runFuncName(), \"\u83b7\u53d6\u53d1\u7968\u4fe1\u606f done...\")  return }  \/\/ CouponComponent \u4f18\u60e0\u5238\u7ec4\u4ef6 type CouponComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *CouponComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u4f18\u60e0\u5238\u7ec4\u4ef6...\")  fmt.Println(runFuncName(), \"\u83b7\u53d6\u6700\u4f18\u4f18\u60e0\u5238 ing...\")   \/\/ \u6a21\u62df\u8fdc\u7a0b\u8c03\u7528\u4f18\u60e0\u5238\u670d\u52a1  http.Get(\"http:\/\/example.com\/\")   \/\/ \u5199\u5165\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  resChan &lt;- struct{}{}  fmt.Println(runFuncName(), \"\u83b7\u53d6\u6700\u4f18\u4f18\u60e0\u5238 done...\")  return }  \/\/ GiftCardComponent \u793c\u54c1\u5361\u7ec4\u4ef6 type GiftCardComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseConcurrencyComponent }  \/\/ BusinessLogicDo \u5e76\u53d1\u7ec4\u4ef6\u5b9e\u9645\u586b\u5145\u4e1a\u52a1\u903b\u8f91\u7684\u5730\u65b9 func (bc *GiftCardComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u793c\u54c1\u5361\u7ec4\u4ef6...\")  fmt.Println(runFuncName(), \"\u83b7\u53d6\u793c\u54c1\u5361\u4fe1\u606f ing...\")  \/\/ \u6a21\u62df\u8fdc\u7a0b\u8c03\u7528\u5730\u5740\u670d\u52a1 \u7565  resChan &lt;- struct{}{} \/\/ \u5199\u5165\u4e1a\u52a1\u6267\u884c\u7ed3\u679c  fmt.Println(runFuncName(), \"\u83b7\u53d6\u793c\u54c1\u5361\u4fe1\u606f done...\")  return }  \/\/ OrderComponent \u8ba2\u5355\u91d1\u989d\u8be6\u7ec6\u4fe1\u606f\u7ec4\u4ef6 type OrderComponent struct {  \/\/ \u5408\u6210\u590d\u7528\u57fa\u7840\u7ec4\u4ef6  BaseComponent }  \/\/ BusinessLogicDo \u5f53\u524d\u7ec4\u4ef6\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u586b\u5145\u5904 func (bc *OrderComponent) BusinessLogicDo(resChan chan interface{}) (err error) {  \/\/ \u5f53\u524d\u7ec4\u4ef6\u7684\u4e1a\u52a1\u903b\u8f91\u5199\u8fd9  fmt.Println(runFuncName(), \"\u8ba2\u5355\u91d1\u989d\u8be6\u7ec6\u4fe1\u606f\u7ec4\u4ef6...\")  return }  \/\/ Demo \u793a\u4f8b func Demo() {  \/\/ \u521d\u59cb\u5316\u8ba2\u5355\u7ed3\u7b97\u9875\u9762 \u8fd9\u4e2a\u5927\u7ec4\u4ef6  checkoutPage := &amp;CheckoutPageComponent{}   \/\/ \u6302\u8f7d\u5b50\u7ec4\u4ef6  storeComponent := &amp;StoreComponent{}  skuComponent := &amp;SkuComponent{}  skuComponent.Mount(   &amp;PromotionComponent{},   &amp;AftersaleComponent{},  )  storeComponent.Mount(   skuComponent,   &amp;ExpressComponent{},  )   \/\/ ---\u6302\u8f7d\u7ec4\u4ef6---   \/\/ \u666e\u901a\u7ec4\u4ef6  checkoutPage.Mount(   storeComponent,   &amp;OrderComponent{},  )  \/\/ \u5e76\u53d1\u7ec4\u4ef6  checkoutPage.MountConcurrency(   &amp;AddressComponent{},   &amp;PayMethodComponent{},   &amp;InvoiceComponent{},   &amp;CouponComponent{},   &amp;GiftCardComponent{},  )   \/\/ \u521d\u59cb\u5316\u4e1a\u52a1\u4e0a\u4e0b\u6587 \u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4  ctx := GetContext(5 * time.Second)  defer ctx.CancelFunc()  \/\/ \u5f00\u59cb\u6784\u5efa\u9875\u9762\u7ec4\u4ef6\u6570\u636e  checkoutPage.ChildsDo(ctx) }  func main() {  runtime.GOMAXPROCS(runtime.NumCPU() - 1)  DemoConcurrency( }  \/\/ \u83b7\u53d6\u6b63\u5728\u8fd0\u884c\u7684\u51fd\u6570\u540d func runFuncName() string {  pc := make([]uintptr, 1)  runtime.Callers(2, pc)  f := runtime.FuncForPC(pc[0])  return f.Name()  return \"\" }   <\/code><\/pre>\n<p>\u4ee3\u7801\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>Running] go run \"..\/easy-tips\/go\/patterns\/composite\/concurrency\/composite-concurrency.go\" main.(*StoreComponent).BusinessLogicDo \u5e97\u94fa\u7ec4\u4ef6... main.(*SkuComponent).BusinessLogicDo \u5546\u54c1\u7ec4\u4ef6... main.(*PromotionComponent).BusinessLogicDo \u4f18\u60e0\u4fe1\u606f\u7ec4\u4ef6... main.(*AftersaleComponent).BusinessLogicDo \u552e\u540e\u7ec4\u4ef6... main.(*ExpressComponent).BusinessLogicDo \u7269\u6d41\u7ec4\u4ef6... main.(*OrderComponent).BusinessLogicDo \u8ba2\u5355\u91d1\u989d\u8be6\u7ec6\u4fe1\u606f\u7ec4\u4ef6... main.(*PayMethodComponent).BusinessLogicDo \u652f\u4ed8\u65b9\u5f0f\u7ec4\u4ef6... main.(*PayMethodComponent).BusinessLogicDo \u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f ing... main.(*InvoiceComponent).BusinessLogicDo \u53d1\u7968\u7ec4\u4ef6... main.(*InvoiceComponent).BusinessLogicDo \u83b7\u53d6\u53d1\u7968\u4fe1\u606f ing... main.(*GiftCardComponent).BusinessLogicDo \u793c\u54c1\u5361\u7ec4\u4ef6... main.(*GiftCardComponent).BusinessLogicDo \u83b7\u53d6\u793c\u54c1\u5361\u4fe1\u606f ing... main.(*CouponComponent).BusinessLogicDo \u4f18\u60e0\u5238\u7ec4\u4ef6... main.(*CouponComponent).BusinessLogicDo \u83b7\u53d6\u53d1\u7968\u4fe1\u606f ing... main.(*AddressComponent).BusinessLogicDo \u5730\u5740\u7ec4\u4ef6... main.(*AddressComponent).BusinessLogicDo \u83b7\u53d6\u5730\u5740\u4fe1\u606f ing... main.(*InvoiceComponent).BusinessLogicDo \u83b7\u53d6\u53d1\u7968\u4fe1\u606f done... main.(*BaseConcurrencyComponent).Do bc.BusinessLogicDo wait.done... main.(*BaseConcurrencyComponent).Do bc.BusinessLogicDo wait.done... main.(*PayMethodComponent).BusinessLogicDo \u83b7\u53d6\u652f\u4ed8\u65b9\u5f0f done... main.(*AddressComponent).BusinessLogicDo \u83b7\u53d6\u5730\u5740\u4fe1\u606f done... main.(*BaseConcurrencyComponent).Do bc.BusinessLogicDo wait.done... main.(*CouponComponent).BusinessLogicDo \u83b7\u53d6\u53d1\u7968\u4fe1\u606f done... main.(*BaseConcurrencyComponent).Do bc.BusinessLogicDo wait.done... main.(*GiftCardComponent).BusinessLogicDo \u83b7\u53d6\u793c\u54c1\u5361\u4fe1\u606f done... main.(*BaseConcurrencyComponent).Do bc.BusinessLogicDo wait.done... <\/code><\/pre>\n<h4>\u300c\u7ec4\u5408\u6a21\u5f0f\u300d\u548c\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u57fa\u51c6\u6d4b\u8bd5\u5bf9\u6bd4<\/h4>\n<p>\u57fa\u51c6\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code> func Benchmark_Normal(b *testing.B) {  b.SetParallelism(runtime.NumCPU())  b.RunParallel(func(pb *testing.PB) {   for pb.Next() {    normal.Demo()   }  }) }  func Benchmark_Concurrency(b *testing.B) {  b.SetParallelism(runtime.NumCPU())  b.RunParallel(func(pb *testing.PB) {   for pb.Next() {    concurrency.Demo()   }  }) } <\/code><\/pre>\n<p>\u672c\u5730\u673a\u5668 Benchmark \u5bf9\u6bd4\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n<pre><code>go test -benchmem -run=^$ easy-tips\/go\/patterns\/composite -bench . -v -count=1 --benchtime 20s  goos: darwin goarch: amd64 pkg: easy-tips\/go\/patterns\/composite Benchmark_Normal-4                   376          56666895 ns\/op           35339 B\/op        286 allocs\/op Benchmark_Concurrency-4              715          32669301 ns\/op           36445 B\/op        299 allocs\/op <\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\u6765<code>Benchmark_Concurrency-4<\/code>\u5e73\u5747\u6bcf\u6b21\u7684\u6267\u884c\u65f6\u95f4\u662f<code>32669301 ns<\/code>\u662f\u8981\u4f18\u4e8e<code>Benchmark_Normal<\/code>\u7684<code>56666895 ns<\/code>\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u300c\u5e76\u53d1\u7ec4\u5408\u6a21\u5f0f\u300d\u662f\u4e00\u4e2a\u7531\u7279\u5b9a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u7ed3\u5408 Go \u8bed\u8a00\u5929\u751f\u7684\u5e76\u53d1\u7279\u6027\uff0c\u901a\u8fc7\u9002\u5f53\u5c01\u88c5\u5f62\u6210\u7684\u201c\u65b0\u6a21\u5f0f\u201d\u3002<\/p>\n<pre><code>\u7279\u522b\u8bf4\u660e\uff1a \u672c\u7cfb\u5217\u7684\u4e00\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u6982\u5ff5\u53ef\u80fd\u548c\u539f\u6982\u5ff5\u5b58\u5728\u5dee\u5f02\uff0c\u56e0\u4e3a\u4f1a\u7ed3\u5408\u5b9e\u9645\u4f7f\u7528\uff0c\u53d6\u5176\u7cbe\u534e\uff0c\u9002\u5f53\u6539\u53d8\uff0c\u7075\u6d3b\u4f7f\u7528\u3002 <\/code><\/pre>\n<blockquote>\n<p>Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\u7cfb\u5217 \u66f4\u591a\u6587\u7ae0 \u70b9\u51fb\u6b64\u5904\u67e5\u770b<\/p>\n<\/blockquote>\n<p>\u83b7\u53d6\u66f4\u591a\u539f\u521b\u6280\u672f\u6587\u7ae0:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/033f853ef3834b8ab6c626c3a4564acdtplv-k3u1fbpfcp-zoom-1.image_.png\" alt=\"\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218\" \/><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4065337\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hzjseasea <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u5f88\u725b\u903c\uff0c\u6536\u85cf\u4e86<br \/>\u501f\u4e2a\uff0c\u6c42\u5927\u4f6c\u4eec\u591a\u63a8\u8350\u63a8\u8350\u8fd9\u4e9b\u6587\u7ae0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065338\" 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>                                                             \u603b\u7ed3:\u4e71\u9020\u65b0\u540d\u8bcd,\u672c\u8d28\u4e0a\u8fd8\u662f\u54cd\u5e94\u5f0f\u7f16\u7a0b,\u5e76\u884c\u7f16\u7a0b,Future \u6a21\u5f0f\u90a3\u4e00\u5957                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065339\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PiersSoCool <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u6ca1\u4ed4\u7ec6\u770b\uff0c\u4e0d\u5c31\u662f\u7528 wait.group \u8fdb\u884c\u540c\u6b65\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065340\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiazhoulvke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b66\u4e60\u4e86\uff0c\u611f\u8c22\u5206\u4eab                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065341\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lbp0200 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8111\u5b50\u7b28\uff0c\u770b\u89c1\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5c31\u60f3\u9003                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065342\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @PiersSoCool \u662f\u7684 \u548c \u7ec4\u5408\u6a21\u5f0f\u7ed3\u5408\u4e86\u4e0b \u8fdb\u884c\u4e86\u4e00\u6b21\u5c01\u88c5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065343\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hzjseasea \u4e92\u76f8\u5b66\u4e60~ \u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065344\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jiazhoulvke \u4e92\u76f8\u5b66\u4e60~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065345\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lbp0200 \u5176\u5b9e\u5c31\u662f\u628a\u53d8\u5316\u7684\u903b\u8f91\u62bd\u8c61\u51fa\u6765 \u5bf9\u5b9e\u9645\u7684\u4f9d\u8d56\u8f6c\u5316\u4e3a\u5bf9\u62bd\u8c61\u7684\u4f9d\u8d56                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4065346\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : TIGERB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wysnylc Future \u6a21\u5f0f\u8fd9\u4e2a\u6211\u4e0d\u592a\u61c2 \u6211\u5b66\u4e60\u4e0b\u54c8 \u611f\u8c22\u5206\u4eab~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5e76\u53d1\u7ec4\u4ef6 | Go \u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u6218 &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\/187293"}],"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=187293"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/187293\/revisions"}],"predecessor-version":[{"id":187295,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/187293\/revisions\/187295"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187293"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}