{"id":206286,"date":"2025-06-12T14:07:41","date_gmt":"2025-06-12T06:07:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206286\/"},"modified":"2025-06-12T14:07:41","modified_gmt":"2025-06-12T06:07:41","slug":"go-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e7%ad%89%e5%be%85%e6%9c%ba%e5%88%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206286\/","title":{"rendered":"Go \u8bed\u8a00\u4e2d\u5982\u4f55\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f"},"content":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u8bed\u8a00\u4e2d\u5982\u4f55\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731058606672ddbae1624b.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u901a\u8fc7\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236<\/strong>,\u5728 go \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 channel \u6216 context \u5b9e\u73b0\u534f\u7a0b\u4e4b\u95f4\u7684\u7b49\u5f85\u3002,<br \/>\n<strong>channel \u65b9\u5f0f<\/strong>,\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u7f13\u51b2\u533a\u7684 channel\uff0c\u5b50\u534f\u7a0b\u5728\u5b8c\u6210\u4efb\u52a1\u540e\u5411 channel \u53d1\u9001\u6d88\u606f\uff0c\u4e3b\u534f\u7a0b\u4ece channel \u63a5\u6536\u6d88\u606f\u7b49\u5f85\u5b50\u534f\u7a0b\u5b8c\u6210\u3002,<br \/>\n<strong>context \u65b9\u5f0f<\/strong>,\u4f7f\u7528 context.context \u6765\u4f20\u64ad\u53d6\u6d88\u4fe1\u53f7\uff0c\u5f53\u7236 context \u88ab\u53d6\u6d88\u65f6\uff0c\u6240\u6709\u5173\u8054\u7684\u5b50\u534f\u7a0b\u4e5f\u4f1a\u81ea\u52a8\u53d6\u6d88\u3002,\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002,\u901a\u8fc7\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236,\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u8bed\u8a00\u4e2d\u5982\u4f55\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~,<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\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f<\/span><\/p>\n<h1>Go \u8bed\u8a00\u4e2d\u5982\u4f55\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f<\/h1>\n<p><span>2024-11-08 17:37:01<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u8bed\u8a00\u4e2d\u5982\u4f55\u5229\u7528\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731058606672ddbae1624b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u8fc7\u534f\u7a0b\u5b9e\u73b0\u7b49\u5f85\u673a\u5236<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 channel \u6216 context \u5b9e\u73b0\u534f\u7a0b\u4e4b\u95f4\u7684\u7b49\u5f85\u3002<\/p>\n<p><strong>channel \u65b9\u5f0f<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u7f13\u51b2\u533a\u7684 channel\uff0c\u5b50\u534f\u7a0b\u5728\u5b8c\u6210\u4efb\u52a1\u540e\u5411 channel \u53d1\u9001\u6d88\u606f\uff0c\u4e3b\u534f\u7a0b\u4ece channel \u63a5\u6536\u6d88\u606f\u7b49\u5f85\u5b50\u534f\u7a0b\u5b8c\u6210\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    ch := make(chan int, 10)\n\n    for i := 0; i &lt; 10; i++ {\n        go func(ch chan int, key int) {\n            fmt.println(\"goroutine start: \", key)\n            time.sleep(time.duration(key) * time.second)\n            fmt.println(\"goroutine end: \", key)\n            ch &lt;- key\n        }(ch, i)\n    }\n\n    fmt.println(\"main start\")\n    for i := 0; i &lt; 10; i++ {\n        key := &lt;-ch\n        fmt.println(\"main get: \", key)\n    }\n    fmt.println(\"main end\")\n}<\/pre>\n<p><strong>context \u65b9\u5f0f<\/strong><\/p>\n<p>\u4f7f\u7528 context.context \u6765\u4f20\u64ad\u53d6\u6d88\u4fe1\u53f7\uff0c\u5f53\u7236 context \u88ab\u53d6\u6d88\u65f6\uff0c\u6240\u6709\u5173\u8054\u7684\u5b50\u534f\u7a0b\u4e5f\u4f1a\u81ea\u52a8\u53d6\u6d88\u3002<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n\n    for i := 0; i &lt; 10; i++ {\n        go func(ctx context.Context, key int) {\n            fmt.Println(\"goroutine start: \", key)\n            select {\n            case &lt;-ctx.Done():\n                fmt.Println(\"goroutine canceled: \", key)\n            case &lt;-time.After(time.Duration(key) * time.Second):\n                fmt.Println(\"goroutine end: \", key)\n            }\n        }(ctx, i)\n    }\n\n    fmt.Println(\"main start\")\n    time.Sleep(time.Duration(3) * time.Second)\n    cancel()\n    fmt.Println(\"main end\")\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Element Plus \u4e2d &#8220; \u7684\u542b\u4e49\u662f\u4ec0\u4e48\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   HTTP \u670d\u52a1\u5668\u5982\u4f55\u76d1\u6d4b\u5ba2\u6237\u7aef\u8d85\u65f6\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9 GOLA&#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-206286","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206286","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=206286"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206286\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}