1、简介
1.1. 介绍
-
Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确,具有快速灵活,容错方便等特点
-
对于golang而言,web框架的依赖要远比Python,Java之类的要小。自身的
net/http
足够简单,性能也非常不错 -
借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范
1.2. 安装
要安装Gin软件包,您需要安装Go并首先设置Go工作区。
1.首先需要安装Go(需要1.10+版本),然后可以使用下面的Go命令安装Gin。
go get -u github.com/gin-gonic/gin
2.将其导入您的代码中:
import "github.com/gin-gonic/gin"
3.(可选)导入net/http。例如,如果使用常量,则需要这样做http.StatusOK。
import "net/http"
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello world!!")
})
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8001")
}
终端运行命令
go run main.go
打开浏览器,访问:localhost:8001
2、基本路由
-
gin 框架中采用的路由库是基于httprouter做的
后端请求同样分为:GET POST PUT
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello world!!")
})
// post请求
r.POST("/post")
// put 请求
r.PUT("/put")
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8001")
}
3. Restful风格的API
-
gin支持Restful风格的API
-
即Representational State Transfer的缩写。直接翻译的意思是"表现层状态转化",是一种互联网应用程序的API设计理念:URL定位资源,用HTTP描述操作
1.获取文章 /blog/getXxx Get blog/Xxx
2.添加 /blog/addXxx POST blog/Xxx
3.修改 /blog/updateXxx PUT blog/Xxx
4.删除 /blog/delXxxx DELETE blog/Xxx
4、API参数
通过Context的Param方法来获取API参数 (不推荐)
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
// 截取
action = strings.Trim(action, "/")
c.String(http.StatusOK, name+"hello !!"+action)
})
// post请求
// r.POST("/post1")
// put 请求
// r.PUT("/put1")
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8001")
}
表单参数(推荐)
- 表单传输为post请求,http常见的传输格式为四种:
- application/json
- application/x-www-form-urlencoded
- application/xml
- multipart/form-data
- 表单参数可以通过PostForm()方法获取,该方法默认解析的是x-www-form-urlencoded或from-data格式的参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8080/form" method="post" action="application/x-www-form-urlencoded" style="left: auto;">
用户名:<input type="text" name="username" placeholder="请输入你的用户名"> <br>
密 码:<input type="password" name="userpassword" placeholder="请输入你的密码"> <br>
<input type="submit" value="提交">
</form>
</body>
</html>
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.POST("/form", func(c *gin.Context) {
types := c.DefaultPostForm("type", "post")
username := c.PostForm("username")
pwd := c.PostForm("userpassword")
c.String(http.StatusOK, fmt.Sprintf("username: %s, pwd: %s, type: %s", username, pwd, types))
})
// post请求
// r.POST("/post1")
// put 请求
// r.PUT("/put1")
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run()
}