Go to source in github
Example Source Link
Directory structure
Directory structure
1
2
3
4
5
6
7
8
9
10
| .
├── go.mod
├── go.sum
├── main.go
├── post
│ └── routes.go
├── schedule
│ └── routes.go
└── user
└── routes.go
|
Source
/post/routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package post
import "github.com/gin-gonic/gin"
func Routes(r *gin.Engine) {
post := r.Group("/post")
{
post.GET("/1", func(c *gin.Context) {
c.JSON(200, gin.H{
"post_id": 1,
})
})
}
}
|
The post
is an app you can make.
And each app have a routes.go
file has Routes
function.
The Routes
is received a parameter r *gin.Engine
from main.go
.
Then each app make a r.Group
as an url path.
After that, you can make sub url of the r.Group
.
/schedule/routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package schedule
import "github.com/gin-gonic/gin"
func Routes(r *gin.Engine) {
schedule := r.Group("/schedule")
{
schedule.GET("/day", func(c *gin.Context) {
c.JSON(200, gin.H{
"day": "today",
})
})
}
}
|
/user/routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package user
import "github.com/gin-gonic/gin"
func Routes(r *gin.Engine) {
user := r.Group("/user")
{
user.GET("/id", func(c *gin.Context) {
c.JSON(200, gin.H{
"username": "User ID",
})
})
}
}
|
/main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| package main
import (
"github.com/gin-gonic/gin"
"route-sample/post"
"route-sample/schedule"
"route-sample/user"
)
func main() {
r := gin.Default()
post.Routes(r)
user.Routes(r)
schedule.Routes(r)
err := r.Run()
if err != nil {
return
}
}
|
You have to pass the parameter like post.Routes(r)
to each app from main.go
.