当前位置: 首页> 健康> 养生 > 蜜蜂vp加速器七天试用_云南人力资源和社会保障网_企业培训课程清单_电商培训机构排名

蜜蜂vp加速器七天试用_云南人力资源和社会保障网_企业培训课程清单_电商培训机构排名

时间:2025/7/12 21:58:18来源:https://blog.csdn.net/zyh_2021131030/article/details/144540900 浏览次数:0次
蜜蜂vp加速器七天试用_云南人力资源和社会保障网_企业培训课程清单_电商培训机构排名

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/gin"func 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风格的API,rest是一种架构风格,跟编程语言无关,跟平台无关,采用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/gin""github.com/thinkerou/favicon""net/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 lang="en">
<head><meta charset="UTF-8"><title>我的Go页面</title><link rel="stylesheet" href="/static/css/style.css"><script src="/static/js/common.js"></script>
</head>
<body><h1>首页</h1>
</body>
</html>

 style.css

body {background: rosybrown;
}

 

        css,js之后也会出文章

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:"username"`PassWord 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:"username"`PassWord 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:"username"`PassWord 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 = "/test2"router.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/gin"func 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}
}

关键字:蜜蜂vp加速器七天试用_云南人力资源和社会保障网_企业培训课程清单_电商培训机构排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: