{"id":207253,"date":"2025-07-08T09:15:26","date_gmt":"2025-07-08T01:15:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207253\/"},"modified":"2025-07-08T09:15:26","modified_gmt":"2025-07-08T01:15:26","slug":"%e5%a6%82%e4%bd%95%e4%bb%a5%e6%83%af%e7%94%a8%e7%9a%84%e6%96%b9%e5%bc%8f%e4%bc%98%e9%9b%85%e5%9c%b0%e5%85%b3%e9%97%ad%e9%93%be%e5%bc%8f-goroutine","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207253\/","title":{"rendered":"\u5982\u4f55\u4ee5\u60ef\u7528\u7684\u65b9\u5f0f\u4f18\u96c5\u5730\u5173\u95ed\u94fe\u5f0f goroutine"},"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\u4ee5\u60ef\u7528\u7684\u65b9\u5f0f\u4f18\u96c5\u5730\u5173\u95ed\u94fe\u5f0f goroutine<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 09:33: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>\u600e\u4e48\u5165\u95e8Golang\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u4ee5\u60ef\u7528\u7684\u65b9\u5f0f\u4f18\u96c5\u5730\u5173\u95ed\u94fe\u5f0f goroutine\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u521b\u5efa\u591a\u4e2a goroutine\uff0c\u8fd9\u4e9b goroutine \u5c06\u5177\u6709\u5d4c\u5957 goroutine\uff0c\u540c\u65f6\u4ee5\u591a\u7ea7\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff08\u60f3\u8c61\u4e00\u68f5 goroutine \u6811\uff0c\u6bcf\u4e2a\u7ea7\u522b\u53ef\u4ee5\u6709\u8bb8\u591a\u53f6\u5b50\uff09\u3002<\/p>\n<p><strong>\u6309\u987a\u5e8f\u4f18\u96c5\u5730\u5173\u95ed\u8fd9\u4e9b goroutine<\/strong> \u5e76\u7b49\u5f85\u5b83\u4eec\u56de\u6765\u7684\u60ef\u7528\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u987a\u5e8f\u662f\u4ece\u4e0b\u5230\u4e0a\uff08\u6700\u6df1\u7684\u5b69\u5b50\u5728\u524d\uff09\uff0c\u5e76\u4e14\u5047\u8bbe\u6211\u4e0d\u77e5\u9053\u6211\u5c06\u63d0\u524d\u542f\u52a8\u591a\u5c11\u4e2a goroutine\uff08\u52a8\u6001\uff09\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u53ea\u662f\u4ee5<strong>\u975e\u6709\u5e8f<\/strong>\u65b9\u5f0f\u4f18\u96c5\u5730\u5173\u95ed\u5b83\u4eec\u3002<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    ctx := context.Background()\n    ctx, cancel := context.WithCancel(ctx)\n\n    \/\/level1\n    go func() {\n        fmt.Println(\"level1 started\")\n        \/\/level2\n        go func() {\n            fmt.Println(\"level2 started\")\n\n            \/\/level3\n            go func() {\n                fmt.Println(\"level3 started\")\n                select {\n                case &lt;-ctx.Done():\n                    fmt.Println(\"Done called on level3\")\n                case &lt;-time.After(5* time.Second):\n                    fmt.Println(\"After called on level3\")\n                }\n\n            }()\n            select {\n            case &lt;-ctx.Done():\n                fmt.Println(\"Done called on level2\")\n            case &lt;-time.After(7* time.Second):\n                fmt.Println(\"After called on level2\")\n            }\n\n        }()\n        select {\n        case &lt;-ctx.Done():\n            fmt.Println(\"Done called on level1\")\n        case &lt;-time.After(10* time.Second):\n            fmt.Println(\"After called on level1\")\n        }\n\n\n    }()\n    time.Sleep(1*time.Second)\n    cancel()\n    time.Sleep(1 * time.Second)\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8981\u7b49\u5f85\u4e00\u7ec4 goroutine\uff0c \u662f\u60ef\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5f53\u4f60\u542f\u52a8\u4e00\u4e2a\u65b0\u7684 goroutine () \u65f6\uff0c\u4f60\u53ef\u4ee5\u7ed9\u5b83\u7684\u8ba1\u6570\u5668\u52a0 1\uff0c\u5e76\u4e14 goroutine \u53ef\u4ee5\u7528 \u53d1\u51fa\u4fe1\u53f7\u8868\u660e\u5b83\u5df2\u7ecf\u5b8c\u6210\u3002\u7236 goroutine \u53ef\u4ee5\u8c03\u7528 \u6765\u7b49\u5f85\u5b83\u7684\u6240\u6709\u5b50 goroutine \u5b8c\u6210\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u6bcf\u4e2a\u7ea7\u522b\u4e0a\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u3002\u5728\u542f\u52a8\u5b50\u534f\u7a0b\u7684\u6bcf\u4e2a\u7ea7\u522b\u4e0a\u521b\u5efa\u4e00\u4e2a <code>waitgroup<\/code>\uff0c\u5e76\u4e14\u4ec5\u5f53\u8be5\u534f\u7a0b\u7684 <code>wait()<\/code> \u8fd4\u56de\u65f6\u624d\u8fd4\u56de\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5b83\u5728\u60a8\u7684\u793a\u4f8b\u4e2d\u7684\u5e94\u7528\u65b9\u5f0f\uff1a<\/p>\n<pre>ctx := context.background()\nctx, cancel := context.withcancel(ctx)\n\n\/\/level1\nwg1 := &amp;sync.waitgroup{}\nwg1.add(1)\ngo func() {\n    defer wg1.done()\n    fmt.println(\"level1 started\")\n    \/\/level2\n    wg2 := &amp;sync.waitgroup{}\n    wg2.add(1)\n    go func() {\n        defer wg2.done()\n        fmt.println(\"level2 started\")\n\n        \/\/level3\n        wg3 := &amp;sync.waitgroup{}\n        wg3.add(1)\n        go func() {\n            defer wg3.done()\n            fmt.println(\"level3 started\")\n            select {\n            case &lt;-ctx.done():\n                fmt.println(\"done called on level3\")\n            case &lt;-time.after(5 * time.second):\n                fmt.println(\"after called on level3\")\n            }\n            fmt.println(\"level 3 ended.\")\n        }()\n\n        select {\n        case &lt;-ctx.done():\n            fmt.println(\"done called on level2\")\n        case &lt;-time.after(7 * time.second):\n            fmt.println(\"after called on level2\")\n        }\n        wg3.wait()\n        fmt.println(\"level 2 ended.\")\n    }()\n\n    select {\n    case &lt;-ctx.done():\n        fmt.println(\"done called on level1\")\n    case &lt;-time.after(10 * time.second):\n        fmt.println(\"after called on level1\")\n    }\n    wg2.wait()\n    fmt.println(\"level 1 ended.\")\n}()\n\ntime.sleep(1 * time.second)\ncancel()\nwg1.wait()\nfmt.println(\"main ended.\")<\/pre>\n<p>\u6b64\u8f93\u51fa\uff08\u5728 \u4e0a\u5c1d\u8bd5\uff09\uff1a<\/p>\n<pre>level1 started\nlevel2 started\nlevel3 started\ndone called on level1\ndone called on level3\nlevel 3 ended.\ndone called on level2\nlevel 2 ended.\nlevel 1 ended.\nparent ended.<\/pre>\n<p>\u8f93\u51fa\u4e2d\u91cd\u8981\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>Level 3 ended.\nLevel 2 ended.\nLevel 1 ended.\nMain ended.<\/pre>\n<p>\u7ea7\u522b\u4ee5\u964d\u5e8f\u7ea7\u522b\u7ed3\u675f\uff08\u4ece\u4e0b\u5230\u4e0a\uff09\uff0c\u4ee5 <code>\u201c\u4e3b\u8981\u7ed3\u675f\u3002\u201d<\/code> \u7ed3\u675f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\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-207253","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207253","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=207253"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207253\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}