{"id":57860,"date":"2024-10-10T21:44:49","date_gmt":"2024-10-10T13:44:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57860\/"},"modified":"2024-10-10T21:44:49","modified_gmt":"2024-10-10T13:44:49","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%82%ba%e4%b8%a6%e7%99%bc%e8%80%8c%e7%94%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57860\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u70ba\u4e26\u767c\u800c\u751f"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%82%ba%e4%b8%a6%e7%99%bc%e8%80%8c%e7%94%9f-tYmgZDurzA\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u70ba\u4e26\u767c\u800c\u751f<\/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\u63a2\u8a0e Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\u53ca\u5176\u5982\u4f55\u7c21\u5316\u4e26\u767c\u7de8\u7a0b\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e8%a8%ad%e8%a8%88%e7%90%86%e5%bf%b5-tYmgZDurzA\">Go \u8a9e\u8a00\u7684\u8a2d\u8a08\u7406\u5ff5<\/h2>\n<p>Go \u8a9e\u8a00\u7531 Google \u7684 Robert Griesemer\u3001Rob Pike \u548c Ken Thompson \u65bc 2007 \u5e74\u8a2d\u8a08\uff0c\u4e26\u65bc 2009 \u5e74\u6b63\u5f0f\u767c\u5e03\u3002\u5176\u8a2d\u8a08\u7406\u5ff5\u662f\u7c21\u5316\u7de8\u7a0b\u904e\u7a0b\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u4e26\u767c\u6642\u3002Go \u8a9e\u8a00\u7684\u8a9e\u6cd5\u7c21\u6f54\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u4e26\u767c\u6027\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u66f4\u8f15\u9b06\u5730\u7de8\u5beb\u9ad8\u6548\u7684\u4e26\u767c\u7a0b\u5f0f\u3002<\/p>\n<h2 id=\"go-%e7%9a%84%e4%b8%a6%e7%99%bc%e6%a8%a1%e5%9e%8b-tYmgZDurzA\">Go \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\u2014\u2014\u5373 goroutines\u3002\u9019\u4e9b goroutines \u662f\u7531 Go \u7684\u904b\u884c\u6642\u7ba1\u7406\u7684\uff0c\u4e26\u4e14\u76f8\u8f03\u65bc\u50b3\u7d71\u7684\u57f7\u884c\u7dd2\uff0c\u5b83\u5011\u7684\u958b\u92b7\u975e\u5e38\u5c0f\u3002\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u555f\u52d5\u6578\u5343\u500b goroutines\uff0c\u800c\u4e0d\u5fc5\u64d4\u5fc3\u7cfb\u7d71\u8cc7\u6e90\u7684\u8017\u76e1\u3002<\/p>\n<h3 id=\"%e5%95%9f%e5%8b%95-goroutines-tYmgZDurzA\">\u555f\u52d5 Goroutines<\/h3>\n<p>\u555f\u52d5\u4e00\u500b goroutine \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u51fd\u6578\u8abf\u7528\u524d\u52a0\u4e0a <code>go<\/code> \u95dc\u9375\u5b57\u3002\u4f8b\u5982\uff1a<\/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 goroutine\n    time.Sleep(1 * time.Second) \/\/ \u7b49\u5f85 goroutine \u5b8c\u6210\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>sayHello<\/code> \u51fd\u6578\u6703\u5728\u4e00\u500b\u65b0\u7684 goroutine \u4e2d\u57f7\u884c\uff0c\u800c\u4e3b\u51fd\u6578\u5247\u6703\u7b49\u5f85\u4e00\u79d2\u9418\u4ee5\u78ba\u4fdd goroutine \u6709\u8db3\u5920\u7684\u6642\u9593\u5b8c\u6210\u3002<\/p>\n<h2 id=\"%e9%80%9a%e9%81%93%ef%bc%88channels%ef%bc%89-tYmgZDurzA\">\u901a\u9053\uff08Channels\uff09<\/h2>\n<p>Go \u8a9e\u8a00\u9084\u63d0\u4f9b\u4e86\u901a\u9053\uff08channels\uff09\u4f86\u9032\u884c goroutines \u4e4b\u9593\u7684\u901a\u4fe1\u3002\u901a\u9053\u662f\u4e00\u7a2e\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8b93\u4e0d\u540c\u7684 goroutines \u4ea4\u63db\u6578\u64da\u3002\u4f7f\u7528\u901a\u9053\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u9396\uff08locks\uff09\uff0c\u5f9e\u800c\u6e1b\u5c11\u6b7b\u9396\u7684\u98a8\u96aa\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e9%80%9a%e9%81%93%e7%9a%84%e7%af%84%e4%be%8b-tYmgZDurzA\">\u4f7f\u7528\u901a\u9053\u7684\u7bc4\u4f8b<\/h3>\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 \/\/ \u5f9e\u901a\u9053\u63a5\u6536\u6578\u64da\n    fmt.Println(msg) \/\/ \u8f38\u51fa\u63a5\u6536\u5230\u7684\u6578\u64da\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u901a\u9053\u4e26\u5728 goroutine \u4e2d\u767c\u9001\u6578\u64da\u3002\u4e3b\u51fd\u6578\u5247\u5f9e\u901a\u9053\u63a5\u6536\u6578\u64da\u4e26\u8f38\u51fa\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e7%9a%84%e5%84%aa%e5%8b%a2-tYmgZDurzA\">Go \u8a9e\u8a00\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Go \u8a9e\u8a00\u7684\u8a9e\u6cd5\u7c21\u6f54\uff0c\u6613\u65bc\u5b78\u7fd2\uff0c\u7279\u5225\u9069\u5408\u65b0\u624b\u3002<\/li>\n<li><strong>\u9ad8\u6548\u7684\u4e26\u767c\u652f\u6301\uff1a<\/strong>goroutines \u548c\u901a\u9053\u4f7f\u5f97\u4e26\u767c\u7de8\u7a0b\u8b8a\u5f97\u76f4\u89c0\u4e14\u9ad8\u6548\u3002<\/li>\n<li><strong>\u5f37\u5927\u7684\u6a19\u6e96\u5eab\uff1a<\/strong>Go \u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u6a19\u6e96\u5eab\uff0c\u652f\u6301\u7db2\u7d61\u7de8\u7a0b\u3001\u6578\u64da\u5eab\u64cd\u4f5c\u7b49\u591a\u7a2e\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-tYmgZDurzA\">\u7d50\u8ad6<\/h2>\n<p>Go \u8a9e\u8a00\u56e0\u5176\u70ba\u4e26\u767c\u800c\u751f\u7684\u7279\u6027\uff0c\u6210\u70ba\u4e86\u73fe\u4ee3\u8edf\u9ad4\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u7121\u8ad6\u662f\u8655\u7406\u9ad8\u4f75\u767c\u7684\u7db2\u7d61\u61c9\u7528\uff0c\u9084\u662f\u9032\u884c\u6578\u64da\u8655\u7406\uff0cGo \u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0cGo \u8a9e\u8a00\u7684\u61c9\u7528\u7bc4\u570d\u5c07\u6703\u8d8a\u4f86\u8d8a\u5ee3\u6cdb\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\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u7684\u4e26\u767c\u7279\u6027\uff0c\u5b78\u7fd2\u5982\u4f55\u5229\u7528\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u9ad8\u6548\u7684\u6027\u80fd\u4f86\u69cb\u5efa\u9ad8\u6548\u80fd\u7684\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-57860","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57860","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=57860"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57860\/revisions"}],"predecessor-version":[{"id":57861,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57860\/revisions\/57861"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}