{"id":206443,"date":"2025-06-12T11:01:28","date_gmt":"2025-06-12T03:01:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206443\/"},"modified":"2025-06-12T11:01:28","modified_gmt":"2025-06-12T03:01:28","slug":"%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95-gin-context-%e4%bb%a5%e8%87%aa%e5%ae%9a%e4%b9%89%e5%93%8d%e5%ba%94%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206443\/","title":{"rendered":"\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f"},"content":{"rendered":"<p>\u600e\u4e48\u5165\u95e8GOLANG\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762GOLANG\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313761336732b405cce38.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u6269\u5c55 gin context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5<\/strong>,\u5728 gin \u4e2d\uff0c\u60a8\u901a\u5e38\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u8bf7\u6c42\u5904\u7406\u4ee3\u7801\uff1a,\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u81ea\u5b9a\u4e49\u4e00\u4e2a\u54cd\u5e94\u65b9\u6cd5\uff0c\u4f8b\u5982 c.fail(&#8220;\u4e0a\u4f20\u5931\u8d25&#8221;)\u3002\u5982\u4f55\u6269\u5c55 gin context \u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u5462\uff1f,<br \/>\n<strong>\u6269\u5c55 gin context<\/strong>,gin context \u662f\u4e00\u79cd\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7\u95ed\u5305\u7684\u65b9\u5f0f\u6765\u6269\u5c55\u5176\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684 context \u7ed3\u6784\u4f53\uff0c\u5305\u542b gin context \u548c\u5176\u4ed6\u6240\u9700\u7684\u65b9\u6cd5\u3002,<br \/>\n<strong>\u6269\u5c55\u8def\u7531\u5904\u7406\u51fd\u6570<\/strong>,\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u4fee\u6539\u8def\u7531\u5904\u7406\u51fd\u6570\uff0c\u5c06\u81ea\u5b9a\u4e49 context \u4f20\u9012\u7ed9\u5b83\u3002\u4f7f\u7528 newextendcontext() \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5904\u7406\u51fd\u6570\uff0c\u5b83\u5c06\u8c03\u7528\u81ea\u5b9a\u4e49\u7684 context\u3002,\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u901a\u8fc7 c.fail() \u65b9\u6cd5\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u54cd\u5e94\u3002,\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f\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\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01,\u6269\u5c55 gin context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5,\u600e\u4e48\u5165\u95e8GOLANG\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762GOLANG\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b,<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\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f<\/h1>\n<p><span>2024-11-12 09:49:09<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8GOLANG\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762GOLANG\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313761336732b405cce38.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6269\u5c55 gin context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 gin \u4e2d\uff0c\u60a8\u901a\u5e38\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u6837\u7684\u8bf7\u6c42\u5904\u7406\u4ee3\u7801\uff1a<\/p>\n<pre>func indexhandler(c *gin.context) {\n  if err := c.abortwitherror(500, err); err != nil {\n    c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n  }\n}<\/pre>\n<p>\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u81ea\u5b9a\u4e49\u4e00\u4e2a\u54cd\u5e94\u65b9\u6cd5\uff0c\u4f8b\u5982 c.fail(&#8220;\u4e0a\u4f20\u5931\u8d25&#8221;)\u3002\u5982\u4f55\u6269\u5c55 gin context \u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u5462\uff1f<\/p>\n<p><strong>\u6269\u5c55 gin context<\/strong><\/p>\n<p>gin context \u662f\u4e00\u79cd\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7\u95ed\u5305\u7684\u65b9\u5f0f\u6765\u6269\u5c55\u5176\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684 context \u7ed3\u6784\u4f53\uff0c\u5305\u542b gin context \u548c\u5176\u4ed6\u6240\u9700\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>type context struct {\n    *gin.context\n}\n\nfunc (ctx context) fail(err string) {\n    ctx.json(http.statusinternalservererror, gin.h{\"error\": err})\n}<\/pre>\n<p><strong>\u6269\u5c55\u8def\u7531\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u4fee\u6539\u8def\u7531\u5904\u7406\u51fd\u6570\uff0c\u5c06\u81ea\u5b9a\u4e49 context \u4f20\u9012\u7ed9\u5b83\u3002\u4f7f\u7528 newextendcontext() \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5904\u7406\u51fd\u6570\uff0c\u5b83\u5c06\u8c03\u7528\u81ea\u5b9a\u4e49\u7684 context\u3002<\/p>\n<pre>func IndexHandler(ctx Context) {\n  if err := c.AbortWithError(500, err); err != nil {\n    ctx.Fail(\"\u4e0a\u4f20\u5931\u8d25\")\n  }\n}\n\napp.Get(\"\/index\", NewExtendContext(IndexHandler))<\/pre>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u901a\u8fc7 c.fail() \u65b9\u6cd5\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u6269\u5c55 Gin Context \u4ee5\u81ea\u5b9a\u4e49\u54cd\u5e94\u65b9\u6cd5\uff1f\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\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Gin\u6846\u67b6\u5982\u4f55\u6821\u9a8c\u8def\u7531\u53c2\u6570\u4e3a\u6570\u503c\u7c7b\u578b\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u591a\u5217\u5e03\u5c40\u5728\u73b0\u4ee3 CSS \u5e03\u5c40\u4e2d\u8fd8\u6709\u7528\u6b66\u4e4b\u5730\u5417\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u4e48\u5165\u95e8GOLANG\u7f16\u7a0b\uff1f\u9700\u8981\u5b66&#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-206443","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206443","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=206443"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206443\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}