{"id":295950,"date":"2024-05-20T15:49:11","date_gmt":"2024-05-20T07:49:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/295950.html"},"modified":"2024-05-20T15:49:35","modified_gmt":"2024-05-20T07:49:35","slug":"%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3-go-%e8%a8%80%e4%b8%ad%e7%9a%84-context","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295950.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3 Go \u8a00\u4e2d\u7684 Context"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27143343\/570be04c-3adf-48fc-8086-6a653f041a3a.webp\" alt=\"\u5982\u4f55\u7406\u89e3 Go \u8a00\u4e2d\u7684 Context\" \/><\/p>\n<p><p>\u5728Go\u8bed\u8a00\u4e2d\uff0cContext\u7684\u672c\u8d28\u662f\u5728\u8fdb\u7a0b\u6216\u51fd\u6570\u8c03\u7528\u7684\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u4f20\u9012\u4e0a\u4e0b\u6587\u4fe1\u606f\u3001<strong>\u63a7\u5236\u4fe1\u53f7\u3001\u53d6\u6d88\u4fe1\u53f7<\/strong>\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u63a7\u5236\u7a0b\u5e8f\u5185\u90e8\u591a\u534f\u7a0b\u4e4b\u95f4\u7684\u4ea4\u4e92\u95ee\u9898\uff0c\u6210\u4e3a\u5e76\u53d1\u7f16\u7a0b\u4e2d\u5904\u7406\u8d85\u65f6\u3001\u53d6\u6d88\u64cd\u4f5c\u4e0d\u53ef\u6216\u7f3a\u7684\u7ec4\u4ef6\u3002<strong>\u63a7\u5236\u4fe1\u53f7<\/strong>\u662fContext\u6700\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u901a\u8fc7\u5b83\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u5bf9\u67d0\u4e2a\u64cd\u4f5c\u8fdb\u884c\u63a7\u5236\uff0c\u6bd4\u5982\u8d85\u65f6\u53d6\u6d88\u6216\u624b\u52a8\u53d6\u6d88\u67d0\u4e2a\u6b63\u5728\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>Go\u8bed\u8a00\u4e2d\u7684Context\u4e3b\u8981\u63d0\u4f9b\u4e86\u56db\u79cd\u529f\u80fd\uff1a\u8d85\u65f6\u63a7\u5236\u3001\u53d6\u6d88\u4fe1\u53f7\u4f20\u9012\u3001\u503c\u4f20\u9012\u3001\u622a\u6b62\u65f6\u95f4\u8bbe\u7f6e\u3002\u8fd9\u56db\u5927\u529f\u80fd\u6784\u6210\u4e86Context\u7684\u6838\u5fc3\uff0c\u4f7f\u5f97\u5e76\u53d1\u7f16\u7a0b\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u5b89\u5168\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e00\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u8d85\u65f6\u63a7\u5236<\/h2>\n<\/p>\n<p><p>\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u8d85\u65f6\u63a7\u5236\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u673a\u5236\u3002\u901a\u8fc7\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u7a0b\u5e8f\u80fd\u591f\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u672a\u5b8c\u6210\u64cd\u4f5c\u65f6\u81ea\u52a8\u53d6\u6d88\uff0c\u907f\u514d\u65e0\u4f11\u6b62\u5730\u7b49\u5f85\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\u6216\u6b7b\u9501\u3002Go\u8bed\u8a00\u7684Context\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4f18\u96c5\u7684\u8d85\u65f6\u63a7\u5236\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528context\u5305\u4e2d\u7684<code>WithTimeout<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u8d85\u65f6\u65f6\u95f4\u7684Context\u5bf9\u8c61\u3002\u8fd9\u4e2aContext\u5bf9\u8c61\u5728\u8d85\u8fc7\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u4f1a\u81ea\u52a8\u53d1\u51fa\u53d6\u6d88\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">ctx, cancel := context.WithTimeout(parentCtx, 10*time.Second)<\/p>\n<p>defer cancel() \/\/ \u5728\u4e0d\u518d\u9700\u8981\u8fd9\u4e2aContext\u5bf9\u8c61\u65f6\u53d6\u6d88\uff0c\u91ca\u653e\u8d44\u6e90<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\u4e3a10\u79d2\u7684Context\u3002\u5982\u679c10\u79d2\u5185\u64cd\u4f5c\u672a\u5b8c\u6210\uff0c\u8fd9\u4e2aContext\u4f1a\u81ea\u52a8\u89e6\u53d1\u53d6\u6d88\u64cd\u4f5c\uff0c\u4e0e\u6b64\u540c\u65f6\uff0c\u4efb\u4f55\u76d1\u542c\u8fd9\u4e2a\u53d6\u6d88\u4fe1\u53f7\u7684\u534f\u7a0b\u90fd\u5e94\u8be5\u7acb\u5373\u505c\u6b62\u5f53\u524d\u64cd\u4f5c\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u53d6\u6d88\u4fe1\u53f7\u4f20\u9012<\/h2>\n<\/p>\n<p><p>\u53d6\u6d88\u64cd\u4f5c\u662f\u5e76\u53d1\u7a0b\u5e8f\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\u3002Go\u7684Context\u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u53d6\u6d88\u4fe1\u53f7\u4f20\u9012\u529f\u80fd\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u53d6\u6d88\u67d0\u4e2a\u6b63\u5728\u8fdb\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>context.WithCancel<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u53ef\u53d6\u6d88\u7684Context\u3002\u5f53\u8c03\u7528\u8fd4\u56de\u7684<code>cancel<\/code>\u51fd\u6570\u65f6\uff0c\u4e0e\u6b64Context\u5173\u8054\u7684\u6240\u6709\u534f\u7a0b\u90fd\u5e94\u8be5\u63a5\u6536\u5230\u53d6\u6d88\u4fe1\u53f7\uff0c\u5e76\u505c\u6b62\u5f53\u524d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">ctx, cancel := context.WithCancel(parentCtx)<\/p>\n<p>defer cancel() \/\/ \u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u8c03\u7528cancel\u6765\u53d6\u6d88\u4e0ectx\u76f8\u5173\u7684\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u8c03\u7528<code>cancel()<\/code>\u65f6\uff0c\u4e0e<code>ctx<\/code>\u76f8\u5173\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u5e94\u8be5\u5c3d\u5feb\u505c\u6b62\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u503c\u4f20\u9012<\/h2>\n<\/p>\n<p><p>Context\u8fd8\u63d0\u4f9b\u4e86\u8de8\u534f\u7a0b\u4f20\u9012\u503c\u7684\u529f\u80fd\u3002\u901a\u8fc7<code>context.WithValue<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u952e\u503c\u5bf9\u9644\u52a0\u5230Context\u5bf9\u8c61\u4e0a\uff0c\u8fd9\u4e9b\u503c\u5728\u6574\u4e2aContext\u6811\u4e2d\u90fd\u662f\u53ef\u8bbf\u95ee\u7684\uff0c\u975e\u5e38\u9002\u5408\u4f20\u9012\u8bf7\u6c42\u8303\u56f4\u5185\u7684\u6570\u636e\uff0c\u6bd4\u5982\u8bf7\u6c42ID\u3001\u8ba4\u8bc1token\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">ctx := context.WithValue(parentCtx, key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cContext\u4e2d\u7684\u503c\u5e94\u8be5\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff0c\u4e14\u4e0d\u5e94\u8be5\u88ab\u4fee\u6539\u3002<code>WithValue<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u6570\u636e\uff0c\u800c\u4e0d\u662f\u6570\u636e\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u622a\u6b62\u65f6\u95f4\u8bbe\u7f6e<\/h2>\n<\/p>\n<p><p>Context\u8fd8\u80fd\u591f\u8bbe\u7f6e\u622a\u6b62\u65f6\u95f4\uff08Deadline\uff09\uff0c\u8fd9\u4e0e\u8d85\u65f6\u63a7\u5236\u7c7b\u4f3c\uff0c\u4f46\u66f4\u4e3a\u901a\u7528\u3002\u4f7f\u7528<code>context.WithDeadline<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u5177\u4f53\u7684\u65f6\u95f4\u70b9\uff0c\u5982\u679c\u5230\u4e86\u8fd9\u4e2a\u65f6\u95f4\u70b9\u64cd\u4f5c\u8fd8\u672a\u5b8c\u6210\uff0c\u5c31\u4f1a\u81ea\u52a8\u89e6\u53d1\u53d6\u6d88\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">deadline := time.Now().Add(10 * time.Second)<\/p>\n<p>ctx, cancel := context.WithDeadline(parentCtx, deadline)<\/p>\n<p>defer cancel()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u8bbe\u7f6e\u4e86\u4e00\u4e2a10\u79d2\u540e\u7684\u622a\u6b62\u65f6\u95f4\u3002\u5982\u679c10\u79d2\u8fc7\u540e\uff0c\u76f8\u5173\u64cd\u4f5c\u8fd8\u5728\u6267\u884c\uff0cContext\u4f1a\u81ea\u52a8\u53d6\u6d88\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1Context\u7684\u8fd9\u56db\u4e2a\u6838\u5fc3\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728Go\u8bed\u8a00\u4e2d\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u3001\u5b89\u5168\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u9ad8\u5e76\u53d1\u3001\u9700\u8981\u4f18\u96c5\u7ec8\u6b62\u7ebf\u7a0b\u6216\u534f\u7a0b\u7684\u573a\u666f\u4e0b\uff0cContext\u7684\u91cd\u8981\u6027\u4e0d\u8a00\u800c\u55bb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662f Go \u8bed\u8a00\u4e2d\u7684 Context\uff1f<\/strong><\/p>\n<p>Go \u8bed\u8a00\u4e2d\u7684 Context \u662f\u4e00\u79cd\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u4f20\u9012\u8bf7\u6c42\u4f5c\u7528\u57df\u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u5728\u4e0d\u5f15\u5165\u5168\u5c40\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\u5c06\u4e0a\u4e0b\u6587\u503c\u4e0e\u8bf7\u6c42\u5173\u8054\u8d77\u6765\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5728\u6574\u4e2a\u8bf7\u6c42\u5904\u7406\u94fe\u4e0a\u900f\u660e\u5730\u8bbf\u95ee\u5b83\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u4f7f\u7528 Go \u8bed\u8a00\u7684 Context\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 Context \u53ef\u4ee5\u89e3\u51b3\u4ee5\u4e0b\u95ee\u9898\uff1a\u5f53\u4e00\u4e2a\u8bf7\u6c42\u88ab\u53d1\u9001\u5230\u591a\u4e2a goroutine \u65f6\uff0c\u9700\u8981\u5b89\u5168\u5730\u4f20\u9012\u8bf7\u6c42\u76f8\u5173\u7684\u503c\uff1b\u9700\u8981\u9650\u5236\u8bf7\u6c42\u7684\u5904\u7406\u65f6\u95f4\u6216\u53d6\u6d88\u5904\u7406\u8fc7\u7a0b\uff1b\u9700\u8981\u8ddf\u8e2a\u8bf7\u6c42\u7684\u5904\u7406\u8fc7\u7a0b\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u53d6\u6d88\u6216\u8d85\u65f6\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u6b63\u786e\u4f7f\u7528 Go \u8bed\u8a00\u7684 Context\uff1f<\/strong><\/p>\n<p>\u6b63\u786e\u4f7f\u7528 Context \u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u4e2a Context \u5bf9\u8c61\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u9700\u8981\u8bbf\u95ee\u4e0a\u4e0b\u6587\u7684 goroutine\uff1b\u5728\u51fd\u6570\u4e4b\u95f4\u4f20\u9012 Context\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u4f20\u9012\u8bf7\u6c42\u76f8\u5173\u7684\u503c\uff1b\u5728\u9700\u8981\u7684\u65f6\u5019\u68c0\u67e5 Context \u662f\u5426\u5df2\u88ab\u53d6\u6d88\u6216\u8d85\u65f6\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff1b\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684 Context \u8d44\u6e90\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5728\u4f7f\u7528 Context \u65f6\uff0c\u5e94\u8be5\u9075\u5faa\u539f\u5219\u201c\u5c06 Context \u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\uff0c\u800c\u4e0d\u8981\u5c06\u5176\u4fdd\u5b58\u5728\u7ed3\u6784\u4f53\u4e2d\u201d\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u51fd\u6570\u90fd\u53ea\u80fd\u8bbf\u95ee\u81ea\u5df1\u9700\u8981\u7684\u4e0a\u4e0b\u6587\u503c\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u51fd\u6570\u7684\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Go\u8bed\u8a00\u4e2d\uff0cContext\u7684\u672c\u8d28\u662f\u5728\u8fdb\u7a0b\u6216\u51fd\u6570\u8c03\u7528\u7684\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u4f20\u9012\u4e0a\u4e0b\u6587\u4fe1\u606f\u3001\u63a7\u5236\u4fe1\u53f7\u3001\u53d6\u6d88\u4fe1\u53f7\uff0c\u5b83\u4e3b\u8981\u7528 [&hellip;]","protected":false},"author":3,"featured_media":295975,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295950"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=295950"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295950\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295975"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}