{"id":207911,"date":"2025-07-08T16:24:31","date_gmt":"2025-07-08T08:24:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207911\/"},"modified":"2025-07-08T16:24:31","modified_gmt":"2025-07-08T08:24:31","slug":"%e5%85%b3%e4%ba%8e%e4%b8%8a%e4%b8%8b%e6%96%87%e5%8c%85%e4%b8%ad-golang-%e5%b5%8c%e5%85%a5%e6%8e%a5%e5%8f%a3%e5%ad%97%e6%ae%b5%e5%88%86%e9%85%8d%e7%9a%84%e6%b7%b7%e4%b9%b1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207911\/","title":{"rendered":"\u5173\u4e8e\u4e0a\u4e0b\u6587\u5305\u4e2d Golang \u5d4c\u5165\u63a5\u53e3\u5b57\u6bb5\u5206\u914d\u7684\u6df7\u4e71"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5173\u4e8e\u4e0a\u4e0b\u6587\u5305\u4e2d Golang \u5d4c\u5165\u63a5\u53e3\u5b57\u6bb5\u5206\u914d\u7684\u6df7\u4e71<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 18:09:26<\/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\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5173\u4e8e\u4e0a\u4e0b\u6587\u5305\u4e2d Golang \u5d4c\u5165\u63a5\u53e3\u5b57\u6bb5\u5206\u914d\u7684\u6df7\u4e71\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f53\u6211\u9605\u8bfb<code>context<\/code>\u5305\u6e90\u4ee3\u7801\u65f6\uff0c\u6211\u53d1\u73b0<code>context<\/code>\u7ed3\u6784\u4f53\u4e2d\u6709\u4e00\u4e2a<code>context<\/code>\u63a5\u53e3\u5b57\u6bb5\u3002\u5728 <code>newcancelctx<\/code> \u65b9\u6cd5\u4e2d\uff0c\u5b83\u5c06 <code>parent<\/code> \u53c2\u6570\u5206\u914d\u7ed9 <code>cancelctx.context<\/code>\u3002 \u6211\u5f88\u56f0\u60d1\u8fd9\u6837\u7684\u4f5c\u4e1a\u662f\u8f6f\u62f7\u8d1d\u8fd8\u662f\u6df1\u5c42\u62f7\u8d1d\uff1f struct <code>cancelctx<\/code> \u4e2d\u7684 <code>context<\/code> \u5b57\u6bb5\u662f\u5426\u4fdd\u5b58\u6307\u5411 <code>parent<\/code> \u63a5\u53e3\u7684\u6307\u9488\uff1f<\/p>\n<pre>\/\/ Canceling this context releases resources associated with it, so code should\n\/\/ call cancel as soon as the operations running in this Context complete.\nfunc WithCancel(parent Context) (ctx Context, cancel CancelFunc) {\n    c := newCancelCtx(parent)\n    propagateCancel(parent, &amp;c)\n    return &amp;c, func() { c.cancel(true, Canceled) }\n}\n\n\/\/ newCancelCtx returns an initialized cancelCtx.\nfunc newCancelCtx(parent Context) cancelCtx {\n    return cancelCtx{Context: parent}\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u63a5\u53e3\u5305\u542b\u6307\u5411\u57fa\u7840\u6570\u636e\u9879\u548c\u7c7b\u578b\u7684\u6307\u9488\u3002\u5f53\u60a8\u5c06\u503c\u5206\u914d\u7ed9\u63a5\u53e3\u65f6\uff0c\u60a8\u4f1a\u5206\u914d\u4e24\u4e2a\u503c\uff1a\u6307\u5411\u8be5\u503c\u7684\u6307\u9488\uff08\u6216\u6307\u5411\u8be5\u503c\u526f\u672c\u7684\u6307\u9488\uff09\u548c\u7c7b\u578b\u3002\u6240\u4ee5\uff0c\u5b83\u4e0d\u662f\u6df1\u590d\u5236\u3002<\/p>\n<p>\u60a8\u6d4b\u8bd5\u4ee3\u7801\u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\u6253\u5370 <code>a<\/code> \uff0c\u5728\u7b2c\u4e8c\u79cd\u60c5\u51b5\u4e0b\u6253\u5370 <code>b.impl<\/code> \uff0c\u6211\u731c\u8fd9\u5c31\u662f\u8f93\u51fa\u770b\u8d77\u6765\u4ee4\u4eba\u56f0\u60d1\u7684\u539f\u56e0\u3002\u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u6253\u5370 <code>b<\/code> \uff0c\u5b83\u5e94\u8be5\u662f\u663e\u800c\u6613\u89c1\u7684\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5173\u4e8e\u4e0a\u4e0b\u6587\u5305\u4e2d Golang \u5d4c\u5165\u63a5\u53e3\u5b57\u6bb5\u5206\u914d\u7684\u6df7\u4e71\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\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207911","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207911","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=207911"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207911\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}