当前位置:网站首页>gin-gonic/gin使用详解
gin-gonic/gin使用详解
2022-08-10 09:49:00 【dream21st】
文章目录
Gin 是一个用 Go (Golang) 编写的 HTTP web 框架。 它是一个类似于 martini 但拥有更好性能的 API 框架, 由于 httprouter,速度提高了近 40 倍。如果你需要极好的性能,使用 Gin 吧。
gin的github地址:https://github.com/gin-gonic/gin
在项目中使用gin框架只需要下面三步:
第一步:运行下面指令安装gin(注意go的版本要在1.15以上)
go get -u github.com/gin-gonic/gin
第二步:在项目中导入包
import "github.com/gin-gonic/gin"
第三步:一般情况下,还需要导入net/http
import "net/http"
1 编写一个例子
我们编写一个例子,代码内容如下:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
//创建一个默认的引擎实例
engine := gin.Default()
//定义一个请求url为ask的GET请求
engine.GET("/ask", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"message": "hello world!",
})
})
//在8080端口启动服务
engine.Run(":8080")
}
启动上面代码,打开浏览器在上面输入http://127.0.0.1:8080/ask,看到响应结果,至此一个简单的gin服务器搭建完成。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zijU84ji-1660057955069)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809220546432.png)]](https://img-blog.csdnimg.cn/ec8f5ea4b65c4eec98c578aab992b975.png)
2 gin的api
在gin中可以使用GET, POST, PUT, PATCH, DELETE 和OPTIONS的方法,使用方式如下:
func main() {
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.GET("/someGet", getting)
router.POST("/somePost", posting)
router.PUT("/somePut", putting)
router.DELETE("/someDelete", deleting)
router.PATCH("/somePatch", patching)
router.HEAD("/someHead", head)
router.OPTIONS("/someOptions", options)
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run()
// router.Run(":3000") for a hard coded port
}
3 GET请求传递参数
下面通过三个例子来演示一下GET请求的参数获取。
3.1 例子一
绑定一个router的url,并建立一个处理函数handleGetParam:
engine := gin.Default()
//http://127.0.0.1:8080/v1/wangwu/17
engine.GET("/v1/:name/:age", handleGetParam)
/** * 通过context.Param直接拿到请求中的参数 */
func handleGetParam(context *gin.Context) {
age := context.Param("age")
name := context.Param("name") //context.DefaultQuery("name","0") 没有值就默认
context.JSON(http.StatusOK, gin.H{
"age": age,
"name": name,
})
}
启动项目,在浏览器中输入http://127.0.0.1:8080/v1/wangwu/17,浏览器响应结果如下,满足预期,代表参数的传值正常取到。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-avEkSX8C-1660057955073)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809224210679.png)]](https://img-blog.csdnimg.cn/37955479828649c6a23bf679b2cb4fc9.png)
3.2 例子二
绑定一个router的url,并建立一个处理函数handleV2GetParam,在这个例子中还需要创建一个Student的结构体。
//http://127.0.0.1:8080/v2/wangwu/17
engine.GET("/v2/:name/:age", handleV2GetParam)
type Student struct {
Age int `uri:"age" binding:"required"`
Name string `uri:"name" binding:"required"`
}
/** * 通过绑定的方式获取参数 */
func handleV2GetParam(context *gin.Context) {
var student Student
if err := context.ShouldBindUri(&student); err != nil {
context.Status(404)
return
}
context.JSON(http.StatusOK, gin.H{
"name": student.Name,
"age": student.Age,
})
}
启动项目,在浏览器中输入http://127.0.0.1:8080/v2/wangwu/17,浏览器响应结果如下,满足预期,代表参数的传值正常取到。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tpqINqvQ-1660057955074)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809224559475.png)]](https://img-blog.csdnimg.cn/d5bad83bbdaf417db4c63cdf5d33bd91.png)
3.3 例子三
绑定一个router的url,并建立一个处理函数handleV3GetParam,这个例子是通过?在请求url后面拼接参数。
//http://127.0.0.1:8080/v3?name=wangwu&age=18
engine.GET("/v3", handleV3GetParam)
/** * 获取?形式传参的数据 */
func handleV3GetParam(context *gin.Context) {
age := context.Query("age")
name := context.Query("name")
context.JSON(http.StatusOK, gin.H{
"age": age,
"name": name,
})
}
启动项目,在浏览器上输入http://127.0.0.1:8080/v3?name=wangwu&age=18也可以拿到预期的结果。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mExj2643-1660057955077)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809224922398.png)]](https://img-blog.csdnimg.cn/447f166403c24b62b4b9559e3fcb8c6c.png)
4 POST请求
4.1 form表单传参
绑定一个router的url,并建立一个处理函数handleForm,对应代码如下:
//处理post请求from表单传参的方式
engine.POST("/v1", handleForm)
/** * post以form表单的形式发送请求 */
func handleForm(context *gin.Context) {
name := context.PostForm("name")
age := context.DefaultPostForm("age", string(18))
context.JSON(http.StatusOK, gin.H{
"age": age,
"name": name,
})
}
由于发送的是POST请求,我们可以在postman中进行测试,测试结果满足预期:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TTbHCUZI-1660057955078)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809225500148.png)]](https://img-blog.csdnimg.cn/4fe7515cf91946bb82821b7bcaa61f65.png)
4.2 json传参
绑定一个router的url,并建立一个处理函数handleJson,例子中使用到的Student结构体在上面例子有定义,其他对应代码如下:
//处理post请求json传参
engine.POST("/v2", handleJson)
/** * post以json的形式发送请求 */
func handleJson(context *gin.Context) {
var student Student
if err := context.ShouldBind(&student); err != nil {
fmt.Println(err.Error())
context.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
context.JSON(http.StatusOK, gin.H{
"name": student.Name,
"age": student.Age,
})
}
启动服务,进行测试,测试结果满足预期:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6rqXflsa-1660057955098)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809225808451.png)]](https://img-blog.csdnimg.cn/21ff3a8edfef4904b4acbcda1f212cfd.png)
4.3 上传单个文件
编写一个url,绑定的处理器handleFile,对应代码如下:
//处理文件上传(单文件上传)
engine.POST("/v3", handleFile)
/** * 单文件上传 */
func handleFile(context *gin.Context) {
file, err := context.FormFile("file")
if err != nil {
fmt.Println("产生错误:", err)
}
source := "D:\\code\\other\\go-project\\src\\project-study\\gin-study-01\\ch02\\" + file.Filename
if err := context.SaveUploadedFile(file, source); err != nil {
fmt.Println("保存文件出错:", err)
}
}
测试代码,接口调用成功之后会在指定目录下找到上传文件:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b26rQXBH-1660057955101)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809230223790.png)]](https://img-blog.csdnimg.cn/406e0f08dbdc4978ad63f33c82b5c907.png)
4.4 多文件上传
编写一个url,绑定的处理器handleFiles,对应代码如下:
//多文件上传
engine.POST("/v4", handleFiles)
/** * 多文件上传 */
func handleFiles(context *gin.Context) {
form, err := context.MultipartForm()
if err != nil {
fmt.Println("处理文件出错:", err)
return
}
files := form.File
for _, v := range files {
for _, f := range v {
s := f.Filename
source := "D:\\code\\other\\go-project\\src\\project-study\\gin-study-01\\ch02\\" + s
if err := context.SaveUploadedFile(f, source); err != nil {
fmt.Println("保存文件出错:", err)
}
}
}
values := form.Value
for k, v := range values {
fmt.Printf("key:%s,value:%v", k, v)
}
}
测试接口,本测试上传两个文件都成功了:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZQYMfdEo-1660057955102)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809230709806.png)]](https://img-blog.csdnimg.cn/41d365aa22a9403692a6f5f82e35dbcb.png)
5 接口组
在实际项目开发中,针对相同资源的操作请求的url的前缀一般都是一样的,在gin框架里面,我们可以通过Group实现,编写代码:
// 接口分组,这样可以保证请求接口url前缀一样
group := engine.Group("/openapi")
{
group.GET("/get", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"msg": "getsuccess",
})
})
group.POST("/post", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"msg": "postsuccess",
})
})
}
在上述例子中创建了一个Group,这个Group里面定义了两个接口,它们的请求前缀都需要带上openapi。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f0gLAtQo-1660057955104)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809231142330.png)]](https://img-blog.csdnimg.cn/7a447d7322954c4593ccbc2d4ca6a7fe.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QNCwfIIC-1660057955106)(D:\developsoftware\mayun\note\study-note\go\images\image-20220809231201043.png)]](https://img-blog.csdnimg.cn/9122e23957c84a3699dd1239a120d5b7.png)
边栏推荐
- UE4 Sequence添加基础动画效果 (04-在序列中使用粒子效果)
- Payment x Aggregation x Ledger Separation - The Way to Avoid Risk of "Erqing" on the Return Platform
- makefile 杂项
- Property animation QPropertyAnimation
- 【Prometheus】Node Exporter常用查询PromQL 语句大总结
- 数据库中的schema
- Tencent releases the second-generation version of the quadruped robot Max, which completes jumps and somersaults on the plum blossom pile
- 关于编程本质那些事
- 因子分析(SPSS)
- Flink快速上手 完整使用 (第二章)
猜你喜欢
随机推荐
The first offline workshop in 2022!Data application experience day for application developers is coming | TiDB Workshop Day
地平线:面向规模化量产的智能驾驶系统和软件开发
Relearn bubble sort
高通 msm8953 LCD 休眠/唤醒 流程
2022年固定资产管理系统的概况
90.(cesium之家)cesium高度监听事件
"Microservice Architecture" Arrangement and Choreography - Different Models for Making Systems Work Together
线程池的基本概念、结构、类
【数据架构】概念数据模型和逻辑数据模型有什么区别
web项目访问引用jar内部的静态资源
Static关键字及应用,继承的概念
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
10 【异步组件 组合式函数(hooks)】
【元宇宙欧米说】看UCOUCO如何将行为艺术融入元宇宙
多元线性回归分析(Stata)
[System Design] S3 Object Storage
LCD DRM component 框架分析
【Prometheus】Node Exporter常用查询PromQL 语句大总结
「应用架构」TOGAF建模:企业可管理性图
如何理解BIO、NIO、AIO的区别









