{"id":91387,"date":"2020-05-02T04:38:06","date_gmt":"2020-05-01T20:38:06","guid":{"rendered":"http:\/\/4563.org\/?p=91387"},"modified":"2020-05-02T04:38:06","modified_gmt":"2020-05-01T20:38:06","slug":"%e5%8d%8f%e7%a8%8b%e6%89%a7%e8%a1%8c%e9%97%ae%e9%a2%98%e8%af%b7%e6%95%99","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=91387","title":{"rendered":"\u534f\u7a0b\u6267\u884c\u95ee\u9898\u8bf7\u6559"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u534f\u7a0b\u6267\u884c\u95ee\u9898\u8bf7\u6559               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : davidyanxw <\/span>  <span><i><\/i> 19<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        package main<\/p>\n<p>import (<br \/> &#8220;fmt&#8221;<br \/> &#8220;time&#8221;<br \/>)<\/p>\n<p>var x = 0<\/p>\n<p>func main() {<br \/> var num = 123<br \/> var p = &amp;num<br \/> c := make(chan int)<br \/> go func() {<br \/> c &lt;- *p + x \/\/ 1. \u8fd4\u56de 123<br \/> \/\/ c &lt;- *p \/\/ 2. \u8fd4\u56de 789<br \/> }()<br \/> time.Sleep(time.Second)<br \/> num = 789<br \/> fmt.Println(&lt;-c)<\/p>\n<p>}<\/p>\n<p>\u5b50\u534f\u7a0b\uff0c\u6267\u884c 1 \u8fd4\u56de 123\uff0c\u6267\u884c 2 \u8fd4\u56de 789<br \/>\u6ca1\u770b\u51fa\u6765\u6709\u4ec0\u4e48\u5dee\u522b\u554a\uff1f      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1246940\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kaifang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u533a\u522b\uff0c<br \/>1 \u8fdb\u884c\u4e86\u8fd0\u7b97\uff0c\u53d6\u8fd0\u7b97\u540e\u7684\u7ed3\u679c\uff0c\u4f60\u52a0\u4e86 1 \u79d2\u7684\u5ef6\u65f6\uff0c\u5f88\u660e\u663e\u5f53\u65f6\u7684 num \u7684\u503c\u4e3a 123\uff0c\u6240\u4ee5\u8f93\u51fa\u662f 123\uff0c\u5982\u679c\u53bb\u6389\u5ef6\u65f6\uff0c\u5219\u4e3a 789<br \/>2 \u4e00\u76f4\u6307\u5411 num \u7684\u5730\u5740\uff0c\u4ece\u901a\u9053\u91cc\u8bfb\u7684\u65f6\u5019\u8bfb\u7684\u662f 789                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246941\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bwangel <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             time.Sleep \u4e0d\u80fd\u4fdd\u8bc1 goroutine \u4e00\u5b9a\u6bd4 main \u5148\u6267\u884c\uff0c\u5982\u679c\u60f3\u8981\u8ba9 Goroutine \u6bd4 main \u5148\u6267\u884c\u7684\u8bdd\uff0c\u53ef\u4ee5\u901a\u8fc7 waitGroup \u6765\u540c\u6b65\uff1a<\/p>\n<p>https:\/\/gist.github.com\/bwangelme\/5e71895d40130521b71828cef72adc1f <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246942\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : MoYi123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             c &lt;- *p + x \/\/ 1. \u8fd4\u56de 123 \u7b49\u4e8e tmp:= *p + x ; c&lt;- tmp ,\u56e0\u4e3a chan \u7684 size \u662f 0 \u6240\u4ee5\u8981\u7b49\u5230\u4e3b\u8fdb\u7a0b\u5230 fmt.Println(&lt;-c)\u65f6\u624d\u4f1a\u8fd0\u884c c&lt;- tmp                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246943\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : blip <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u201dBy default channels are unbuffered, meaning that they will only accept sends (chan &lt;-) if there is a corresponding receive (&lt;- chan) ready to receive the sent value\u201c<br \/>@MoYi123 \u6b63\u89e3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246944\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : useben <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @MoYi123 \u6b63\u89e3, 1 2 \u7b54\u7684\u4e0d\u662f\u8fd9\u4e2a\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246945\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : davidyanxw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @blip <br \/>\u6309\u7167\u8fd9\u4e2a\u8bf4\u6cd5\u7684\u8bdd\uff0c\u5e94\u8be5\u4e24\u4e2a\u503c\u90fd\u4f1a\u8fd4\u56de 123 \u624d\u5bf9\u554a\uff1f<br \/>\u4e3a\u5565\u7b2c 1 \u79cd\u8fd4\u56de 123\uff0c\u7b2c 2 \u79cd\u8fd4\u56de 789                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246946\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : davidyanxw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kaifang 1 \u8fd0\u7b97\u4e86\uff0c2 \u4e3a\u5565\u4e0d\u8fd0\u7b97\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246947\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kaifang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @davidyanxw #7 2\uff0c\u5411\u901a\u9053\u53d1\u9001\u7684 num \u7684\u5730\u5740\uff0c\u6b64\u65f6\u901a\u9053\u662f\u963b\u585e\u7684\uff0c\u8fc7\u4e00\u79d2\u540e num \u56e0\u4e3a\u8d4b\u503c 789 \u5730\u5740\u53d1\u751f\u6539\u53d8\uff0c\u7136\u540e\u4ece\u901a\u9053\u91cc\u8bfb\u53d6 num \u503c\u662f 789                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246948\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : blip <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @davidyanxw good ask, \u6211\u89c9\u5f97\u6211\u4e4b\u524d\u6ca1\u6709\u6293\u4f4f\u5bf9\u7684\u70b9\uff0c\u6211\u5bf9 unbuffered channel \u7684\u89e3\u91ca\u5176\u5b9e\u5e76\u6ca1\u6709\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff0c\u8fd9\u91cc\u7684\u91cd\u70b9\u5176\u5b9e\u662f<br \/>1\uff0c *p+x \u4f1a create \u4e00\u4e2a\u65b0\u7684 int \uff08 123 \uff09\u5b58\u653e\u5728\u53e6\u4e00\u4e2a\u5730\u5740\uff0c\u8fd9\u4e2a\u5730\u5740\u5b58\u653e\u7684\u503c\u6ca1\u6709\u6539\u53d8\u8fc7<br \/>2\uff0c*p \u4f1a\u4e00\u76f4\u7b49\u4e8e num \u7684\u5730\u5740\u6240\u5b58\u50a8\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u5728\u4ee3\u7801\u4e2d\u88ab\u66f4\u6539\u4e3a 789<br \/>\u6240\u4ee5\u624d\u4f1a\u9020\u6210\u4e24\u4e2a case \u7ed3\u679c\u4e0d\u540c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1246949\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : davidyanxw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @blip <br \/>\u611f\u8c22<\/p>\n<p>ch &lt;- expression<\/p>\n<p>expression \u8ba1\u7b97\u7684\u65f6\u673a\u662f\u95ee\u9898\u7684\u5173\u952e\u3002<br \/>\u5177\u4f53\u7684\u7ec6\u8282\uff0c\u5e0c\u671b\u5927\u725b\u4eec\u7ed9\u8be6\u7ec6\u8bf4\u8bf4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u534f\u7a0b\u6267\u884c\u95ee\u9898\u8bf7\u6559 \u8cc7\u6df1\u5927\u4f6c : d&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91387"}],"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=91387"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91387\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91387"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}