Gin框架快速上手使用

1 . 环境配置

1. Gin 下载 使用镜像源

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
​
go get -u github.com/gin-gonic/gin

2 . 初步上手

package main
​
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
​
func main() {
    r := gin.Default() // 生成一个实例
​
    r.GET("/hello", func(context *gin.Context) {   // 设置路由
        context.String(http.StatusOK, "hello Gin")
​
    })
    r.Run(":9999") // 启动端口号
}

2 . 实操

1 . 路由 Router

// 多种路由方式
r.GET("/hello", func(context *gin.Context) {
    context.String(http.StatusOK, "hello Gin")
})
r.POST("/user/list", func(context *gin.Context) {
    context.String(http.StatusOK, "user list")
})
r.PUT("/user/put", func(context *gin.Context) {
    context.String(http.StatusOK, "user put")
})
r.DELETE("/user/delete", func(context *gin.Context) {
    context.String(http.StatusOK, "user delete")
})

路由分组

user := r.Group("/user")
{
    user.GET("/hello", func(context *gin.Context) {
        context.String(http.StatusOK, "hello Gin")
​
    })
    user.POST("/post", func(context *gin.Context) {
        context.String(http.StatusOK, "user post")
    })
    user.PUT("/put", func(context *gin.Context) {
        context.String(http.StatusOK, "user put")
    })
    user.DELETE("/delete", func(context *gin.Context) {
        context.String(http.StatusOK, "user delete")
    })
}

2 . 实现Json返回并统一返回数据格式

// JsonStruct 定义一个返回结果格式
type JsonStruct struct {
    Code  int         `json:"code"`
    Msg   interface{} `json:"msg"`
    Data  interface{} `json:"data"`
    Count int64       `json:"count"`
}
​
// ReturnSuccess 成功响应返回数据
func ReturnSuccess(c *gin.Context, code int, msg interface{}, data interface{}, count int64) {
    json := &JsonStruct{
        code, msg, data, count,
    }
    c.JSON(200, json)
}
​
// ReturnError 失败响应结果
func ReturnError(c *gin.Context, code int, msg interface{}) {
    json := &JsonStruct{
        Code: code, Msg: msg,
    }
    c.JSON(200, json)
}

3 . 实现控制器Controller

// controller 中实现方法
// 但是这种方式有一个弊端,多个Controller 中如果有相同的方法就会报错,所以我们可以使用一个结构体来维护这些方法
func GetUserInfo(c *gin.Context) {
    ReturnSuccess(c, 200, "ydy", "2024-8-12", 1)
}
​
// 在router中调用方法
user.GET("/hello", controllers.GetUserInfo) /// 直接调用 Controller中实现的方法
​
​
// 结构体定义
type UserController struct {
}
​
func (u UserController) GetUserInfo(c *gin.Context) {
    ReturnSuccess(c, 200, "ydy", "2024-8-12", 1)
}
​
func (u UserController) GetUserList(c *gin.Context) {
    ReturnError(c, 200, "信息不存在")
}
​
// 通过上面这个方式就可以避免多个Controller之间的方法冲突
// 调用方式:
user.GET("/hello", controllers.UserController{}.GetUserInfo) /// 直接调用 Controller中实现的方法
user.POST("/post", controllers.UserController{}.GetUserList)
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值