{"id":207778,"date":"2025-07-08T10:44:46","date_gmt":"2025-07-08T02:44:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207778\/"},"modified":"2025-07-08T10:44:46","modified_gmt":"2025-07-08T02:44:46","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-goroutine-%e4%b8%ad%e7%9a%84%e6%97%a0%e7%bc%93%e5%86%b2%e9%80%9a%e9%81%93%e5%be%97%e5%88%b0%e8%bf%99%e4%b8%aa%e9%a1%ba%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207778\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 goroutine \u4e2d\u7684\u65e0\u7f13\u51b2\u901a\u9053\u5f97\u5230\u8fd9\u4e2a\u987a\u5e8f"},"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>\u4e3a\u4ec0\u4e48 goroutine \u4e2d\u7684\u65e0\u7f13\u51b2\u901a\u9053\u5f97\u5230\u8fd9\u4e2a\u987a\u5e8f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 15:57:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48 goroutine \u4e2d\u7684\u65e0\u7f13\u51b2\u901a\u9053\u5f97\u5230\u8fd9\u4e2a\u987a\u5e8f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u7f16\u5199\u4e00\u4e9b\u5e26\u6709 goroutine \u548c\u901a\u9053\u7684 golang \u5e76\u53d1\u4ee3\u7801 \u8fd9\u662f\u6211\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc main() {\n    in := make(chan int)\n\n    go func() {\n        fmt.println(\"adding num to channel\")\n        in &lt;- 1\n        fmt.println(\"done\")\n    }()\n    val := &lt;- in\n    fmt.println(val)\n}<\/pre>\n<p>\u6211\u505a\u4e86\u4e00\u4e2a\u65e0\u7f13\u51b2\u7684\u901a\u9053\uff0c\u5728\u6211\u770b\u6765\uff0c\u91cc\u9762\u7684\u901a\u9053\u5fc5\u987b\u7b49\u5230\u5916\u9762\u7684\u901a\u9053\u8bfb\u53d6\u5b83\uff0c\u8f93\u51fa\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>adding num to channel\n1\ndone<\/pre>\n<p>\u4f46\u4e8b\u5b9e\u4e0a\uff0c\u8f93\u51fa\u662f\uff1a<\/p>\n<pre>Adding num to channel\nDone\n1<\/pre>\n<p>\u6211\u5f88\u56f0\u60d1\u4e3a\u4ec0\u4e48\u5185\u90e8\u65e0\u7f13\u51b2\u901a\u9053\u4e0d\u7b49\u5f85\u8bfb\u53d6\u5c31\u8fd0\u884c<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5bf9\u8f93\u51fa\u7684\u89e3\u91ca\u4e0d\u6b63\u786e\u3002 goroutine \u786e\u5b9e\u5199\u5165\u4e86\u901a\u9053\uff0c\u6b64\u65f6\u4e3b goroutine \u786e\u5b9e\u8bfb\u53d6\u4e86\uff0c\u4f46\u662f\u201cDone\u201d\u7684 printf \u5728\u503c\u7684 printf \u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p>\u540c\u6b65\u64cd\u4f5c\u5728 goroutine \u4e4b\u95f4\u5efa\u7acb\u201c\u4e4b\u524d\u53d1\u751f\u201d\u7684\u5173\u7cfb\u3002\u5f53 goroutine \u5199\u5165\u901a\u9053\u65f6\uff0c\u5728\u901a\u9053\u5199\u5165\u4e4b\u524d\u552f\u4e00\u4fdd\u8bc1\u53d1\u751f\u7684\u4e8b\u60c5\u662f goroutine \u4e2d\u7684\u7b2c\u4e00\u4e2a println\u3002\u4e00\u65e6\u901a\u9053\u5199\u5165\u548c\u76f8\u5e94\u7684\u8bfb\u53d6\u5b8c\u6210\uff0c\u5269\u4e0b\u7684 Goroutine \u548c\u4e3b Goroutine \u5c31\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<p>\u5728\u4f60\u7684\u4f8b\u5b50\u4e2d\uff0cgoroutine \u5728\u4e3b goroutine \u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e3a\u4ec0\u4e48 goroutine \u4e2d\u7684\u65e0\u7f13\u51b2\u901a\u9053\u5f97\u5230\u8fd9\u4e2a\u987a\u5e8f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/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-207778","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207778","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=207778"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207778\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}