{"id":206161,"date":"2025-06-12T08:41:43","date_gmt":"2025-06-12T00:41:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206161\/"},"modified":"2025-06-12T08:41:43","modified_gmt":"2025-06-12T00:41:43","slug":"go%e8%af%ad%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%90%8c%e6%97%b6%e7%9b%91%e5%90%ac%e5%ae%a2%e6%88%b7%e7%ab%af%e8%bf%9e%e6%8e%a5%e5%92%8c%e7%bb%88%e7%ab%af%e5%91%bd%e4%bb%a4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206161\/","title":{"rendered":"Go\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f"},"content":{"rendered":"<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aGo\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/1730777255672990a79d185.jpg\" class=\"aligncenter\">,<br \/>\n<strong>go\u8bed\u8a00\u4e2d\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4<\/strong>,\u5728 go \u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0\u4e00\u8fb9\u76d1\u542c\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\uff0c\u4e00\u8fb9\u5904\u7406\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4\u9700\u8981\u4f7f\u7528\u5e76\u53d1\u673a\u5236\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong>,<br \/>\n<strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong>,<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong>,\u4ee5\u4e0a\u5c31\u662f\u300aGo\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,go\u8bed\u8a00\u4e2d\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4,\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aGo\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Go\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f<\/span><\/p>\n<h1>Go\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f<\/h1>\n<p><span>2024-11-05 11:27:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aGo\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/1730777255672990a79d185.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>go\u8bed\u8a00\u4e2d\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u5b9e\u73b0\u4e00\u8fb9\u76d1\u542c\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\uff0c\u4e00\u8fb9\u5904\u7406\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4\u9700\u8981\u4f7f\u7528\u5e76\u53d1\u673a\u5236\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li>\u521b\u5efa\u4e24\u4e2a\u901a\u9053\uff1arecvfromterminalchan \u548c recvfromnetchan\uff0c\u7528\u4e8e\u5206\u522b\u63a5\u6536\u7ec8\u7aef\u8f93\u5165\u548c\u7f51\u7edc\u6570\u636e\u3002<\/li>\n<li>\u542f\u52a8\u4e00\u4e2a goroutine \u7528\u4e8e\u4ece\u7ec8\u7aef\u8bfb\u53d6\u8f93\u5165\u5e76\u5c06\u5176\u53d1\u9001\u5230 recvfromterminalchan \u901a\u9053\u3002<\/li>\n<li>\u5728\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u4e2d\u4f7f\u7528 select \u8bed\u53e5\u76d1\u542c\u4e24\u4e2a\u901a\u9053\u3002\u5f53\u6709\u6570\u636e\u5199\u5165\u901a\u9053\u65f6\uff0c\u76f8\u5e94\u7684 goroutine \u88ab\u89e6\u53d1\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nvar recvFromTerminalChan = make(chan string)\nvar recvFromNetChan = make(chan string)\n\nfunc main() {\n    go readFromTerminal()\n\n    for {\n        select {\n        case str := &lt;-recvFromTerminalChan:\n            go sendCommandToNet(str)\n        case str := &lt;-recvFromNetChan:\n            go doContentFromNet(str)\n        }\n    }\n}\n\nfunc readFromTerminal() {\n    var input string\n\n    for {\n        fmt.Scan(&amp;input)\n        fmt.Println(\"input content : \", input)\n        recvFromTerminalChan &lt;- input\n    }\n}\n\nfunc sendCommandToNet(command string) {\n    fmt.Println(\"command : \", command)\n\n    if command == \"quit\" {\n        close(recvFromTerminalChan)\n        close(recvFromNetChan)\n        os.Exit(0)\n    }\n\n    content := command\n\n    recvFromNetChan &lt;- content\n}\n\nfunc doContentFromNet(content string) {\n    fmt.Println(\"content from net : \", content)\n}<\/pre>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ol>\n<li>\u5f53\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4\u4e3a &#8220;quit&#8221; \u65f6\uff0c\u5173\u95ed\u4e24\u4e2a\u901a\u9053\u5e76\u9000\u51fa\u7a0b\u5e8f\u3002<\/li>\n<li>\u4f18\u5316\u4ee3\u7801\u53ef\u8003\u8651\u4f7f\u7528\u7f13\u51b2\u901a\u9053\uff0c\u907f\u514d\u56e0\u901a\u9053\u963b\u585e\u5bfc\u81f4\u7a0b\u5e8f\u505c\u6ede\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGo\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u540c\u65f6\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u7ec8\u7aef\u547d\u4ee4\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5229\u7528\u4e8b\u52a1\u548c FOR UPDATE \u5b9e\u73b0\u6570\u636e\u5e93\u961f\u5217\u4efb\u52a1\u7684\u5e76\u53d1\u6267\u884c\u9501\u673a\u5236\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6570\u636e\u5e93\u8bbe\u8ba1\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7ecf\u5e38\u8c03\u7528\u7684\u5b57\u6bb5\u5197\u4f59\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206161","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206161","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=206161"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206161\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}