制作网站要多少钱,广州外贸公司联系方式,手机小程序开发教程,长沙微网站gin是什么
Gin 是一个用 Go (Golang) 编写的 HTTP Web 框架。 它具有类似 Martini 的 API#xff0c;但性能比 Martini 快 40 倍。如果你需要极好的性能#xff0c;使用 Gin 吧。
特点#xff1a;gin是golang的net/http库封装的web框架#xff0c;api友好#xff0c;注…gin是什么
Gin 是一个用 Go (Golang) 编写的 HTTP Web 框架。 它具有类似 Martini 的 API但性能比 Martini 快 40 倍。如果你需要极好的性能使用 Gin 吧。
特点gin是golang的net/http库封装的web框架api友好注释明确具有快速灵活容错方便等特点。 go其他web框架
beego开源的高性能Go语言Web框架。Iris全宇宙最快的Go语言Web框架支持MVC。 gin的安装 go语言包的安装都十分简单对与gin的安装仅需要一行命令开启go mod并且配置了正确的代理 go get -u github.com/gin-gonic/gin gin框架中文文档https://gin-gonic.com/zh-cn/docs/ gin的使用
使用gin创建一个hello world网页
package mainimport github.com/gin-gonic/ginfunc main() {router : gin.Default()router.GET(/hello, func(c *gin.Context) {c.JSON(200, gin.H{message: Hello World!})})router.Run(127.0.0.1:8080)
}启动成功 十分的快捷简单 RESTful API
55RESTful用url去定位资源、用HTTP动词GET、POST、DELETE、PUT去描述操作。
RESTful API就是REST风格的APIrest是一种架构风格跟编程语言无关跟平台无关采用HTTP做传输协议。
REST的含义就是客户端与Web服务器之间进行交互的时候使用HTTP协议中的4个请求方法代表不同的动作。
GET获取资源POST新建资源PUT更新资源DELETE删除资源
只要API程序遵循了REST风格就可以成为RESTful API。 Gin框架支持RESTful API的开发 router.GET(/get, func(c *gin.Context) {c.JSON(200, gin.H{message: get})})router.POST(/post, func(c *gin.Context) {c.JSON(200, gin.H{message: post})})router.PUT(/put, func(c *gin.Context) {c.JSON(200, gin.H{message: put})})router.DELETE(/delete, func(c *gin.Context) {c.JSON(200, gin.H{message: delete})}) 响应HTML页面
目录 main.go
package mainimport (github.com/gin-gonic/gingithub.com/thinkerou/faviconnet/http
)func main() {router : gin.Default()router.GET(/index, func(c *gin.Context) {c.HTML(http.StatusOK, index.html, gin.H{message: myHTML,})})// Gin框架中使用LoadHTMLGlob()或者LoadHTMLFiles()方法进行HTML模板渲染。//router.LoadHTMLGlob(template/*)router.LoadHTMLFiles(template/index.html)// 当我们渲染的HTML文件中引用了静态文件时// 我们只需要按照以下方式在渲染页面前调用gin.Static方法即可。router.Static(/static, ./static)router.Run(127.0.0.1:8080)
}index.html
!DOCTYPE html
html langen
headmeta charsetUTF-8title我的Go页面/titlelink relstylesheet href/static/css/style.cssscript src/static/js/common.js/script
/head
bodyh1首页/h1
/body
/html style.css
body {background: rosybrown;
} cssjs之后也会出文章 JSON响应
1、返回普通数据类型
router.GET(/hello, func(c *gin.Context) {c.JSON(200,request success)}) 2、返回结构体 router.GET(/hello, func(c *gin.Context) {user : struct {Username string json:usernamePassWord string json:password}{Username: zhangsan,PassWord: 123456,}c.JSON(http.StatusOK, user)})
3、返回map router.GET(/hello, func(c *gin.Context) {type user struct {Username string json:usernamePassWord string json:password}m : map[int]user{}m[1] user{zhangsan, 123456}m[2] user{lisi, 123456}c.JSON(http.StatusOK, m)}) 4、返回切片结构体 router.GET(/hello, func(c *gin.Context) {type user struct {Username string json:usernamePassWord string json:password}users : make([]user, 2)users[0] user{zhangsan, 123456}users[1] user{lisi, 123456}c.JSON(http.StatusOK, users)}) 获取请求参数
1、获取url中的参数 当form表单中的method属性为get我们提交的字段值会显示在url中 router.GET(/login, func(c *gin.Context) {c.HTML(200, login.html, nil)})router.LoadHTMLGlob(template/*)
获取url中的参数方法 router.GET(/login, func(c *gin.Context) {username : c.Query(username)password, ok : c.GetQuery(password)if !ok {password 获取password失败}c.JSON(http.StatusOK, gin.H{username: username,password: password,})}) 2、接收restful风格的参数
请求的参数通过URL路径传递例如/login/zhangsan/123456。 获取请求URL路径中的参数的方式如下。 router.GET(/login/:username/:password, func(c *gin.Context) {// 通过 param 获取参数username : c.Param(username)password : c.Param(password)//返回json数据c.JSON(http.StatusOK, gin.H{username: username,password: password,})}) 3、接收form表单提交的数据 router.POST(/login, func(c *gin.Context) {username : c.PostForm(username)password : c.PostForm(password)c.JSON(http.StatusOK, gin.H{username: username,password: password,})}) 4、获取json参数
当前端请求的数据通过JSON提交时例如向/json发送一个POST请求则获取请求参数的方式如下
// 编写请求
router.POST(/json, func(c *gin.Context) {// GetRawData : 从c.Request.Body读取请求数据, 返回 []byteb, _ : c.GetRawData()// 定义map或结构体接收var m map[string]interface{}// 包装为json数据_ json.Unmarshal(b, m)c.JSON(http.StatusOK, m)
}) 路由
1、重定向
http重定向 //重定向router.GET(/test, func(c *gin.Context) {c.Redirect(http.StatusMovedPermanently, http://www.google.com)}) 2、路由重定向 router.GET(/test, func(c *gin.Context) {c.Request.URL.Path /test2router.HandleContext(c)})router.GET(/test2, func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{message: test2})}) 3、404页面
没有匹配到路由的请求都返回404.html页面。 router.NoRoute(func(c *gin.Context) {c.HTML(http.StatusNotFound, 404.html, nil)}) 4、路由组
我们可以将拥有共同URL前缀的路由划分为一个路由组也可以多重嵌套。
package mainimport github.com/gin-gonic/ginfunc Group(router *gin.Engine) {userGroup : router.Group(/user){ //习惯性一对{}包裹同组的路由这只是为了看着清晰你用不用{}包裹功能上没什么区别userGroup.GET(/1, func(c *gin.Context) {}) // /user/1userGroup.GET(/2, func(c *gin.Context) {}) // /user/2userGroup.GET(/3, func(c *gin.Context) {}) // /user/3}
}