{"id":150043,"date":"2020-09-02T16:32:36","date_gmt":"2020-09-02T08:32:36","guid":{"rendered":"http:\/\/4563.org\/?p=150043"},"modified":"2020-09-02T16:32:36","modified_gmt":"2020-09-02T08:32:36","slug":"%e6%b1%82%e8%a7%a3%e9%87%8a%e4%b8%80%e4%b8%aa-golang-%e5%b9%b6%e5%8f%91-chan-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=150043","title":{"rendered":"\u6c42\u89e3\u91ca\u4e00\u4e2a Golang \u5e76\u53d1 Chan \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u89e3\u91ca\u4e00\u4e2a Golang \u5e76\u53d1 Chan \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>func Test_TarsSelect(t *testing.T) {  trueCount := 0  falseCount := 0  for i := 0; i &lt; 100000; i++ {   if TarsThread() {    trueCount++   } else {    falseCount++   }  }   fmt.Println(\"trueCount:\", trueCount)  fmt.Println(\"falseCount:\", falseCount) }  func TarsThread() bool {  someChan := make(chan bool)  var gotChan bool   go SomeFunc(someChan)  select {     \/\/10 \u6beb\u79d2\u8d85\u65f6  case &lt;-time.After(10 * time.Millisecond):  case gotChan = &lt;-someChan:  }   return gotChan }  func SomeFunc(someChan chan bool) {  \/\/\u4f11\u7720 10 \u5fae\u79d2  time.Sleep(10 * time.Microsecond)  select {  case someChan &lt;- true:  default:  } } <\/code><\/pre>\n<p>\u5982\u4e0a\u6240\u793a\u7684\u4ee3\u7801\uff0c\u5728\u6211\u8fd9\u8fb9\u6267\u884c <code>Test_TarsSelect<\/code> \u7684\u7ed3\u679c\u662f<\/p>\n<pre><code>=== RUN   Test_TarsSelect trueCount: 99961 falseCount: 39 <\/code><\/pre>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u4f1a\u8ba4\u4e3a\u5e94\u8be5\u5168\u90e8\u4e3a <code>true<\/code>\u3002<\/p>\n<p>\u5982\u4f55\u89e3\u91ca\u8fd9\u79cd\u73b0\u8c61\u5462\uff1f golang \u4e2d\u6240\u6709\u5e76\u53d1\u7684\u7a0b\u5e8f\u7684\u5148\u540e\u6027\u90fd\u662f\u4e0d\u4fdd\u8bc1\u7684\u5417\uff1f<\/p>\n<p>\u6211\u8ba4\u4e3a\u89e3\u51b3\u529e\u6cd5\u662f someChan \u521d\u59cb\u5316\u65f6\uff0c\u53ef\u4ee5\u4fee\u6539\u4e3a\u7f13\u51b2\u533a\u957f\u5ea6\u4e3a 1 \u3002<br \/> \u53e6\u5916\u4e00\u4e2a\u540c\u4e8b\u8ba4\u4e3a\u662f\u5c06 <code>SomeFunc<\/code> \u4e2d\u7684 default \u903b\u8f91\u53bb\u6389\uff0c\u6539\u6210 <code>case &lt;- time.After(time.MillionSecond * 10)<\/code>\u3002<\/p>\n<p>\u4e8c\u8005\u90fd\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff0c\u4f60\u4eec\u63a8\u8350\u4f7f\u7528\u54ea\u4e00\u79cd\uff0c\u6216\u8005\u6709\u66f4\u5408\u7406\u7684\u89e3\u51b3\u529e\u6cd5\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>17<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3081186\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : securityCoding <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081187\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lance6716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             go \u4e5f\u662f\u53ea\u6709 happens before \u7684\u5427\uff0c\u4e0d\u80fd\u4f9d\u9760\u5176\u4ed6\u5047\u8bbe                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081188\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : whimsySun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             runtime.GOMAXPROCS(1) \u4f60\u5c31\u53ef\u4ee5\u80fd\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u4e86\uff0c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081189\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sujin190 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e76\u53d1\u672c\u6765\u5c31\u6ca1\u6709\u5148\u540e\uff0c\u6709\u5148\u540e\u53eb\u5565\u5e76\u53d1\uff0c\u961f\u5217\u624d\u6709\u5148\u540e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081190\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : goofool <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u731c\u6d4b\u662f timer \u6027\u80fd\u95ee\u9898\uff0c\u6211\u8bb0\u5f97 time.After \u5e95\u5c42\u8d85\u65f6\u524d\u90fd\u4e0d\u4f1a\u88ab GC \u56de\u6536\uff0c\u6211\u8fd9\u53f0\u7535\u8111\u5b9a\u65f6\u5668\u7cbe\u5ea6\u53ea\u6709 1ms                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081191\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @goofool \u7cbe\u5ea6\u9ad8\u4e86\u624d\u4f1a\u6709\u4e0a\u8fb9\u7684\u95ee\u9898\u5427\uff0c\u4f60\u5728\u81ea\u5df1\u7535\u8111\u4e0a\u8dd1\u4e00\u4e0b\u770b\u770b\u662f\u4e0d\u662f\u90fd\u662f true                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081192\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @whimsySun \u4e0d\u884c\u7684\u554a\uff0c\u6211\u8bd5\u4e86\u4e00\u4e0b\u4ecd\u7136\u51fa\u73b0\u5f88\u591a false                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081193\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @whimsySun \u95ee\u9898\u53ea\u662f\u7f13\u89e3\u4e86\uff0c\u5e76\u6ca1\u6d88\u9664\uff0c\u9000\u4e00\u6b65\u8bf4\u8fd9\u4e5f\u4e0d\u662f\u89e3\u51b3\u95ee\u9898\u7684\u529e\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081194\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yianing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u4f60\u8fd9\u662f\u5728\u5835 timer \u548c sleep \u554a\uff0c\u548c chan \u6ca1\u5173\u7cfb\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081195\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunxiansong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`golang<br \/>select {<br \/> \/\/10 \u6beb\u79d2\u8d85\u65f6<br \/> case &lt;-time.After(500 * time.Microsecond):<br \/> fmt.Println(&#8220;im here&#8221;) \/\/ &lt;&#8212;- \u95ee\u9898\u5728\u8fd9\u91cc<br \/> case gotChan = &lt;-someChan:<br \/> }<br \/>&#8220;`<\/p>\n<p>\u4e3b\u4f60\u5927\u6982\u6ca1\u53d1\u73b0\u95ee\u9898\u5728\u54ea\uff0c\u6240\u4ee5\u5b9e\u9645\u4e0a\u4f60\u7684 2 \u4e2a\u529e\u6cd5\u90fd\u4e0d\u80fd\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u4f60\u53ea\u662f\u60f3\u8981\u4e2a\u8d85\u65f6\u7684\u8bdd\uff0c\u4e00\u822c\u7528 context \u5427\uff0c\u6bd4\u5982\u8fd9\u6837<br \/>&#8220;`golang<\/p>\n<p>func ThreadWithContext(ctx context.Context) bool {<br \/> someChan := make(chan bool)<br \/> go SomeFunc(someChan)<br \/> select {<br \/> case &lt;-ctx.Done():<br \/> return false<br \/> case x := &lt;-someChan:<br \/> return x<br \/> }<br \/>}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081196\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sunxiansong \u4f60\u8fd9\u4e2a\u5e76\u4e0d\u80fd\u89e3\u51b3\u8fd4\u56de false \u7684\u95ee\u9898\u554a\uff0c\u6211\u73b0\u5728\u662f\u60f3\u641e\u6e05\u695a\u4e3a\u4ec0\u4e48\u8fd4\u56de false \u7684\u95ee\u9898\uff0ccontext \u7684 withTimeout \u548c withDeadline \u5728\u8fd9\u91cc\u5e76\u4e0d\u5f71\u54cd\u95ee\u9898\u7684\u903b\u8f91\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081197\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunxiansong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @phpcyy \u8fd4\u56de false \u662f\u56e0\u4e3a `case &lt;-time.After(10 * time.Millisecond):` \uff0creturn \u4e86 gotChan \u7684\u9ed8\u8ba4\u503c false . goroutine \u7684\u8c03\u5ea6\u662f\u4e0d\u80fd\u4fdd\u8bc1\u7684\uff0c\u8bf4\u767d\u4e86\u4f60\u60f3\u8981\u7684\u4ee3\u7801\u8fd8\u6ca1\u8c03\u5ea6\u6267\u884c\u5230 \u5c31\u8d85\u65f6\u4e86(10 * time.Millisecond)<\/p>\n<p>\u4e0d\u6e05\u695a\u4f60\u8fd9\u4e2a\u4ee3\u7801\u8981\u5b9e\u73b0\u4ec0\u4e48\u76ee\u7684\uff0c\u8fd8\u4ee5\u4e3a\u662f\u4e2a\u7b80\u5355\u7684\u8d85\u65f6\u9650\u5236\u3002\u4e0d\u77e5\u9053\u60f3\u505a\u4ec0\u4e48\u600e\u4e48\u7528\uff0c\u90a3\u4e48\u4e5f\u4e0d\u77e5\u9053\u600e\u4e48\u6539\uff0c\u5982\u679c\u53ea\u662f\u8981 100%\u8fd4\u56de true\uff0c \u90a3\u4e48\u529e\u6cd5\u591a\u7684\u662f\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081198\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sunxiansong \u6211\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u5bf9\u4e00\u4e2a\u95ee\u9898\u7684\u62bd\u8c61\uff0c\u53ef\u80fd\u62bd\u8c61\u8fc7\u5ea6\u4e86\uff0c\u6211\u8bf4\u4e0b\u539f\u6765\u7684\u573a\u666f\u3002<\/p>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u539f\u6765\u662f\u5728 `go SomeFunc(someChan)` \u8fd9\u4e00\u53e5\u53d1\u9001\u4e86\u4e00\u4e2a tcp \u8bf7\u6c42\uff0c\u7136\u540e\u4e3b\u7a0b\u5e8f\u8fdb\u5165\u7b49\u5f85\u54cd\u5e94\u9636\u6bb5\uff1b SomeFunc \u8fd9\u4e2a goroutine \u4f1a\u6267\u884c tcp \u8bf7\u6c42\uff0c\u5e76\u5c06\u7ed3\u679c\u5199\u5165 someChan \u3002SomeFunc \u91cc\u7684 `time.Sleep(10 * time.MicroSecond)` \u662f\u6a21\u62df\u7684\u8bf7\u6c42\u8fc7\u7a0b\u7684\u8bf7\u6c42\u65f6\u95f4\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u4eec\u4f7f\u7528\u7684\u4e00\u4e2a\u6846\u67b6\u7684\u5e95\u5c42\u6e90\u4ee3\u7801\uff0c\u6211\u4eec\u53d1\u73b0\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e76\u60f3\u7740\u624b\u89e3\u51b3\uff0c\u8fd9\u5c31\u662f\u524d\u56e0\u540e\u679c\uff0c\u62b1\u6b49\u8ba9\u4f60\u8bef\u89e3\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081199\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sunxiansong \u8fd9\u91cc\u7684\u8d85\u65f6\u786e\u5b9e\u662f\u4f7f\u7528 Context \u5305\u66f4\u597d\uff0c\u4f46\u662f\u6846\u67b6\u7684\u8bbe\u8ba1\u8005\u4e0d\u662f\u8fd9\u4e48\u505a\u7684\uff0c\u6211\u4eec\u65e8\u5728\u89e3\u51b3\u8bf7\u6c42\u672a\u8d85\u65f6\u4f46\u662f\u8fdb\u5165\u4e86 Timeout \u7684\u60c5\u51b5\u3002\u6211\u8ba4\u4e3a\u4e4b\u6240\u4ee5\u51fa\u73b0\u4e86 false\uff0c\u662f\u56e0\u4e3a SomeFunc \u8c03\u5ea6\u7684\u65f6\u5019\uff0cgotChan \u8fd8\u6ca1\u6709\u8fdb\u5165\u5230\u8bfb\u72b6\u6001\uff0c\u8fd9\u65f6\u5019\u5411 someChan \u5199\u5165\uff0c\u4f1a\u8fdb\u5165 default \u903b\u8f91\uff0c\u8fdb\u800c\u5bfc\u81f4\u4ea7\u751f\u4e86\u8d85\u65f6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081200\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yianing \u8fd9\u4e2a\u8d85\u65f6\u95ee\u9898\u5173\u952e\u5728\u5411 chan \u5199\u5165\u7684\u65f6\u5019\uff0c\u8bfb\u8fd8\u672a\u5c31\u7eea\uff0c\u4ece\u800c\u8fdb\u5165\u4e86 select \u4e2d\u7684 default \u9009\u9879\uff1b\u5f53\u8bfb\u5c31\u7eea\u7684\u65f6\u5019\uff0cchan \u5df2\u7ecf\u5199\u5165\u8fc7\u4e86\uff0c\u4ece\u800c\u963b\u585e\u5728\u8bfb\u53d6\u72b6\u6001\uff0c\u76f4\u81f3\u8d85\u65f6\uff0c\u8ddf Sleep \u6ca1\u592a\u5927\u5173\u7cfb\u554a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081201\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yianing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @phpcyy \u90a3\u4e48\u662f\u5426\u5e94\u8be5\u8003\u8651\u8bfb\u8005\u6ca1\u51c6\u5907\u597d\u65f6\u5199\u7684\u963b\u585e\u5462\uff1f\u5199\u8005\u8fdb\u5230 default \u6d88\u606f\u5c31\u76f4\u63a5\u4e22\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3081202\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : phpcyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yianing \u6240\u4ee5\u6211\u63d0\u51fa\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7f13\u51b2\u533a\u6216\u8005\u5199\u7684\u65f6\u5019\u8bbe\u7f6e\u4e2a\u8d85\u65f6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u89e3\u91ca\u4e00\u4e2a Golang \u5e76\u53d1 C&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\/150043"}],"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=150043"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/150043\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150043"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}