{"id":419021,"date":"2021-03-21T21:44:52","date_gmt":"2021-03-21T13:44:52","guid":{"rendered":"http:\/\/4563.org\/?p=419021"},"modified":"2021-03-21T21:44:52","modified_gmt":"2021-03-21T13:44:52","slug":"golang-gin-%e6%a1%86%e6%9e%b6%e5%88%86%e7%bb%84%e8%b7%af%e7%94%b1%e7%9a%84%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=419021","title":{"rendered":"golang gin \u6846\u67b6\u5206\u7ec4\u8def\u7531\u7684\u539f\u7406"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang gin \u6846\u67b6\u5206\u7ec4\u8def\u7531\u7684\u539f\u7406               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liulei281 <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>gin \u6587\u6863\u4e2d\u7684\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<pre><code>func main() {  router := gin.Default()   \/\/ Simple group: v1  v1 := router.Group(\"\/v1\")  {   v1.POST(\"\/login\", loginEndpoint)   v1.POST(\"\/submit\", submitEndpoint)   v1.POST(\"\/read\", readEndpoint)  }   \/\/ Simple group: v2  v2 := router.Group(\"\/v2\")  {   v2.POST(\"\/login\", loginEndpoint)   v2.POST(\"\/submit\", submitEndpoint)   v2.POST(\"\/read\", readEndpoint)  }   router.Run(\":8080\") } <\/code><\/pre>\n<p><code>router.Group<\/code>\u65b9\u6cd5\u6e90\u7801\u662f\u8fd9\u6837\u7684:<\/p>\n<pre><code>\/\/ Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix. \/\/ For example, all the routes that use a common middleware for authorization could be grouped. func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {  return &amp;RouterGroup{   Handlers: group.combineHandlers(handlers),   basePath: group.calculateAbsolutePath(relativePath),   engine:   group.engine,  } } <\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u770b, <code>v1 := router.Group(\"\/v1\")<\/code>\u521b\u5efa\u4e00\u4e2a\u5b50\u5206\u7ec4<code>v1<\/code>, \u5e76\u4e14<code>v1.engine<\/code>\u4e2d\u5b58\u50a8\u4e86\u7236\u7ea7\u7684\u4fe1\u606f, \u8fd9\u65f6\u5019<code>v1<\/code>\u77e5\u9053\u5b83\u7684\u7236\u7ea7\u662f<code>router<\/code>. \u4f46\u662f\u7a0b\u5e8f\u7684\u5165\u53e3\u662f<code>router<\/code>, \u4f46\u597d\u50cf\u5e76\u6ca1\u6709\u628a<code>v1<\/code>\u5173\u8054\u7ed9<code>router<\/code>, \u90a3\u4e48<code>router<\/code>\u600e\u4e48\u77e5\u9053<code>v1<\/code>\u7684\u5b58\u5728?<\/p>\n<p>(\u521d\u5b66 golang, \u8bf7\u5927\u4f6c\u89e3\u60d1, \u8c22\u8c22)<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5591779\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abersheeran <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u7eaf Golang \u8def\u4eba\u3002\u4f46\u662f Gin \u7528\u7684 Router \u662f\u57fa\u4e8e httprouter \u7684\uff0chttprouter \u7528\u7684\u662f Radix Tree \u3002\u6240\u4ee5\u4e00\u5b9a\u6709\u4e00\u6b65\u662f\u628a\u8282\u70b9\u52a0\u5165\u6811\u91cc\u3002\u4f60\u518d\u770b\u770b\u6e90\u7801\u5427\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5591780\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 676529483 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             router \u5c31\u662f Engine \u5bf9\u8c61\uff0c\u6e90\u7801\u91cc\u9762\u4f60\u770b\u4e0d\u662f\u6709 engine: group.engine \u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5591781\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xxjwxc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/xxjwxc\/ginrpc<br \/>\u63a8\u8350\u8fd9\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5591782\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : catror <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             v1.POST() \u548c v2.POST() \u624d\u662f\u771f\u6b63\u6ce8\u518c route \u7684\u65b9\u6cd5\uff0cgroup \u53ea\u662f\u65b9\u4fbf\u7406\u89e3\u548c\u64cd\u4f5c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5591783\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cyjme <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90a3\u4e48 router \u600e\u4e48\u77e5\u9053 v1 \u7684\u5b58\u5728?<\/p>\n<p>=======<\/p>\n<p>\/\/ Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.<br \/>\/\/ For example, all the routes that use a common middleware for authorization could be grouped.<br \/>func (group *RouterGroup) Group(relativePath string, handlers &#8230;HandlerFunc) *RouterGroup {<br \/> return &amp;RouterGroup{<br \/> Handlers: group.combineHandlers(handlers),<br \/> basePath: group.calculateAbsolutePath(relativePath),<br \/> engine: group.engine,<br \/> }<br \/>}<\/p>\n<p>\u6e90\u7801\u4e2d\u628a \/v1 \u7528\u4e8e\u4e86 basePath \u3002<\/p>\n<p>\u7136\u540e\u4ece router.GET() \u8fd9\u4e2a\u65b9\u6cd5\u5f00\u59cb\u770b\uff0c\u770b\u4ec0\u4e48\u65f6\u5019\u7528\u5230 basePath \uff08\u4e5f\u5c31\u662f \/v1 \uff09 <\/p>\n<p>\/\/ GET is a shortcut for router.Handle(&#8220;GET&#8221;, path, handle).<br \/>func (group *RouterGroup) GET(relativePath string, handlers &#8230;HandlerFunc) IRoutes {<br \/> return group.handle( http.MethodGet, relativePath, handlers) \/\/\u8fdb\u5165<br \/>}<\/p>\n<p>func (group *RouterGroup) handle( httpMethod, relativePath string, handlers HandlersChain) IRoutes {<br \/> absolutePath := group.calculateAbsolutePath(relativePath) \/\/\u8fdb\u5165<br \/> handlers = group.combineHandlers(handlers)<br \/> group.engine.addRoute( httpMethod, absolutePath, handlers)<br \/> return group.returnObj()<br \/>}<\/p>\n<p>func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {<br \/> return joinPaths(group.basePath, relativePath) \/\/\u8fd9\u91cc\u7684 basePath \u5c31\u662f\u4f60\u7684 &#8220;\/v1&#8243;\uff0c\u5982\u679c\u6d89\u53ca\u5230\u591a\u91cd group\uff0c\u53ef\u80fd\u662f \/xxx\/v1<br \/>}<\/p>\n<p>=================<br \/>\u4e0a\u9762\u5e94\u8be5\u89e3\u7b54\u4e86\u4f60\u7684\u7591\u95ee\u300e\u600e\u4e48\u77e5\u9053 v1 \u5b58\u5728\u300f\uff0c\u5728\u6bcf\u4e00\u6b21 GET\/POST \u6267\u884c\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u6839\u636e\u4e4b\u524d\u7684 basePath \u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<p>\u7ee7\u7eed\u6df1\u5165\uff0c\u53ef\u4ee5\u770b addRoute() &#8212;-&gt; engine.trees \u76f8\u5173\u3002\u7136\u540e\u5c31\u662f 1 \u8bf4\u7684 httprouter tree \u76f8\u5173\uff0c\u5982\u4f55\u5feb\u901f\u67e5\u627e\u5339\u914d\u8def\u7531\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5591784\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liulei281 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @catror <br \/>@cyjme <br \/>\u8c22\u8c22, \u660e\u767d\u600e\u4e48\u56de\u4e8b\u4e86.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang gin \u6846\u67b6\u5206\u7ec4\u8def\u7531&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419021"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419021"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/419021\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419021"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}