{"id":6568,"date":"2024-09-19T19:28:11","date_gmt":"2024-09-19T11:28:11","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6568"},"modified":"2024-09-19T19:28:11","modified_gmt":"2024-09-19T11:28:11","slug":"go-context-%e6%9c%89%e5%93%aa%e4%ba%9b%e4%bd%9c%e7%94%a8%e5%92%8c%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6568","title":{"rendered":"go context \u6709\u54ea\u4e9b\u4f5c\u7528\u548c\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<code>context<\/code> \u662f\u4e00\u79cd\u7528\u4e8e\u5728 Goroutines \u4e4b\u95f4\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u5185\u7684\u6570\u636e\u3001\u53d6\u6d88\u4fe1\u53f7\u548c\u622a\u6b62\u65f6\u95f4\u7684\u673a\u5236\u3002\u4ee5\u4e0b\u662f <code>context<\/code> \u7684\u4e3b\u8981\u4f5c\u7528\u548c\u4e00\u4e9b\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<h3>\u4e3b\u8981\u4f5c\u7528<\/h3>\n<ol>\n<li>\n<p><strong>\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u5185\u7684\u6570\u636e<\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u53ef\u4ee5\u7528\u6765\u5728\u51fd\u6570\u8c03\u7528\u94fe\u4e2d\u4f20\u9012\u6570\u636e\uff0c\u6bd4\u5982\u7528\u6237\u8ba4\u8bc1\u4fe1\u606f\u3001\u8bf7\u6c42 ID \u7b49\u3002\u8fd9\u4f7f\u5f97\u5728\u4e0d\u540c\u7684 Goroutine \u6216\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u8fd9\u4e9b\u6570\u636e\u53d8\u5f97\u7b80\u5355\u800c\u5b89\u5168\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53d6\u6d88\u4fe1\u53f7\u4f20\u9012<\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u53ef\u4ee5\u7528\u4e8e\u4f20\u9012\u53d6\u6d88\u4fe1\u53f7\u3002\u5f53\u4f60\u9700\u8981\u53d6\u6d88\u4e00\u9879\u64cd\u4f5c\uff08\u4f8b\u5982\u8bf7\u6c42\u8d85\u65f6\u6216\u7528\u6237\u4e3b\u52a8\u53d6\u6d88\u8bf7\u6c42\uff09\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>context<\/code> \u7684 <code>CancelFunc<\/code> \u6765\u901a\u77e5\u6240\u6709\u76f8\u5173\u7684 Goroutine\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u622a\u6b62\u65f6\u95f4\u548c\u8d85\u65f6\u5904\u7406<\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u53ef\u4ee5\u8bbe\u5b9a\u622a\u6b62\u65f6\u95f4\u6216\u8d85\u65f6\u3002\u5f53\u64cd\u4f5c\u8d85\u8fc7\u8bbe\u5b9a\u7684\u65f6\u95f4\u9650\u5236\u65f6\uff0c<code>context<\/code> \u4f1a\u81ea\u52a8\u53d6\u6d88\u6240\u6709\u4e0e\u4e4b\u5173\u8054\u7684 Goroutine\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u6d88\u8017\u548c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ol>\n<li>\n<p><strong>\u907f\u514d\u5728 Goroutine \u4e2d\u4fee\u6539 <code>context<\/code><\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u4f60\u4e0d\u5e94\u8be5\u5c1d\u8bd5\u4fee\u6539\u5b83\u3002\u4f7f\u7528 <code>context.WithValue<\/code> \u521b\u5efa\u65b0 <code>context<\/code> \u65f6\uff0c\u4ec5\u5e94\u6dfb\u52a0\u6216\u4fee\u6539\u6570\u636e\uff0c\u800c\u4e0d\u662f\u66f4\u6539\u539f\u6709\u7684 <code>context<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u5728 Goroutine \u4e2d\u4f7f\u7528\u539f\u59cb <code>context<\/code><\/strong>\uff1a<\/p>\n<ul>\n<li>\u5728 Goroutine \u4e2d\u4f7f\u7528 <code>context<\/code> \u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u53ea\u8bfb\u526f\u672c\u800c\u4e0d\u662f\u539f\u59cb <code>context<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d Goroutine \u5728 <code>context<\/code> \u88ab\u53d6\u6d88\u6216\u8d85\u65f6\u65f6\u53d7\u5230\u610f\u5916\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5c3d\u91cf\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 <code>context<\/code> \u5b9e\u73b0<\/strong>\uff1a<\/p>\n<ul>\n<li>Go \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 <code>context<\/code> \u5305\uff0c\u5efa\u8bae\u4f7f\u7528\u8fd9\u4e2a\u6807\u51c6\u5b9e\u73b0\uff0c\u800c\u4e0d\u662f\u81ea\u5df1\u91cd\u65b0\u5b9e\u73b0\u6216\u7b2c\u4e09\u65b9\u5e93\u7684\u5b9e\u73b0\uff0c\u4ee5\u786e\u4fdd\u4e00\u81f4\u6027\u548c\u517c\u5bb9\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5c3d\u91cf\u4f20\u9012 <code>context<\/code> \u800c\u4e0d\u662f\u5c06\u5176\u5d4c\u5957\u5728\u7ed3\u6784\u4f53\u4e2d<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06 <code>context<\/code> \u4f20\u9012\u7ed9\u51fd\u6570\u800c\u4e0d\u662f\u5c06\u5176\u5d4c\u5957\u5728\u7ed3\u6784\u4f53\u4e2d\uff0c\u53ef\u4ee5\u786e\u4fdd <code>context<\/code> \u751f\u547d\u5468\u671f\u7684\u6b63\u786e\u7ba1\u7406\uff0c\u5e76\u907f\u514d\u5728\u4e0d\u540c\u7684\u8c03\u7528\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u9519\u8bef\u7684 <code>context<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u5728 <code>context<\/code> \u4e2d\u5b58\u50a8\u5927\u6570\u636e\u6216\u590d\u6742\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u5185\u7684\u5c0f\u6570\u636e\uff08\u5982\u7528\u6237 ID\u3001\u8bf7\u6c42\u72b6\u6001\uff09\uff0c\u4e0d\u5e94\u5b58\u50a8\u5927\u578b\u6570\u636e\u7ed3\u6784\u6216\u590d\u6742\u5bf9\u8c61\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5f71\u54cd\u6027\u80fd\u548c\u8d44\u6e90\u6d88\u8017\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f <code>context<\/code> \u7684\u4f20\u9012\u987a\u5e8f<\/strong>\uff1a<\/p>\n<ul>\n<li><code>context<\/code> \u901a\u5e38\u5728\u8bf7\u6c42\u5904\u7406\u7684\u5f00\u59cb\u5904\u521b\u5efa\uff0c\u5e76\u4e14\u5e94\u5728\u6574\u4e2a\u8bf7\u6c42\u751f\u547d\u5468\u671f\u5185\u4f20\u9012\uff0c\u786e\u4fdd\u6240\u6709\u76f8\u5173\u7684 Goroutine \u90fd\u80fd\u8bbf\u95ee\u5230\u6b63\u786e\u7684 <code>context<\/code> \u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528 <code>context<\/code>\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406 Goroutines\u3001\u4f20\u9012\u6570\u636e\u548c\u5904\u7406\u8d85\u65f6\u6216\u53d6\u6d88\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0ccontext \u662f\u4e00\u79cd\u7528\u4e8e\u5728 Goroutines \u4e4b\u95f4\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u5185\u7684\u6570\u636e\u3001\u53d6\u6d88\u4fe1\u53f7\u548c\u622a\u6b62&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6568\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6568","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6568"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6568\/revisions"}],"predecessor-version":[{"id":6569,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6568\/revisions\/6569"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}