{"id":3700,"date":"2022-12-30T13:39:37","date_gmt":"2022-12-30T05:39:37","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3700"},"modified":"2022-12-30T13:39:37","modified_gmt":"2022-12-30T05:39:37","slug":"%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84%e9%80%9a%e9%81%93%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/go\/%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84%e9%80%9a%e9%81%93%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5b78\u7fd2Go\u8a9e\u8a00\u4e2d\u7684\u901a\u9053\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Go, \u901a\u9053\"><\/p>\n<p>Go \u8a9e\u8a00\u7684\u901a\u9053\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684 goroutine \u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\u3002\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u901a\u9053\u662f\u4e00\u500b\u7279\u6b8a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684 goroutine \u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\u3002<\/p>\n<h2>Go \u8a9e\u8a00\u7684\u901a\u9053<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u901a\u9053\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684 goroutine \u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\u3002\u901a\u9053\u53ef\u4ee5\u88ab\u60f3\u50cf\u6210\u4e00\u500b\u7ba1\u9053\uff0c\u53ef\u4ee5\u5c07\u8cc7\u6599\u5f9e\u4e00\u500b goroutine \u50b3\u905e\u5230\u53e6\u4e00\u500b goroutine\u3002<\/p>\n<p><!--more--><\/p>\n<p>Go \u8a9e\u8a00\u7684\u901a\u9053\u662f\u4e00\u500b\u7279\u6b8a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684 goroutine \u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\u3002\u901a\u9053\u53ef\u4ee5\u88ab\u60f3\u50cf\u6210\u4e00\u500b\u7ba1\u9053\uff0c\u53ef\u4ee5\u5c07\u8cc7\u6599\u5f9e\u4e00\u500b goroutine \u50b3\u905e\u5230\u53e6\u4e00\u500b goroutine\u3002<\/p>\n<pre class=\"brush: go\">\n\/\/ \u5efa\u7acb\u4e00\u500b\u65b0\u7684\u901a\u9053\nch := make(chan int)\n\n\/\/ \u5c07\u8cc7\u6599\u50b3\u905e\u5230\u901a\u9053\ngo func() {\n    ch <- 42\n}()\n\n\/\/ \u5f9e\u901a\u9053\u63a5\u6536\u8cc7\u6599\nfmt.Println(<-ch) \/\/ 42\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u7684\u901a\u9053\u4f86\u50b3\u905e\u8cc7\u6599\u3002\u9996\u5148\uff0c\u6211\u5011\u4f7f\u7528 <code>make()<\/code> \u51fd\u5f0f\u4f86\u5efa\u7acb\u4e00\u500b\u65b0\u7684\u901a\u9053\uff0c\u7136\u5f8c\u6211\u5011\u4f7f\u7528 <code>go<\/code> \u95dc\u9375\u5b57\u4f86\u5efa\u7acb\u4e00\u500b\u65b0\u7684 goroutine\uff0c\u4e26\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u901a\u9053\u4e2d\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code><-<\/code> \u904b\u7b97\u5b50\u4f86\u5f9e\u901a\u9053\u4e2d\u63a5\u6536\u8cc7\u6599\u3002<\/p>\n<p>Go \u8a9e\u8a00\u7684\u901a\u9053\u975e\u5e38\u5f37\u5927\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684 goroutine \u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u6d41\u52d5\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5be6\u73fe\u4e26\u884c\u7a0b\u5f0f\u8a2d\u8a08\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5229\u7528\u591a\u6838\u5fc3\u7cfb\u7d71\u7684\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Go\u8a9e\u8a00\u4e2d\u7684\u901a\u9053\u529f\u80fd\uff0c\u8a73\u7d30\u89e3\u91cb\u4e86\u901a\u9053\u7684\u5b9a\u7fa9\u3001\u4f7f\u7528\u65b9\u6cd5\u3001\u512a\u9ede\u548c\u7f3a\u9ede\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\uff0c\u4ee5\u4fbf\u958b\u767c\u8005\u80fd\u5920\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Go\u8a9e\u8a00\u4e2d\u7684\u901a\u9053\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[171,172],"tags":[169,170],"class_list":["post-3700","post","type-post","status-publish","format-standard","hentry","category-go","category-golang","tag-go","tag-golang"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-XG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3700"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3700\/revisions"}],"predecessor-version":[{"id":3701,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3700\/revisions\/3701"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}