Salmon的全栈知识 Salmon的全栈知识
首页
  • JavaSE
  • JavaWeb
  • Spring生态
  • JUC
  • JVM
  • Netty
  • Java各版本特性
  • 23种设计模式
  • Maven
  • Java常用框架
  • Dubbo
  • OpenFeign
  • Nacos
  • Zookeeper
  • Sentinel
  • Seata
  • Gateway
  • Go基础
  • Gin
  • SQL数据库

    • MySQL
    • Oracle
  • NoSQL数据库

    • Redis
    • MongoDB
    • ElasticSearch
  • 消息中间件

    • RabbitMQ
    • RocketMQ
    • Kafka
    • ActiveMQ
    • MQTT
    • NATS
  • 网关中间件

    • Nginx
  • Linux
  • Docker
  • Git
  • K8s
  • Solidity
  • Java
  • 计算机网络
  • 操作系统
GitHub (opens new window)
首页
  • JavaSE
  • JavaWeb
  • Spring生态
  • JUC
  • JVM
  • Netty
  • Java各版本特性
  • 23种设计模式
  • Maven
  • Java常用框架
  • Dubbo
  • OpenFeign
  • Nacos
  • Zookeeper
  • Sentinel
  • Seata
  • Gateway
  • Go基础
  • Gin
  • SQL数据库

    • MySQL
    • Oracle
  • NoSQL数据库

    • Redis
    • MongoDB
    • ElasticSearch
  • 消息中间件

    • RabbitMQ
    • RocketMQ
    • Kafka
    • ActiveMQ
    • MQTT
    • NATS
  • 网关中间件

    • Nginx
  • Linux
  • Docker
  • Git
  • K8s
  • Solidity
  • Java
  • 计算机网络
  • 操作系统
GitHub (opens new window)
npm

(进入注册为作者充电)

  • Gin 介绍
  • Gin 环境搭建
  • golang 程序的热加载
  • Gin 框架中的路由
  • Gin HTML 模板渲染
  • 静态文件服务
  • 路由详解
  • Gin 中自定义控制器
    • 1、控制器分组
    • 2、控制器的继承
  • Gin 中间件
  • Gin 中自定义 Model
  • Gin 文件上传
  • Gin 中的 Cookie
  • Gin 中的 Session
  • Gin 中使用 GORM 操作 mysql 数据库
  • 原生 SQL 和 SQL 生成器
  • Gin 中使用 GORM 实现表关联查询
  • GORM 中使用事务
  • Gin 中使用 go-ini 加载.ini 配置文件
  • 《Gin》笔记
Salmon
2025-04-03
目录

Gin 中自定义控制器

# 1、控制器分组

当我们的项目比较大的时候有必要对我们的控制器进行分组 新建 controller/admin/NewsController.go

package admin

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

type NewsController struct {
}

func (c NewsController) Index(ctx *gin.Context) {
    ctx.String(http.StatusOK, "新闻首页")
}

新建 controller/admin/UserController.go

package admin

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

type UserController struct {
}

func (u UserController) Index(ctx *gin.Context) {
    ctx.String(http.StatusOK, "这是用户首页")
}

func (u UserController) Add(ctx *gin.Context) {
    ctx.String(http.StatusOK, "增加用户")
}

......

配置对应的路由 -- adminRoutes.go

其他路由的配置方法类似

package routes

import (
    "github.com/gin-gonic/gin"
    "test4/controller/admin"
)

func AdminRoutesInit(router *gin.Engine) {
    adminRouter := router.Group("/admin")
    {
       adminRouter.GET("/user", admin.UserController{}.Index)
       adminRouter.GET("/user/add", admin.UserController{}.Add)
       adminRouter.GET("/news", admin.NewsController{}.Index)
    }
}

效果:

image-20250403155956465

# 2、控制器的继承

1、新建 controller/admin/BaseController.go

package admin

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

type BaseController struct {
}

func (c BaseController) Success(ctx *gin.Context) {
    ctx.String(http.StatusOK, "成功")
}

func (c BaseController) Error(ctx *gin.Context) {
    ctx.String(http.StatusOK, "失败")
}

2、NewsController 继承 BaseController,继承后就可以调用控制器里面的公共方法了

package admin

import (
    "github.com/gin-gonic/gin"
)

type NewsController struct {
    BaseController
}

func (c NewsController) Index(ctx *gin.Context) {
    c.Success(ctx)
}
上次更新: 2025/04/03, 16:12:12
路由详解
Gin 中间件

← 路由详解 Gin 中间件→

Theme by Vdoing | Copyright © 2022-2025 Salmon's Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式