{"id":6583,"date":"2024-09-20T19:21:21","date_gmt":"2024-09-20T11:21:21","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6583"},"modified":"2024-09-20T19:21:21","modified_gmt":"2024-09-20T11:21:21","slug":"gin-%e4%b8%ad%e5%8a%a8%e6%80%81%e8%b7%af%e7%94%b1%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6583","title":{"rendered":"Gin \u4e2d\u52a8\u6001\u8def\u7531\u5e94\u7528\u573a\u666f"},"content":{"rendered":"<pre><code class=\"language-go\">router.GET(\"\/user\/:name\/*action\", func(c *gin.Context) {\n        name := c.Param(\"name\")\n        action := c.Param(\"action\")\n        message := name + \" is \" + action\n        c.String(http.StatusOK, message)\n    })<\/code><\/pre>\n<p>\u5728 Gin \u4e2d\uff0c\u6ce8\u518c\u8def\u5f84 <code>\/user\/:name\/*action<\/code> \u662f\u4e00\u79cd\u52a8\u6001\u8def\u7531\u7684\u8bbe\u8ba1\uff0c\u5b83\u5141\u8bb8\u901a\u8fc7 URL \u4e2d\u7684\u5360\u4f4d\u7b26\u52a8\u6001\u4f20\u9012\u8def\u5f84\u53c2\u6570\u3002\u5728\u8fd9\u4e2a\u8def\u7531\u4e2d\uff1a<\/p>\n<ul>\n<li><code>:name<\/code> \u662f\u4e00\u4e2a\u8def\u5f84\u53c2\u6570\uff0c\u7528\u6765\u5339\u914d URL \u4e2d\u7684\u5177\u4f53\u503c\uff0c\u901a\u5e38\u7528\u4e8e\u6807\u8bc6\u7528\u6237\u6216\u5176\u4ed6\u8d44\u6e90\u3002<\/li>\n<li><code>*action<\/code> \u662f\u4e00\u4e2a\u901a\u914d\u7b26\u53c2\u6570\uff0c\u5339\u914d\u8def\u5f84\u4e2d\u5269\u4e0b\u7684\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u5339\u914d\u67d0\u4e2a\u7279\u5b9a\u8d44\u6e90\u4e0b\u7684\u5b50\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3><strong>RestAPI \u793a\u4f8b<\/strong><\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u8fd9\u6837\u4e00\u4e2a\u8def\u7531 <code>\/user\/:name\/*action<\/code>\uff1a<\/p>\n<pre><code class=\"language-go\">router.GET(&quot;\/user\/:name\/*action&quot;, func(c *gin.Context) {\n    name := c.Param(&quot;name&quot;)       \/\/ \u83b7\u53d6\u8def\u5f84\u4e2d\u7684 `name` \u53c2\u6570\n    action := c.Param(&quot;action&quot;)   \/\/ \u83b7\u53d6\u8def\u5f84\u4e2d\u7684 `action` \u53c2\u6570\n    c.String(http.StatusOK, &quot;User: %s, Action: %s&quot;, name, action)\n})<\/code><\/pre>\n<p>\u53ef\u4ee5\u5339\u914d\u4ee5\u4e0b URL\uff1a<\/p>\n<ul>\n<li><code>\/user\/johndoe\/profile<\/code><\/li>\n<li><code>\/user\/johndoe\/settings<\/code><\/li>\n<li><code>\/user\/johndoe\/posts\/123<\/code><\/li>\n<\/ul>\n<h3><strong>\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<p>\u8fd9\u79cd\u8def\u7531\u53ef\u4ee5\u7528\u4e8e\u7c7b\u4f3c\u4ee5\u4e0b\u7684 REST API \u573a\u666f\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u7528\u6237\u64cd\u4f5c\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>URL<\/strong>: <code>\/user\/johndoe\/profile<\/code>\n<ul>\n<li><strong>\u89e3\u91ca<\/strong>: \u8fd9\u91cc <code>johndoe<\/code> \u662f\u7528\u6237\u7684\u7528\u6237\u540d\uff0c<code>profile<\/code> \u662f\u7528\u6237\u7684\u52a8\u4f5c\uff0c\u53ef\u4ee5\u4ee3\u8868\u7528\u6237\u6b63\u5728\u67e5\u770b\u81ea\u5df1\u7684\u4e2a\u4eba\u8d44\u6599\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>URL<\/strong>: <code>\/user\/johndoe\/posts\/123<\/code>\n<ul>\n<li><strong>\u89e3\u91ca<\/strong>: <code>johndoe<\/code> \u662f\u7528\u6237\u540d\uff0c<code>posts\/123<\/code> \u53ef\u4ee5\u4ee3\u8868\u7528\u6237\u6b63\u5728\u67e5\u770b\u6216\u7f16\u8f91\u67d0\u4e00\u7bc7\u7279\u5b9a\u7684\u5e16\u5b50\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u7528\u6237\u8bbe\u7f6e<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>URL<\/strong>: <code>\/user\/johndoe\/settings<\/code>\n<ul>\n<li><strong>\u89e3\u91ca<\/strong>: <code>settings<\/code> \u4ee3\u8868\u7528\u6237\u7684\u8bbe\u7f6e\u9875\u9762\uff0c\u6b64\u65f6\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u8fd4\u56de\u8be5\u7528\u6237\u7684\u8bbe\u7f6e\u9879\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>API \u8bbe\u8ba1\u4e2d\u7684\u7075\u6d3b\u5904\u7406<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8be5\u8def\u7531\u6a21\u5f0f\u975e\u5e38\u9002\u5408\u90a3\u4e9b\u9700\u8981\u5bf9\u67d0\u4e2a\u7528\u6237\u8d44\u6e90\u8fdb\u884c\u591a\u79cd\u64cd\u4f5c\u7684 API \u8bbe\u8ba1\uff0c\u4f8b\u5982\u67e5\u770b\u3001\u7f16\u8f91\u3001\u5220\u9664\u7528\u6237\u7684\u591a\u4e2a\u8d44\u6e90\u9879\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>\u5b9e\u9645\u5f00\u53d1\u4e2d\u5982\u4f55\u9009\u62e9\u8fd9\u6837\u7684 URL<\/strong><\/h3>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u4f7f\u7528\u8fd9\u79cd\u52a8\u6001\u8def\u7531\u7684 URL \u53d6\u51b3\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li><strong>\u8d44\u6e90\u5c42\u7ea7\u7ed3\u6784<\/strong>\uff1a\u5982\u679c\u7528\u6237\u8d44\u6e90\u4e0b\u6709\u591a\u4e2a\u64cd\u4f5c\u6216\u5b50\u8d44\u6e90\uff0c\u6bd4\u5982\u7528\u6237\u7684\u8bbe\u7f6e\u3001\u4e2a\u4eba\u8d44\u6599\u3001\u5e16\u5b50\u7b49\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u5f62\u5f0f\u6765\u7b80\u5316\u8def\u7531\u8bbe\u8ba1\u3002<\/li>\n<li><strong>\u7b80\u5316\u4ee3\u7801<\/strong>\uff1a\u4f7f\u7528 <code>*action<\/code> \u4f5c\u4e3a\u901a\u914d\u7b26\u53ef\u4ee5\u907f\u514d\u4e3a\u6bcf\u4e2a\u5177\u4f53\u64cd\u4f5c\u7f16\u5199\u72ec\u7acb\u7684\u8def\u7531\u5904\u7406\u51fd\u6570\uff0c\u4ece\u800c\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/li>\n<li><strong>\u53ef\u6269\u5c55\u6027<\/strong>\uff1a\u5f53\u7528\u6237\u7684\u64cd\u4f5c\u79cd\u7c7b\u8f83\u591a\u4e14\u53ef\u80fd\u4f1a\u968f\u9700\u6c42\u53d8\u5316\u65f6\uff0c\u4f7f\u7528\u52a8\u6001\u8def\u7531\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u9002\u5e94\u672a\u6765\u7684\u6269\u5c55\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u79cd\u8bbe\u8ba1\u5e38\u7528\u4e8e RESTful API \u7684\u573a\u666f\u4e0b\uff0c\u8def\u5f84\u8868\u793a\u8d44\u6e90\uff0c\u800c\u64cd\u4f5c\u901a\u8fc7\u8def\u5f84\u4e2d\u7684\u5176\u4ed6\u90e8\u5206\u6216\u67e5\u8be2\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>router.GET(&#8220;\/user\/:name\/*action&#8221;, func(c *gin.Context) &#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6583\">\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-6583","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\/6583","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=6583"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6583\/revisions"}],"predecessor-version":[{"id":6584,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6583\/revisions\/6584"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}