当我们的项目比较大的时候有必要对我们的控制器进行分组,我们采用MVC架构,自定义C层controller,控制器
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() }
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) } }
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) } }
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) } }
package good import ( "github.com/gin-gonic/gin" "net/http" ) // 定义一个GoodController结构体 type GoodController struct { } // 把如下方法绑定给GoodController结构体 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") }
package order import ( "github.com/gin-gonic/gin" "net/http" ) // 定义一个OrderController结构体 type OrderController struct { } // 把如下方法绑定给OrderController结构体 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") }
package user import ( "github.com/gin-gonic/gin" "net/http" ) // 定义一个UserController结构体 type UserController struct { } // 把如下方法绑定给UserController结构体 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") }
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,"商品失败") }
package good import ( "github.com/gin-gonic/gin" "net/http" ) // 定义一个GoodController结构体 type GoodController struct { BaseController // 继承BaseController } // 把如下方法绑定给GoodController结构体 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") }