门户网站建设依据,桂林小学网站建设,企业网站建设最需要的是什么,庆阳网站建设Go web框架——Gin入门与路由 1 Gin框架介绍1.1 基础介绍1.2 安装Gin1.3 快速使用 2 路由2.1 基本路由GET请求POST请求 2.2 路由参数2.3 路由分组基本分组带中间件的分组 2.4 重定向 1 Gin框架介绍
github链接#xff1a;https://github.com/gin-gonic/gin
中文文档#xf… Go web框架——Gin入门与路由 1 Gin框架介绍1.1 基础介绍1.2 安装Gin1.3 快速使用 2 路由2.1 基本路由GET请求POST请求 2.2 路由参数2.3 路由分组基本分组带中间件的分组 2.4 重定向 1 Gin框架介绍
github链接https://github.com/gin-gonic/gin
中文文档https://gin-gonic.com/zh-cn/docs/
学习链接博主在bilibili有视频http://www.fengfengzhidao.com/
1.1 基础介绍
Gin是一个轻量级的、高性能的web框架由Golang语言开发。Gin的核心设计理念是提供快速建立API的开发方式同时保持良好的性能和高度易用性。
下面是Gin框架的主要特点
快速性能 – Gin采用了诸如Like-nginx等类似的底层库因此Gin的性能非常高可以达到每秒处理数百万个请求的水平。简单易用 – Gin框架采用了类似于Martini的API设计非常容易学习和使用。模块化 – Gin的中间件机制使其易于构建和扩展应用程序。松散耦合 – Gin的设计允许使用者在不破坏当前应用的前提下快速添加新的特性或更改现有特性。适用范围广 – Gin的设计允许用户构建任何类型的web应用程序包括API、代理、Websocket服务等。
除此之外Gin框架还提供了大量的中间件来提供更加丰富的功能例如日志、跨域请求、认证和授权、压缩等。
1.2 安装Gin
要求Go 1.13 及以上版本
创建一个新项目
注意我们创建项目要添加一个国内的代理不然下载会很慢或者失败
代理GOPROXYhttps://goproxy.cn,direct 下载并安装Gin
go get -u github.com/gin-gonic/gin然后我们会在go.mod里面会发现多了很多东西这些就Gin直接使用
安装postman
主要是用于对接口进行调试的工具有类似的工具都可以例如Apifox
postman官网https://www.postman.com/downloads/?utm_sourcepostman-home
1.3 快速使用
创建文件夹和文件
我这里创建了一个GinStudy01_HelloWord的文件夹在里面又创建了一个main文件夹在里面创建main文件
将 gin 引入到代码中
import github.com/gin-gonic/gin可选如果使用诸如 http.StatusOK 之类的常量则需要引入 net/http 包
import net/http写代码
package mainimport (github.com/gin-gonic/ginnet/http
)func main() {router : gin.Default()router.GET(/index, func(c *gin.Context) {c.String(http.StatusOK, Hello World)})// 启动方式一router.Run(:8000) // 监听并在 0.0.0.0:8000 上启动服务// 启动方式二// http.ListenAndServe(:8000, router)
}然后运行等待一会成功后游览器访问http://127.0.0.1:8000/index会发现有Hello World router:gin.Default()这是默认的服务器。使用gin的Default方法创建一个路由Handler然后通过Http方法绑定路由规则和路由函数。不同于net/http库的路由函数gin进行了封装把request和response都封装到了gin.Context的上下文环境中。最后启动路由的Run方法监听端口。还可以用http.ListenAndServe(:8080, router)或者自定义Http服务器配置。
2 路由
本文将介绍Gin框架的路由功能包括路由的基本使用、路由参数、路由分组、重定向、请求响应和中间件等。
2.1 基本路由
Router是Gin框架中的一个HTTP路由。Gin框架使用Router来接受HTTP请求并根据请求的路径和HTTP方法来判断执行的处理函数。
Gin框架的Router支持HTTP的所有方法GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS。
我们首先需要引入gin包
import github.com/gin-gonic/gin然后创建一个Gin框架的Router
r : gin.Default()GET请求
使用GET方法接受GET方式的HTTP请求。
func main() {r : gin.Default()r.GET(/hello, func(c *gin.Context) {c.String(http.StatusOK, Hello World)})r.Run(:8080)
}当我们访问http://localhost:8080/hello时就能够在浏览器上看到输出了Hello World。
POST请求
使用POST方法接受POST方式的HTTP请求。
import (github.com/gin-gonic/ginnet/http
)
// 这里只是把func给提出来这样代码看起来更简洁了
func postRequest(c *gin.Context) {username : c.PostForm(username)password : c.PostForm(password)c.JSON(http.StatusOK, gin.H{username: username,password: password,})
}func main() {r : gin.Default()// post请求r.POST(/login, postRequest)r.Run(:8080)
}当我们通过POST方式访问http://localhost:8080/login然后通过POST方法提交username和password参数就能够在返回结果中看到我们提交的参数值。
至于其他的请求略。。。。
2.2 路由参数
Gin框架中的Router支持动态路由参数在路由路径中使用冒号加参数名的方式表示参数。
func main() {r : gin.Default()r.GET(/users/:id, func(c *gin.Context) {id : c.Param(id)c.String(http.StatusOK, User ID: %s, id)})r.Run(:8080)
}使用Param方法获取路由中的参数值。
当我们访问http://localhost:8080/users/123时就能够在浏览器上看到输出了User ID: 123。 2.3 路由分组
Gin框架的Router也支持路由分组可以按照功能分组路由这样能够更好地管理代码并且能够为每个路由分组设置不同的中间件。
基本分组
我们可以使用Gin框架的Group方法将路由按照功能进行分组。
func main() {r : gin.Default()api : r.Group(/api){api.GET(/users, func(c *gin.Context) {c.String(http.StatusOK, API Users)})api.GET(/products, func(c *gin.Context) {c.String(http.StatusOK, API Products)})}admin : r.Group(/admin){admin.GET(/users, func(c *gin.Context) {c.String(http.StatusOK, Admin Users)})admin.GET(/products, func(c *gin.Context) {c.String(http.StatusOK, Admin Products)})}r.Run(:8080)
}我们可以将API路由和管理员路由分别归为一个Group并在Group中设置对应的路由处理函数。
当我们访问http://localhost:8080/api/users和http://localhost:8080/admin/users时就能够在浏览器上看到输出了API Users和Admin Users。
带中间件的分组
中间件还没有讲解等后面学到了可以再回来看~
我们还可以在路由分组中指定中间件这样能够更好地进行控制对于需要进行身份验证或者请求限制的路由我们需要通过Group进行中间件的指定。
func main() {r : gin.Default()api : r.Group(/api){api.Use(AuthMiddleware())api.GET(/users, func(c *gin.Context) {c.String(http.StatusOK, API Users)})api.GET(/products, func(c *gin.Context) {c.String(http.StatusOK, API Products)})}admin : r.Group(/admin){admin.Use(AuthMiddleware(), LimitMiddleware())admin.GET(/users, func(c *gin.Context) {c.String(http.StatusOK, Admin Users)})admin.GET(/products, func(c *gin.Context) {c.String(http.StatusOK, Admin Products)})}r.Run(:8080)
}使用Use方法指定中间件多个中间件可以通过逗号分隔。
2.4 重定向
在Gin中我们可以使用路由重定向功能将一条路由重定向到另一条路由。
func main() {r : gin.Default()r.GET(/users, func(c *gin.Context) {c.Redirect(http.StatusMovedPermanently, /api/users)})r.GET(/api/users, func(c *gin.Context) {c.String(http.StatusOK, API Users)})r.Run(:8080)
}当我们访问http://localhost:8080/users时Gin框架会将请求重定向到http://localhost:8080/api/users。 文章转载自: http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.snmth.cn.gov.cn.snmth.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.baohum.com.gov.cn.baohum.com http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.xqcgb.cn.gov.cn.xqcgb.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn