{"id":29,"date":"2022-11-23T20:07:35","date_gmt":"2022-11-23T12:07:35","guid":{"rendered":"https:\/\/www.road-trip.cc\/?p=29"},"modified":"2024-05-06T16:04:03","modified_gmt":"2024-05-06T08:04:03","slug":"sync-waitgroup%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.road-trip.cc\/?p=29","title":{"rendered":"sync.WaitGroup\u89e3\u6790"},"content":{"rendered":"\n<p>\u5f53\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u591a\u4e2a\u7ebf\u7a0b\u4e00\u8d77\u53bb\u5b8c\u6210\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4e0d\u80fd\u9000\u51fa\uff0c\u4e3b\u7ebf\u7a0b\u4e00\u65e6\u9000\u51fa\uff0c\u8fdb\u7a0b\u7ed3\u675f\uff0c\u6240\u6709\u7684\u7ebf\u7a0b\u4e5f\u88ab\u9500\u6bc1\u4e86\u3002\u5982\u679c\u53ef\u4ee5\u7528\u4e00\u4e2a\u5f88\u7c97\u7cd9\u7684\u65b9\u5f0f\u8ba9\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u6240\u6709\u7684\u5de5\u4f5c\u7ebf\u7a0b\u90fd\u7ed3\u675f\u540e\u5728\u9000\u51fa\uff0c\u53c2\u8003\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\" style=\"border-width:1px\"><code>package main\n\nimport \"time\"\n\nfunc main() {\n    for i := 1; i &lt; 10; i++ {\n        go func() {\n            \/\/ do something...\n        }()\n    }\n\n    time.Sleep(10 * time.Second)\n}<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4f30\u7b97\uff0c\u6211\u4eec\u8ba9\u4e3b\u7ebf\u7a0bsleep10\u79d2\u949f\u540e\u9000\u51fa\uff0c\u8fd9\u662f\u4e00\u79cd\u975e\u5e38ugly\u7684\u65b9\u5f0f\u3002Go\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f88\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002\u53c2\u8003\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\" style=\"border-style:none;border-width:0px\"><code>package main\n\nimport \"sync\"\n\nfunc main() {\n    var wg sync.WaitGroup\n    wg.Add(10)\n    for i := 1; i &lt; 10; i++ {\n        go func() {\n            \/\/ do something...\n            wg.Done()\n        }()\n    }\n    wg.Wait()\n}<\/code><\/pre>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e2a\u7a0d\u5fae\u9ebb\u70e6\u70b9\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4f7f\u7528Go\u7684channel\uff0c\u53c2\u8003\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\" style=\"border-width:1px\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    done := make(chan bool)\n    for i := 0; i &lt; 10; i++ {\n        go func(i int) {\n            fmt.Println(i)\n            done &lt;- true\n        }(i)\n    }\n    for i := 0; i &lt; 10; i++ {\n        &lt;-done\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e00\u4e2a\u7f16\u7a0b\u6280\u5de7\uff0c\u6765\u81eansq\u7684\u6e90\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-tertiary-background-color has-background\" style=\"border-width:1px\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\ntype WaitGroupWrapper struct {\n    sync.WaitGroup\n}\n\nfunc (w *WaitGroupWrapper) Wrapper(cb func()) {\n    w.Add(1)\n    go func() {\n        cb()\n        w.Done()\n    }()\n    w.Wait()\n}\n\ntype Person struct {\n    Wrapper WaitGroupWrapper\n    Name    string\n    Age     int\n}\n\nfunc main() {\n    var p Person\n    exitFunc := func() {\n        fmt.Println(\"exit func\")\n    }\n\n    p.Wrapper.Wrapper(exitFunc)\n    p.Wrapper.Wrapper(exitFunc)\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u591a\u4e2a\u7ebf\u7a0b\u4e00\u8d77\u53bb\u5b8c\u6210\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4e0d\u80fd\u9000\u51fa\uff0c\u4e3b\u7ebf\u7a0b\u4e00\u65e6\u9000\u51fa\uff0c\u8fdb\u7a0b\u7ed3\u675f\uff0c\u6240\u6709\u7684\u7ebf\u7a0b\u4e5f\u88ab\u9500\u6bc1\u4e86\u3002\u5982\u679c\u53ef\u4ee5\u7528 &#8230; <a title=\"sync.WaitGroup\u89e3\u6790\" class=\"read-more\" href=\"https:\/\/www.road-trip.cc\/?p=29\" aria-label=\"\u9605\u8bfb sync.WaitGroup\u89e3\u6790\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[15,13,14,16,17,18,19],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-7","tag-gl","tag-go","tag-golang","tag-programming","tag-sync","tag-threads","tag-waitgroup"],"_links":{"self":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":2,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}