{"id":205564,"date":"2025-06-12T11:14:30","date_gmt":"2025-06-12T03:14:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205564\/"},"modified":"2025-06-12T11:14:30","modified_gmt":"2025-06-12T03:14:30","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e4%b8%ad%e4%bd%bf%e7%94%a8-context-%e5%ae%9e%e7%8e%b0%e5%87%bd%e6%95%b0%e5%9b%9e%e8%b0%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205564\/","title":{"rendered":"\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~,Context \u7b80\u5316\u4e86\u5e76\u53d1\u56de\u8c03\u51fd\u6570\u7684\u7ba1\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u6765\u68c0\u6d4b\u53d6\u6d88\u548c\u5b58\u50a8\u503c\uff1aDeadline() \u8fd4\u56de\u5269\u4f59\u65f6\u95f4\uff0cDone() \u8fd4\u56de\u5173\u95ed\u4fe1\u9053\uff0cErr() \u8fd4\u56de\u53d6\u6d88\u539f\u56e0\uff0cValue() \u4f7f\u7528\u952e\u68c0\u7d22\u503c\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924235671c8c8b79b2f.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 GOLANG\u4e2d\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/strong>,<br \/>\n<strong>\u80cc\u666f<\/strong>,\u5728 GOLANG \u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u51fd\u6570\u5b8c\u6210\u6267\u884c\u540e\u6267\u884c\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ba1\u7406\u56de\u8c03\u7684\u6267\u884c\u987a\u5e8f\u548c\u53d6\u6d88\u53d8\u5f97\u5f88\u68d8\u624b\u3002 Context \u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u673a\u5236\u6765\u5904\u7406\u6b64\u7c7b\u573a\u666f\u3002,<br \/>\n<strong>Context<\/strong>,Context \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e86\u5982\u4e0b\u65b9\u6cd5\uff1a,<br \/>\n<strong>\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/strong>,\u8981\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5f02\u6b65 HTTP \u8bf7\u6c42<\/strong>,\u6211\u4eec\u53ef\u4ee5\u5728\u5f02\u6b65 HTTP \u8bf7\u6c42\u4e2d\u4f7f\u7528 Context \u6765\u7ba1\u7406\u56de\u8c03\u3002\u4f8b\u5982\uff1a,\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Context\uff0c\u5e76\u5728\u8bf7\u6c42\u6267\u884c\u540e\u5c06\u5176\u4f20\u9012\u7ed9 HTTP \u5ba2\u6237\u7aef\u3002\u5982\u679c\u5728\u8bf7\u6c42\u5904\u7406\u671f\u95f4 Context \u88ab\u53d6\u6d88\uff0c\u5219\u8bf7\u6c42\u5c06\u88ab\u653e\u5f03\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<br \/>\n<code>context.Canceled<\/code> \u9519\u8bef\u3002,<br \/>\n<strong>\u7ed3\u8bba<\/strong>,\u4f7f\u7528 Context \u53ef\u4ee5\u7b80\u5316\u5e76\u53d1\u56de\u8c03\u51fd\u6570\u7684\u7ba1\u7406\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u6784\u5efa\u53ef\u9760\u4e14\u54cd\u5e94\u8fc5\u901f\u7684\u5e94\u7528\u7a0b\u5e8f\u3002,\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,<br \/>\n<code>Done()<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a\u4fe1\u9053\uff0c\u5f53Context\u88ab\u53d6\u6d88\u65f6\uff0c\u8be5\u4fe1\u9053\u5c06\u5173\u95ed\u3002,<br \/>\n<code>Deadline()<\/code>\uff1a\u8fd4\u56de\u5230\u622a\u6b62\u65e5\u671f\u5269\u4f59\u7684\u65f6\u95f4\u3002,<br \/>\n<code>Err()<\/code>\uff1a\u8fd4\u56deContext\u88ab\u53d6\u6d88\u7684\u539f\u56e0\u3002,\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~,<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>\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/span><\/p>\n<h1>\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/h1>\n<p><span>2024-10-26 14:30:50<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>Context \u7b80\u5316\u4e86\u5e76\u53d1\u56de\u8c03\u51fd\u6570\u7684\u7ba1\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u6765\u68c0\u6d4b\u53d6\u6d88\u548c\u5b58\u50a8\u503c\uff1aDeadline() \u8fd4\u56de\u5269\u4f59\u65f6\u95f4\uff0cDone() \u8fd4\u56de\u5173\u95ed\u4fe1\u9053\uff0cErr() \u8fd4\u56de\u53d6\u6d88\u539f\u56e0\uff0cValue() \u4f7f\u7528\u952e\u68c0\u7d22\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924235671c8c8b79b2f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 GOLANG\u4e2d\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/strong><\/p>\n<p><strong>\u80cc\u666f<\/strong><\/p>\n<p>\u5728 GOLANG \u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u51fd\u6570\u5b8c\u6210\u6267\u884c\u540e\u6267\u884c\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u5e76\u53d1\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ba1\u7406\u56de\u8c03\u7684\u6267\u884c\u987a\u5e8f\u548c\u53d6\u6d88\u53d8\u5f97\u5f88\u68d8\u624b\u3002 Context \u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u673a\u5236\u6765\u5904\u7406\u6b64\u7c7b\u573a\u666f\u3002<\/p>\n<p><strong>Context<\/strong><\/p>\n<p>Context \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e86\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>Deadline()<\/code>\uff1a\u8fd4\u56de\u5230\u622a\u6b62\u65e5\u671f\u5269\u4f59\u7684\u65f6\u95f4\u3002<\/li>\n<li><code>Done()<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a\u4fe1\u9053\uff0c\u5f53Context\u88ab\u53d6\u6d88\u65f6\uff0c\u8be5\u4fe1\u9053\u5c06\u5173\u95ed\u3002<\/li>\n<li><code>Err()<\/code>\uff1a\u8fd4\u56deContext\u88ab\u53d6\u6d88\u7684\u539f\u56e0\u3002<\/li>\n<li><code>Value(key interface{}) interface{}<\/code>\uff1a\u4f7f\u7528\u7ed9\u5b9a\u952e\u68c0\u7d22Context \u4e2d\u5b58\u50a8\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 Context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u8c03\u7528\u51fd\u6570\u4e4b\u524d\uff0c\u521b\u5efa\u4e00\u4e2a Context\uff1a<\/li>\n<\/ol>\n<pre>ctx := context.Background()<\/pre>\n<ol>\n<li>\u5c06 Context \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<pre>func myFunction(ctx context.Context) {\n    \/\/ ...\n}<\/pre>\n<ol>\n<li>\u5728\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 Context \u4e2d\u7684\u65b9\u6cd5\u6765\u68c0\u6d4b\u53d6\u6d88\uff1a<\/li>\n<\/ol>\n<pre>select {\ncase &lt;-ctx.Done():\n    \/\/ \u53d6\u6d88\u5df2\u88ab\u89e6\u53d1\uff0c\u8fd4\u56de\n}<\/pre>\n<ol>\n<li>\u5728\u9700\u8981\u65f6\uff0c\u5c06\u503c\u5b58\u50a8\u5728 Context \u4e2d\uff1a<\/li>\n<\/ol>\n<pre>newCtx := context.WithValue(ctx, \"key\", \"value\")<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u5f02\u6b65 HTTP \u8bf7\u6c42<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u5f02\u6b65 HTTP \u8bf7\u6c42\u4e2d\u4f7f\u7528 Context \u6765\u7ba1\u7406\u56de\u8c03\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>func asynchronousHTTPClient(ctx context.Context, url string) (*http.Response, error) {\n    httpClient := &amp;http.Client{}\n    req, _ := http.NewRequest(http.MethodGet, url, nil)\n    req = req.WithContext(ctx)\n\n    resp, err := httpClient.Do(req)\n    if err != nil {\n        return nil, err\n    }\n    return resp, nil\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Context\uff0c\u5e76\u5728\u8bf7\u6c42\u6267\u884c\u540e\u5c06\u5176\u4f20\u9012\u7ed9 HTTP \u5ba2\u6237\u7aef\u3002\u5982\u679c\u5728\u8bf7\u6c42\u5904\u7406\u671f\u95f4 Context \u88ab\u53d6\u6d88\uff0c\u5219\u8bf7\u6c42\u5c06\u88ab\u653e\u5f03\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a <code>context.Canceled<\/code> \u9519\u8bef\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528 Context \u53ef\u4ee5\u7b80\u5316\u5e76\u53d1\u56de\u8c03\u51fd\u6570\u7684\u7ba1\u7406\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u6784\u5efa\u53ef\u9760\u4e14\u54cd\u5e94\u8fc5\u901f\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 GOLANG \u4e2d\u4f7f\u7528 context \u5b9e\u73b0\u51fd\u6570\u56de\u8c03\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   golang\u6846\u67b6\u6570\u636e\u5e93\u8bbf\u95ee\u4e0e\u6301\u4e45\u5316\u6a21\u5f0f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   ## \u767e\u4e07\u7ea7 MySQL \u6570\u636e\u5982\u4f55\u5b9e\u73b0\u9ad8\u6548\u7684\u6a21\u7cca\u641c\u7d22\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404&#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-205564","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205564","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=205564"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205564\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}