{"id":555399,"date":"2022-02-22T16:12:12","date_gmt":"2022-02-22T08:12:12","guid":{"rendered":"http:\/\/4563.org\/?p=555399"},"modified":"2022-02-22T16:12:12","modified_gmt":"2022-02-22T08:12:12","slug":"golang-%e4%b8%ad-goroutine-%e4%bd%bf%e7%94%a8-chan-%e6%95%b0%e6%8d%ae%e4%b8%a2%e5%a4%b1%e7%9a%84%e9%97%ae%e9%a2%98-2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=555399","title":{"rendered":"golang \u4e2d goroutine \u4f7f\u7528 chan \u6570\u636e\u4e22\u5931\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> golang \u4e2d goroutine \u4f7f\u7528 chan \u6570\u636e\u4e22\u5931\u7684\u95ee\u9898  <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reber0ask <\/span>  <span><i><\/i> 40<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u6ce8\u91ca\u7684\u5730\u65b9\uff0c\u901a\u8fc7 for range \u904d\u5386\u65f6 go show1(x) \u53ef\u4ee5\u6b63\u5e38\u8f93\u51fa 1 \u5230 10<\/p>\n<p>\u4f46\u662f\u4f7f\u7528 for \u904d\u5386\u65f6 go show2(x) \u6709\u65f6\u5019\u4e0d\u80fd\u5b8c\u6574\u7684\u8f93\u51fa\uff0c\u53ea\u80fd\u8f93\u51fa 1 \u5230 9 \uff0c\u4f1a\u4e22\u5931\u4e00\u90e8\u5206<\/p>\n<p>\u60f3\u95ee\u4e0b\u662f\u5426\u6211\u4ee3\u7801\u4e2d goroutine \u4f7f\u7528\u7684\u6709\u95ee\u9898\uff0c\u8fd8\u662f\u8bf4\u53ea\u80fd\u901a\u8fc7 for range \u7ed9 goroutine \u4f20\u503c\uff1f<\/p>\n<pre><code>var (  wg = sync.WaitGroup{} )  func show1(x int) {  defer wg.Done()   fmt.Println(x) }  func show2(ch chan int) {  defer wg.Done()   c := &lt;-ch  fmt.Println(c) }  func main() {  ch1 := make(chan int, 40)   for i := 1; i &lt; 11; i++ {   ch1 &lt;- i  }  close(ch1)   \/\/ for x := range ch1 {  \/\/  wg.Add(1)  \/\/  go show1(x)   \/\/ }   for j := 1; j &lt; len(ch1)+1; j++ {   wg.Add(1)   go show2(ch1)  }   wg.Wait() } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7070494\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : k9982874 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u8fd9\u4ee3\u7801\u5608\u70b9\u592a\u591a\u3002\u3002\u81ea\u5df1\u518d\u4ed4\u7ec6\u81ea\u67e5\u4e00\u4e0b <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070495\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : reber0ask <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @k9982874 \u521a\u5f00\u59cb\u5b66 go \uff0c\uff0c <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070496\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yanzhiling2001 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @k9982874 \u6c42\u6307\u70b9 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070497\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : FakNoCNName <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u5148\u8003\u8651\u7b97\u6cd5\u95ee\u9898\u3002<br \/>len(ch1)\u662f\u4e2a\u52a8\u6001\u7684\u503c\uff0c\u4f60\u6bcf\u6b21\u8bfb\u53d6\u3001\u5199\u5165\u6570\u636e\u90fd\u4f1a\u53d8\u5316\u3002<br \/>\u6309\u7167\u4f60\u5199\u7684\u7b97\u6cd5\uff0c\u6700\u591a\u8f93\u51fa 10~6 \u8fd9\u51e0\u4e2a\u6570\u5b57\uff0c\u5f88\u597d\u5947\u4e3a\u4ec0\u4e48\u53ea\u4e22\u4e86\u4e00\u4e2a\uff1f<br \/>\u6539\u6210\u4e0b\u9762\u7684\u8bd5\u8bd5\uff1a<br \/> n := len(ch1)<br \/> for j := 0; j &lt; n; j++ {<br \/> wg.Add(1)<br \/> go show2(ch1)<br \/> } <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070498\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LemonK <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> len(ch1) \u4f1a\u53d8\u5c0f\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070499\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : clearT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u5c31\u662f\u4e0a\u9762\u5c42\u8bf4\u7684\u95ee\u9898\uff0clen(ch1) \u662f\u4e2a\u52a8\u6001\u7684\u503c\uff0c\u6309\u7167\u4f60\u90a3\u79cd\u5199\u6cd5\u4e5f\u4e0d\u4e00\u5b9a\u53ea\u4f1a\u4e22\u4e00\u4e2a\uff0c\u548c goroutine \u7684\u8c03\u5ea6\u4e5f\u6709\u5173\u7cfb\uff0c\u6240\u4ee5\u8fd0\u884c\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u4e22\u51e0\u4e2a\u90fd\u6709\u53ef\u80fd <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070500\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : reber0ask <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @LemonK \u5982\u679c\u53ea\u5411 chan \u4e2d\u4f20\u5165\u4e86 1 \u5230 10 \u7684\u8bdd\u53ef\u80fd\u4e22\u5931\u4e00\u4e2a\u6570\u5b57 10 \uff0c\u5982\u679c\u4f20\u5165 1 \u5230 20 \u7684\u8bdd\u53ef\u80fd\u53ea\u80fd\u8f93\u51fa 1 \u5230 17 \u300118 \u8fd9\u6837\u5b50\uff0c \u6309\u4f60\u7684\u4ee3\u7801\u5728 for \u7684\u5916\u9762\u4f7f\u7528 n := len(ch1)\u7684\u8bdd\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u786e\u5b9e\u53ef\u4ee5\u8f93\u51fa 1 \u5230 10 \uff0c\u4e0d\u4f1a\u4e22\u5931\u6570\u636e <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070501\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : reber0ask <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @LemonK @FakNoCNName @clearT \u8c22\u8c22\uff0c\u660e\u767d\u4e86\uff0c\u786e\u5b9e\u662f\u8fd9\u6837 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070502\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mmrindextt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> for \u4e2d go , \u5c31\u503c\u5f97\u7ec6\u54c1\u4e86 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070503\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zjj19950716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @mmrindextt \u5c55\u5f00\u8bb2\u8bb2  <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070504\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : snowlyg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u53c8\u770b\u5230\u8fd9\u4e2a\u95ee\u9898\uff0c\u597d\u60f3\u5410\u69fd\u4e00\u4e0b\u3002\u54c8\u54c8 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7070505\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wakaka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u4e0d\u5f97\u8981\u9886 <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u4e2d goroutin&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/555399"}],"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=555399"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/555399\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555399"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}