首页 > 其他分享 >Gin框架

Gin框架

时间:2022-12-29 14:12:56浏览次数:35  
标签:gt 框架 quot gin lt amp Gin td

<article class="post-article">     <section class="markdown-content"><p><code>Gin</code>是一个用Go语言编写的web框架。它是一个类似于<code>martini</code>但拥有更好性能的API框架, 由于使用了<code>httprouter</code>,速度提高了近40倍。 如果你是性能和高效的追求者, 你会爱上<code>Gin</code>。</p>   <h1 id="Gin框架介绍"><a href="#Gin框架介绍" class="headerlink" title="Gin框架介绍"></a>Gin框架介绍</h1><p>Go世界里最流行的Web框架,<a href="https://github.com/gin-gonic/gin" target="_blank" rel="noopener">Github</a>上有<code>24K+</code>star。 基于<a href="https://github.com/julienschmidt/httprouter" target="_blank" rel="noopener">httprouter</a>开发的Web框架。 <a href="https://gin-gonic.com/zh-cn/docs/" target="_blank" rel="noopener">中文文档</a>齐全,简单易用的轻量级框架。</p>   <h1 id="Gin框架安装与使用"><a href="#Gin框架安装与使用" class="headerlink" title="Gin框架安装与使用"></a>Gin框架安装与使用</h1><h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><p>下载并安装<code>Gin</code>:</p>   <figure class="highlight bash" data-lang="BASH"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="bash"><span class="line">1</span><br></code></pre></td><td class="code"><pre class="hljs cs"><code class="bash"><span class="line">go <span class="hljs-keyword">get</span> -u github.com/gin-gonic/gin</span><br></code></pre></td></tr></tbody></table></figure>   <h1 id="第一个Gin示例:"><a href="#第一个Gin示例:" class="headerlink" title="第一个Gin示例:"></a>第一个Gin示例:</h1><pre><code class="language-golang">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() }   <figure class="highlight plain" data-lang="PLAIN"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="plain"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></code></pre></td><td class="code"><pre class="hljs xml"><code class="plain"><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>将上面的代码保存并编译执行,然后使用浏览器打开<span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>127.0.0.1:8080/hello<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>就能看到一串JSON字符串。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"># RESTful API</span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”或“表现层状态转化”。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>推荐阅读<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://www.ruanyifeng.com/blog/2011/09/restful.html"</span>&gt;</span>阮一峰 理解RESTful架构<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>简单来说,REST的含义就是客户端与Web服务器之间进行交互的时候,使用HTTP协议中的4个请求方法代表不同的动作。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来获取资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来新建资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>PUT<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来更新资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>DELETE<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来删除资源。<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>只要API程序遵循了REST风格,那就可以称其为RESTful API。目前在前后端分离的架构中,前后端基本都是通过RESTful API来进行交互。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>例如,我们现在要编写一个管理书籍的系统,我们可以查询对一本书进行查询、创建、更新和删除等操作,我们在编写程序的时候就要设计客户端浏览器与我们Web服务端交互的方式和路径。按照经验我们通常会设计成如下模式:<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>请求方法<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>URL<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>含义<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>查询书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/create_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>创建书籍记录<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/update_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>更新书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/delete_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>删除书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>同样的需求我们按照RESTful API设计如下:<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>请求方法<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>URL<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>含义<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>查询书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>创建书籍记录<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>PUT<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>更新书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>DELETE<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>删除书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Gin框架支持开发RESTful API的开发。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">​```go</span><br><span class="line">func main() {</span><br><span class="line">r := gin.Default()</span><br><span class="line">r.GET("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "GET",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.POST("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "POST",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.PUT("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "PUT",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.DELETE("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "DELETE",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   <p>开发RESTful API的时候我们通常使用<a href="https://www.getpostman.com/" target="_blank" rel="noopener">Postman</a>来作为客户端的测试工具。</p>   # Gin渲染   # HTML渲染   <p>我们首先定义一个存放模板文件的<code>templates</code>文件夹,然后在其内部按照业务分别定义一个<code>posts</code>文件夹和一个<code>users</code>文件夹。 <code>posts/index.html</code>文件的内容如下:</p> <figure class="highlight html" data-lang="HTML"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="html"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs perl"><code class="html"><span class="line"><span class="tag">&lt;<span class="name">pre</span>&gt;</span><span class="tag">&lt;<span class="name">code</span> <span class="attr">class</span>=<span class="string"><span class="hljs-string">"language-template"</span></span>&gt;</span>{{define <span class="symbol">&amp;quot;</span>posts/index.html<span class="symbol">&amp;quot;</span>}}</span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>!DOCTYPE html<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>html lang=<span class="symbol">&amp;quot;</span>en<span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>head<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta charset=<span class="symbol">&amp;quot;</span>UTF-<span class="hljs-number">8</span><span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta name=<span class="symbol">&amp;quot;</span>viewport<span class="symbol">&amp;quot;</span> content=<span class="symbol">&amp;quot;</span>width=device-width, initial-scale=<span class="hljs-number">1.0</span><span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta http-equiv=<span class="symbol">&amp;quot;</span>X-UA-Compatible<span class="symbol">&amp;quot;</span> content=<span class="symbol">&amp;quot;</span>ie=edge<span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>title<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span>posts/<span class="hljs-keyword">index</span><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span><span class="hljs-regexp">/title</span><span class="symbol"><span class="hljs-regexp">&amp;gt;</span></span></span><br><span class="line"><span class="symbol"><span class="hljs-regexp">&amp;lt;</span></span><span class="hljs-regexp">/head</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>body<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"></span><br><span class="line">    {{.title}}</span><br><span class="line"></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span><span class="hljs-regexp">/body</span><span class="symbol"><span class="hljs-regexp">&amp;gt;</span></span></span><br><span class="line"><span class="symbol"><span class="hljs-regexp">&amp;lt;</span></span><span class="hljs-regexp">/html</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line">{<span class="hljs-string">{end}</span>}</span><br></code></pre></td></tr></tbody></table></figure>       <p><code>users/index.html</code>文件的内容如下:</p>     <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs perl"><code class="go"><span class="line">&lt;pre&gt;&lt;code class=<span class="string"><span class="hljs-string">"language-template"</span></span>&gt;{{define &amp;quot;users/index.html&amp;quot;}}</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;!DOCTYPE html&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;html lang=&amp;quot;en&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line"></span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;head&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta charset=&amp;quot;UTF<span class="number">-<span class="hljs-number">8</span></span>&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;width=device-width, initial-scale=<span class="number"><span class="hljs-number">1.0</span></span>&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta http-equiv=&amp;quot;X-UA-Compatible&amp;quot; content=&amp;quot;ie=edge&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;title&amp;<span class="hljs-keyword">gt</span>;users/<span class="hljs-keyword">index</span>&amp;<span class="hljs-keyword">lt</span>;<span class="hljs-regexp">/title&amp;gt;</span></span><br><span class="line"><span class="hljs-regexp">&amp;lt;/head</span>&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line"></span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;body&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    {{.title}}</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;<span class="hljs-regexp">/body&amp;gt;</span></span><br><span class="line"><span class="hljs-regexp">&amp;lt;/html</span>&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">{<span class="hljs-string">{end}</span>}</span><br></code></pre></td></tr></tbody></table></figure>   <p>Gin框架中使用<code>LoadHTMLGlob()</code>或者<code>LoadHTMLFiles()</code>方法进行HTML模板渲染。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.LoadHTMLGlob(&amp;quot;templates<span class="comment">/**<span class="hljs-regexp">/</span></span><span class="hljs-regexp">*&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/r.LoadHTMLFiles(&amp;quot;templates/posts</span><span class="hljs-regexp">/index.html&amp;quot;, &amp;quot;templates/users</span><span class="hljs-regexp">/index.html&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/posts</span><span class="hljs-regexp">/index&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.HTML(http.StatusOK, &amp;quot;posts/index</span>.html&amp;quot;, gin.H{</span><br><span class="line">&amp;quot;title&amp;quot;: &amp;quot;posts/index&amp;quot;,</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.GET(&amp;quot;users/index&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.HTML(http.StatusOK, &amp;quot;users/index.html&amp;quot;, gin.H{</span><br><span class="line">&amp;quot;title&amp;quot;: &amp;quot;users/index&amp;quot;,</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.Run(&amp;quot;<span class="hljs-symbol">:</span><span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   # 静态文件处理   <p>当我们渲染的HTML文件中引用了静态文件时,我们只需要按照以下方式在渲染页面前调用<code>gin.Static</code>方法即可。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></code></pre></td><td class="code"><pre class="hljs css"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-selector-tag">func</span></span> <span class="title"><span class="hljs-selector-tag">main</span></span><span class="params">()</span></span> {</span><br><span class="line"><span class="hljs-attribute">r </span>:= gin.<span class="hljs-built_in">Default</span>()</span><br><span class="line">r.<span class="hljs-built_in">Static</span>(&amp;quot;/static&amp;quot;, &amp;quot;./static&amp;quot;)</span><br><span class="line">r.<span class="hljs-built_in">LoadHTMLGlob</span>(&amp;quot;templates<span class="comment">/**/</span>*&amp;quot;)</span><br><span class="line">   ...</span><br><span class="line">r.<span class="hljs-built_in">Run</span>(&amp;quot;:<span class="number">8080</span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   # 补充文件路径处理   <p>关于模板文件和静态文件的路径,我们需要根据公司/项目的要求进行设置。可以使用下面的函数获取当前执行程序的路径。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-function">func</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-title">getCurrentPath</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">()</span></span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-built_in">string</span></span></span></span><span class="hljs-function"> </span>{</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> ex, err := os.Executable(); err == <span class="literal">nil</span> {</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span> filepath.Dir(ex)</span><br><span class="line">}</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span> &amp;quot;./&amp;quot;</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   # JSON渲染   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r := gin.Default()</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> gin.H 是map[string]interface{}的缩写</span></span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/someJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 方式一:自己拼接JSON</span></span><br><span class="line">c.JSON(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;})</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/moreJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 方法二:使用结构体</span></span><br><span class="line"><span class="keyword">var</span> msg <span class="keyword">struct</span> {</span><br><span class="line">Name    <span class="keyword">string</span> <span class="string">`<span class="javascript">json:&amp;quot;user&amp;quot;</span>`</span></span><br><span class="line">Message <span class="keyword">string</span></span><br><span class="line">Age     <span class="keyword">int</span></span><br><span class="line">}</span><br><span class="line">msg.Name = &amp;quot;小王子&amp;quot;</span><br><span class="line">msg.Message = &amp;quot;Hello world!&amp;quot;</span><br><span class="line">msg.Age = <span class="number"><span class="hljs-number">18</span></span></span><br><span class="line">c.JSON(http.StatusOK, msg)</span><br><span class="line">})</span><br><span class="line">r.Run(&amp;quot;:<span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   # XML渲染   <p>注意需要使用具名的结构体类型。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ gin.H 是map[string]interface{}的缩写</span></span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/some</span>XML&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 方式一:自己拼接JSON</span></span><br><span class="line">c.XML(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;})</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/moreXML&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 方法二:使用结构体</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">type</span></span><span class="hljs-regexp"> MessageRecord </span><span class="keyword"><span class="hljs-regexp">struct</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">Name    </span><span class="keyword"><span class="hljs-regexp">string</span></span></span><br><span class="line"><span class="hljs-regexp">Message </span><span class="keyword"><span class="hljs-regexp">string</span></span></span><br><span class="line"><span class="hljs-regexp">Age     </span><span class="keyword"><span class="hljs-regexp">int</span></span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> msg MessageRecord</span></span><br><span class="line"><span class="hljs-regexp">msg.Name = &amp;quot;小王子&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp">msg.Message = &amp;quot;Hello world!&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp">msg.Age = </span><span class="number"><span class="hljs-regexp">18</span></span></span><br><span class="line"><span class="hljs-regexp">c.XML(http.StatusOK, msg)</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # YMAL渲染   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs"><code class="go"><span class="line">r.GET(&amp;quot;/someYAML&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.YAML(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;ok&amp;quot;, &amp;quot;status&amp;quot;: http.StatusOK})</span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>   # protobuf渲染   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/someProtoBuf&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">reps := []</span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">{</span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">(</span><span class="number"><span class="hljs-regexp">1</span></span><span class="hljs-regexp">), </span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">(</span><span class="number"><span class="hljs-regexp">2</span></span><span class="hljs-regexp">)}</span></span><br><span class="line"><span class="hljs-regexp">label := &amp;quot;test&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ protobuf 的具体定义写在 testdata/</span>protoexample 文件中。</span></span><br><span class="line">data := &amp;amp;protoexample.Test{</span><br><span class="line"><span class="hljs-attr">Label</span>: &amp;amp;label,</span><br><span class="line"><span class="hljs-attr">Reps</span>:  reps,</span><br><span class="line">}</span><br><span class="line"><span class="comment"><span class="hljs-comment">// 请注意,数据在响应中变为二进制数据</span></span></span><br><span class="line"><span class="hljs-comment"></span><span class="comment"><span class="hljs-comment">// 将输出被 protoexample.Test protobuf 序列化了的数据</span></span></span><br><span class="line"><span class="hljs-comment">c.ProtoBuf(http.StatusOK, data)</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br></code></pre></td></tr></tbody></table></figure>   # 获取参数   # 获取querystring参数   <p><code>querystring</code>指的是URL中<code>?</code>后面携带的参数,例如:<code>/user/search?username=小王子&amp;address=沙河</code>。 获取请求的querystring参数的方法如下:</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/user/search</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">username <span class="hljs-symbol">:</span>= c.DefaultQuery(&amp;quot;username&amp;quot;, &amp;quot;小王子&amp;quot;)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/username := c.Query(&amp;quot;username&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">address := c.Query(&amp;quot;address&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp">r.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # 获取form参数   <p>请求的数据通过form表单来提交,例如向<code>/user/search</code>发送一个POST请求,获取请求数据的方式如下:</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.POST(&amp;quot;<span class="hljs-regexp">/user/search</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> DefaultPostForm取不到值时会返回指定的默认值</span></span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/username := c.DefaultPostForm(&amp;quot;username&amp;quot;, &amp;quot;小王子&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">username := c.PostForm(&amp;quot;username&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">address := c.PostForm(&amp;quot;address&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # 获取path参数   <p>请求的参数通过URL路径传递,例如:<code>/user/search/小王子/沙河</code>。 获取请求URL路径中的参数的方式如下。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/user/search</span><span class="hljs-regexp">/:username/</span><span class="hljs-symbol">:address&amp;quot</span>;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">username <span class="hljs-symbol">:</span>= c.Param(&amp;quot;username&amp;quot;)</span><br><span class="line">address <span class="hljs-symbol">:</span>= c.Param(&amp;quot;address&amp;quot;)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # 参数绑定   <p>为了能够更方便的获取请求相关参数,提高开发效率,我们可以基于请求的<code>content-type</code>识别请求数据类型并利用反射机制自动提取请求中<code>querystring</code>、form表单、JSON、XML等参数到结构体中。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Binding <span class="hljs-keyword">from</span> JSON</span></span><br><span class="line"><span class="keyword">type</span> Login <span class="keyword">struct</span> {</span><br><span class="line">User     <span class="keyword">string</span> <span class="string">`<span class="javascript">form:&amp;quot;user&amp;quot; json:&amp;quot;user&amp;quot; binding:&amp;quot;required&amp;quot;</span>`</span></span><br><span class="line">Password <span class="keyword">string</span> <span class="string">`<span class="javascript">form:&amp;quot;password&amp;quot; json:&amp;quot;password&amp;quot; binding:&amp;quot;required&amp;quot;</span>`</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router := gin.Default()</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 绑定JSON的示例 ({&amp;quot;user&amp;quot;: &amp;quot;q1mi&amp;quot;, &amp;quot;password&amp;quot;: &amp;quot;<span class="hljs-number">123456</span>&amp;quot;})</span></span><br><span class="line">router.POST(&amp;quot;<span class="hljs-regexp">/loginJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">if</span></span><span class="hljs-regexp"> err := c.ShouldBindJSON(&amp;amp;login); err == </span><span class="literal"><span class="hljs-regexp">nil</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">fmt.Printf(&amp;quot;login info:%#v\n&amp;quot;, login)</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;user&amp;quot;:     login.User,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;password&amp;quot;: login.Password,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">} </span><span class="keyword"><span class="hljs-regexp">else</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 绑定form表单示例 (user=q1mi&amp;amp;password=<span class="hljs-number">123456</span>)</span></span><br><span class="line">router.POST(&amp;quot;<span class="hljs-regexp">/loginForm&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ ShouldBind()会根据请求的Content-Type自行选择绑定器</span></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err := c.ShouldBind(&amp;amp;login); err == <span class="literal">nil</span> {</span><br><span class="line">c.JSON(http.StatusOK, gin.H{</span><br><span class="line">&amp;quot;user&amp;quot;:     login.User,</span><br><span class="line">&amp;quot;password&amp;quot;: login.Password,</span><br><span class="line">})</span><br><span class="line">} <span class="keyword"><span class="hljs-keyword">else</span></span> {</span><br><span class="line">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span><br><span class="line">}</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 绑定querystring示例 (user=q1mi&amp;amp;password=<span class="hljs-number">123456</span>)</span></span><br><span class="line">router.GET(&amp;quot;<span class="hljs-regexp">/loginForm&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ ShouldBind()会根据请求的Content-Type自行选择绑定器</span></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err := c.ShouldBind(&amp;amp;login); err == <span class="literal">nil</span> {</span><br><span class="line">c.JSON(http.StatusOK, gin.H{</span><br><span class="line">&amp;quot;user&amp;quot;:     login.User,</span><br><span class="line">&amp;quot;password&amp;quot;: login.Password,</span><br><span class="line">})</span><br><span class="line">} <span class="keyword"><span class="hljs-keyword">else</span></span> {</span><br><span class="line">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span><br><span class="line">}</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Listen <span class="hljs-keyword">and</span> serve <span class="hljs-literal">on</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">8080</span></span></span><br><span class="line">router.Run(&amp;quot;:<span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   # 文件上传   # 单个文件上传   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 处理multipart forms提交文件时默认的内存限制是32 MiB</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 可以通过下面的方式修改</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ router.MaxMultipartMemory = 8 &amp;lt;&amp;lt; 20  /</span><span class="hljs-regexp">/ 8 MiB</span></span></span><br><span class="line"><span class="hljs-regexp">router.POST(&amp;quot;/upload</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 单个文件</span></span><br><span class="line">file, err <span class="hljs-symbol">:</span>= c.FormFile(&amp;quot;file&amp;quot;)</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err != <span class="literal"><span class="hljs-literal">nil</span></span> {</span><br><span class="line">c.JSON(http.StatusInternalServerError, gin.H{</span><br><span class="line">&amp;quot;message&amp;quot;: err.Error(),</span><br><span class="line">})</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">log.Println(file.Filename)</span><br><span class="line">dst <span class="hljs-symbol">:</span>= fmt.Sprintf(&amp;quot;<span class="hljs-symbol">C:</span>/tmp/%s&amp;quot;, file.Filename)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 上传文件到指定的目录</span></span></span><br><span class="line"><span class="hljs-regexp">c.SaveUploadedFile(file, dst)</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;: fmt.Sprintf(&amp;quot;</span><span class="string"><span class="hljs-regexp">'%s'</span></span><span class="hljs-regexp"> uploaded!&amp;quot;, file.Filename),</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">router.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # 多个文件上传   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 处理multipart forms提交文件时默认的内存限制是32 MiB</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 可以通过下面的方式修改</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ router.MaxMultipartMemory = 8 &amp;lt;&amp;lt; 20  /</span><span class="hljs-regexp">/ 8 MiB</span></span></span><br><span class="line"><span class="hljs-regexp">router.POST(&amp;quot;/upload</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Multipart form</span></span><br><span class="line">form, <span class="hljs-number">_</span> <span class="hljs-symbol">:</span>= c.MultipartForm()</span><br><span class="line">files <span class="hljs-symbol">:</span>= form.File[&amp;quot;file&amp;quot;]</span><br><span class="line"></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">for</span></span> index, file <span class="hljs-symbol">:</span>= <span class="keyword">range</span> files {</span><br><span class="line">log.Println(file.Filename)</span><br><span class="line">dst <span class="hljs-symbol">:</span>= fmt.Sprintf(&amp;quot;<span class="hljs-symbol">C:</span>/tmp/%s<span class="hljs-number">_</span>%d&amp;quot;, file.Filename, index)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 上传文件到指定的目录</span></span></span><br><span class="line"><span class="hljs-regexp">c.SaveUploadedFile(file, dst)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;: fmt.Sprintf(&amp;quot;%d files uploaded!&amp;quot;, </span><span class="built_in"><span class="hljs-regexp">len</span></span><span class="hljs-regexp">(files)),</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">router.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   # Gin中间件   <p>Gin框架允许开发者在处理请求的过程中,加入用户自己的钩子(Hook)函数。这个钩子函数就叫中间件,中间件适合处理一些公共的业务逻辑,比如登录校验、日志打印、耗时统计等。</p>   <p>Gin中的中间件必须是一个<code>gin.HandlerFunc</code>类型。例如我们像下面的代码一样定义一个中间件。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="comment">/<span class="hljs-regexp">/ StatCost 是一个统计耗时请求耗时的中间件</span></span></span><br><span class="line"><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="hljs-regexp"> </span><span class="title"><span class="hljs-regexp">StatCost</span></span><span class="params"><span class="hljs-regexp">()</span></span><span class="hljs-regexp"> </span><span class="title"><span class="hljs-regexp">gin</span></span><span class="hljs-regexp">.</span><span class="title"><span class="hljs-regexp">HandlerFunc</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">return</span></span><span class="hljs-regexp"> </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">start := time.Now()</span></span><br><span class="line"><span class="hljs-regexp">c.Set(&amp;quot;name&amp;quot;, &amp;quot;小王子&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 执行其他中间件</span></span></span><br><span class="line"><span class="hljs-regexp">c.Next()</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 计算耗时</span></span></span><br><span class="line"><span class="hljs-regexp">cost := time.Since(start)</span></span><br><span class="line"><span class="hljs-regexp">log.Println(cost)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>   <p>然后注册中间件的时候,可以在全局注册。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-function">func</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-title">main</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">()</span></span></span></span><span class="hljs-function"> </span>{</span><br><span class="line"><span class="comment"><span class="hljs-comment">// 新建一个没有任何默认中间件的路由</span></span></span><br><span class="line"><span class="hljs-comment">r := gin.New()</span></span><br><span class="line"><span class="hljs-comment"></span><span class="comment"><span class="hljs-comment">// 注册一个全局中间件</span></span></span><br><span class="line"><span class="hljs-comment">r.Use(StatCost())</span></span><br><span class="line"><span class="hljs-comment"></span></span><br><span class="line"><span class="hljs-comment">r.GET(&amp;quot;/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-comment">func</span></span><span class="params"><span class="hljs-comment">(c *gin.Context)</span></span></span><span class="hljs-comment"> {</span></span><br><span class="line"><span class="hljs-comment">name := c.MustGet(&amp;quot;name&amp;quot;).(</span><span class="keyword"><span class="hljs-comment">string</span></span><span class="hljs-comment">)</span></span><br><span class="line"><span class="hljs-comment">log.Println(name)</span></span><br><span class="line"><span class="hljs-comment">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-comment">&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;,</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">r.Run()</span></span><br><span class="line"><span class="hljs-comment">}</span></span><br></code></pre></td></tr></tbody></table></figure>   <p>也可以给某个路由单独注册中间件。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="comment"><span class="hljs-comment">// 给/test2路由单独注册中间件(可注册多个)</span></span></span><br><span class="line"><span class="hljs-comment">r.GET(&amp;quot;/test2&amp;quot;, StatCost(), </span><span class="function"><span class="keyword"><span class="hljs-comment">func</span></span><span class="params"><span class="hljs-comment">(c *gin.Context)</span></span></span><span class="hljs-comment"> {</span></span><br><span class="line"><span class="hljs-comment">name := c.MustGet(&amp;quot;name&amp;quot;).(</span><span class="keyword"><span class="hljs-comment">string</span></span><span class="hljs-comment">)</span></span><br><span class="line"><span class="hljs-comment">log.Println(name)</span></span><br><span class="line"><span class="hljs-comment">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-comment">&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;,</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br></code></pre></td></tr></tbody></table></figure>   # 重定向   # HTTP重定向   <p>HTTP 重定向很容易。 内部、外部重定向均支持。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.Redirect(http.StatusMovedPermanently, &amp;quot;http:</span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/www.google.com/</span>&amp;quot;)</span></span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>   # 路由重定向   <p>路由重定向,使用<code>HandleContext</code>:</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">    </span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 指定重定向的URL</span></span></span><br><span class="line"><span class="hljs-regexp">    c.Request.URL.Path = &amp;quot;/</span>test2&amp;quot;</span><br><span class="line">    r.HandleContext(c)</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test2&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">    c.JSON(http.StatusOK, gin.H{&amp;quot;hello&amp;quot;: &amp;quot;world&amp;quot;})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br></code></pre></td></tr></tbody></table></figure>   # Gin路由   # 普通路由   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/index&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/</span>login&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">r.POST(&amp;quot;<span class="hljs-regexp">/login&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br></code></pre></td></tr></tbody></table></figure>   <p>此外,还有一个可以匹配所有请求方法的<code>Any</code>方法如下:</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br></code></pre></td><td class="code"><pre class="hljs bash"><code class="go"><span class="line">r.Any(&amp;quot;/<span class="hljs-built_in">test</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br></code></pre></td></tr></tbody></table></figure>   <p>为没有配置处理函数的路由添加处理程序。默认情况下它返回404代码。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs objectivec"><code class="go"><span class="line">r.NoRoute(<span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.HTML(http.StatusNotFound, &amp;quot;views/<span class="number"><span class="hljs-number">404.</span></span>html&amp;quot;, <span class="literal"><span class="hljs-literal">nil</span></span>)</span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>   # 路由组   <p>我们可以将拥有共同URL前缀的路由划分为一个路由组。</p>   <figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="hljs-attr">r</span> := gin.Default()</span><br><span class="line">userGroup := r.Group(&amp;quot;<span class="hljs-regexp">/user&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">{</span></span><br><span class="line"><span class="hljs-regexp">userGroup.GET(&amp;quot;/i</span>ndex&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">userGroup.GET(&amp;quot;<span class="hljs-regexp">/login&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">userGroup.POST(&amp;quot;/</span>login&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">shopGroup := r.Group(&amp;quot;<span class="hljs-regexp">/shop&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">{</span></span><br><span class="line"><span class="hljs-regexp">shopGroup.GET(&amp;quot;/i</span>ndex&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">shopGroup.GET(&amp;quot;<span class="hljs-regexp">/cart&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">shopGroup.POST(&amp;quot;/</span>checkout&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">}</span><br><span class="line">r.Run()</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>   <p>通常我们将路由分组用在划分业务逻辑或划分API版本时。</p>   # 路由原理   <p>Gin框架中的路由使用的是<a href="https://github.com/julienschmidt/httprouter" target="_blank" rel="noopener">httprouter</a>这个库。</p>   <p>其基本原理就是构造一个路由地址的前缀树。</p></code></pre></section><code class="language-golang">     <!-- Tags START -->            <div class="tags">         <span>Tags:</span>            <a href="/tags#Go">     <span class="tag-code">Go</span>   </a>         </div>          <!-- Tags END -->     <!-- NAV START -->        <div class="nav-container">     <!-- reverse left and right to put prev and next in a more logic postition -->            <a class="nav-left" href="/others/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB%E7%B3%BB%E5%88%97/">         <span class="nav-arrow">← </span>                    [置顶]杂七杂八系列                </a>                 <a class="nav-right" href="/Elasticsearch/Elasticsearch%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B/">                    [置顶]Elasticsearch系列教程                  <span class="nav-arrow"> →</span>       </a>        </div>       <!-- NAV END -->     <!-- 打赏 START -->            <div class="money-like">         <div class="reward-btn">           赏           <span class="money-code">             <span class="alipay-code">               <div class="code-image"></div>               <b>使用支付宝打赏</b>             </span>             <span class="wechat-code">               <div class="code-image"></div>               <b>使用微信打赏</b>             </span>           </span>         </div>         <p class="notice">点击上方按钮,请我喝杯咖啡!</p>       </div>          <!-- 打赏 END -->     <!-- 二维码 START -->            <div class="qrcode">         <canvas id="share-qrcode" height="100" width="100"></canvas>         <p class="notice">扫描二维码,分享此文章</p>       </div>          <!-- 二维码 END -->            <!-- Gitment START -->       <div id="comments"><div lang="en-US" class="gitment-container gitment-root-container"><div><div lang="en-US" class="gitment-container gitment-header-container"><span class="gitment-header-like-btn">     <svg class="gitment-heart-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25 39.7l-.6-.5C11.5 28.7 8 25 8 19c0-5 4-9 9-9 4.1 0 6.4 2.3 8 4.1 1.6-1.8 3.9-4.1 8-4.1 5 0 9 4 9 9 0 6-3.5 9.7-16.4 20.2l-.6.5zM17 12c-3.9 0-7 3.1-7 7 0 5.1 3.2 8.5 15 18.1 11.8-9.6 15-13 15-18.1 0-3.9-3.1-7-7-7-3.5 0-5.4 2.1-6.9 3.8L25 17.1l-1.1-1.3C22.4 14.1 20.5 12 17 12z"></path></svg>     Like        </span><span>        </span><a class="gitment-header-issue-link" href="undefined" target="_blank">Issue Page</a></div></div><div><div lang="en-US" class="gitment-container gitment-editor-container">       <a class="gitment-editor-avatar" href="https://github.com/login/oauth/authorize?scope=public_repo&amp;redirect_uri=https%3A%2F%2Fwww.liuqingzheng.top%2Fgo%2F4-Go%25E8%25AF%25AD%25E8%25A8%2580%25E6%25A1%2586%25E6%259E%25B6%2F01-Web%25E6%25A1%2586%25E6%259E%25B6%25E4%25B9%258BGin%2F&amp;client_id=32a4076431cf39d0ecea&amp;client_secret=94484bd79b3346a949acb2fda3c8a76ce16990c6" title="login with GitHub">               <svg class="gitment-github-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25 10c-8.3 0-15 6.7-15 15 0 6.6 4.3 12.2 10.3 14.2.8.1 1-.3 1-.7v-2.6c-4.2.9-5.1-2-5.1-2-.7-1.7-1.7-2.2-1.7-2.2-1.4-.9.1-.9.1-.9 1.5.1 2.3 1.5 2.3 1.5 1.3 2.3 3.5 1.6 4.4 1.2.1-1 .5-1.6 1-2-3.3-.4-6.8-1.7-6.8-7.4 0-1.6.6-3 1.5-4-.2-.4-.7-1.9.1-4 0 0 1.3-.4 4.1 1.5 1.2-.3 2.5-.5 3.8-.5 1.3 0 2.6.2 3.8.5 2.9-1.9 4.1-1.5 4.1-1.5.8 2.1.3 3.6.1 4 1 1 1.5 2.4 1.5 4 0 5.8-3.5 7-6.8 7.4.5.5 1 1.4 1 2.8v4.1c0 .4.3.9 1 .7 6-2 10.2-7.6 10.2-14.2C40 16.7 33.3 10 25 10z"></path></svg>             </a>          <div class="gitment-editor-main">       <div class="gitment-editor-header">         <nav class="gitment-editor-tabs">           <button class="gitment-editor-tab gitment-selected">Write</button>           <button class="gitment-editor-tab">Preview</button>         </nav>         <div class="gitment-editor-login">           <a class="gitment-editor-login-link" href="https://github.com/login/oauth/authorize?scope=public_repo&amp;redirect_uri=https%3A%2F%2Fwww.liuqingzheng.top%2Fgo%2F4-Go%25E8%25AF%25AD%25E8%25A8%2580%25E6%25A1%2586%25E6%259E%25B6%2F01-Web%25E6%25A1%2586%25E6%259E%25B6%25E4%25B9%258BGin%2F&amp;client_id=32a4076431cf39d0ecea&amp;client_secret=94484bd79b3346a949acb2fda3c8a76ce16990c6">Login</a> with GitHub         </div>       </div>       <div class="gitment-editor-body">         <div class="gitment-editor-write-field">           <textarea placeholder="Leave a comment" title="Login to Comment" disabled=""></textarea>         </div>         <div class="gitment-editor-preview-field gitment-hidden">           <div class="gitment-editor-preview gitment-markdown"></div>         </div>       </div>     </div>     <div class="gitment-editor-footer">       <a class="gitment-editor-footer-tip" href="https://guides.github.com/features/mastering-markdown/" target="_blank">         Styling with Markdown is supported       </a>       <button class="gitment-editor-submit" title="Login to Comment" disabled="">Comment</button>     </div>   </div></div><div><div lang="en-US" class="gitment-container gitment-comments-container"><div class="gitment-comments-error">Error: Comments Not Initialized</div></div></div><div><div lang="en-US" class="gitment-container gitment-footer-container">     Powered by     <a class="gitment-footer-project-link" href="https://github.com/imsun/gitment" target="_blank">       Gitment     </a>   </div></div></div></div>       <!-- Gitment END -->        </code></article> <article class="post-article">    <section class="markdown-content"><p><code>Gin</code>是一个用Go语言编写的web框架。它是一个类似于<code>martini</code>但拥有更好性能的API框架, 由于使用了<code>httprouter</code>,速度提高了近40倍。 如果你是性能和高效的追求者, 你会爱上<code>Gin</code>。</p>
<h1 id="Gin框架介绍"><a href="#Gin框架介绍" class="headerlink" title="Gin框架介绍"></a>Gin框架介绍</h1><p>Go世界里最流行的Web框架,<a href="https://github.com/gin-gonic/gin" target="_blank" rel="noopener">Github</a>上有<code>24K+</code>star。基于<a href="https://github.com/julienschmidt/httprouter" target="_blank" rel="noopener">httprouter</a>开发的Web框架。<a href="https://gin-gonic.com/zh-cn/docs/" target="_blank" rel="noopener">中文文档</a>齐全,简单易用的轻量级框架。</p>
<h1 id="Gin框架安装与使用"><a href="#Gin框架安装与使用" class="headerlink" title="Gin框架安装与使用"></a>Gin框架安装与使用</h1><h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><p>下载并安装<code>Gin</code>:</p>
<figure class="highlight bash" data-lang="BASH"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="bash"><span class="line">1</span><br></code></pre></td><td class="code"><pre class="hljs cs"><code class="bash"><span class="line">go <span class="hljs-keyword">get</span> -u github.com/gin-gonic/gin</span><br></code></pre></td></tr></tbody></table></figure>
<h1 id="第一个Gin示例:"><a href="#第一个Gin示例:" class="headerlink" title="第一个Gin示例:"></a>第一个Gin示例:</h1><pre><code class="language-golang">package mainimport (​    "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()}
<figure class="highlight plain" data-lang="PLAIN"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="plain"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></code></pre></td><td class="code"><pre class="hljs xml"><code class="plain"><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>将上面的代码保存并编译执行,然后使用浏览器打开<span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>127.0.0.1:8080/hello<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>就能看到一串JSON字符串。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"># RESTful API</span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”或“表现层状态转化”。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>推荐阅读<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://www.ruanyifeng.com/blog/2011/09/restful.html"</span>&gt;</span>阮一峰 理解RESTful架构<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>简单来说,REST的含义就是客户端与Web服务器之间进行交互的时候,使用HTTP协议中的4个请求方法代表不同的动作。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来获取资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来新建资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>PUT<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来更新资源<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>DELETE<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>用来删除资源。<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>只要API程序遵循了REST风格,那就可以称其为RESTful API。目前在前后端分离的架构中,前后端基本都是通过RESTful API来进行交互。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>例如,我们现在要编写一个管理书籍的系统,我们可以查询对一本书进行查询、创建、更新和删除等操作,我们在编写程序的时候就要设计客户端浏览器与我们Web服务端交互的方式和路径。按照经验我们通常会设计成如下模式:<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>请求方法<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>URL<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>含义<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>查询书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/create_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>创建书籍记录<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/update_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>更新书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/delete_book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>删除书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>同样的需求我们按照RESTful API设计如下:<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>请求方法<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>URL<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>含义<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>GET<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>查询书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>POST<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>创建书籍记录<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>PUT<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>更新书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>DELETE<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>/book<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>删除书籍信息<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Gin框架支持开发RESTful API的开发。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">​```go</span><br><span class="line">func main() {</span><br><span class="line">r := gin.Default()</span><br><span class="line">r.GET("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "GET",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.POST("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "POST",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.PUT("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "PUT",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.DELETE("/book", func(c *gin.Context) {</span><br><span class="line">c.JSON(200, gin.H{</span><br><span class="line">"message": "DELETE",</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
<p>开发RESTful API的时候我们通常使用<a href="https://www.getpostman.com/" target="_blank" rel="noopener">Postman</a>来作为客户端的测试工具。</p>
# Gin渲染
# HTML渲染
<p>我们首先定义一个存放模板文件的<code>templates</code>文件夹,然后在其内部按照业务分别定义一个<code>posts</code>文件夹和一个<code>users</code>文件夹。<code>posts/index.html</code>文件的内容如下:</p><figure class="highlight html" data-lang="HTML"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="html"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs perl"><code class="html"><span class="line"><span class="tag">&lt;<span class="name">pre</span>&gt;</span><span class="tag">&lt;<span class="name">code</span> <span class="attr">class</span>=<span class="string"><span class="hljs-string">"language-template"</span></span>&gt;</span>{{define <span class="symbol">&amp;quot;</span>posts/index.html<span class="symbol">&amp;quot;</span>}}</span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>!DOCTYPE html<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>html lang=<span class="symbol">&amp;quot;</span>en<span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>head<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta charset=<span class="symbol">&amp;quot;</span>UTF-<span class="hljs-number">8</span><span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta name=<span class="symbol">&amp;quot;</span>viewport<span class="symbol">&amp;quot;</span> content=<span class="symbol">&amp;quot;</span>width=device-width, initial-scale=<span class="hljs-number">1.0</span><span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>meta http-equiv=<span class="symbol">&amp;quot;</span>X-UA-Compatible<span class="symbol">&amp;quot;</span> content=<span class="symbol">&amp;quot;</span>ie=edge<span class="symbol">&amp;quot;</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>title<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span>posts/<span class="hljs-keyword">index</span><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span><span class="hljs-regexp">/title</span><span class="symbol"><span class="hljs-regexp">&amp;gt;</span></span></span><br><span class="line"><span class="symbol"><span class="hljs-regexp">&amp;lt;</span></span><span class="hljs-regexp">/head</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span>body<span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line"></span><br><span class="line">    {{.title}}</span><br><span class="line"></span><br><span class="line"><span class="symbol">&amp;<span class="hljs-keyword">lt</span>;</span><span class="hljs-regexp">/body</span><span class="symbol"><span class="hljs-regexp">&amp;gt;</span></span></span><br><span class="line"><span class="symbol"><span class="hljs-regexp">&amp;lt;</span></span><span class="hljs-regexp">/html</span><span class="symbol">&amp;<span class="hljs-keyword">gt</span>;</span></span><br><span class="line">{<span class="hljs-string">{end}</span>}</span><br></code></pre></td></tr></tbody></table></figure>


<p><code>users/index.html</code>文件的内容如下:</p>

<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs perl"><code class="go"><span class="line">&lt;pre&gt;&lt;code class=<span class="string"><span class="hljs-string">"language-template"</span></span>&gt;{{define &amp;quot;users/index.html&amp;quot;}}</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;!DOCTYPE html&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;html lang=&amp;quot;en&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line"></span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;head&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta charset=&amp;quot;UTF<span class="number">-<span class="hljs-number">8</span></span>&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;width=device-width, initial-scale=<span class="number"><span class="hljs-number">1.0</span></span>&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;meta http-equiv=&amp;quot;X-UA-Compatible&amp;quot; content=&amp;quot;ie=edge&amp;quot;&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    &amp;<span class="hljs-keyword">lt</span>;title&amp;<span class="hljs-keyword">gt</span>;users/<span class="hljs-keyword">index</span>&amp;<span class="hljs-keyword">lt</span>;<span class="hljs-regexp">/title&amp;gt;</span></span><br><span class="line"><span class="hljs-regexp">&amp;lt;/head</span>&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line"></span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;body&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">    {{.title}}</span><br><span class="line">&amp;<span class="hljs-keyword">lt</span>;<span class="hljs-regexp">/body&amp;gt;</span></span><br><span class="line"><span class="hljs-regexp">&amp;lt;/html</span>&amp;<span class="hljs-keyword">gt</span>;</span><br><span class="line">{<span class="hljs-string">{end}</span>}</span><br></code></pre></td></tr></tbody></table></figure>
<p>Gin框架中使用<code>LoadHTMLGlob()</code>或者<code>LoadHTMLFiles()</code>方法进行HTML模板渲染。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.LoadHTMLGlob(&amp;quot;templates<span class="comment">/**<span class="hljs-regexp">/</span></span><span class="hljs-regexp">*&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/r.LoadHTMLFiles(&amp;quot;templates/posts</span><span class="hljs-regexp">/index.html&amp;quot;, &amp;quot;templates/users</span><span class="hljs-regexp">/index.html&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/posts</span><span class="hljs-regexp">/index&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.HTML(http.StatusOK, &amp;quot;posts/index</span>.html&amp;quot;, gin.H{</span><br><span class="line">&amp;quot;title&amp;quot;: &amp;quot;posts/index&amp;quot;,</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.GET(&amp;quot;users/index&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.HTML(http.StatusOK, &amp;quot;users/index.html&amp;quot;, gin.H{</span><br><span class="line">&amp;quot;title&amp;quot;: &amp;quot;users/index&amp;quot;,</span><br><span class="line">})</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">r.Run(&amp;quot;<span class="hljs-symbol">:</span><span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
# 静态文件处理
<p>当我们渲染的HTML文件中引用了静态文件时,我们只需要按照以下方式在渲染页面前调用<code>gin.Static</code>方法即可。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></code></pre></td><td class="code"><pre class="hljs css"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-selector-tag">func</span></span> <span class="title"><span class="hljs-selector-tag">main</span></span><span class="params">()</span></span> {</span><br><span class="line"><span class="hljs-attribute">r </span>:= gin.<span class="hljs-built_in">Default</span>()</span><br><span class="line">r.<span class="hljs-built_in">Static</span>(&amp;quot;/static&amp;quot;, &amp;quot;./static&amp;quot;)</span><br><span class="line">r.<span class="hljs-built_in">LoadHTMLGlob</span>(&amp;quot;templates<span class="comment">/**/</span>*&amp;quot;)</span><br><span class="line">   ...</span><br><span class="line">r.<span class="hljs-built_in">Run</span>(&amp;quot;:<span class="number">8080</span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
# 补充文件路径处理
<p>关于模板文件和静态文件的路径,我们需要根据公司/项目的要求进行设置。可以使用下面的函数获取当前执行程序的路径。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-function">func</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-title">getCurrentPath</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">()</span></span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-built_in">string</span></span></span></span><span class="hljs-function"> </span>{</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> ex, err := os.Executable(); err == <span class="literal">nil</span> {</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span> filepath.Dir(ex)</span><br><span class="line">}</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span> &amp;quot;./&amp;quot;</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
# JSON渲染
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r := gin.Default()</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> gin.H 是map[string]interface{}的缩写</span></span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/someJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 方式一:自己拼接JSON</span></span><br><span class="line">c.JSON(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;})</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/moreJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 方法二:使用结构体</span></span><br><span class="line"><span class="keyword">var</span> msg <span class="keyword">struct</span> {</span><br><span class="line">Name    <span class="keyword">string</span> <span class="string">`<span class="javascript">json:&amp;quot;user&amp;quot;</span>`</span></span><br><span class="line">Message <span class="keyword">string</span></span><br><span class="line">Age     <span class="keyword">int</span></span><br><span class="line">}</span><br><span class="line">msg.Name = &amp;quot;小王子&amp;quot;</span><br><span class="line">msg.Message = &amp;quot;Hello world!&amp;quot;</span><br><span class="line">msg.Age = <span class="number"><span class="hljs-number">18</span></span></span><br><span class="line">c.JSON(http.StatusOK, msg)</span><br><span class="line">})</span><br><span class="line">r.Run(&amp;quot;:<span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
# XML渲染
<p>注意需要使用具名的结构体类型。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ gin.H 是map[string]interface{}的缩写</span></span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/some</span>XML&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 方式一:自己拼接JSON</span></span><br><span class="line">c.XML(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;})</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/moreXML&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 方法二:使用结构体</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">type</span></span><span class="hljs-regexp"> MessageRecord </span><span class="keyword"><span class="hljs-regexp">struct</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">Name    </span><span class="keyword"><span class="hljs-regexp">string</span></span></span><br><span class="line"><span class="hljs-regexp">Message </span><span class="keyword"><span class="hljs-regexp">string</span></span></span><br><span class="line"><span class="hljs-regexp">Age     </span><span class="keyword"><span class="hljs-regexp">int</span></span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> msg MessageRecord</span></span><br><span class="line"><span class="hljs-regexp">msg.Name = &amp;quot;小王子&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp">msg.Message = &amp;quot;Hello world!&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp">msg.Age = </span><span class="number"><span class="hljs-regexp">18</span></span></span><br><span class="line"><span class="hljs-regexp">c.XML(http.StatusOK, msg)</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# YMAL渲染
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs"><code class="go"><span class="line">r.GET(&amp;quot;/someYAML&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.YAML(http.StatusOK, gin.H{&amp;quot;message&amp;quot;: &amp;quot;ok&amp;quot;, &amp;quot;status&amp;quot;: http.StatusOK})</span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>
# protobuf渲染
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/someProtoBuf&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">reps := []</span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">{</span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">(</span><span class="number"><span class="hljs-regexp">1</span></span><span class="hljs-regexp">), </span><span class="keyword"><span class="hljs-regexp">int64</span></span><span class="hljs-regexp">(</span><span class="number"><span class="hljs-regexp">2</span></span><span class="hljs-regexp">)}</span></span><br><span class="line"><span class="hljs-regexp">label := &amp;quot;test&amp;quot;</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ protobuf 的具体定义写在 testdata/</span>protoexample 文件中。</span></span><br><span class="line">data := &amp;amp;protoexample.Test{</span><br><span class="line"><span class="hljs-attr">Label</span>: &amp;amp;label,</span><br><span class="line"><span class="hljs-attr">Reps</span>:  reps,</span><br><span class="line">}</span><br><span class="line"><span class="comment"><span class="hljs-comment">// 请注意,数据在响应中变为二进制数据</span></span></span><br><span class="line"><span class="hljs-comment"></span><span class="comment"><span class="hljs-comment">// 将输出被 protoexample.Test protobuf 序列化了的数据</span></span></span><br><span class="line"><span class="hljs-comment">c.ProtoBuf(http.StatusOK, data)</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br></code></pre></td></tr></tbody></table></figure>
# 获取参数
# 获取querystring参数
<p><code>querystring</code>指的是URL中<code>?</code>后面携带的参数,例如:<code>/user/search?username=小王子&amp;address=沙河</code>。获取请求的querystring参数的方法如下:</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/user/search</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">username <span class="hljs-symbol">:</span>= c.DefaultQuery(&amp;quot;username&amp;quot;, &amp;quot;小王子&amp;quot;)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/username := c.Query(&amp;quot;username&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">address := c.Query(&amp;quot;address&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp">r.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# 获取form参数
<p>请求的数据通过form表单来提交,例如向<code>/user/search</code>发送一个POST请求,获取请求数据的方式如下:</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.POST(&amp;quot;<span class="hljs-regexp">/user/search</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> DefaultPostForm取不到值时会返回指定的默认值</span></span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/username := c.DefaultPostForm(&amp;quot;username&amp;quot;, &amp;quot;小王子&amp;quot;)</span></span></span><br><span class="line"><span class="hljs-regexp">username := c.PostForm(&amp;quot;username&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">address := c.PostForm(&amp;quot;address&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# 获取path参数
<p>请求的参数通过URL路径传递,例如:<code>/user/search/小王子/沙河</code>。获取请求URL路径中的参数的方式如下。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span>Default返回一个默认的路由引擎</span></span><br><span class="line">r <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/user/search</span><span class="hljs-regexp">/:username/</span><span class="hljs-symbol">:address&amp;quot</span>;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">username <span class="hljs-symbol">:</span>= c.Param(&amp;quot;username&amp;quot;)</span><br><span class="line">address <span class="hljs-symbol">:</span>= c.Param(&amp;quot;address&amp;quot;)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/输出json结果给调用方</span></span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;:  &amp;quot;ok&amp;quot;,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;username&amp;quot;: username,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;address&amp;quot;:  address,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp">r.Run(&amp;quot;:</span><span class="number"><span class="hljs-regexp">8080</span></span><span class="hljs-regexp">&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# 参数绑定
<p>为了能够更方便的获取请求相关参数,提高开发效率,我们可以基于请求的<code>content-type</code>识别请求数据类型并利用反射机制自动提取请求中<code>querystring</code>、form表单、JSON、XML等参数到结构体中。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Binding <span class="hljs-keyword">from</span> JSON</span></span><br><span class="line"><span class="keyword">type</span> Login <span class="keyword">struct</span> {</span><br><span class="line">User     <span class="keyword">string</span> <span class="string">`<span class="javascript">form:&amp;quot;user&amp;quot; json:&amp;quot;user&amp;quot; binding:&amp;quot;required&amp;quot;</span>`</span></span><br><span class="line">Password <span class="keyword">string</span> <span class="string">`<span class="javascript">form:&amp;quot;password&amp;quot; json:&amp;quot;password&amp;quot; binding:&amp;quot;required&amp;quot;</span>`</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router := gin.Default()</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 绑定JSON的示例 ({&amp;quot;user&amp;quot;: &amp;quot;q1mi&amp;quot;, &amp;quot;password&amp;quot;: &amp;quot;<span class="hljs-number">123456</span>&amp;quot;})</span></span><br><span class="line">router.POST(&amp;quot;<span class="hljs-regexp">/loginJSON&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">if</span></span><span class="hljs-regexp"> err := c.ShouldBindJSON(&amp;amp;login); err == </span><span class="literal"><span class="hljs-regexp">nil</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">fmt.Printf(&amp;quot;login info:%#v\n&amp;quot;, login)</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;user&amp;quot;:     login.User,</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;password&amp;quot;: login.Password,</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">} </span><span class="keyword"><span class="hljs-regexp">else</span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ 绑定form表单示例 (user=q1mi&amp;amp;password=<span class="hljs-number">123456</span>)</span></span><br><span class="line">router.POST(&amp;quot;<span class="hljs-regexp">/loginForm&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ ShouldBind()会根据请求的Content-Type自行选择绑定器</span></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err := c.ShouldBind(&amp;amp;login); err == <span class="literal">nil</span> {</span><br><span class="line">c.JSON(http.StatusOK, gin.H{</span><br><span class="line">&amp;quot;user&amp;quot;:     login.User,</span><br><span class="line">&amp;quot;password&amp;quot;: login.Password,</span><br><span class="line">})</span><br><span class="line">} <span class="keyword"><span class="hljs-keyword">else</span></span> {</span><br><span class="line">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span><br><span class="line">}</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 绑定querystring示例 (user=q1mi&amp;amp;password=<span class="hljs-number">123456</span>)</span></span><br><span class="line">router.GET(&amp;quot;<span class="hljs-regexp">/loginForm&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">var</span></span><span class="hljs-regexp"> login Login</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span>/ ShouldBind()会根据请求的Content-Type自行选择绑定器</span></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err := c.ShouldBind(&amp;amp;login); err == <span class="literal">nil</span> {</span><br><span class="line">c.JSON(http.StatusOK, gin.H{</span><br><span class="line">&amp;quot;user&amp;quot;:     login.User,</span><br><span class="line">&amp;quot;password&amp;quot;: login.Password,</span><br><span class="line">})</span><br><span class="line">} <span class="keyword"><span class="hljs-keyword">else</span></span> {</span><br><span class="line">c.JSON(http.StatusBadRequest, gin.H{&amp;quot;error&amp;quot;: err.Error()})</span><br><span class="line">}</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Listen <span class="hljs-keyword">and</span> serve <span class="hljs-literal">on</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">8080</span></span></span><br><span class="line">router.Run(&amp;quot;:<span class="number"><span class="hljs-number">8080</span></span>&amp;quot;)</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
# 文件上传
# 单个文件上传
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 处理multipart forms提交文件时默认的内存限制是32 MiB</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 可以通过下面的方式修改</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ router.MaxMultipartMemory = 8 &amp;lt;&amp;lt; 20  /</span><span class="hljs-regexp">/ 8 MiB</span></span></span><br><span class="line"><span class="hljs-regexp">router.POST(&amp;quot;/upload</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> 单个文件</span></span><br><span class="line">file, err <span class="hljs-symbol">:</span>= c.FormFile(&amp;quot;file&amp;quot;)</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">if</span></span> err != <span class="literal"><span class="hljs-literal">nil</span></span> {</span><br><span class="line">c.JSON(http.StatusInternalServerError, gin.H{</span><br><span class="line">&amp;quot;message&amp;quot;: err.Error(),</span><br><span class="line">})</span><br><span class="line"><span class="keyword"><span class="hljs-keyword">return</span></span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">log.Println(file.Filename)</span><br><span class="line">dst <span class="hljs-symbol">:</span>= fmt.Sprintf(&amp;quot;<span class="hljs-symbol">C:</span>/tmp/%s&amp;quot;, file.Filename)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 上传文件到指定的目录</span></span></span><br><span class="line"><span class="hljs-regexp">c.SaveUploadedFile(file, dst)</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;: fmt.Sprintf(&amp;quot;</span><span class="string"><span class="hljs-regexp">'%s'</span></span><span class="hljs-regexp"> uploaded!&amp;quot;, file.Filename),</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">router.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# 多个文件上传
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line">router <span class="hljs-symbol">:</span>= gin.Default()</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 处理multipart forms提交文件时默认的内存限制是32 MiB</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 可以通过下面的方式修改</span></span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ router.MaxMultipartMemory = 8 &amp;lt;&amp;lt; 20  /</span><span class="hljs-regexp">/ 8 MiB</span></span></span><br><span class="line"><span class="hljs-regexp">router.POST(&amp;quot;/upload</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line"><span class="comment"><span class="hljs-regexp">//</span> Multipart form</span></span><br><span class="line">form, <span class="hljs-number">_</span> <span class="hljs-symbol">:</span>= c.MultipartForm()</span><br><span class="line">files <span class="hljs-symbol">:</span>= form.File[&amp;quot;file&amp;quot;]</span><br><span class="line"></span><br><span class="line"><span class="keyword"><span class="hljs-keyword">for</span></span> index, file <span class="hljs-symbol">:</span>= <span class="keyword">range</span> files {</span><br><span class="line">log.Println(file.Filename)</span><br><span class="line">dst <span class="hljs-symbol">:</span>= fmt.Sprintf(&amp;quot;<span class="hljs-symbol">C:</span>/tmp/%s<span class="hljs-number">_</span>%d&amp;quot;, file.Filename, index)</span><br><span class="line"><span class="comment">/<span class="hljs-regexp">/ 上传文件到指定的目录</span></span></span><br><span class="line"><span class="hljs-regexp">c.SaveUploadedFile(file, dst)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-regexp">&amp;quot;message&amp;quot;: fmt.Sprintf(&amp;quot;%d files uploaded!&amp;quot;, </span><span class="built_in"><span class="hljs-regexp">len</span></span><span class="hljs-regexp">(files)),</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br><span class="line"><span class="hljs-regexp">router.Run()</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
# Gin中间件
<p>Gin框架允许开发者在处理请求的过程中,加入用户自己的钩子(Hook)函数。这个钩子函数就叫中间件,中间件适合处理一些公共的业务逻辑,比如登录校验、日志打印、耗时统计等。</p>
<p>Gin中的中间件必须是一个<code>gin.HandlerFunc</code>类型。例如我们像下面的代码一样定义一个中间件。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></code></pre></td><td class="code"><pre class="hljs ruby"><code class="go"><span class="line"><span class="comment">/<span class="hljs-regexp">/ StatCost 是一个统计耗时请求耗时的中间件</span></span></span><br><span class="line"><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="hljs-regexp"> </span><span class="title"><span class="hljs-regexp">StatCost</span></span><span class="params"><span class="hljs-regexp">()</span></span><span class="hljs-regexp"> </span><span class="title"><span class="hljs-regexp">gin</span></span><span class="hljs-regexp">.</span><span class="title"><span class="hljs-regexp">HandlerFunc</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="keyword"><span class="hljs-regexp">return</span></span><span class="hljs-regexp"> </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">start := time.Now()</span></span><br><span class="line"><span class="hljs-regexp">c.Set(&amp;quot;name&amp;quot;, &amp;quot;小王子&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 执行其他中间件</span></span></span><br><span class="line"><span class="hljs-regexp">c.Next()</span></span><br><span class="line"><span class="hljs-regexp"></span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 计算耗时</span></span></span><br><span class="line"><span class="hljs-regexp">cost := time.Since(start)</span></span><br><span class="line"><span class="hljs-regexp">log.Println(cost)</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br><span class="line"><span class="hljs-regexp">}</span></span><br></code></pre></td></tr></tbody></table></figure>
<p>然后注册中间件的时候,可以在全局注册。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="function"><span class="keyword"><span class="hljs-function">func</span></span><span class="hljs-function"> </span><span class="title"><span class="hljs-function"><span class="hljs-title">main</span></span></span><span class="params"><span class="hljs-function"><span class="hljs-params">()</span></span></span></span><span class="hljs-function"> </span>{</span><br><span class="line"><span class="comment"><span class="hljs-comment">// 新建一个没有任何默认中间件的路由</span></span></span><br><span class="line"><span class="hljs-comment">r := gin.New()</span></span><br><span class="line"><span class="hljs-comment"></span><span class="comment"><span class="hljs-comment">// 注册一个全局中间件</span></span></span><br><span class="line"><span class="hljs-comment">r.Use(StatCost())</span></span><br><span class="line"><span class="hljs-comment"></span></span><br><span class="line"><span class="hljs-comment">r.GET(&amp;quot;/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-comment">func</span></span><span class="params"><span class="hljs-comment">(c *gin.Context)</span></span></span><span class="hljs-comment"> {</span></span><br><span class="line"><span class="hljs-comment">name := c.MustGet(&amp;quot;name&amp;quot;).(</span><span class="keyword"><span class="hljs-comment">string</span></span><span class="hljs-comment">)</span></span><br><span class="line"><span class="hljs-comment">log.Println(name)</span></span><br><span class="line"><span class="hljs-comment">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-comment">&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;,</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">r.Run()</span></span><br><span class="line"><span class="hljs-comment">}</span></span><br></code></pre></td></tr></tbody></table></figure>
<p>也可以给某个路由单独注册中间件。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></code></pre></td><td class="code"><pre class="hljs cpp"><code class="go"><span class="line"><span class="comment"><span class="hljs-comment">// 给/test2路由单独注册中间件(可注册多个)</span></span></span><br><span class="line"><span class="hljs-comment">r.GET(&amp;quot;/test2&amp;quot;, StatCost(), </span><span class="function"><span class="keyword"><span class="hljs-comment">func</span></span><span class="params"><span class="hljs-comment">(c *gin.Context)</span></span></span><span class="hljs-comment"> {</span></span><br><span class="line"><span class="hljs-comment">name := c.MustGet(&amp;quot;name&amp;quot;).(</span><span class="keyword"><span class="hljs-comment">string</span></span><span class="hljs-comment">)</span></span><br><span class="line"><span class="hljs-comment">log.Println(name)</span></span><br><span class="line"><span class="hljs-comment">c.JSON(http.StatusOK, gin.H{</span></span><br><span class="line"><span class="hljs-comment">&amp;quot;message&amp;quot;: &amp;quot;Hello world!&amp;quot;,</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br><span class="line"><span class="hljs-comment">})</span></span><br></code></pre></td></tr></tbody></table></figure>
# 重定向
# HTTP重定向
<p>HTTP 重定向很容易。 内部、外部重定向均支持。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs coffeescript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">c.Redirect(http.StatusMovedPermanently, &amp;quot;http:</span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/www.google.com/</span>&amp;quot;)</span></span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>
# 路由重定向
<p>路由重定向,使用<code>HandleContext</code>:</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">    </span><span class="comment"><span class="hljs-regexp">/</span><span class="hljs-regexp">/ 指定重定向的URL</span></span></span><br><span class="line"><span class="hljs-regexp">    c.Request.URL.Path = &amp;quot;/</span>test2&amp;quot;</span><br><span class="line">    r.HandleContext(c)</span><br><span class="line">})</span><br><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/test2&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {</span></span><br><span class="line"><span class="hljs-regexp">    c.JSON(http.StatusOK, gin.H{&amp;quot;hello&amp;quot;: &amp;quot;world&amp;quot;})</span></span><br><span class="line"><span class="hljs-regexp">})</span></span><br></code></pre></td></tr></tbody></table></figure>
# Gin路由
# 普通路由
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line">r.GET(&amp;quot;<span class="hljs-regexp">/index&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">r.GET(&amp;quot;/</span>login&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">r.POST(&amp;quot;<span class="hljs-regexp">/login&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br></code></pre></td></tr></tbody></table></figure>
<p>此外,还有一个可以匹配所有请求方法的<code>Any</code>方法如下:</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br></code></pre></td><td class="code"><pre class="hljs bash"><code class="go"><span class="line">r.Any(&amp;quot;/<span class="hljs-built_in">test</span>&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br></code></pre></td></tr></tbody></table></figure>
<p>为没有配置处理函数的路由添加处理程序。默认情况下它返回404代码。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></code></pre></td><td class="code"><pre class="hljs objectivec"><code class="go"><span class="line">r.NoRoute(<span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {</span><br><span class="line">c.HTML(http.StatusNotFound, &amp;quot;views/<span class="number"><span class="hljs-number">404.</span></span>html&amp;quot;, <span class="literal"><span class="hljs-literal">nil</span></span>)</span><br><span class="line">})</span><br></code></pre></td></tr></tbody></table></figure>
# 路由组
<p>我们可以将拥有共同URL前缀的路由划分为一个路由组。</p>
<figure class="highlight go" data-lang="GO"><table><tbody><tr><td class="gutter"><pre class="hljs"><code class="go"><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></code></pre></td><td class="code"><pre class="hljs javascript"><code class="go"><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"><span class="hljs-attr">r</span> := gin.Default()</span><br><span class="line">userGroup := r.Group(&amp;quot;<span class="hljs-regexp">/user&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">{</span></span><br><span class="line"><span class="hljs-regexp">userGroup.GET(&amp;quot;/i</span>ndex&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">userGroup.GET(&amp;quot;<span class="hljs-regexp">/login&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">userGroup.POST(&amp;quot;/</span>login&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">shopGroup := r.Group(&amp;quot;<span class="hljs-regexp">/shop&amp;quot;)</span></span><br><span class="line"><span class="hljs-regexp">{</span></span><br><span class="line"><span class="hljs-regexp">shopGroup.GET(&amp;quot;/i</span>ndex&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">shopGroup.GET(&amp;quot;<span class="hljs-regexp">/cart&amp;quot;, </span><span class="function"><span class="keyword"><span class="hljs-regexp">func</span></span><span class="params"><span class="hljs-regexp">(c *gin.Context)</span></span></span><span class="hljs-regexp"> {...})</span></span><br><span class="line"><span class="hljs-regexp">shopGroup.POST(&amp;quot;/</span>checkout&amp;quot;, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> {...})</span><br><span class="line">}</span><br><span class="line">r.Run()</span><br><span class="line">}</span><br></code></pre></td></tr></tbody></table></figure>
<p>通常我们将路由分组用在划分业务逻辑或划分API版本时。</p>
# 路由原理
<p>Gin框架中的路由使用的是<a href="https://github.com/julienschmidt/httprouter" target="_blank" rel="noopener">httprouter</a>这个库。</p>
<p>其基本原理就是构造一个路由地址的前缀树。</p></code></pre></section><code class="language-golang">    <!-- Tags START -->          <div class="tags">        <span>Tags:</span>          <a href="/tags#Go">    <span class="tag-code">Go</span>  </a>
      </div>        <!-- Tags END -->    <!-- NAV START -->      <div class="nav-container">    <!-- reverse left and right to put prev and next in a more logic postition -->          <a class="nav-left" href="/others/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB%E7%B3%BB%E5%88%97/">        <span class="nav-arrow">← </span>                  [置顶]杂七杂八系列              </a>              <a class="nav-right" href="/Elasticsearch/Elasticsearch%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8B/">                  [置顶]Elasticsearch系列教程                <span class="nav-arrow"> →</span>      </a>      </div>
    <!-- NAV END -->    <!-- 打赏 START -->          <div class="money-like">        <div class="reward-btn">          赏          <span class="money-code">            <span class="alipay-code">              <div class="code-image"></div>              <b>使用支付宝打赏</b>            </span>            <span class="wechat-code">              <div class="code-image"></div>              <b>使用微信打赏</b>            </span>          </span>        </div>        <p class="notice">点击上方按钮,请我喝杯咖啡!</p>      </div>        <!-- 打赏 END -->    <!-- 二维码 START -->          <div class="qrcode">        <canvas id="share-qrcode" height="100" width="100"></canvas>        <p class="notice">扫描二维码,分享此文章</p>      </div>        <!-- 二维码 END -->          <!-- Gitment START -->      <div id="comments"><div lang="en-US" class="gitment-container gitment-root-container"><div><div lang="en-US" class="gitment-container gitment-header-container"><span class="gitment-header-like-btn">    <svg class="gitment-heart-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25 39.7l-.6-.5C11.5 28.7 8 25 8 19c0-5 4-9 9-9 4.1 0 6.4 2.3 8 4.1 1.6-1.8 3.9-4.1 8-4.1 5 0 9 4 9 9 0 6-3.5 9.7-16.4 20.2l-.6.5zM17 12c-3.9 0-7 3.1-7 7 0 5.1 3.2 8.5 15 18.1 11.8-9.6 15-13 15-18.1 0-3.9-3.1-7-7-7-3.5 0-5.4 2.1-6.9 3.8L25 17.1l-1.1-1.3C22.4 14.1 20.5 12 17 12z"></path></svg>    Like      </span><span>      </span><a class="gitment-header-issue-link" href="undefined" target="_blank">Issue Page</a></div></div><div><div lang="en-US" class="gitment-container gitment-editor-container">      <a class="gitment-editor-avatar" href="https://github.com/login/oauth/authorize?scope=public_repo&amp;redirect_uri=https%3A%2F%2Fwww.liuqingzheng.top%2Fgo%2F4-Go%25E8%25AF%25AD%25E8%25A8%2580%25E6%25A1%2586%25E6%259E%25B6%2F01-Web%25E6%25A1%2586%25E6%259E%25B6%25E4%25B9%258BGin%2F&amp;client_id=32a4076431cf39d0ecea&amp;client_secret=94484bd79b3346a949acb2fda3c8a76ce16990c6" title="login with GitHub">              <svg class="gitment-github-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25 10c-8.3 0-15 6.7-15 15 0 6.6 4.3 12.2 10.3 14.2.8.1 1-.3 1-.7v-2.6c-4.2.9-5.1-2-5.1-2-.7-1.7-1.7-2.2-1.7-2.2-1.4-.9.1-.9.1-.9 1.5.1 2.3 1.5 2.3 1.5 1.3 2.3 3.5 1.6 4.4 1.2.1-1 .5-1.6 1-2-3.3-.4-6.8-1.7-6.8-7.4 0-1.6.6-3 1.5-4-.2-.4-.7-1.9.1-4 0 0 1.3-.4 4.1 1.5 1.2-.3 2.5-.5 3.8-.5 1.3 0 2.6.2 3.8.5 2.9-1.9 4.1-1.5 4.1-1.5.8 2.1.3 3.6.1 4 1 1 1.5 2.4 1.5 4 0 5.8-3.5 7-6.8 7.4.5.5 1 1.4 1 2.8v4.1c0 .4.3.9 1 .7 6-2 10.2-7.6 10.2-14.2C40 16.7 33.3 10 25 10z"></path></svg>            </a>        <div class="gitment-editor-main">      <div class="gitment-editor-header">        <nav class="gitment-editor-tabs">          <button class="gitment-editor-tab gitment-selected">Write</button>          <button class="gitment-editor-tab">Preview</button>        </nav>        <div class="gitment-editor-login">          <a class="gitment-editor-login-link" href="https://github.com/login/oauth/authorize?scope=public_repo&amp;redirect_uri=https%3A%2F%2Fwww.liuqingzheng.top%2Fgo%2F4-Go%25E8%25AF%25AD%25E8%25A8%2580%25E6%25A1%2586%25E6%259E%25B6%2F01-Web%25E6%25A1%2586%25E6%259E%25B6%25E4%25B9%258BGin%2F&amp;client_id=32a4076431cf39d0ecea&amp;client_secret=94484bd79b3346a949acb2fda3c8a76ce16990c6">Login</a> with GitHub        </div>      </div>      <div class="gitment-editor-body">        <div class="gitment-editor-write-field">          <textarea placeholder="Leave a comment" title="Login to Comment" disabled=""></textarea>        </div>        <div class="gitment-editor-preview-field gitment-hidden">          <div class="gitment-editor-preview gitment-markdown"></div>        </div>      </div>    </div>    <div class="gitment-editor-footer">      <a class="gitment-editor-footer-tip" href="https://guides.github.com/features/mastering-markdown/" target="_blank">        Styling with Markdown is supported      </a>      <button class="gitment-editor-submit" title="Login to Comment" disabled="">Comment</button>    </div>  </div></div><div><div lang="en-US" class="gitment-container gitment-comments-container"><div class="gitment-comments-error">Error: Comments Not Initialized</div></div></div><div><div lang="en-US" class="gitment-container gitment-footer-container">    Powered by    <a class="gitment-footer-project-link" href="https://github.com/imsun/gitment" target="_blank">      Gitment    </a>  </div></div></div></div>      <!-- Gitment END -->      </code></article>

标签:gt,框架,quot,gin,lt,amp,Gin,td
From: https://www.cnblogs.com/Asura2Ares/p/17012359.html

相关文章