{"id":48,"date":"2015-07-08T11:01:08","date_gmt":"2015-07-08T03:01:08","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=48"},"modified":"2015-07-08T11:05:45","modified_gmt":"2015-07-08T03:05:45","slug":"go-%e5%8d%8f%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=48","title":{"rendered":"go \u534f\u7a0b"},"content":{"rendered":"<h3><strong>\u51fd\u6570\u65e0\u963b\u585e\u6d4b\u8bd5<\/strong><\/h3>\n<pre class=\"lang:go decode:true \">package main\r\nimport \"fmt\"\r\nfunc sum(values []int, myChan chan int) {\r\n    sum := 0\r\n    for _, value := range values {\r\n        sum += value\r\n    }\r\n    fmt.Println(\"Sum:\", sum)\r\n    myChan &lt;- sum\r\n}\r\nfunc main() {\r\n    myChan := make( chan int,2)\r\n    values := []int {1,2,3,5,5,4,23,4,4,233,2,233}\r\n    go sum(values,myChan)\r\n    go sum(values[:1],myChan)\r\n    sum1,sum2 := &lt;-myChan, &lt;-myChan\r\n    fmt.Println(\"Result:\",sum1,sum2,sum1+sum2)\r\n}<\/pre>\n<h3><strong>\u51fd\u6570\u6709\u963b\u585e\u6d4b\u8bd5<\/strong><\/h3>\n<pre class=\"lang:go decode:true\">package main\r\n\r\nimport \"fmt\"\r\nimport \"time\"\r\n\r\nvar quit chan int\r\n\r\nfunc foo(id int) {\r\n    fmt.Println(id)\r\n    time.Sleep(time.Second)\r\n    quit &lt;- 0\r\n}\r\n\r\n\r\nfunc main() {\r\n    count := 1000\r\n    quit = make(chan int, count)\r\n\r\n    for i := 0; i &lt; count; i++ {\r\n        go foo(i)\r\n    }\r\n\r\n    for i :=0 ; i &lt; count; i++ {\r\n        &lt;- quit\r\n    }\r\n}<\/pre>\n<p>\u5982\u679c\u5f53\u524dgoroutine\u4e0d\u53d1\u751f\u963b\u585e\uff0c\u5b83\u662f\u4e0d\u4f1a\u8ba9\u51faCPU\u7ed9\u5176\u4ed6goroutine\u7684, \u6240\u4ee5\u4f8b\u5b50\u4e00\u4e2d\u7684\u8f93\u51fa\u4f1a\u662f\u4e00\u4e2a\u4e00\u4e2agoroutine\u8fdb\u884c\u7684\uff0c\u800csleep\u51fd\u6570\u5219\u963b\u585e\u6389\u4e86 \u5f53\u524dgoroutine, \u5f53\u524dgoroutine\u4e3b\u52a8\u8ba9\u5176\u4ed6goroutine\u6267\u884c, \u6240\u4ee5\u5f62\u6210\u4e86\u903b\u8f91\u4e0a\u7684\u5e76\u884c, \u4e5f\u5c31\u662f\u5e76\u53d1\u3002<\/p>\n<h3><strong>\u771f\u6b63\u5e76\u884c<\/strong><\/h3>\n<pre class=\"lang:go decode:true \">import (\r\n    \"fmt\"\r\n    \"runtime\"\r\n)\r\n\r\nvar quit chan int = make(chan int)\r\n\r\nfunc loop() {\r\n    for i := 0; i &lt; 100; i++ { \/\/\u4e3a\u4e86\u89c2\u5bdf\uff0c\u8dd1\u591a\u4e9b\r\n        fmt.Printf(\"%d \", i)\r\n    }\r\n    quit &lt;- 0\r\n}\r\n\r\nfunc main() {\r\n    runtime.GOMAXPROCS(2) \/\/ \u6700\u591a\u4f7f\u75282\u4e2a\u6838\r\n\r\n    go loop()\r\n    go loop()\r\n\r\n    for i := 0; i &lt; 2; i++ {\r\n        &lt;- quit\r\n    }\r\n}<\/pre>\n<h3>runtime\u8c03\u5ea6\u5668<\/h3>\n<p>runtime\u8c03\u5ea6\u5668\u662f\u4e2a\u5f88\u795e\u5947\u7684\u4e1c\u897f\uff0c\u4f46\u662f\u6211\u771f\u662f\u4f46\u613f\u5b83\u4e0d\u5b58\u5728\uff0c\u6211\u5e0c\u671b\u663e\u5f0f\u8c03\u5ea6\u80fd\u66f4\u4e3a\u81ea\u7136\u4e9b\uff0c\u591a\u6838\u5904\u7406\u9ed8\u8ba4\u5f00\u542f\u3002<br \/>\n\u5173\u4e8eruntime\u5305\u51e0\u4e2a\u51fd\u6570:<\/p>\n<ul style=\"margin-left: 30px;\">\n<li><code>Gosched<\/code>\u00a0\u8ba9\u51facpu<\/li>\n<li><code>NumCPU<\/code>\u00a0\u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7684CPU\u6838\u6570\u91cf<\/li>\n<li><code>GOMAXPROCS<\/code>\u00a0\u8bbe\u7f6e\u6700\u5927\u7684\u53ef\u540c\u65f6\u4f7f\u7528\u7684CPU\u6838\u6570<\/li>\n<li><code>Goexit<\/code>\u00a0\u9000\u51fa\u5f53\u524dgoroutine(\u4f46\u662fdefer\u8bed\u53e5\u4f1a\u7167\u5e38\u6267\u884c)<\/li>\n<\/ul>\n<p>\u53c2\u8003\uff1a<br \/>\nhttp:\/\/blog.csdn.net\/kjfcpua\/article\/details\/18265461<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u65e0\u963b\u585e\u6d4b\u8bd5 package main import &#8220;fmt&#8221; func sum(values []int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=48"}],"version-history":[{"count":4,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}