title: How to combine group of routes in gin?
body: <p>I have created two different Groups for <code>gin</code> routing specifically <code>/user</code> and <code>/todo</code> in two different packages and I want to merge them into one file . Here is my <code>userroutes.go</code> file.</p>
  <pre><code>package userrouter
  
  import (
      &quot;github.com/gin-gonic/gin&quot;
  )
  
  //UserRoutes for user
  func UserRoutes() *gin.RouterGroup {
  
      r := gin.Default()
  
      v1 := r.Group(&quot;/user&quot;)
      {
  
          v1.GET(&quot;/hello&quot;, func(c *gin.Context) {
              c.JSON(200, gin.H{
                  &quot;message&quot;: &quot;pong&quot;,
              })
          })
  
      }
      return v1
  
  }
  </code></pre>
  <p>and <code>todoroutes.go</code> as</p>
  <pre><code>package todorouter
  
  import (
      &quot;github.com/gin-gonic/gin&quot;
  )
  
  //TodoRoutes for creating Todo
  func TodoRoutes() *gin.RouterGroup {
  
      r := gin.Default()
  
      v2 := r.Group(&quot;/todo&quot;)
      {
  
          v2.GET(&quot;/hello&quot;, func(c *gin.Context) {
              c.JSON(200, gin.H{
                  &quot;message&quot;: &quot;pong&quot;,
              })
          })
  
      }
      return v2
  
  }
  </code></pre>
  <p>and then want to merge them in <code>routes.go</code>.</p>
  <pre><code>package router
  
  import (
      todo &quot;TODO/api/Routes/Todo/TodoRoutes&quot;
      user &quot;TODO/api/Routes/User/UserRoutes&quot;
      &quot;fmt&quot;
  
      &quot;github.com/gin-gonic/gin&quot;
  )
  
  
  //Router for all routes
  func Router() {
      // r := gin.Default()
      userRoutes := user.UserRoutes()
      todoRoutes := todo.TodoRoutes()
  
  }
  </code></pre>
  <p>How can I merge them into one file so that they could be fetched with some prefix <code>/api</code> like <code>localhost:8081/api/user/create</code> or <code>localhost:8081/api/todo/create</code>. Additionaly do I need to create <code>r := gin.Default()</code> in every route I've been creating or it can be accomplished using once? Thanks.</p>