{"id":43142,"date":"2024-12-01T15:10:40","date_gmt":"2024-12-01T07:10:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43142\/"},"modified":"2024-12-01T15:10:40","modified_gmt":"2024-12-01T07:10:40","slug":"golang-goroutine-%e5%90%8c%e6%ad%a5%e9%a2%84%e6%9c%9f%e8%a1%8c%e4%b8%ba","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43142\/","title":{"rendered":"golang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a"},"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>golang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a<\/span><\/p>\n<h1>golang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 23:12:24<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300agolang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c1d\u8bd5\u53d1\u9001\u5230\u4e3b goroutine \u4e0a\u7684\u901a\u9053\u5e76\u4ece\u53e6\u4e00\u4e2a goroutine \u63a5\u6536\uff0c\u4f46\u6709\u51e0\u6b21\u5b83\u6309\u9884\u671f\u8fd4\u56de\uff0c\u4f46\u6709\u51e0\u6b21\u5b83\u9000\u51fa\u800c\u4e0d\u5728\u63a7\u5236\u53f0\u5c4f\u5e55\u4e0a\u6253\u5370\u4efb\u4f55\u5185\u5bb9<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch := make(chan bool)\n\n    go func() {\n        data := &lt;-ch\n        fmt.printf(\"received: %t\", data)\n    }()\n    ch &lt;- true\n}<\/pre>\n<p>\u540c\u65f6\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6bcf\u6b21\u90fd\u6309\u9884\u671f\u5de5\u4f5c\uff0c\u4e00\u4e2a\u533a\u522b\u662f\u6dfb\u52a0\u4e86\u989d\u5916\u7684\u68c0\u67e5\u6765\u68c0\u67e5\u901a\u9053\u662f\u5426\u5173\u95ed\uff0c\u8fd9\u603b\u662f\u629b\u51fa\u76f8\u540c\u7684\u9884\u671f\u8f93\u51fa\u3002 \u8fd9\u662f\u5426\u786e\u4fdd\u5bf9\u901a\u9053\u7684\u68c0\u67e5\u662f\u5fc5\u987b\u7684\u800c\u4e0d\u662f\u53ef\u9009\u7684\uff1f\u6216\u4ee3\u7801\u6709\u4efb\u4f55\u95ee\u9898<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch := make(chan bool)\n\n    go func() {\n        data, ok := &lt;-ch\n        if !ok {\n            fmt.Println(\"Channel closed\")\n            return\n        }\n        fmt.Printf(\"Received: %t\", data)\n    }()\n    ch &lt;- true\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u4e3b\u4f8b\u7a0b\u9000\u51fa\u4e4b\u524d\uff0c\u60a8\u5e94\u8be5\u7b49\u5f85 goroutine \u5b8c\u6210\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    ch := make(chan bool)\n    var wg sync.WaitGroup\n    wg.Add(1)\n    go func() {\n        defer wg.Done()\n        data := &lt;-ch\n        fmt.Printf(\"Received: %t\", data)\n    }()\n    ch &lt;- true\n    wg.Wait()\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\u300agolang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\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; &gt; &gt; golang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a golang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a \u6765\u6e90\uff1astackoverflow 2024-04-29 23:12:24 0\u6d4f\u89c8 \u6536\u85cf \u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300agolang goroutine \u540c\u6b65\u9884\u671f\u884c\u4e3a\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u95ee\u9898\u5185\u5bb9 \u4e0b\u9762\u7684\u4ee3\u7801\u5c1d\u8bd5\u53d1\u9001\u5230\u4e3b goroutine \u4e0a\u7684\u901a\u9053\u5e76\u4ece\u53e6\u4e00\u4e2a goroutine \u63a5\u6536\uff0c\u4f46\u6709\u51e0\u6b21\u5b83\u6309\u9884\u671f\u8fd4\u56de\uff0c\u4f46\u6709\u51e0\u6b21\u5b83\u9000\u51fa\u800c\u4e0d\u5728\u63a7\u5236\u53f0\u5c4f\u5e55\u4e0a\u6253\u5370\u4efb\u4f55\u5185\u5bb9 package main import &#8220;fmt&#8221; func main() { ch := make(chan bool) go func() { data := &lt;-ch fmt.printf(&#8220;received: %t&#8221;, data) }() ch &lt;- true } \u540c\u65f6\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6bcf\u6b21\u90fd\u6309\u9884\u671f\u5de5\u4f5c\uff0c\u4e00\u4e2a\u533a\u522b\u662f\u6dfb\u52a0\u4e86\u989d\u5916\u7684\u68c0\u67e5\u6765\u68c0\u67e5\u901a\u9053\u662f\u5426\u5173\u95ed\uff0c\u8fd9\u603b\u662f\u629b\u51fa\u76f8\u540c\u7684\u9884\u671f\u8f93\u51fa\u3002 \u8fd9\u662f\u5426\u786e\u4fdd\u5bf9\u901a\u9053\u7684\u68c0\u67e5\u662f\u5fc5\u987b\u7684\u800c\u4e0d\u662f\u53ef\u9009\u7684\uff1f\u6216\u4ee3\u7801\u6709\u4efb\u4f55\u95ee\u9898 package main import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43142","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43142"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43142\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}