Gin + Vue全栈开发实战(二)

      尝试地写了第一篇自己学习 Go Web 框架的感受和入门的文章,发现反响还不错,大家也提出了很多的问题来一起交流。近期也渐渐地出现了很多有关 go 语言开发的相关文章,包括有在蚂蚁金服的大牛的分享,我也一直有在看博客园和学习,这里越来越多人的去学习和使用 Go,感觉也是非常好的趋势。希望和大家一起来继续学习。

 

Gin 路由

本章概要

  • 路由 (Router) 介绍
  • Handler(处理器) 介绍

 

2.1 路由 (Router) 介绍

        路由是一个非常重要的概念,所有的接口都要有路由来进行管理。虽然传统的 J2EE(通过 Spring 框架) 和.Net(ABP 框架) 通过注解已经将这个概念给弱化了,但是无论是 Python(Django) 和 PHP(Laravel) 都还是非常强调路由的重要性的。所有的接口都必须通过路由来指向,现在包括很多的前端框架如 React 和 Vue 也都已经添加这个路由的方式。Gin 的路由是基于 httprouter 进行开发,有非常好的性能和表现力。

2.1.1 使用路由的示例

Gin 的路由支持 GET , POST , PUT , DELETE , PATCH , HEAD , OPTIONS 请求,同时还有一个 Any 函数,可以同时支持以上的所有请求。(分别对应 SpringMvc 框架中的 @GetMapping, @PostMapping, @PutMapping, @DeleteMapping,@PatchMapping, 无, 无和 @RequestMapping 注解),其使用方式大同小异,我们可以通过以下代码添加对应的路由:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 添加 Get 请求路由
engine.GET("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin get method")
})
// 添加 Post 请求路由
engine.POST("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin post method")
})
// 添加 Put 请求路由
engine.PUT("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin put method")
})
// 添加 Delete 请求路由
engine.DELETE("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin delete method")
})
// 添加 Patch 请求路由
engine.PATCH("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin patch method")
})
// 添加 Head 请求路由
engine.HEAD("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin head method")
})
// 添加 Options 请求路由
engine.OPTIONS("/", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin options method")
})
// 添加处理任意方法的请求路由
engine.Any("/hello", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin any method")
})
// 使用Handle方法添加 Get 请求路由
engine.Handle("GET", "/ping", func(context *gin.Context) {
    context.String(http.StatusOK, "hello gin handle get method")
})

以上接口大家启动应用后可以在浏览器和 Postman 等工具进行尝试。

2.1.2 获取各种参数

1. 获取 Query 参数 (url 参数)

Gin 中使用 Query 方法获取 url 参数:

1
2
3
4
engine.GET("/user", func(context *gin.Context) {
    name := context.Query("name")
    context.String(http.StatusOK, "hello " + name)
})

运行后在浏览器中访问 “http://localhost:8080/user?name=itachizhu" 就可以看到相关的结果了。

2. 获取 Form 表单参数

Gin 中使用 PostForm 获取表单参数 (Content-Type=application/x-www-form-urlencoded), 使用 FormFile 获取表单提交的文件参数 (Content-Type=multipart/form-data)

1
2
3
4
engine.POST("/user", func(context *gin.Context) {
    name := context.PostForm("name")
    context.String(http.StatusOK, "hello " + name)
})

运行后在 Postman 工具中访问相关的接口后可以看到相关的结果。

3. 获取请求 Body 中 json 串

Gin 中使用 BindJSON 方法可以将请求中的 json 数据反序列化为对象或者 map 和 slice(Content-Type=application/json)

1
2
3
4
5
6
7
8
engine.PUT("/user", func(context *gin.Context) {
    var m map[string]string
    if err := context.BindJSON(&m); err != nil {
        context.String(http.StatusInternalServerError, "error data!")
        return
    }
    context.String(http.StatusOK, "hello " + m["name"])
})

运行后在 Postman 工具中访问相关的接口后可以看到相关的结果。

4. 获取路劲参数

Gin 中使用 Param 方法获取路径参数:

1
2
3
4
engine.GET("/user/:name", func(context *gin.Context) {
    name := context.Param("name")
    context.String(http.StatusOK, "hello " + name)
})

运行后在浏览器中访问 “http://localhost:8080/user/itachizhu" 就可以看到相关的结果了。

2.1.3 路由分组

Gin 可以将请求路径前面相同归并为组的概念 (就和在 SpringMVC 的 Controller 类中注解 @RequestMapping 中添加公共路径是一样的)

1
2
3
4
5
6
admin := engine.Group("/admin")
{
    admin.Any("/hello", func(context *gin.Context) {
        context.String(http.StatusOK, "hello we are admin group!")
    })
}

本节代码地址  

 

2.2 Handler(处理器) 介绍

        经过上面简单的例子的演示和操作,现在我们大概可以了解到路由需要传入两个参数,一个为路径,另一个为路由执行的方法,我们叫做它处理器 Handler(在 J2EE 中我们通过叫它 Action 或者 Controller),而且,该参数是可变长参数。也就是说,可以传入多个 handler,形成一条 handler chain 。同时对 handler 该函数有着一些要求,该函数需要传入一个 Gin.Context 指针,同时要通过该指针进行值得处理。Handler 函数可以对前端返回 字符串,Json,Html 等多种格式或形式文件,之后我们会慢慢逐一介绍。

        因为 Go 本身支持函数式编程,所以很多就直接用匿名函数方式直接作为参数传入方法中去了。在真正的编程中,我们也通常会将它抽象成 mvc 模式,由另外的包方法中进行承载。

        下面我们就用已学到知识,先将 Gin 进行 Router(路由) 和 Controller(控制器) 的抽象,渐渐形成和其他语言框架那样的 MVC 模式。

        项目结构入如图 2-1 所示:

        图 2-1

本节代码地址

 

2.3 小结

        本章主要向读者介绍了 Gin 的路由和处理器,通过简单的路由的使用,基本明白了路由在 Gin 中的地位,也对一些常见的使用方式有了一些直观的认识。并且能够使用面向对象的思维将路由和处理器抽象成 MVC 模式。第 3 章将向读者介绍使用模板整合视图层的技术。