Web框架之Gin

Gin是一个用Go语言编写的web框架。它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter,速度提高了近40倍。 如果你是性能和高效的追求者, 你会爱上Gin

Gin框架介绍

Go世界里最流行的Web框架,Github上有24K+star。 基于httprouter开发的Web框架。 中文文档齐全,简单易用的轻量级框架。

Gin框架安装与使用

安装

下载并安装Gin:

1
go get -u github.com/gin-gonic/gin

第一个Gin示例:

package main
import (
​    "github.com/gin-gonic/gin"
)

func main() {
​    // 创建一个默认的路由引擎
​    r := gin.Default()
​    // GET:请求方式;/hello:请求的路径// 当客户端以GET方法请求/hello路径时,会执行后面的匿名函数
​    r.GET("/hello", func(c *gin.Context) {
​        // c.JSON:返回JSON格式的数据
​        c.JSON(200, gin.H{
​            "message": "Hello world!",
​        })
​    })
​    // 启动HTTP服务,默认在0.0.0.0:8080启动服务
​    r.Run()
}
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

<p>将上面的代码保存并编译执行,然后使用浏览器打开<code>127.0.0.1:8080/hello</code>就能看到一串JSON字符串。</p>

# RESTful API

<p>REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”或“表现层状态转化”。</p>

<p>推荐阅读<a href="http://www.ruanyifeng.com/blog/2011/09/restful.html">阮一峰 理解RESTful架构</a></p>

<p>简单来说,REST的含义就是客户端与Web服务器之间进行交互的时候,使用HTTP协议中的4个请求方法代表不同的动作。</p>

<ul>
<li><code>GET</code>用来获取资源</li>
<li><code>POST</code>用来新建资源</li>
<li><code>PUT</code>用来更新资源</li>
<li><code>DELETE</code>用来删除资源。</li>
</ul>

<p>只要API程序遵循了REST风格,那就可以称其为RESTful API。目前在前后端分离的架构中,前后端基本都是通过RESTful API来进行交互。</p>

<p>例如,我们现在要编写一个管理书籍的系统,我们可以查询对一本书进行查询、创建、更新和删除等操作,我们在编写程序的时候就要设计客户端浏览器与我们Web服务端交互的方式和路径。按照经验我们通常会设计成如下模式:</p>

<table>
<thead>
<tr>
<th>请求方法</th>
<th>URL</th>
<th>含义</th>
</tr>
</thead>

<tbody>
<tr>
<td>GET</td>
<td>/book</td>
<td>查询书籍信息</td>
</tr>

<tr>
<td>POST</td>
<td>/create_book</td>
<td>创建书籍记录</td>
</tr>

<tr>
<td>POST</td>
<td>/update_book</td>
<td>更新书籍信息</td>
</tr>

<tr>
<td>POST</td>
<td>/delete_book</td>
<td>删除书籍信息</td>
</tr>
</tbody>
</table>

<p>同样的需求我们按照RESTful API设计如下:</p>

<table>
<thead>
<tr>
<th>请求方法</th>
<th>URL</th>
<th>含义</th>
</tr>
</thead>

<tbody>
<tr>
<td>GET</td>
<td>/book</td>
<td>查询书籍信息</td>
</tr>

<tr>
<td>POST</td>
<td>/book</td>
<td>创建书籍记录</td>
</tr>

<tr>
<td>PUT</td>
<td>/book</td>
<td>更新书籍信息</td>
</tr>

<tr>
<td>DELETE</td>
<td>/book</td>
<td>删除书籍信息</td>
</tr>
</tbody>
</table>

<p>Gin框架支持开发RESTful API的开发。</p>

​```go
func main() {
	r := gin.Default()
	r.GET("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "GET",
		})
	})

	r.POST("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "POST",
		})
	})

	r.PUT("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "PUT",
		})
	})

	r.DELETE("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "DELETE",
		})
	})
}

开发RESTful API的时候我们通常使用Postman来作为客户端的测试工具。

# Gin渲染 # HTML渲染

我们首先定义一个存放模板文件的templates文件夹,然后在其内部按照业务分别定义一个posts文件夹和一个users文件夹。 posts/index.html文件的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<pre><code class="language-template">{{define &quot;posts/index.html&quot;}}
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;posts/index&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

    {{.title}}

&lt;/body&gt;
&lt;/html&gt;
{{end}}

users/index.html文件的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<pre><code class="language-template">{{define &quot;users/index.html&quot;}}
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;users/index&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    {{.title}}
&lt;/body&gt;
&lt;/html&gt;
{{end}}

Gin框架中使用LoadHTMLGlob()或者LoadHTMLFiles()方法进行HTML模板渲染。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func main() {
	r := gin.Default()
	r.LoadHTMLGlob(&quot;templates/**/*&quot;)
	//r.LoadHTMLFiles(&quot;templates/posts/index.html&quot;, &quot;templates/users/index.html&quot;)
	r.GET(&quot;/posts/index&quot;, func(c *gin.Context) {
		c.HTML(http.StatusOK, &quot;posts/index.html&quot;, gin.H{
			&quot;title&quot;: &quot;posts/index&quot;,
		})
	})

	r.GET(&quot;users/index&quot;, func(c *gin.Context) {
		c.HTML(http.StatusOK, &quot;users/index.html&quot;, gin.H{
			&quot;title&quot;: &quot;users/index&quot;,
		})
	})

	r.Run(&quot;:8080&quot;)
}
# 静态文件处理

当我们渲染的HTML文件中引用了静态文件时,我们只需要按照以下方式在渲染页面前调用gin.Static方法即可。

1
2
3
4
5
6
7
func main() {
	r := gin.Default()
	r.Static(&quot;/static&quot;, &quot;./static&quot;)
	r.LoadHTMLGlob(&quot;templates/**/*&quot;)
   ...
	r.Run(&quot;:8080&quot;)
}
# 补充文件路径处理

关于模板文件和静态文件的路径,我们需要根据公司/项目的要求进行设置。可以使用下面的函数获取当前执行程序的路径。

1
2
3
4
5
6
func getCurrentPath() string {
	if ex, err := os.Executable(); err == nil {
		return filepath.Dir(ex)
	}
	return &quot;./&quot;
}
# JSON渲染
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func main() {
	r := gin.Default()

	// gin.H 是map[string]interface{}的缩写
	r.GET(&quot;/someJSON&quot;, func(c *gin.Context) {
		// 方式一:自己拼接JSON
		c.JSON(http.StatusOK, gin.H{&quot;message&quot;: &quot;Hello world!&quot;})
	})
	r.GET(&quot;/moreJSON&quot;, func(c *gin.Context) {
		// 方法二:使用结构体
		var msg struct {
			Name    string `json:&quot;user&quot;`
			Message string
			Age     int
		}
		msg.Name = &quot;小王子&quot;
		msg.Message = &quot;Hello world!&quot;
		msg.Age = 18
		c.JSON(http.StatusOK, msg)
	})
	r.Run(&quot;:8080&quot;)
}
# XML渲染

注意需要使用具名的结构体类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func main() {
	r := gin.Default()
	// gin.H 是map[string]interface{}的缩写
	r.GET(&quot;/someXML&quot;, func(c *gin.Context) {
		// 方式一:自己拼接JSON
		c.XML(http.StatusOK, gin.H{&quot;message&quot;: &quot;Hello world!&quot;})
	})
	r.GET(&quot;/moreXML&quot;, func(c *gin.Context) {
		// 方法二:使用结构体
		type MessageRecord struct {
			Name    string
			Message string
			Age     int
		}
		var msg MessageRecord
		msg.Name = &quot;小王子&quot;
		msg.Message = &quot;Hello world!&quot;
		msg.Age = 18
		c.XML(http.StatusOK, msg)
	})
	r.Run(&quot;:8080&quot;)
}
# YMAL渲染
1
2
3
r.GET(&quot;/someYAML&quot;, func(c *gin.Context) {
	c.YAML(http.StatusOK, gin.H{&quot;message&quot;: &quot;ok&quot;, &quot;status&quot;: http.StatusOK})
})
# protobuf渲染
1
2
3
4
5
6
7
8
9
10
11
12
r.GET(&quot;/someProtoBuf&quot;, func(c *gin.Context) {
	reps := []int64{int64(1), int64(2)}
	label := &quot;test&quot;
	// protobuf 的具体定义写在 testdata/protoexample 文件中。
	data := &amp;protoexample.Test{
		Label: &amp;label,
		Reps:  reps,
	}
	// 请注意,数据在响应中变为二进制数据
	// 将输出被 protoexample.Test protobuf 序列化了的数据
	c.ProtoBuf(http.StatusOK, data)
})
# 获取参数 # 获取querystring参数

querystring指的是URL中?后面携带的参数,例如:/user/search?username=小王子&address=沙河。 获取请求的querystring参数的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func main() {
	//Default返回一个默认的路由引擎
	r := gin.Default()
	r.GET(&quot;/user/search&quot;, func(c *gin.Context) {
		username := c.DefaultQuery(&quot;username&quot;, &quot;小王子&quot;)
		//username := c.Query(&quot;username&quot;)
		address := c.Query(&quot;address&quot;)
		//输出json结果给调用方
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;:  &quot;ok&quot;,
			&quot;username&quot;: username,
			&quot;address&quot;:  address,
		})
	})

	r.Run()
}
# 获取form参数

请求的数据通过form表单来提交,例如向/user/search发送一个POST请求,获取请求数据的方式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func main() {
	//Default返回一个默认的路由引擎
	r := gin.Default()
	r.POST(&quot;/user/search&quot;, func(c *gin.Context) {
		// DefaultPostForm取不到值时会返回指定的默认值
		//username := c.DefaultPostForm(&quot;username&quot;, &quot;小王子&quot;)
		username := c.PostForm(&quot;username&quot;)
		address := c.PostForm(&quot;address&quot;)
		//输出json结果给调用方
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;:  &quot;ok&quot;,
			&quot;username&quot;: username,
			&quot;address&quot;:  address,
		})
	})
	r.Run(&quot;:8080&quot;)
}
# 获取path参数

请求的参数通过URL路径传递,例如:/user/search/小王子/沙河。 获取请求URL路径中的参数的方式如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func main() {
	//Default返回一个默认的路由引擎
	r := gin.Default()
	r.GET(&quot;/user/search/:username/:address&quot;, func(c *gin.Context) {
		username := c.Param(&quot;username&quot;)
		address := c.Param(&quot;address&quot;)
		//输出json结果给调用方
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;:  &quot;ok&quot;,
			&quot;username&quot;: username,
			&quot;address&quot;:  address,
		})
	})

	r.Run(&quot;:8080&quot;)
}
# 参数绑定

为了能够更方便的获取请求相关参数,提高开发效率,我们可以基于请求的content-type识别请求数据类型并利用反射机制自动提取请求中querystring、form表单、JSON、XML等参数到结构体中。

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Binding from JSON
type Login struct {
	User     string `form:&quot;user&quot; json:&quot;user&quot; binding:&quot;required&quot;`
	Password string `form:&quot;password&quot; json:&quot;password&quot; binding:&quot;required&quot;`
}

func main() {
	router := gin.Default()

	// 绑定JSON的示例 ({&quot;user&quot;: &quot;q1mi&quot;, &quot;password&quot;: &quot;123456&quot;})
	router.POST(&quot;/loginJSON&quot;, func(c *gin.Context) {
		var login Login

		if err := c.ShouldBindJSON(&amp;login); err == nil {
			fmt.Printf(&quot;login info:%#v\n&quot;, login)
			c.JSON(http.StatusOK, gin.H{
				&quot;user&quot;:     login.User,
				&quot;password&quot;: login.Password,
			})
		} else {
			c.JSON(http.StatusBadRequest, gin.H{&quot;error&quot;: err.Error()})
		}
	})

	// 绑定form表单示例 (user=q1mi&amp;password=123456)
	router.POST(&quot;/loginForm&quot;, func(c *gin.Context) {
		var login Login
		// ShouldBind()会根据请求的Content-Type自行选择绑定器
		if err := c.ShouldBind(&amp;login); err == nil {
			c.JSON(http.StatusOK, gin.H{
				&quot;user&quot;:     login.User,
				&quot;password&quot;: login.Password,
			})
		} else {
			c.JSON(http.StatusBadRequest, gin.H{&quot;error&quot;: err.Error()})
		}
	})

	// 绑定querystring示例 (user=q1mi&amp;password=123456)
	router.GET(&quot;/loginForm&quot;, func(c *gin.Context) {
		var login Login
		// ShouldBind()会根据请求的Content-Type自行选择绑定器
		if err := c.ShouldBind(&amp;login); err == nil {
			c.JSON(http.StatusOK, gin.H{
				&quot;user&quot;:     login.User,
				&quot;password&quot;: login.Password,
			})
		} else {
			c.JSON(http.StatusBadRequest, gin.H{&quot;error&quot;: err.Error()})
		}
	})

	// Listen and serve on 0.0.0.0:8080
	router.Run(&quot;:8080&quot;)
}
# 文件上传 # 单个文件上传
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
func main() {
	router := gin.Default()
	// 处理multipart forms提交文件时默认的内存限制是32 MiB
	// 可以通过下面的方式修改
	// router.MaxMultipartMemory = 8 &lt;&lt; 20  // 8 MiB
	router.POST(&quot;/upload&quot;, func(c *gin.Context) {
		// 单个文件
		file, err := c.FormFile(&quot;file&quot;)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				&quot;message&quot;: err.Error(),
			})
			return
		}

		log.Println(file.Filename)
		dst := fmt.Sprintf(&quot;C:/tmp/%s&quot;, file.Filename)
		// 上传文件到指定的目录
		c.SaveUploadedFile(file, dst)
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;: fmt.Sprintf(&quot;'%s' uploaded!&quot;, file.Filename),
		})
	})
	router.Run()
}
# 多个文件上传
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func main() {
	router := gin.Default()
	// 处理multipart forms提交文件时默认的内存限制是32 MiB
	// 可以通过下面的方式修改
	// router.MaxMultipartMemory = 8 &lt;&lt; 20  // 8 MiB
	router.POST(&quot;/upload&quot;, func(c *gin.Context) {
		// Multipart form
		form, _ := c.MultipartForm()
		files := form.File[&quot;file&quot;]

		for index, file := range files {
			log.Println(file.Filename)
			dst := fmt.Sprintf(&quot;C:/tmp/%s_%d&quot;, file.Filename, index)
			// 上传文件到指定的目录
			c.SaveUploadedFile(file, dst)
		}
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;: fmt.Sprintf(&quot;%d files uploaded!&quot;, len(files)),
		})
	})
	router.Run()
}
# Gin中间件

Gin框架允许开发者在处理请求的过程中,加入用户自己的钩子(Hook)函数。这个钩子函数就叫中间件,中间件适合处理一些公共的业务逻辑,比如登录校验、日志打印、耗时统计等。

Gin中的中间件必须是一个gin.HandlerFunc类型。例如我们像下面的代码一样定义一个中间件。

1
2
3
4
5
6
7
8
9
10
11
12
// StatCost 是一个统计耗时请求耗时的中间件
func StatCost() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		c.Set(&quot;name&quot;, &quot;小王子&quot;)
		// 执行其他中间件
		c.Next()
		// 计算耗时
		cost := time.Since(start)
		log.Println(cost)
	}
}

然后注册中间件的时候,可以在全局注册。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func main() {
	// 新建一个没有任何默认中间件的路由
	r := gin.New()
	// 注册一个全局中间件
	r.Use(StatCost())
	
	r.GET(&quot;/test&quot;, func(c *gin.Context) {
		name := c.MustGet(&quot;name&quot;).(string)
		log.Println(name)
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;: &quot;Hello world!&quot;,
		})
	})
	r.Run()
}

也可以给某个路由单独注册中间件。

1
2
3
4
5
6
7
8
// 给/test2路由单独注册中间件(可注册多个)
	r.GET(&quot;/test2&quot;, StatCost(), func(c *gin.Context) {
		name := c.MustGet(&quot;name&quot;).(string)
		log.Println(name)
		c.JSON(http.StatusOK, gin.H{
			&quot;message&quot;: &quot;Hello world!&quot;,
		})
	})
# 重定向 # HTTP重定向

HTTP 重定向很容易。 内部、外部重定向均支持。

1
2
3
r.GET(&quot;/test&quot;, func(c *gin.Context) {
	c.Redirect(http.StatusMovedPermanently, &quot;http://www.google.com/&quot;)
})
# 路由重定向

路由重定向,使用HandleContext

1
2
3
4
5
6
7
8
r.GET(&quot;/test&quot;, func(c *gin.Context) {
    // 指定重定向的URL
    c.Request.URL.Path = &quot;/test2&quot;
    r.HandleContext(c)
})
r.GET(&quot;/test2&quot;, func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{&quot;hello&quot;: &quot;world&quot;})
})
# Gin路由 # 普通路由
1
2
3
r.GET(&quot;/index&quot;, func(c *gin.Context) {...})
r.GET(&quot;/login&quot;, func(c *gin.Context) {...})
r.POST(&quot;/login&quot;, func(c *gin.Context) {...})

此外,还有一个可以匹配所有请求方法的Any方法如下:

1
r.Any(&quot;/test&quot;, func(c *gin.Context) {...})

为没有配置处理函数的路由添加处理程序。默认情况下它返回404代码。

1
2
3
r.NoRoute(func(c *gin.Context) {
		c.HTML(http.StatusNotFound, &quot;views/404.html&quot;, nil)
	})
# 路由组

我们可以将拥有共同URL前缀的路由划分为一个路由组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func main() {
	r := gin.Default()
	userGroup := r.Group(&quot;/user&quot;)
	{
		userGroup.GET(&quot;/index&quot;, func(c *gin.Context) {...})
		userGroup.GET(&quot;/login&quot;, func(c *gin.Context) {...})
		userGroup.POST(&quot;/login&quot;, func(c *gin.Context) {...})

	}
	shopGroup := r.Group(&quot;/shop&quot;)
	{
		shopGroup.GET(&quot;/index&quot;, func(c *gin.Context) {...})
		shopGroup.GET(&quot;/cart&quot;, func(c *gin.Context) {...})
		shopGroup.POST(&quot;/checkout&quot;, func(c *gin.Context) {...})
	}
	r.Run()
}

通常我们将路由分组用在划分业务逻辑或划分API版本时。

# 路由原理

Gin框架中的路由使用的是httprouter这个库。

其基本原理就是构造一个路由地址的前缀树。

posted @   那就凑个整吧  阅读(191)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示