Introduction to gin of go language, routing group settings, data parsing and binding, and gin middleware

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
insert image description here
front-end page html
insert image description here

Gin implements file upload

The front-end page html
insert image description here
main.go
insert image description here
limits the file upload size and uploads multiple files
insert image description here

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:
insert image description here
insert image description here

gin routing principle

httprpter will construct a prefix tree of all routing rules
insert image description here

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:
insert image description here

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:
insert image description here

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:
insert image description here

gin middleware

insert image description here

global middleware

insert image description here
insert image description here
Console effect:
insert image description here

partial middleware

insert image description here

Guess you like

Origin blog.csdn.net/ydl1128/article/details/126360673