[10-Gin定义控制器]
一 自定义控制器
当我们的项目比较大的时候有必要对我们的控制器进行分组,我们采用MVC架构,自定义C层controller,控制器
目录结构

main.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package main
import ( "gin_test/routers" "github.com/gin-gonic/gin" )
func main() { r := gin.Default() routers.LoadGoodsRouters(r) routers.LoadOrderRouters(r) routers.LoadUserRouters(r) r.Run() }
|
routers/goods.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package routers
import ( "gin_test/controllers/good" "github.com/gin-gonic/gin"
)
func LoadGoodsRouters(r *gin.Engine) { r1 := r.Group("/good") { r1.GET("/", good.GoodController{}.GoodHandler) r1.GET("/goodlist", good.GoodController{}.GoodlistHandler) r1.GET("/updategood",good.GoodController{}.UpdategoodHandler) } }
|
routers/order.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package routers
import ( "gin_test/controllers/order" "github.com/gin-gonic/gin" )
func LoadOrderRouters(r *gin.Engine) { r1 := r.Group("/oOder") { r1.GET("/", order.OrderController{}.OrderHandler) r1.GET("/orderlist", order.OrderController{}.OrderlistHandler) r1.GET("/updateorder", order.OrderController{}.OpdateorderHandler) } }
|
routers/user.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package routers
import ( "gin_test/controllers/user" "github.com/gin-gonic/gin" )
func LoadUserRouters(r *gin.Engine) { r1 := r.Group("/user") { r1.GET("/", user.UserController{}.UserHandler) r1.GET("/userlist", user.UserController{}.UserlistHandler) r1.GET("/updateuser", user.UserController{}.UpdateuserHandler) } }
|
controllers/good/good_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package good
import ( "github.com/gin-gonic/gin" "net/http" )
type GoodController struct { }
func (g GoodController)GoodHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "www.liuqingzheng.top", }) }
func (g GoodController)GoodlistHandler(c *gin.Context) { c.String(http.StatusOK, "goodlistHandler") } func (g GoodController)UpdategoodHandler(c *gin.Context) { c.String(http.StatusOK, "updategoodHandler") }
|
controllers/order/order_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package order
import ( "github.com/gin-gonic/gin" "net/http" )
type OrderController struct { }
func (o OrderController)OrderHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "www.liuqingzheng.top", }) }
func (o OrderController)OrderlistHandler(c *gin.Context) { c.String(http.StatusOK, "orderlistHandler") } func (o OrderController)OpdateorderHandler(c *gin.Context) { c.String(http.StatusOK, "updateorderHandler") }
|
controllers/user/user_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package user
import ( "github.com/gin-gonic/gin" "net/http" )
type UserController struct { }
func (u UserController)UserHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "www.liuqingzheng.top", }) }
func (u UserController)UserlistHandler(c *gin.Context) { c.String(http.StatusOK, "userlistHandler") } func (u UserController)UpdateuserHandler(c *gin.Context) { c.String(http.StatusOK, "updateuserHandler") }
|
二 控制器继承
目录结构

controllers/good/base_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package good
import "github.com/gin-gonic/gin"
type BaseController struct {
}
func (b BaseController)Success(c *gin.Context) { c.String(200,"商品成功") } func (b BaseController)Error(c *gin.Context) { c.String(200,"商品失败") }
|
controllers/good/good_controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package good
import ( "github.com/gin-gonic/gin" "net/http" )
type GoodController struct { BaseController }
func (g GoodController)GoodHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "msg": "www.liuqingzheng.top", }) }
func (g GoodController)GoodlistHandler(c *gin.Context) { g.Success(c) } func (g GoodController)UpdategoodHandler(c *gin.Context) { c.String(http.StatusOK, "updategoodHandler") }
|