gin introduction
Gin is a golang micro-framework with elegant encapsulation, friendly API, clear source code annotations, fast, flexible, and fault-tolerant convenience.
For golang, the dependency of the web framework is much smaller than that of Python, Java and the like. Its own net/http is simple enough, and its performance is also very good.
With the help of framework development, it can not only save a lot of time brought by commonly used packaging, but also help the team's coding style and form norms
gin install
go get -u github.com/gin-gonic/gin
gin route
func main() {
//创建路由
r :=gin.Default()
//绑定路由,执行的函数
//gin.Context,封装了request和respose
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK,"hello hello")
})
r.Run(":8080")
}
Browse and enter: localhost:8080.
gin implements simple form submission
main.go
front-end page html
Gin implements file upload
The front-end page html
main.go
limits the file upload size and uploads multiple files
gin routing group
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
//创建路由
r :=gin.Default()
v1 := r.Group("/v1")
{
v1.GET("/login",login)
v1.GET("/registry",registry)
}
v2 := r.Group("/v2")
{
v2.POST("/login",login)
v2.POST("/registry",registry)
}
//绑定路由,执行的函数
//gin.Context,封装了request和respose
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK,"hello hello")
})
r.Run(":8080")
}
func login(c *gin.Context) {
name := c.DefaultQuery("name","jack")
c.String(200,fmt.Sprintf("login: hello %s\n",name))
}
func registry(c *gin.Context) {
name := c.DefaultQuery("name","jack")
c.String(200,fmt.Sprintf("registry: hello %s\n",name))
}
Browser input:
gin routing principle
httprpter will construct a prefix tree of all routing rules
gin data analysis and binding
Structure:
type Login struct {
User string `form:"username" json:"user" url:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" url:"password" xml:"password" binding:"required"`
}
JSON data parsing and binding
r :=gin.Default()
//绑定路由,执行的函数
//gin.Context,封装了request和respose
r.GET("/", func(c *gin.Context) {
var json Login
err := c.ShouldBindJSON(&json)
if err != nil {
//返回错误
c.JSON(500,gin.H{
"error":err.Error()})
}
fmt.Println(json.User,json.Password)
c.String(200,fmt.Sprintf("login: user = %s,password = %s \n",json.User,json.Password))
//c.JSON(200,json)
})
r.Run(":8080")
postman call:
form binding
r.GET("/", func(c *gin.Context) {
var form Login
if err :=c.Bind(&form); err != nil{
//返回错误
c.JSON(500,gin.H{
"error":err.Error()})
}
c.JSON(http.StatusOK,gin.H{
"ok":form})
})
r.Run(":8080")
Postman test results:
URL data parsing and binding
r.POST("/:user/:password", func(c *gin.Context) {
var login Login
//如果想用地址栏拼参数的方法,例如:http://localhost:8000/gin?user=zhangsan&password=123
//可以用c.Query("key")来获取值
err := c.ShouldBindUri(&login)
if err != nil {
//返回错误
c.JSON(500,gin.H{
"error":err.Error()})
}
fmt.Println(login.User,login.Password)
//c.String(200,fmt.Sprintf("login: user = %s,password = %s \n",login.User,login.Password))
//c.JSON(200,json)
c.JSON(http.StatusOK,gin.H{
"ok":login})
})
r.Run(":8080")
postman test:
gin middleware
global middleware
Console effect: