{"id":84692,"date":"2020-05-15T02:52:16","date_gmt":"2020-05-14T18:52:16","guid":{"rendered":"http:\/\/4563.org\/?p=84692"},"modified":"2020-05-15T02:52:16","modified_gmt":"2020-05-14T18:52:16","slug":"%e5%88%86%e4%ba%ab%e4%b8%80%e6%ac%a1-golang-%e7%9a%84%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%8c%e6%9c%80%e5%90%8e%e4%b8%80%e9%a2%98%e6%b1%82%e8%a7%a3%e7%ad%94","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=84692","title":{"rendered":"\u5206\u4eab\u4e00\u6b21 golang \u7684\u9762\u8bd5\u9898\uff0c\u6700\u540e\u4e00\u9898\u6c42\u89e3\u7b54"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5206\u4eab\u4e00\u6b21 golang \u7684\u9762\u8bd5\u9898\uff0c\u6700\u540e\u4e00\u9898\u6c42\u89e3\u7b54               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yngzij <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u5206\u4eab\u4e00\u4e0b Golang \u9762\u8bd5\u7684\u7b14\u8bd5\u9898\u76ee\uff0c\u524d\u4e24\u9898\u81ea\u5df1\u5199\u7684\u7b54\u6848\u5728\u540e\u9762\uff0c\u7b2c\u4e09\u9898\u6ca1\u6709\u89e3\u7b54\u51fa\u6765\u3002<\/h1>\n<p> <\/p>\n<h4>\u68c0\u6d4b\u4e00\u4e2a\u51fd\u6570\uff0c\u5f53\u5b83\u5728 3 \u79d2\u5185\u8fd8\u6ca1\u6709\u5b8c\u6210\u5c31\u6253\u5370\u9519\u8bef\u3002<\/h4>\n<h4>\u4f7f\u7528 slice,mutex,channel,goroute \u7f16\u5199\u8f93\u51fa 100 \u6b21\u7684\u968f\u673a\u6570\u7684\u7f13\u51b2\u961f\u5217\u3002<\/h4>\n<h4>\u7f16\u5199\u53ef\u4ee5\u6269\u5bb9\u6536\u7f29\u7684\u5faa\u73af\u6570\u7ec4\uff0c\u652f\u6301\u6dfb\u52a0\u548c\u5220\u9664\u3002\uff08\u6c42\u89e3\u7b54\uff09<\/h4>\n<pre><code>package main  import (  \"log\"  \"time\" )func main() {sig := make(chan interface{})  go func() {getResult()   sig &lt;- struct{}{}  }()  for {   select {   case &lt;-sig:    log.Println(\"ok\")   case &lt;-time.After(time.Second * 3):    log.Println(\"error\")   }   break  } }  func getResult() {time.Sleep(time.Second * 2) } <\/code><\/pre>\n<pre><code>package main  import (  \"log\"  \"math\/rand\"  \"sync\"  \"time\" )  var mutex sync.Mutex var list []int  func main() {sig := make(chan interface{}, 100)  for i := 0; i &lt; 100; i++ {go insert(sig)  }  for i := 0; i &lt; 100; i++ {&lt;-sig}  for i, v := range list {log.Println(i+1, v)  } }  func insert(sig chan interface{}) {mutex.Lock()  r := rand.New(rand.NewSource(time.Now().UnixNano()))n := r.Intn(100)  list = append(list, n)  mutex.Unlock()sig &lt;- struct{}{}}  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1474122\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teawithlife <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u73b0\u5728\u7684\u516c\u53f8\uff0c\u51fa\u7b14\u8bd5\u9898\u53ef\u4ee5\u8fd9\u4e48\u968f\u610f\u5417\uff1f\u611f\u89c9\u50cf\u4f5c\u4e1a\u591a\u4e9b\u54e6<br \/>\u7b2c\u4e00\u4e2a\u9898\u76ee\u8fd8\u53ef\u5708\u53ef\u70b9\uff0c\u7b2c\u4e8c\u4e2a\u9898\u76ee\u4e3a\u5565\u89c4\u5b9a\u8981\u7528 slice\uff0cmutex\uff0c\u4e0d\u662f\u4e00\u4e2a channel \u7684\u4e8b\u60c5\u5417\uff1f<br \/>\u7b2c\u4e09\u9898\u5c31\u662f\u5faa\u73af\u961f\u5217\uff0c\u8981\u70b9\u5c31\u662f\u5934\u5c3e\u4e24\u4e2a\u6307\u9488\u548c\u53d6\u6a21\u64cd\u4f5c\uff0c\u5904\u7406\u597d\u8fb9\u754c\u6761\u4ef6\u5c31\u597d\u4e86\uff0c\u4f46\u662f\u8fd9\u4e2a\u201c\u652f\u6301\u6dfb\u52a0\u548c\u5220\u9664\u201d\u592a\u4e0d\u4e25\u8c28\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1474123\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yngzij <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @teawithlife #1 \u6211\u5f53\u65f6\u4e5f\u95ee\u4e86\uff0c\u4e3a\u5565\u5fc5\u987b\u4f7f\u7528\uff0c\u4ed6\u8bf4\u4e3a\u4e86\u8003\u5bdf\u57fa\u7840\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1474124\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Acoolda <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u80fd\u597d\u597d\u8d34\u4e00\u4e0b\u4ee3\u7801\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1474125\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yngzij <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Acoolda #3 \u662f v2 \u7684\u7f16\u8f91\u5668\u81ea\u5df1\u683c\u5f0f\u5316\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e00\u6b21 golang \u7684\u9762\u8bd5\u9898\uff0c&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\/84692"}],"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=84692"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/84692\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84692"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}