{"id":455,"date":"2023-10-14T22:44:43","date_gmt":"2023-10-14T14:44:43","guid":{"rendered":"https:\/\/blog.xjfvps.top\/?p=455"},"modified":"2023-10-14T22:47:47","modified_gmt":"2023-10-14T14:47:47","slug":"go%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84context","status":"publish","type":"post","link":"https:\/\/blog.fs.cloudns.biz\/index.php\/2023\/10\/14\/go%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84context\/","title":{"rendered":"\u7406\u89e3Go\u8bed\u8a00\u4e2dContext\u7684\u4f7f\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728Go\u8bed\u8a00\u4e2d\uff0cContext\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u4e00\u6b21\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u540c\u7684Goroutine\u4e4b\u95f4\u4f20\u9012\u8bf7\u6c42\u7279\u5b9a\u7684\u503c\u3001\u53d6\u6d88\u4fe1\u53f7\u4ee5\u53ca\u622a\u6b62\u65f6\u95f4\u3002Context\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u63a7\u5236\u5e76\u53d1\u8bf7\u6c42\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u9732\u4ee5\u53ca\u8fc7\u5ea6\u7684\u7b49\u5f85\u65f6\u95f4\u3002\u8ba9\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e00\u4e0bGo\u8bed\u8a00\u4e2dContext\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662fContext<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Context\u662fGo\u8bed\u8a00\u4e2d\u7528\u4e8e\u8ddf\u8e2a\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u5b83\u53ef\u4ee5\u8de8\u591a\u4e2aGoroutine\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u7684\u503c\uff0c\u63a7\u5236\u8bf7\u6c42\u7684\u622a\u6b62\u65f6\u95f4\uff0c\u4ee5\u53ca\u5728\u9700\u8981\u65f6\u53d6\u6d88\u8bf7\u6c42\u3002Context\u662f\u591a\u534f\u7a0b\u5b89\u5168\u7684\uff0c\u5b83\u662fGo\u4e2d\u5904\u7406\u5e76\u53d1\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Context\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165&#8221;context&#8221;\u5305\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>context.Background()<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u6839Context\uff0c\u5b83\u662f\u6240\u6709Context\u7684\u8d77\u70b9\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tctx := context.Background()\n\tfmt.Println(\"Root Context:\", ctx)\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">WithCancel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u65f6\u53d6\u6d88Context\u3002\u4f7f\u7528<code>context.WithCancel()<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u53d6\u6d88\u4fe1\u53f7\u7684\u65b0Context\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func main() {\n\tparent := context.Background()\n\tctx, cancel := context.WithCancel(parent)\n\n\tgo func() {\n\t\t\/\/ \u6a21\u62df\u53d6\u6d88\u64cd\u4f5c\n\t\tcancel()\n\t}()\n\n\tselect {\n\tcase &lt;-ctx.Done():\n\t\tfmt.Println(\"Context is canceled\")\n\t}\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">WithDeadline<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5728\u4e00\u5b9a\u65f6\u95f4\u5185\u6267\u884c\u4efb\u52a1\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u5c31\u53d6\u6d88\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528<code>context.WithDeadline()<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u622a\u6b62\u65f6\u95f4\u7684Context\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import (\n\t\"time\"\n)\n\nfunc main() {\n\tparent := context.Background()\n\td := time.Now().Add(50 * time.Millisecond)\n\tctx, cancel := context.WithDeadline(parent, d)\n\n\tselect {\n\tcase &lt;-time.After(1 * time.Second):\n\t\tfmt.Println(\"Done\")\n\tcase &lt;-ctx.Done():\n\t\tfmt.Println(\"Context is canceled\")\n\t}\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">WithTimeout<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>context.WithTimeout()<\/code>\u51fd\u6570\u662f<code>context.WithDeadline()<\/code>\u7684\u4e00\u79cd\u7b80\u5316\u5f62\u5f0f\uff0c\u5b83\u521b\u5efa\u4e00\u4e2a\u5728\u6307\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u53d6\u6d88\u7684Context\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func main() {\n\tparent := context.Background()\n\tctx, cancel := context.WithTimeout(parent, 50*time.Millisecond)\n\tdefer cancel()\n\n\tselect {\n\tcase &lt;-time.After(1 * time.Second):\n\t\tfmt.Println(\"Done\")\n\tcase &lt;-ctx.Done():\n\t\tfmt.Println(\"Context is canceled\")\n\t}\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e86Go\u8bed\u8a00\u4e2dContext\u7684\u7528\u6cd5\u3002\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Context\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528<code>WithCancel<\/code>\u3001<code>WithDeadline<\/code>\u548c<code>WithTimeout<\/code>\u7b49\u51fd\u6570\u521b\u5efa\u5e26\u6709\u53d6\u6d88\u4fe1\u53f7\u3001\u622a\u6b62\u65f6\u95f4\u6216\u8d85\u65f6\u65f6\u95f4\u7684Context\u3002Context\u662fGo\u8bed\u8a00\u4e2d\u975e\u5e38\u5f3a\u5927\u4e14\u5b9e\u7528\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5e76\u53d1\u8bf7\u6c42\u8fdb\u884c\u7ec6\u7c92\u5ea6\u63a7\u5236\u7684\u573a\u666f\u3002\u4f7f\u7528Context\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5e76\u53d1\u8bf7\u6c42\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u548c\u8fc7\u5ea6\u7b49\u5f85\u65f6\u95f4\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go\u8bed\u8a00\u4e2d\uff0cContext\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u4e00\u6b21\u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u5728\u4e0d &hellip; <a href=\"https:\/\/blog.fs.cloudns.biz\/index.php\/2023\/10\/14\/go%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84context\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-455","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/posts\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":4,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":459,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/posts\/455\/revisions\/459"}],"wp:attachment":[{"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fs.cloudns.biz\/index.php\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}