{"id":205798,"date":"2025-06-12T08:56:03","date_gmt":"2025-06-12T00:56:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205798\/"},"modified":"2025-06-12T08:56:03","modified_gmt":"2025-06-12T00:56:03","slug":"golang-%e5%87%bd%e6%95%b0%ef%bc%9a%e7%94%a8-context-withtimeout-%e5%88%9b%e5%bb%ba%e5%b8%a6%e8%b6%85%e6%97%b6%e7%9a%84%e5%ad%90%e4%b8%8a%e4%b8%8b%e6%96%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205798\/","title":{"rendered":"Golang \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587"},"content":{"rendered":"<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300aGOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002,context.WithTimeout() \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\uff0c\u5b50\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u7236\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5b50\u4e0a\u4e0b\u6587\u4e5f\u4f1a\u88ab\u53d6\u6d88\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a\u521b\u5efa\u7236\u4e0a\u4e0b\u6587\u3002\u8c03\u7528 context.WithTimeout(parent, timeout) \u521b\u5efa\u5b50\u4e0a\u4e0b\u6587\uff0c\u5176\u4e2d parent \u4e3a\u7236\u4e0a\u4e0b\u6587\uff0ctimeout \u4e3a\u5b50\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u8c03\u7528\u5b50\u4e0a\u4e0b\u6587\u7684 CancelFunc \u53d6\u6d88\u5b50\u4e0a\u4e0b\u6587\uff0c\u65e0\u8bba\u662f\u5426\u8d85\u65f6\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730002618671dbebacb907.jpg\" class=\"aligncenter\">,<br \/>\n<strong>GOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587<\/strong>,<br \/>\n<code>context.WithTimeout<\/code> \u51fd\u6570\u5728 Go \u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u4e0a\u4e0b\u6587\uff0c\u5e76\u4e3a\u5176\u8bbe\u5b9a\u4e00\u4e2a\u8d85\u65f6\u3002\u5f53\u5b50\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u7236\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5b50\u4e0a\u4e0b\u6587\u4e5f\u4f1a\u88ab\u53d6\u6d88\u3002,<br \/>\n<strong>\u8bed\u6cd5\uff1a<\/strong>,\u5176\u4e2d\uff1a,<br \/>\n<strong>\u8fd4\u56de\uff1a<\/strong>,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong>,\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528<br \/>\n<code>context.WithTimeout<\/code> \u51fd\u6570\uff1a,<br \/>\n<strong>\u6ce8\u610f\uff1a<\/strong>,\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aGOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8econtext,timeout\u7684\u77e5\u8bc6\u70b9\uff01,<br \/>\n<code>CancelFunc<\/code>\uff1a\u53d6\u6d88\u5b50\u4e0a\u4e0b\u6587\u7684\u51fd\u6570,<br \/>\n<code>Context<\/code>\uff1a\u65b0\u7684\u5b50\u4e0a\u4e0b\u6587,<br \/>\n<code>timeout<\/code>\uff1a\u5b50\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u65f6\u95f4,<br \/>\n<code>parent<\/code>\uff1a\u7236\u4e0a\u4e0b\u6587,\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300aGOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002,<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>GOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587<\/h1>\n<p><span>2024-10-27 12:17:13<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929GOLANG\u5c31\u6574\u7406\u5206\u4eab\u300aGOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9GOLANG\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7GOLANG\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>context.WithTimeout() \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\uff0c\u5b50\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u7236\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5b50\u4e0a\u4e0b\u6587\u4e5f\u4f1a\u88ab\u53d6\u6d88\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a\u521b\u5efa\u7236\u4e0a\u4e0b\u6587\u3002\u8c03\u7528 context.WithTimeout(parent, timeout) \u521b\u5efa\u5b50\u4e0a\u4e0b\u6587\uff0c\u5176\u4e2d parent \u4e3a\u7236\u4e0a\u4e0b\u6587\uff0ctimeout \u4e3a\u5b50\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u8c03\u7528\u5b50\u4e0a\u4e0b\u6587\u7684 CancelFunc \u53d6\u6d88\u5b50\u4e0a\u4e0b\u6587\uff0c\u65e0\u8bba\u662f\u5426\u8d85\u65f6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730002618671dbebacb907.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>GOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587<\/strong><\/p>\n<p><code>context.WithTimeout<\/code> \u51fd\u6570\u5728 Go \u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u4e0a\u4e0b\u6587\uff0c\u5e76\u4e3a\u5176\u8bbe\u5b9a\u4e00\u4e2a\u8d85\u65f6\u3002\u5f53\u5b50\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u7236\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\uff0c\u5b50\u4e0a\u4e0b\u6587\u4e5f\u4f1a\u88ab\u53d6\u6d88\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>parent<\/code>\uff1a\u7236\u4e0a\u4e0b\u6587<\/li>\n<li><code>timeout<\/code>\uff1a\u5b50\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u65f6\u95f4<\/li>\n<\/ul>\n<p><strong>\u8fd4\u56de\uff1a<\/strong><\/p>\n<ul>\n<li><code>Context<\/code>\uff1a\u65b0\u7684\u5b50\u4e0a\u4e0b\u6587<\/li>\n<li><code>CancelFunc<\/code>\uff1a\u53d6\u6d88\u5b50\u4e0a\u4e0b\u6587\u7684\u51fd\u6570<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>context.WithTimeout<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa\u7236\u4e0a\u4e0b\u6587\n    ctx := context.Background()\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u4e3a\u671f 1 \u79d2\u7684\u5b50\u4e0a\u4e0b\u6587\n    subCtx, cancel := context.WithTimeout(ctx, 1*time.Second)\n    defer cancel()\n\n    \/\/ \u5728\u5b50\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u4efb\u52a1\n    go func() {\n        for {\n            select {\n            case &lt;-subCtx.Done():\n                return\n            default:\n                fmt.Println(\"\u4efb\u52a1\u4ecd\u5728\u8fd0\u884c...\")\n            }\n        }\n    }()\n\n    \/\/ \u7b49\u5f85\u5b50\u4e0a\u4e0b\u6587\u5b8c\u6210\n    &lt;-subCtx.Done()\n    fmt.Println(\"\u5b50\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u88ab\u53d6\u6d88\")\n}<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 <code>WithTimeout<\/code> \u8d85\u65f6\u65f6\uff0c\u5b83\u4f1a\u53d1\u9001\u4e00\u4e2a <code>context.DeadlineExceeded<\/code> \u9519\u8bef\u3002<\/li>\n<li>\u5982\u679c\u7236\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\uff0c\u6240\u6709\u5b50\u4e0a\u4e0b\u6587\u4e5f\u4f1a\u88ab\u53d6\u6d88\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u5426\u8d85\u65f6\u3002<\/li>\n<li>\u8c03\u7528 <code>CancelFunc<\/code> \u51fd\u6570\u53ef\u4ee5\u7acb\u5373\u53d6\u6d88\u5b50\u4e0a\u4e0b\u6587\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aGOLANG \u51fd\u6570\uff1a\u7528 context.WithTimeout \u521b\u5efa\u5e26\u8d85\u65f6\u7684\u5b50\u4e0a\u4e0b\u6587\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8econtext,timeout\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u7535\u8111\u5c4f\u5e55\u62d6\u5f71\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u4ee3\u7801\u7b80\u6d01\u548c\u590d\u7528\u6027\u65b9\u9762\u7684\u4f18\u52bf\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f&#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-205798","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205798","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=205798"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205798\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}