10-Gin定义控制器
2022/4/9 23:50:25
本文主要是介绍10-Gin定义控制器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一 自定义控制器
当我们的项目比较大的时候有必要对我们的控制器进行分组,我们采用MVC架构,自定义C层controller,控制器
目录结构
main.go
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
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
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
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
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") }
controllers/order/order_controller
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") }
controllers/user/user_controller
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") }
二 控制器继承
目录结构
controllers/good/base_controller
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
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") }
这篇关于10-Gin定义控制器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01后台管理开发学习:新手入门指南
- 2024-11-01后台管理系统开发学习:新手入门教程
- 2024-11-01后台开发学习:从入门到实践的简单教程
- 2024-11-01后台综合解决方案学习:从入门到初级实战教程
- 2024-11-01接口模块封装学习入门教程
- 2024-11-01请求动作封装学习:新手入门教程
- 2024-11-01登录鉴权入门:新手必读指南
- 2024-11-01动态面包屑入门:轻松掌握导航设计技巧
- 2024-11-01动态权限入门:新手必读指南
- 2024-11-01动态主题处理入门:新手必读指南