{"id":206883,"date":"2025-06-12T13:08:38","date_gmt":"2025-06-12T05:08:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206883\/"},"modified":"2025-06-12T13:08:38","modified_gmt":"2025-06-12T05:08:38","slug":"go-%e8%af%ad%e8%a8%80%e4%b8%ad%ef%bc%8c%e5%bd%93-context-%e8%a2%ab%e5%8f%96%e6%b6%88%e5%90%8e%ef%bc%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206883\/","title":{"rendered":"Go \u8bed\u8a00\u4e2d\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`"},"content":{"rendered":"<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aGo \u8bed\u8a00\u4e2d\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/173268165767469fb9441d1.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u4e0a\u4e0b\u6587\u53d6\u6d88\u540e\uff0c&lt;-ctx.done()\u672a\u6267\u884c\u7684\u539f\u56e0<\/strong>,\u5728 go \u8bed\u8a00\u4e2d\uff0c\u4f7f\u7528 context.context \u5305\u53ef\u4ee5\u5b9e\u73b0\u534f\u7a0b\u95f4\u901a\u4fe1\u548c\u53d6\u6d88\u3002\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c&lt;-ctx.done() \u4f1a\u88ab\u5524\u9192\u5e76\u8fd4\u56de\uff0c\u4f46\u6709\u65f6\u5b83\u53ef\u80fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002,\u8fd9\u662f\u56e0\u4e3a&lt;-ctx.done()\u963b\u585e\u5728\u8bfb\u53d6ctx.done()\u901a\u9053\u4e0a\u3002\u5982\u679c\u5728\u8c03\u7528cancel()\u4e4b\u524d\uff0cctx\u901a\u9053\u4e2d\u7684\u6570\u636e\u5df2\u7ecf\u51c6\u5907\u597d\uff0c\u5219&lt;-ctx.done()\u5c06\u4e0d\u4f1a\u88ab\u5524\u9192\u3002,<br \/>\n<strong>\u5bfc\u81f4\u8be5\u95ee\u9898\u7684\u4e00\u4e2a\u793a\u4f8b<\/strong>,\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u8be5\u95ee\u9898\uff1a,\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cfor\u5faa\u73af\u5c06\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u4e2a\u5143\u7d20\u4e4b\u524d\u4e00\u76f4\u8fd0\u884c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53cancel()\u88ab\u8c03\u7528\u65f6\uff0c\u7b2c\u516d\u4e2a\u5143\u7d20\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u3002\u56e0\u6b64\uff0c&lt;-ctx.done()\u4e0d\u4f1a\u88ab\u5524\u9192\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>,\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u901a\u9053\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u5f53\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5173\u95ed\u901a\u9053\u5c06\u5bfc\u81f4&lt;-ctx.done()\u88ab\u5524\u9192\u3002,\u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u6539\u8fdb\u540e\u7684\u793a\u4f8b\uff1a,\u8fd9\u6837\uff0c\u65e0\u8bba\u7b2c\u516d\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u51c6\u5907\u597d\uff0c&lt;-ctx.done()\u90fd\u5c06\u88ab\u5524\u9192\uff0c\u5e76\u5173\u95ed\u901a\u9053\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,\u4e0a\u4e0b\u6587\u53d6\u6d88\u540e\uff0c&lt;-ctx.done()\u672a\u6267\u884c\u7684\u539f\u56e0,\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aGo \u8bed\u8a00\u4e2d\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01,<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\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`<\/span><\/p>\n<h1>Go \u8bed\u8a00\u4e2d\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`<\/h1>\n<p><span>2024-11-27 12:27:52<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aGo \u8bed\u8a00\u4e2d\uff0c\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c`\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/173268165767469fb9441d1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4e0a\u4e0b\u6587\u53d6\u6d88\u540e\uff0c&lt;-ctx.done()\u672a\u6267\u884c\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u4f7f\u7528 context.context \u5305\u53ef\u4ee5\u5b9e\u73b0\u534f\u7a0b\u95f4\u901a\u4fe1\u548c\u53d6\u6d88\u3002\u5f53 context \u88ab\u53d6\u6d88\u540e\uff0c&lt;-ctx.done() \u4f1a\u88ab\u5524\u9192\u5e76\u8fd4\u56de\uff0c\u4f46\u6709\u65f6\u5b83\u53ef\u80fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a&lt;-ctx.done()\u963b\u585e\u5728\u8bfb\u53d6ctx.done()\u901a\u9053\u4e0a\u3002\u5982\u679c\u5728\u8c03\u7528cancel()\u4e4b\u524d\uff0cctx\u901a\u9053\u4e2d\u7684\u6570\u636e\u5df2\u7ecf\u51c6\u5907\u597d\uff0c\u5219&lt;-ctx.done()\u5c06\u4e0d\u4f1a\u88ab\u5524\u9192\u3002<\/p>\n<p><strong>\u5bfc\u81f4\u8be5\u95ee\u9898\u7684\u4e00\u4e2a\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u8be5\u95ee\u9898\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n)\n\nfunc gen(ctx context.context) &lt;-chan interface{} {\n    ch := make(chan interface{})\n    go func() {\n        n := 0\n        for {\n            select {\n            case &lt;-ctx.done():\n                fmt.println(\"done\")\n            default:\n                n += 1\n                ch &lt;- n\n            }\n        }\n\n    }()\n    return ch\n}\n\nfunc main() {\n    ctx, cancel := context.withcancel(context.background())\n\n    for n := range gen(ctx) {\n        fmt.println(n)\n        if n == 5 {\n            break\n        }\n    }\n    defer cancel()\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cfor\u5faa\u73af\u5c06\u5728\u8bfb\u53d6\u5230\u7b2c\u516d\u4e2a\u5143\u7d20\u4e4b\u524d\u4e00\u76f4\u8fd0\u884c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53cancel()\u88ab\u8c03\u7528\u65f6\uff0c\u7b2c\u516d\u4e2a\u5143\u7d20\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u3002\u56e0\u6b64\uff0c&lt;-ctx.done()\u4e0d\u4f1a\u88ab\u5524\u9192\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u901a\u9053\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u5f53\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5173\u95ed\u901a\u9053\u5c06\u5bfc\u81f4&lt;-ctx.done()\u88ab\u5524\u9192\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u6539\u8fdb\u540e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n)\n\nfunc gen(ctx context.Context) &lt;-chan interface{} {\n    ch := make(chan interface{})\n    go func() {\n        n := 0\n        for {\n            select {\n            case &lt;-ctx.Done():\n                fmt.Println(\"done\")\n                close(ch) \/\/ \u5173\u95ed\u901a\u9053\uff0crange\u7ed3\u675f\n                return\n            default:\n                n += 1\n                ch &lt;- n\n            }\n        }\n\n    }()\n    return ch\n}\n\nfunc main() {\n    ctx, cancel := context.WithCancel(context.Background())\n\n    for n := range gen(ctx) {\n        fmt.Println(n)\n        if n == 5 {\n            cancel()\n            \/\/ break\u4e0d\u80fdbreak\uff0c\u5426\u5219\u6ca1\u6709\u4ecech\u4e2d\u8bfb\u53d6\uff0c\u5c31\u4f1a\u4e00\u76f4\u963b\u585e\u5728ch&lt;-n\n        }\n    }\n}<\/pre>\n<p>\u8fd9\u6837\uff0c\u65e0\u8bba\u7b2c\u516d\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u51c6\u5907\u597d\uff0c&lt;-ctx.done()\u90fd\u5c06\u88ab\u5524\u9192\uff0c\u5e76\u5173\u95ed\u901a\u9053\u3002<\/p>\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   \u89e3\u51b3\u7535\u8111\u65e0\u6cd5\u663e\u793a\u8f93\u5165\u6cd5\u7684\u591a\u79cd\u65b9\u6cd5\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   win10\u600e\u4e48\u5173\u95ed\u63d0\u793a\u97f3 win10\u5173\u95ed\u8bbe\u5907\u5f39\u51fa\u63d0\u793a\u97f3\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd&#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-206883","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206883","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=206883"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206883\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}