{"id":1595,"date":"2020-09-29T05:46:42","date_gmt":"2020-09-28T21:46:42","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1595"},"modified":"2022-02-16T21:55:43","modified_gmt":"2022-02-16T13:55:43","slug":"go-thread-basic-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/09\/29\/go-thread-basic-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u57f7\u884c\u5e8f\u7684\u57fa\u790e"},"content":{"rendered":"<h1>\u57f7\u884c\u5e8f\u7684\u57fa\u790e<\/h1>\n<p>\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e86\u8df3\u812b\u6b63\u898f\u57f7\u884c\u6d41\u7a0b\u7684\u5ef6\u9072\u57f7\u884c <code>defer<\/code> \uff0c\u672c\u7bc7\u958b\u59cb\u8981\u4ecb\u7d39\u53e6\u5916\u4e00\u500b\u57f7\u884c\u5e8f <code>thread<\/code> \u7684\u6982\u5ff5\u4ee5\u53ca\u5982\u4f55\u5728 go \u5167\u8dd1\u591a\u57f7\u884c\u5e8f\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u57f7\u884c\u5e8f<\/h2>\n<p>\u57f7\u884c\u5e8f\u5c31\u662f\u4e00\u500b\u6b63\u898f\u57f7\u884c\u6d41\u7a0b\u7684\u65b9\u5f0f\uff0c\u591a\u57f7\u884c\u5e8f\u5c31\u662f\u591a\u500b\u6d41\u7a0b\u5e73\u884c\u5728\u57f7\u884c\uff0c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39 go \u7684\u591a\u57f7\u884c\u5e8f\u8981\u5982\u4f55\u64b0\u5beb<\/p>\n<h3>\u591a\u57f7\u884c\u5e8f\u7684\u5beb\u6cd5<\/h3>\n<p>\u4e0b\u9762\u4f8b\u5b50\u662f <a href=\"https:\/\/tour.golang.org\/concurrency\/1\" title=\"Go Tour\" rel=\"nofollow noopener\" target=\"_blank\">Go Tour<\/a> \u95dc\u65bc\u591a\u57f7\u884c\u5e8f\u7684\u4f8b\u5b50\uff0c\u8acb\u5927\u5bb6\u5148\u770b\u770b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\nfunc say(s string) {\n    for i := 0; i &lt; 5; i++ {\n        time.Sleep(100 * time.Millisecond)\n        fmt.Println(s)\n    }\n}\n\nfunc main() {\n    go say(&quot;world&quot;)\n    say(&quot;hello&quot;)\n}<\/code><\/pre>\n<p>go \u7684\u591a\u57f7\u884c\u5e8f\u7684\u5beb\u6cd5\u5c31\u662f\u5728\u57f7\u884c\u7684\u65b9\u6cd5\u524d\u52a0\u4e0a <code>go<\/code> \u7684\u95dc\u9375\u5b57\uff0c\u5728\u57f7\u884c\u5f8c\u6703\u770b\u5230\u4e0b\u5217\u7d50\u679c<\/p>\n<pre><code class=\"language-bash\">hello\nworld\nhello\nworld\nhello\nworld\nworld\nhello\nhello<\/code><\/pre>\n<p>\u6709\u4e9b\u8b80\u8005\u6703\u6709\u7591\u554f\uff0c\u70ba\u4ec0\u9ebc\u6211\u8dd1\u51fa\u4f86\u7684\u7d50\u679c\u8ddf\u4e0a\u9762\u7684\u7d50\u679c\u4e0d\u4e00\u81f4\u5462\uff0c\u56e0\u70ba\u9019\u8ddf\u591a\u57f7\u884c\u5e8f\u904b\u884c\u65b9\u5f0f\u6709\u95dc\uff0c\u6240\u4ee5\u63a5\u4e0b\u70ba\u5927\u5bb6\u8aaa\u660e\u3002<\/p>\n<h3>\u591a\u57f7\u884c\u5e8f\u7684\u904b\u884c\u65b9\u5f0f<\/h3>\n<p>\u8207 <code>defer<\/code> \u4e0d\u540c\u7684\u662f\uff0c\u591a\u57f7\u884c\u5e8f\u662f\u5e73\u884c\u904b\u4f5c\u7684\uff0c\u4ee5\u4e0a\u9762\u7684\u4f8b\u5b50\u4f86\u8b1b\u5c31\u662f <code>go<\/code> \u9019\u95dc\u9375\u5b57\u7684 say \u65b9\u6cd5\u8207\u6c92\u6709 <code>go<\/code> \u9019\u95dc\u9375\u5b57\u7684 say \u65b9\u6cd5\u540c\u6642\u88ab\u57f7\u884c\uff0c\u9019\u6642\u5c31\u50cf\u8cfd\u8dd1\u4e00\u6a23\u6709\u7684\u4eba\u8dd1\u5f97\u5feb\uff0c\u6709\u7684\u4eba\u8dd1\u5f97\u6162\uff0c\u6240\u4ee5\u8dd1\u5f97\u5feb\u7684\u5c31\u5148\u88ab\u57f7\u884c\uff0c\u8dd1\u5f97\u6162\u7684\u5f8c\u88ab\u57f7\u884c\uff0c\u5982\u6b64\u5c31\u8868\u73fe\u51fa\u6709\u6642world\u5148\u88ab\u57f7\u884c\u7684\u72c0\u6cc1\u800c\u4e0d\u662fhello\u8ddfworld\u4ea4\u66ff\u51fa\u73fe\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u591a\u57f7\u884c\u5e8f\u53ef\u4ee5\u62ff\u5206\u5de5\u5408\u4f5c\u7576\u4f5c\u4f8b\u5b50\u4f86\u8aaa\u660e\uff0c\u7c21\u55ae\u8b1b\u5c31\u662f\u591a\u500b\u4eba\u540c\u6642\u505a\u4e8b\uff0c\u7576\u7136\u540c\u6642\u505a\u4e8b\u6703\u9047\u5230\u5982\u679c\u8981\u6436\u4e00\u6a23\u6771\u897f\u6642\u9019\u6642\u5019\u591a\u500b\u4eba\u505a\u4e00\u5b9a\u6703\u9047\u5230\u554f\u984c\uff0c\u9019\u4e5f\u662f\u591a\u57f7\u884c\u5e8f\u5e38\u6703\u9047\u5230\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\" title=\"\u7af6\u722d\u5371\u5bb3\" rel=\"nofollow noopener\" target=\"_blank\">race condition<\/a>\u554f\u984c\uff0c\u7576\u7136 go \u4e5f\u6703\u91dd\u5c0d\u9019\u505a\u8655\u7406\uff0c\u5f85\u5f8c\u7e8c\u5728\u8ddf\u5927\u5bb6\u4ecb\u7d39\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57f7\u884c\u5e8f\u7684\u57fa\u790e \u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e86\u8df3\u812b\u6b63\u898f\u57f7\u884c\u6d41\u7a0b\u7684\u5ef6\u9072\u57f7\u884c defer \uff0c\u672c\u7bc7\u958b\u59cb\u8981\u4ecb\u7d39\u53e6\u5916\u4e00\u500b\u57f7\u884c\u5e8f thr &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/09\/29\/go-thread-basic-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u57f7\u884c\u5e8f\u7684\u57fa\u790e\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81],"class_list":["post-1595","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1595"}],"version-history":[{"count":5,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1595\/revisions"}],"predecessor-version":[{"id":2676,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1595\/revisions\/2676"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}