{"id":6557,"date":"2024-09-19T16:05:54","date_gmt":"2024-09-19T08:05:54","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6557"},"modified":"2024-09-19T16:05:54","modified_gmt":"2024-09-19T08:05:54","slug":"go-%e5%86%85%e7%bd%ae%e7%9a%84-type-context-interface-%e5%92%8c-gin-%e7%9a%84-type-context-struct-%e6%9c%89%e4%bb%80%e4%b9%88%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6557","title":{"rendered":"go \u5185\u7f6e\u7684 type Context interface \u548c Gin \u7684 type Context struct \u6709\u4ec0\u4e48\u5173\u7cfb"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>Go \u8bed\u8a00\u4e2d\u7684 <code>type Context interface<\/code>\uff08\u901a\u5e38\u662f\u6307 <code>context.Context<\/code>\uff09\u548c Gin \u6846\u67b6\u4e2d\u7684 <code>type Context struct<\/code> \u662f\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u6982\u5ff5\uff0c\u867d\u7136\u5b83\u4eec\u7684\u540d\u79f0\u76f8\u540c\uff0c\u4f46\u5b83\u4eec\u7684\u529f\u80fd\u548c\u4f7f\u7528\u573a\u666f\u4e0d\u540c\u3002<\/p>\n<h3>1. <strong>Go \u6807\u51c6\u5e93\u4e2d\u7684 <code>context.Context<\/code><\/strong><\/h3>\n<p><code>context.Context<\/code> \u662f Go \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u4e3b\u8981\u7528\u4e8e\u63a7\u5236\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u3001\u4f20\u9012\u5143\u6570\u636e\u548c\u5728\u4e0d\u540c goroutine \u4e4b\u95f4\u540c\u6b65\u8bf7\u6c42\u53d6\u6d88\u3001\u8d85\u65f6\u7b49\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-go\">type Context interface {\n    Deadline() (deadline time.Time, ok bool)\n    Done() &lt;-chan struct{}\n    Err() error\n    Value(key interface{}) interface{}\n}<\/code><\/pre>\n<ul>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1aGo \u7684 <code>context.Context<\/code> \u7528\u4e8e\u7ba1\u7406\u8d85\u65f6\u3001\u53d6\u6d88\u64cd\u4f5c\u6216\u5728\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u4f20\u9012\u4fe1\u606f\u3002\u5b83\u5e7f\u6cdb\u5e94\u7528\u4e8e HTTP \u8bf7\u6c42\u3001\u6570\u636e\u5e93\u8bbf\u95ee\u3001\u8fdc\u7a0b\u8c03\u7528\u7b49\u573a\u666f\uff0c\u786e\u4fdd\u80fd\u591f\u53ca\u65f6\u53d6\u6d88\u6216\u63a7\u5236\u4efb\u52a1\u7684\u6267\u884c\u751f\u547d\u5468\u671f\u3002<\/li>\n<li><strong>\u4f20\u9012\u5143\u6570\u636e<\/strong>\uff1a\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>context.WithValue<\/code> \u6765\u4f20\u9012\u4e00\u4e9b\u8bf7\u6c42\u76f8\u5173\u7684\u5143\u6570\u636e\u3002<\/li>\n<li><strong>\u53d6\u6d88\u4fe1\u53f7<\/strong>\uff1a\u901a\u8fc7 <code>context.WithCancel<\/code>\u3001<code>context.WithTimeout<\/code> \u7b49\u51fd\u6570\u53ef\u4ee5\u4f20\u9012\u53d6\u6d88\u4fe1\u53f7\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-go\">ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\n\nselect {\ncase &lt;-time.After(10 * time.Second):\n    fmt.Println(&quot;Operation timed out&quot;)\ncase &lt;-ctx.Done():\n    fmt.Println(&quot;Context cancelled or timed out:&quot;, ctx.Err())\n}<\/code><\/pre>\n<h3>2. <strong>Gin \u6846\u67b6\u4e2d\u7684 <code>gin.Context<\/code><\/strong><\/h3>\n<p><code>gin.Context<\/code> \u662f Gin \u6846\u67b6\u7279\u6709\u7684\u7ed3\u6784\u4f53\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\uff0c\u5728\u5904\u7406 HTTP \u8bf7\u6c42\u65f6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u8bf7\u6c42\u6570\u636e\u3001\u54cd\u5e94\u5199\u5165\u3001\u8bf7\u6c42\u53c2\u6570\u548c\u4e2d\u95f4\u4ef6\u7684\u7ba1\u7406\u3002<\/p>\n<pre><code class=\"language-go\">type Context struct {\n    Request  *http.Request\n    Writer   http.ResponseWriter\n    Params   Params\n    \/\/ \u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u5b57\u6bb5\u548c\u65b9\u6cd5\n}<\/code><\/pre>\n<ul>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a<code>gin.Context<\/code> \u4e3b\u8981\u7528\u4e8e\u5904\u7406 HTTP \u8bf7\u6c42\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4fbf\u6377\u65b9\u6cd5\u6765\u83b7\u53d6\u8def\u7531\u53c2\u6570\u3001\u67e5\u8be2\u53c2\u6570\u3001\u8868\u5355\u6570\u636e\u3001JSON \u6570\u636e\u7b49\u3002\u5b83\u8fd8\u5305\u542b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bbe\u7f6e\u54cd\u5e94\u5934\u3001\u5199\u5165\u54cd\u5e94\u3001\u7ba1\u7406\u4e2d\u95f4\u4ef6\u7684\u6267\u884c\u7b49\u3002<\/li>\n<li><strong>\u6838\u5fc3\u529f\u80fd<\/strong>\uff1a\n<ul>\n<li>\u83b7\u53d6\u548c\u89e3\u6790\u8bf7\u6c42\u7684\u5404\u7c7b\u53c2\u6570\uff08\u5982 URL \u53c2\u6570\u3001\u8868\u5355\u6570\u636e\u3001JSON \u6570\u636e\u7b49\uff09\u3002<\/li>\n<li>\u63a7\u5236\u54cd\u5e94\u7684\u72b6\u6001\u7801\u548c\u5185\u5bb9\uff08\u5982 <code>c.JSON()<\/code>\u3001<code>c.String()<\/code>\u3001<code>c.Status()<\/code> \u7b49\uff09\u3002<\/li>\n<li>\u7ba1\u7406\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u548c\u4e2d\u95f4\u4ef6\u94fe\u7684\u6267\u884c\uff08\u901a\u8fc7 <code>c.Next()<\/code> \u6216 <code>c.Abort()<\/code> \u7b49\u65b9\u6cd5\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-go\">func exampleHandler(c *gin.Context) {\n    name := c.Query(&quot;name&quot;) \/\/ \u83b7\u53d6\u67e5\u8be2\u53c2\u6570\n    c.JSON(200, gin.H{&quot;message&quot;: &quot;Hello &quot; + name})\n}<\/code><\/pre>\n<h3>3. <strong>\u4e24\u8005\u7684\u5173\u7cfb<\/strong><\/h3>\n<p>Go \u6807\u51c6\u5e93\u7684 <code>context.Context<\/code> \u548c Gin \u7684 <code>gin.Context<\/code> <strong>\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb<\/strong>\uff0c\u5b83\u4eec\u662f\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u4e0a\u4e0b\u6587\uff0c\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong>Go \u7684 <code>context.Context<\/code><\/strong>\uff1a\u7528\u4e8e\u63a7\u5236\u4efb\u52a1\u751f\u547d\u5468\u671f\u3001\u53d6\u6d88\u4fe1\u53f7\u3001\u8d85\u65f6\u3001\u534f\u7a0b\u95f4\u540c\u6b65\u7b49\u3002<\/li>\n<li><strong>Gin \u7684 <code>gin.Context<\/code><\/strong>\uff1a\u7528\u4e8e\u7ba1\u7406 HTTP \u8bf7\u6c42\u7684\u4e0a\u4e0b\u6587\uff0c\u5904\u7406\u8bf7\u6c42\u53c2\u6570\u3001\u5199\u5165\u54cd\u5e94\uff0c\u5e76\u7ba1\u7406\u4e2d\u95f4\u4ef6\u94fe\u3002<\/li>\n<\/ul>\n<p>\u7136\u800c\uff0cGin \u7684 <code>gin.Context<\/code> \u53ef\u4ee5\u5728\u5904\u7406 HTTP \u8bf7\u6c42\u65f6\u5305\u542b\u6216\u5d4c\u5165 Go \u7684 <code>context.Context<\/code>\uff0cGin \u5185\u90e8\u6709\u65f6\u4f1a\u5c06 Go \u7684 <code>context.Context<\/code> \u7528\u4f5c\u8bf7\u6c42\u7684\u4e00\u90e8\u5206\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8d85\u65f6\u6216\u53d6\u6d88\u4fe1\u53f7\u65f6\u3002\u4f60\u53ef\u4ee5\u5728 Gin \u4e2d\u4f7f\u7528 Go \u7684 <code>context.Context<\/code> \u6765\u7ba1\u7406\u8bf7\u6c42\u7684\u8d85\u65f6\u63a7\u5236\uff1a<\/p>\n<pre><code class=\"language-go\">func exampleHandler(c *gin.Context) {\n    ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)\n    defer cancel()\n\n    \/\/ \u4f7f\u7528 ctx \u8fdb\u884c\u4e00\u4e9b\u8017\u65f6\u64cd\u4f5c\n}<\/code><\/pre>\n<h3>\u603b\u7ed3\uff1a<\/h3>\n<ul>\n<li><code>context.Context<\/code> \u662f Go \u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u4efb\u52a1\u63a7\u5236\u7684\u63a5\u53e3\u3002<\/li>\n<li><code>gin.Context<\/code> \u662f Gin \u6846\u67b6\u4e2d\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u7ed3\u6784\u4f53\u3002<\/li>\n<li>\u5b83\u4eec\u7684\u529f\u80fd\u548c\u4f5c\u7528\u57df\u4e0d\u540c\uff0c\u4f46\u53ef\u4ee5\u5728 Gin \u7684 HTTP \u8bf7\u6c42\u5904\u7406\u4e2d\u540c\u65f6\u4f7f\u7528\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210 Go \u8bed\u8a00\u4e2d\u7684 type Context interface\uff08\u901a\u5e38\u662f\u6307 co&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6557\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6557","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6557"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6557\/revisions"}],"predecessor-version":[{"id":6558,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6557\/revisions\/6558"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}