{"id":10471,"date":"2022-11-02T14:08:43","date_gmt":"2022-11-02T06:08:43","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=10471"},"modified":"2022-11-04T17:36:30","modified_gmt":"2022-11-04T09:36:30","slug":"context","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/11\/02\/context\/","title":{"rendered":"go\u8bed\u8a00\u4e4bcontext"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">context\u4e3ago\u81ea\u5e26\u7684\u5305\uff0c\u5728\u5b89\u88c5\u8def\u5f84\u4e0b\u7684src\/context\u6587\u4ef6\u5939\u4e2d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Go http\u5305\u7684Server\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u5728\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684 goroutine \u53bb\u5904\u7406\u3002\u8bf7\u6c42\u5904\u7406\u51fd\u6570\u901a\u5e38\u4f1a\u542f\u52a8\u989d\u5916\u7684 goroutine \u7528\u6765\u8bbf\u95ee\u540e\u7aef\u670d\u52a1\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u548cRPC\u670d\u52a1\u3002\u7528\u6765\u5904\u7406\u4e00\u4e2a\u8bf7\u6c42\u7684 goroutine \u901a\u5e38\u9700\u8981\u8bbf\u95ee\u4e00\u4e9b\u4e0e\u8bf7\u6c42\u7279\u5b9a\u7684\u6570\u636e\uff0c\u6bd4\u5982\u7ec8\u7aef\u7528\u6237\u7684\u8eab\u4efd\u8ba4\u8bc1\u4fe1\u606f\u3001\u9a8c\u8bc1\u76f8\u5173\u7684token\u3001\u8bf7\u6c42\u7684\u622a\u6b62\u65f6\u95f4\u3002 \u5f53\u4e00\u4e2a\u8bf7\u6c42\u88ab\u53d6\u6d88\u6216\u8d85\u65f6\u65f6\uff0c\u6240\u6709\u7528\u6765\u5904\u7406\u8be5\u8bf7\u6c42\u7684 goroutine \u90fd\u5e94\u8be5\u8fc5\u901f\u9000\u51fa\uff0c\u7136\u540e\u7cfb\u7edf\u624d\u80fd\u91ca\u653e\u8fd9\u4e9b goroutine \u5360\u7528\u7684\u8d44\u6e90\uff0c\u56e0\u6b64,&nbsp;Context \u4e13\u95e8\u7528\u6765\u7b80\u5316 \u5bf9\u4e8e\u5904\u7406\u5355\u4e2a\u8bf7\u6c42\u7684\u591a\u4e2a goroutine \u4e4b\u95f4\u4e0e\u8bf7\u6c42\u57df\u7684\u6570\u636e\u3001\u53d6\u6d88\u4fe1\u53f7\u3001\u622a\u6b62\u65f6\u95f4\u7b49\u76f8\u5173\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u80fd\u6d89\u53ca\u591a\u4e2a API \u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Context \u53c8\u88ab\u79f0\u4e3a\u4e0a\u4e0b\u6587\uff0c\u4e0e WaitGroup \u4e0d\u540c\u7684\u662f\uff0ccontext \u5bf9\u4e8e goroutine \u6709\u66f4\u5f3a\u7684\u63a7\u5236\u529b\uff0c\u53ef\u4ee5\u7ba1\u7406\u591a\u7ea7\u7684 goroutine\uff0c\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5f53\u524d\u534f\u7a0b\u53d6\u6d88\u4e86\uff0c\u53ef\u4ee5\u901a\u77e5\u6240\u6709\u7531\u5b83\u521b\u5efa\u7684\u5b50\u534f\u7a0b\u9000\u51fa<\/li><li>\u5f53\u524d\u534f\u7a0b\u53d6\u6d88\u4e86\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5230\u521b\u5efa\u5b83\u7684\u7236\u7ea7\u534f\u7a0b\u7684\u72b6\u6001<\/li><li>\u6269\u5c55\u4e86\u989d\u5916\u7684\u529f\u80fd\uff1a\u8d85\u65f6\u53d6\u6d88\u3001\u5b9a\u65f6\u53d6\u6d88\u3001\u53ef\u4ee5\u548c\u5b50\u534f\u7a0b\u5171\u4eab\u6570\u636e<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u3001Context\u63a7\u5236goroutine<\/strong>(\u53d6\u6d88)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u901a\u8fc7context\u63a7\u5236\u5355\u4e2agoroutine\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"585\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-2.png\" alt=\"\" class=\"wp-image-10515\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-2-300x248.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-2-600x495.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c22\u884c\uff1acontext.Background() \u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684Context\uff0c\u8fd9\u4e2a\u7a7a\u7684Context\u4e00\u822c\u4f5c\u4e3a\u6574\u4e2aContext\u6811\u7684\u6839\u8282\u70b9\uff0c\u7136\u540e\u4f7f\u7528context.WithCancel(parent)\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u53ef\u53d6\u6d88\u7684\u5b50Context\uff0c\u7136\u540e\u5f53\u4f5c\u53c2\u6570\u4f20\u7ed9goroutine\u4f7f\u7528\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5b50Context\u8ddf\u8e2a\u8fd9\u4e2agoroutine<\/li><li>\u7b2c23\u884c\uff1a\u5e76\u53d1\u6267\u884ctest\u51fd\u6570\uff0c\u51fd\u6570\u53c2\u6570\u4e3aContext\u63a5\u53e3\uff0c\u51fd\u6570\u4e2d\u901a\u8fc7for\u6267\u884c\u65e0\u9650\u5faa\u73af\uff0c\u5185\u90e8\u901a\u8fc7select\u5224\u65ad\u662f\u5426\u8981\u7ed3\u675f\uff0c\u5982\u679cctx.Done()\u63a5\u6536\u5230\u4e86\u503c\uff0c\u8bf4\u660egoroutine\u7ed3\u675f\uff0c\u5982\u679c\u63a5\u6536\u4e0d\u5230\uff0c\u7ee7\u7eed\u8fd0\u884c<\/li><li>\u7b2c24\u884c\uff1a\u4f11\u77206\u79d2\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3atest\u51fd\u6570\u8fd0\u884c\u4e866s\uff0c\u7136\u540e\u8c03\u7528cancel\u51fd\u6570\u505c\u6b62goroutine<\/li><li>\u7b2c25\u884c\uff1a\u6267\u884c\u53d6\u6d88\u51fd\u6570\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3ago test\u6267\u884c6s\u4e2d\u540e\u518d\u6267\u884ccancel()\uff0c\u5b83\u662fCancelFunc\u7c7b\u578b\u7684\uff0c\u4e5f\u662fWithCancel\u8fd4\u56de\u503c\uff0c\u901a\u8fc7\u8c03\u7528\u5b83\u5c31\u53ef\u4ee5\u53d1\u51fa\u53d6\u6d88\u6307\u4ee4<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"199\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-3.png\" alt=\"\" class=\"wp-image-10516\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-3-300x84.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-3-600x168.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e0d\u901a\u8fc7cancel\u624b\u52a8\u6765\u505c\u6b62goroutine\uff0c\u6211\u4eec\u770b\u4e0b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff0c\u628acancel\u6ce8\u91ca\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"601\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-3.png\" alt=\"\" class=\"wp-image-10519\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-3-300x254.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/1-3-600x509.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"240\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-4.png\" alt=\"\" class=\"wp-image-10520\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-4.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-4-300x102.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/2-4-600x203.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u4e0d\u901a\u8fc7cancel\u624b\u52a8\u53d6\u6d88goroutine\uff0c\u90a3\u4e48go test\u4f1a\u6301\u7eed\u8fd0\u884c\u5230\u4e3b\u51fd\u6570main\u81ea\u5e26\u7684goroutine\u7ed3\u675f\u540e\u4e00\u8d77\u9000\u51fa<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4e0b\u9762\u4f8b\u5b50\u901a\u8fc7context\u63a7\u5236\u591a\u4e2agoroutine\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"594\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6.png\" alt=\"\" class=\"wp-image-10480\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-300x251.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/6-600x502.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"454\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7.png\" alt=\"\" class=\"wp-image-10482\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-300x192.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/7-600x385.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4f8b\u5b50\u4e2d\u542f\u52a8\u4e863\u4e2agoroutine\uff0c\u6bcf\u4e00\u4e2a\u90fd\u4f7f\u7528\u4e86Context\u8fdb\u884c\u8ddf\u8e2a\uff0c\u5f53\u6211\u4eec\u4f7f\u7528cancel\u51fd\u6570\u901a\u77e5\u53d6\u6d88\u65f6\uff0c\u8fd93\u4e2agoroutine\u90fd\u4f1a\u88ab\u7ed3\u675f\uff0cContext\u5c31\u50cf\u4e00\u4e2a\u63a7\u5236\u5668\u4e00\u6837\uff0c\u6309\u4e0b\u53d6\u6d88\u5f00\u5173\u540e\uff0c\u57fa\u4e8e\u8fd9\u4e2aContext\u6216\u8005\u884d\u751f\u7684\u5b50Context\u90fd\u4f1a\u6536\u5230\u901a\u77e5\uff0c\u8fdb\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u6700\u7ec8\u91ca\u653egoroutine\uff0c\u4ece\u800c\u89e3\u51b3\u4e86goroutine\u542f\u52a8\u540e\u4e0d\u53ef\u63a7\u4ee5\u53ca\u5360\u7528\u8d44\u6e90\u7684\u95ee\u9898<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8c\u3001Context\u63a5\u53e3(go\u7248\u672cv1.18.1)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Context\u7684\u63a5\u53e3\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Context interface {\n  Deadline() (deadline time.Time, ok bool)\n\n  Done() &lt;-chan struct{}\n\n  Err() error\n\n  Value(key any) any\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Deadline\u65b9\u6cd5\u662f\u83b7\u53d6\u8bbe\u7f6e\u7684\u622a\u6b62\u65f6\u95f4\u7684\u610f\u601d\uff0c\u7b2c\u4e00\u4e2a\u8fd4\u56de\u5f0f\u662f\u622a\u6b62\u65f6\u95f4\uff0c\u5230\u4e86\u8fd9\u4e2a\u65f6\u95f4\u70b9\uff0cContext\u4f1a\u81ea\u52a8\u53d1\u8d77\u53d6\u6d88\u8bf7\u6c42\uff1b\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u503cok==false\u65f6\u8868\u793a\u6ca1\u6709\u8bbe\u7f6e\u622a\u6b62\u65f6\u95f4\uff0c\u5982\u679c\u9700\u8981\u53d6\u6d88\u7684\u8bdd\uff0c\u9700\u8981\u8c03\u7528\u53d6\u6d88\u51fd\u6570\u8fdb\u884c\u53d6\u6d88<\/li><li>Done\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u53ea\u8bfb\u7684chan\uff0c\u7c7b\u578b\u4e3astruct{}\uff0c\u6211\u4eec\u5728goroutine\u4e2d\uff0c\u5982\u679c\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684chan\u53ef\u4ee5\u8bfb\u53d6\uff0c\u5219\u610f\u5473\u7740parent context\u5df2\u7ecf\u53d1\u8d77\u4e86\u53d6\u6d88\u8bf7\u6c42\uff0c\u6211\u4eec\u901a\u8fc7Done\u65b9\u6cd5\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u540e\uff0c\u5c31\u5e94\u8be5\u505a\u6e05\u7406\u64cd\u4f5c\uff0c\u7136\u540e\u9000\u51fagoroutine\uff0c\u91ca\u653e\u8d44\u6e90<\/li><li>Err\u65b9\u6cd5\u8fd4\u56de\u53d6\u6d88\u7684\u9519\u8bef\u539f\u56e0\uff0c\u56e0\u4e3a\u4ec0\u4e48Context\u88ab\u53d6\u6d88<\/li><li>Value\u65b9\u6cd5\u83b7\u53d6\u8be5Context\u4e0a\u7ed1\u5b9a\u7684\u503c\uff0c\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u6240\u4ee5\u8981\u901a\u8fc7\u4e00\u4e2aKey\u624d\u53ef\u4ee5\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u4e00\u822c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u65b9\u6cd5\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662fDone()\uff0c\u5982\u679cContext\u53d6\u6d88\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5173\u95ed\u7684chan\uff0c\u5173\u95ed\u7684chan\u662f\u53ef\u4ee5\u8bfb\u53d6\u7684\uff0c\u6240\u4ee5\u53ea\u8981\u53ef\u4ee5\u8bfb\u53d6\u7684\u65f6\u5019\uff0c\u5c31\u610f\u5473\u7740\u6536\u5230Context\u53d6\u6d88\u7684\u4fe1\u53f7\u4e86\uff0c\u7528\u6cd5\u53ef\u53c2\u8003\u4e0a\u9762\u4f8b\u5b50<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Context\u63a5\u53e3\u5e76\u4e0d\u9700\u8981\u6211\u4eec\u5b9e\u73b0\uff0cGo\u5185\u7f6e\u5df2\u7ecf\u5e2e\u6211\u4eec\u5b9e\u73b0\u4e862\u4e2a(\u5b89\u88c5\u8def\u5f84\u4e0b\/src\/context\/context.go\u6587\u4ef6)\uff0c\u4ee3\u7801\u4e2d\u6700\u5f00\u59cb\u90fd\u662f\u4ee5\u8fd9\u4e24\u4e2a\u5185\u7f6e\u7684\u4f5c\u4e3a\u6700\u9876\u5c42\u7684partent context\uff0c\u884d\u751f\u51fa\u66f4\u591a\u7684\u5b50Context\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"407\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-1.png\" alt=\"\" class=\"wp-image-10504\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-1.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-1-300x172.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-1-600x344.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>emptyCtx\u5b9e\u73b0\u4e86Context\u4e2d\u7684\u56db\u4e2a\u65b9\u6cd5\uff0c\u56e0\u6b64\u5b9e\u73b0\u4e86Context\u63a5\u53e3<\/li><li>Background\uff0c\u4e3b\u8981\u7528\u4e8emain\u51fd\u6570\u3001\u521d\u59cb\u5316\u4ee5\u53ca\u6d4b\u8bd5\u4ee3\u7801\u4e2d\uff0c\u4f5c\u4e3aContext\u8fd9\u4e2a\u6811\u7ed3\u6784\u7684\u6700\u9876\u5c42\u7684Context\uff0c\u4e5f\u5c31\u662f\u6839Context<\/li><li>TODO,\u5b83\u76ee\u524d\u8fd8\u4e0d\u77e5\u9053\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u77e5\u9053\u8be5\u4f7f\u7528\u4ec0\u4e48Context\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u4e2a\u672c\u8d28\u4e0a\u90fd\u662femptyCtx\u7ed3\u6784\u4f53\u7c7b\u578b\uff0c\u662f\u4e00\u4e2a\u4e0d\u53ef\u53d6\u6d88\uff0c\u6ca1\u6709\u8bbe\u7f6e\u622a\u6b62\u65f6\u95f4\uff0c\u6ca1\u6709\u643a\u5e26\u4efb\u4f55\u503c\u7684Context<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662femptyCtx\u5b9e\u73b0Context\u63a5\u53e3\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u8fd4\u56de\u90fd\u662fnil\u6216\u8005\u96f6\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"364\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-1.png\" alt=\"\" class=\"wp-image-10506\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-1.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-1-300x154.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-1-600x308.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e09\u3001Context\u7684\u7ee7\u627f\u884d\u751f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e86\u4e0a\u9762\u7684\u6839Context\uff0c\u5982\u4f55\u884d\u751f\u66f4\u591a\u7684\u5b50Context\uff1f\u6b64\u65f6\u9700\u8981\u4f9d\u9760context\u4e2d\u7684with\u7cfb\u5217\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func WithCancel(parent Context) (ctx Context, cancel CancelFunc)\nfunc WithDeadline(parent Context, d time.Time) (Context, CancelFunc)\nfunc WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)\nfunc WithValue(parent Context, key, val any) Context<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>WithCancel\uff1a\u4f20\u9012\u4e00\u4e2a\u7236Context\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u5b50Context\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53d6\u6d88\u51fd\u6570\u7528\u6765\u53d6\u6d88Context<\/li><li>WithDeadline\uff1a\u548cWithCancel\u5dee\u4e0d\u591a\uff0c\u5b83\u4f1a\u591a\u4f20\u9012\u4e00\u4e2a\u622a\u6b62\u65f6\u95f4\u53c2\u6570\uff0c\u610f\u5473\u7740\u5230\u4e86\u8fd9\u4e2a\u65f6\u95f4\u70b9\uff0c\u4f1a\u81ea\u52a8\u53d6\u6d88Context\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e0d\u7b49\u5230\u8fd9\u4e2a\u65f6\u5019\uff0c\u63d0\u524d\u901a\u8fc7\u53d6\u6d88\u51fd\u6570\u8fdb\u884c\u53d6\u6d88<\/li><li>WithTimeout\uff1a\u548cWithDeadline\u57fa\u672c\u4e0a\u4e00\u6837\uff0c\u8fd9\u4e2a\u8868\u793a\u662f\u8d85\u65f6\u81ea\u52a8\u53d6\u6d88\uff0c\u662f\u591a\u5c11\u65f6\u95f4\u540e\u81ea\u52a8\u53d6\u6d88Context\u7684\u610f\u601d<\/li><li>WithValue\uff1a\u548c\u53d6\u6d88Context\u65e0\u5173\uff0c\u5b83\u662f\u4e3a\u4e86\u751f\u6210\u4e00\u4e2a\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u952e\u503c\u5bf9\u6570\u636e\u7684Context\uff0c\u8fd9\u4e2a\u7ed1\u5b9a\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7Context.Value\u65b9\u6cd5\u8bbf\u95ee\u5230<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e0a\u9762\u8fd9\u4e9b\u51fd\u6570\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u9897Context\u6811\uff0c\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u53ef\u4ee5\u6709\u4efb\u610f\u591a\u4e2a\u5b50\u8282\u70b9\uff0c\u8282\u70b9\u5c42\u7ea7\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4e0a\u9762\u56db\u4e2awith\u51fd\u6570\uff0c\u7b2c\u4e00\u4e2a\u63a5\u6536\u53c2\u6570\u90fd\u662fpartent\u53c2\u6570\uff0c\u4e5f\u5c31\u662f\u7236Context\uff0c\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u7236Context\u521b\u5efa\u51fa\u5b50Context\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a\u57fa\u4e8e\u7236Context\u7684\u884d\u751f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u4e09\u4e2a\u51fd\u6570\u90fd\u8fd4\u56de\u4e00\u4e2a\u53d6\u6d88\u51fd\u6570CancelFunc\uff0c\u8fd9\u662f\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type CancelFunc func()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u53d6\u6d88\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u53d6\u6d88\u4e00\u4e2aContext\uff0c\u4ee5\u53ca\u8fd9\u4e2a\u8282\u70b9Context\u4e0b\u6240\u6709\u7684\u6240\u6709\u7684Context\uff0c\u4e0d\u7ba1\u6709\u591a\u5c11\u5c42\u7ea7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u56db\u3001\u901a\u8fc7WithValue\u4f20\u9012\u5143\u6570\u636e<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"667\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-2.png\" alt=\"\" class=\"wp-image-10523\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-2-300x282.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/3-2-600x564.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>22\u884c\uff1a\u5b9a\u4e49key\u7684\u540d\u5b57\u4e3aname\uff0ckey\u4e3a\u81ea\u5b9a\u4e49\u7684\u503c\uff0c\u5199\u6210key100\u4e5f\u53ef\u4ee5<\/li><li>26\u884c\uff1a\u901a\u8fc7WithValue\u6dfb\u52a0K-V\u952e\u503c\u5bf9<\/li><li>16\u884c\uff1a\u901a\u8fc7Value()\u65b9\u6cd5\u8bfb\u53d6key\u7684\u503c<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"196\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-2.png\" alt=\"\" class=\"wp-image-10524\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-2-300x83.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/4-2-600x166.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u4f7f\u7528WithValue\u4f20\u503c\uff0c\u4e00\u822c\u662f\u5fc5\u987b\u7684\u503c\uff0c\u4e0d\u8981\u4ec0\u4e48\u503c\u90fd\u4f20\u9012<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Context \u4f7f\u7528\u539f\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e0d\u8981\u628aContext\u653e\u5728\u7ed3\u6784\u4f53\u4e2d\uff0c\u8981\u4ee5\u53c2\u6570\u7684\u65b9\u5f0f\u4f20\u9012<\/li><li>\u4ee5Context\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u65b9\u6cd5\uff0c\u5e94\u8be5\u628aContext\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u653e\u5728\u7b2c\u4e00\u4f4d<\/li><li>\u7ed9\u4e00\u4e2a\u51fd\u6570\u65b9\u6cd5\u4f20\u9012Context\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u4f20\u9012nil\uff0c\u5982\u679c\u4e0d\u77e5\u9053\u4f20\u9012\u4ec0\u4e48\uff0c\u5c31\u4f7f\u7528context.TODO<\/li><li>Context\u7684Value\u76f8\u5173\u65b9\u6cd5\u5e94\u8be5\u4f20\u9012\u5fc5\u987b\u7684\u6570\u636e\uff0c\u4e0d\u8981\u4ec0\u4e48\u6570\u636e\u90fd\u4f7f\u7528\u8fd9\u4e2a\u4f20\u9012<\/li><li>Context\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u53ef\u4ee5\u653e\u5fc3\u7684\u5728\u591a\u4e2agoroutine\u4e2d\u4f20\u9012<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e94\u3001Context\u5b9e\u73b0\u8d85\u65f6\u529f\u80fd\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u521b\u5efa\u5b50\u8282\u70b9\u7684context\uff0c5s\u540e\u7ed3\u675f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"669\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-2.png\" alt=\"\" class=\"wp-image-10562\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-2-300x283.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/10-2-600x566.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>25\u884c\uff1a\u521b\u5efa\u8fc7\u671f\u65f6\u95f4\u4e3a5s<\/li><li>26\u884c\uff1a\u6839\u636e\u6839\u8282\u70b9context\u521b\u5efa\u5b50\u8282\u70b9context\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5s<\/li><li>27\uff1a\u6267\u884c\u5e76\u53d1\u7a0b\u5e8f\uff0c\u901a\u8fc7ctx\u8ddf\u8e2a\uff0c\u56e0\u6b64\u6267\u884c5s\u540e\u5c06\u6536\u5230\u53d6\u6d88\u901a\u77e5<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"181\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-2.png\" alt=\"\" class=\"wp-image-10563\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-2-300x77.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/11-2-600x153.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u5e76\u53d1\u7a0b\u5e8f\u8fd0\u884c5s\u540e\u6536\u5230\u4e86\u53d6\u6d88\u901a\u77e5\uff0c\u56e0\u6b64\u6267\u884cctx.Done()\u5bf9\u5e94\u7684case\u8bed\u53e5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>context\u4e3ago\u81ea\u5e26\u7684\u5305\uff0c\u5728\u5b89\u88c5\u8def\u5f84\u4e0b\u7684src\/context\u6587\u4ef6\u5939\u4e2d \u5728 Go http\u5305\u7684Server [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":10472,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-10471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=10471"}],"version-history":[{"count":30,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10471\/revisions"}],"predecessor-version":[{"id":10565,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10471\/revisions\/10565"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/10472"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=10471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=10471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=10471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}