{"id":57616,"date":"2024-10-10T19:44:13","date_gmt":"2024-10-10T11:44:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57616\/"},"modified":"2024-10-10T19:44:13","modified_gmt":"2024-10-10T11:44:13","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%b8%a6%e7%99%bc%e7%b0%a1%e8%bf%b0%ef%bc%88%e4%b8%a6%e7%99%bc%e7%9a%84%e5%84%aa%e5%8b%a2%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57616\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e26\u767c\u7c21\u8ff0\uff08\u4e26\u767c\u7684\u512a\u52e2\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%b8%a6%e7%99%bc%e7%b0%a1%e8%bf%b0%ef%bc%88%e4%b8%a6%e7%99%bc%e7%9a%84%e5%84%aa%e5%8b%a2%ef%bc%89-whcJpSmOUT\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e26\u767c\u7c21\u8ff0\uff08\u4e26\u767c\u7684\u512a\u52e2\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u4e26\u767c\u6027\u5df2\u6210\u70ba\u4e00\u500b\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u96a8\u8457\u591a\u6838\u8655\u7406\u5668\u7684\u666e\u53ca\uff0c\u958b\u767c\u8005\u9700\u8981\u80fd\u5920\u6709\u6548\u5730\u5229\u7528\u9019\u4e9b\u8cc7\u6e90\u3002Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u56e0\u5176\u5167\u5efa\u7684\u4e26\u767c\u652f\u6301\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u672c\u6587\u5c07\u7c21\u8981\u4ecb\u7d39 Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\u53ca\u5176\u512a\u52e2\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e4%b8%a6%e7%99%bc%e6%a8%a1%e5%9e%8b-whcJpSmOUT\">Go \u8a9e\u8a00\u7684\u4e26\u767c\u6a21\u578b<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u767c\u6a21\u578b\u57fa\u65bc\u201c\u8f15\u91cf\u7d1a\u57f7\u884c\u7dd2\u201d\uff0c\u7a31\u70ba Goroutines\u3002Goroutines \u662f\u7531 Go \u8a9e\u8a00\u7684\u904b\u884c\u6642\u7ba1\u7406\u7684\uff0c\u9019\u4f7f\u5f97\u5b83\u5011\u6bd4\u50b3\u7d71\u7684\u57f7\u884c\u7dd2\u66f4\u8f15\u91cf\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u540c\u4e00\u500b\u5730\u5740\u7a7a\u9593\u4e2d\u904b\u884c\u3002\u958b\u767c\u8005\u53ea\u9700\u4f7f\u7528 <code>go<\/code> \u95dc\u9375\u5b57\u4f86\u555f\u52d5\u4e00\u500b\u65b0\u7684 Goroutine\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u4e26\u767c\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc sayHello() {\n    fmt.Println(\"Hello, World!\")\n}\n\nfunc main() {\n    go sayHello() \/\/ \u555f\u52d5\u4e00\u500b\u65b0\u7684 Goroutine\n    time.Sleep(1 * time.Second) \/\/ \u7b49\u5f85 Goroutine \u5b8c\u6210\n}\n<\/code><\/pre>\n<h2 id=\"%e9%80%9a%e9%81%93%ef%bc%88channels%ef%bc%89-whcJpSmOUT\">\u901a\u9053\uff08Channels\uff09<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u901a\u9053\u662f\u7528\u4f86\u5728 Goroutines \u4e4b\u9593\u50b3\u905e\u6578\u64da\u7684\u4e3b\u8981\u65b9\u5f0f\u3002\u901a\u9053\u63d0\u4f9b\u4e86\u4e00\u7a2e\u5b89\u5168\u7684\u65b9\u5f0f\u4f86\u9032\u884c\u540c\u6b65\uff0c\u907f\u514d\u4e86\u50b3\u7d71\u591a\u57f7\u884c\u7dd2\u7de8\u7a0b\u4e2d\u7684\u7af6\u722d\u689d\u4ef6\u554f\u984c\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>make<\/code> \u51fd\u6578\u4f86\u5275\u5efa\u901a\u9053\uff0c\u4e26\u4f7f\u7528 <code>&lt;-<\/code> \u64cd\u4f5c\u7b26\u4f86\u767c\u9001\u548c\u63a5\u6536\u6578\u64da\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc sendData(ch chan string) {\n    ch &lt;- \"Hello from Goroutine!\" \/\/ \u767c\u9001\u6578\u64da\u5230\u901a\u9053\n}\n\nfunc main() {\n    ch := make(chan string) \/\/ \u5275\u5efa\u901a\u9053\n    go sendData(ch) \/\/ \u555f\u52d5 Goroutine\n    msg := &lt;-ch \/\/ \u63a5\u6536\u6578\u64da\n    fmt.Println(msg)\n}\n<\/code><\/pre>\n<h2 id=\"%e4%b8%a6%e7%99%bc%e7%9a%84%e5%84%aa%e5%8b%a2-whcJpSmOUT\">\u4e26\u767c\u7684\u512a\u52e2<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\u5e36\u4f86\u4e86\u591a\u65b9\u9762\u7684\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u901a\u904e\u6709\u6548\u5229\u7528\u591a\u6838\u8655\u7406\u5668\uff0cGo \u8a9e\u8a00\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u7c21\u5316\u7684\u4ee3\u78bc\u7d50\u69cb\uff1a<\/strong>\u4f7f\u7528 Goroutines \u548c\u901a\u9053\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u7c21\u55ae\u5730\u7de8\u5beb\u4e26\u767c\u4ee3\u78bc\uff0c\u6e1b\u5c11\u4e86\u50b3\u7d71\u591a\u57f7\u884c\u7dd2\u7de8\u7a0b\u4e2d\u7684\u8907\u96dc\u6027\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u901a\u9053\u63d0\u4f9b\u4e86\u4e00\u7a2e\u5b89\u5168\u7684\u6578\u64da\u50b3\u905e\u65b9\u5f0f\uff0c\u6e1b\u5c11\u4e86\u7af6\u722d\u689d\u4ef6\u548c\u6b7b\u9396\u7684\u98a8\u96aa\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>Go \u8a9e\u8a00\u7684\u4e26\u767c\u6a21\u578b\u4f7f\u5f97\u61c9\u7528\u7a0b\u5e8f\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\uff0c\u4ee5\u61c9\u5c0d\u66f4\u9ad8\u7684\u8ca0\u8f09\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-whcJpSmOUT\">\u7d50\u8ad6<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\u4f7f\u5176\u6210\u70ba\u73fe\u4ee3\u8edf\u9ad4\u958b\u767c\u4e2d\u7684\u4e00\u500b\u5f37\u5927\u5de5\u5177\u3002\u901a\u904e Goroutines \u548c\u901a\u9053\uff0c\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u73fe\u9ad8\u6548\u7684\u4e26\u767c\u7de8\u7a0b\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7dad\u8b77\u6027\u3002\u96a8\u8457\u5c0d\u9ad8\u6548\u80fd\u61c9\u7528\u9700\u6c42\u7684\u589e\u52a0\uff0cGo \u8a9e\u8a00\u7684\u4e26\u767c\u512a\u52e2\u5c07\u6703\u8d8a\u4f86\u8d8a\u53d7\u5230\u91cd\u8996\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\uff0c\u4e86\u89e3\u5176\u512a\u52e2\u5982\u4f55\u63d0\u5347\u6027\u80fd\u8207\u6548\u7387\uff0c\u52a9\u529b\u958b\u767c\u9ad8\u6548\u80fd\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57616","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57616"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57616\/revisions"}],"predecessor-version":[{"id":57617,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57616\/revisions\/57617"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}