gin路由分组 路由文件抽离

gin自定义控制器 实现控制器的继承
This commit is contained in:
2025-12-03 22:19:24 +08:00
parent 4a8e355b73
commit 8155bd710b
11 changed files with 160 additions and 128 deletions

View File

@@ -0,0 +1,10 @@
package admin
import "github.com/gin-gonic/gin"
type ArticleController struct {
}
func (con ArticleController) Article(c *gin.Context) {
c.String(200, "管理员文章列表")
}

View File

@@ -0,0 +1,18 @@
package admin
import "github.com/gin-gonic/gin"
type IndexController struct {
}
func (con IndexController) Index(c *gin.Context) {
c.String(200, "管理员用户列表")
}
func (con IndexController) Plist(c *gin.Context) {
c.String(200, "管理员用户详情")
}
func (con IndexController) Login(c *gin.Context) {
c.String(200, "管理员登录页面")
}

View File

@@ -0,0 +1,14 @@
package admin
import "github.com/gin-gonic/gin"
type UserController struct {
}
func (con UserController) Index(c *gin.Context) {
c.String(200, "管理员用户列表")
}
func (con UserController) Show(c *gin.Context) {
c.String(200, "管理员用户详情")
}

View File

@@ -0,0 +1,18 @@
package api
import "github.com/gin-gonic/gin"
type IndexController struct {
}
func (con IndexController) Index(c *gin.Context) {
c.String(200, "API接口")
}
func (con IndexController) User(c *gin.Context) {
c.String(200, "用户信息")
}
func (con IndexController) Plist(c *gin.Context) {
c.String(200, "用户列表")
}

View File

@@ -0,0 +1,14 @@
package nannanwu
import "github.com/gin-gonic/gin"
type BaseController struct {
}
func (con BaseController) success(c *gin.Context) {
c.String(200, "成功")
}
func (con BaseController) error(c *gin.Context) {
c.String(200, "失败")
}

View File

@@ -0,0 +1,25 @@
package nannanwu
import "github.com/gin-gonic/gin"
type DefaultController struct {
//继承BaseController
BaseController
}
func (con DefaultController) Index(c *gin.Context) {
c.String(200, "首页")
}
func (con DefaultController) Article(c *gin.Context) {
c.String(200, "文章详情")
}
// 使用继承的方式调用BaseController的success方法
func (con DefaultController) Success(c *gin.Context) {
BaseController{}.success(c)
}
// 使用继承的方式调用BaseController的error方法
func (con DefaultController) Error(c *gin.Context) {
BaseController{}.error(c)
}