• Gin学习笔记


    Gin学习笔记

    Gin文档:https://pkg.go.dev/github.com/gin-gonic/gin

    1、快速入门

    1.1、安装Gin

    go get -u github.com/gin-gonic/gin
    
    • 1

    1.2、main.go

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	// 创建路由引擎
    	r := gin.Default()
    
    	// 添加路由监听
    	r.GET("/", func(c *gin.Context) {
    		c.String(http.StatusOK, "Hello Gin!")
    	})
    
    	//启用 web 服务
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    2、配置热更新

    Air文档:https://github.com/cosmtrek/air

    2.1、下载

    # 添加air包
    go get -u github.com/cosmtrek/air
    # 编译并安装air到 $GOPATH/bin 目录(重启一下Goland)
    go install github.com/cosmtrek/air@latest
    
    • 1
    • 2
    • 3
    • 4

    2.2、使用

    # 直接使用air即可热加载
    air
    
    • 1
    • 2

    3、响应数据

    3.1、String

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.LoadHTMLGlob("./template/*.html")
    
    	r.GET("/", func(c *gin.Context) {
    		c.String(http.StatusOK, "Hello Gin!")
    	})
    	r.GET("/hello", func(c *gin.Context) {
    		//c.JSON(http.StatusOK, map[string]interface{}{
    		//	"name": "xumeng03",
    		//	"age":  "24",
    		//})
    
    		c.JSON(http.StatusOK, gin.H{
    			"name": "xumeng03",
    			"age":  "24",
    		})
    	})
    	r.GET("/gin", func(c *gin.Context) {
    		c.HTML(http.StatusOK, "gin.html", gin.H{
    			"path": c.FullPath(),
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    3.2、JSON

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    	r.GET("/hello", func(c *gin.Context) {
    		//c.JSON(http.StatusOK, map[string]interface{}{
    		//	"name": "xumeng03",
    		//	"age":  "24",
    		//})
    
    		c.JSON(http.StatusOK, gin.H{
    			"name": "xumeng03",
    			"age":  "24",
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3.3、HTML

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.LoadHTMLGlob("./template/*.html")
    
    	r.GET("/gin", func(c *gin.Context) {
    		c.HTML(http.StatusOK, "gin.html", gin.H{
    			"path": c.FullPath(),
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Gin Studytitle>
    head>
    <body>
    <h1>Gin Study!h1>
    <p>请求路径:{{.path}}p>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、请求数据

    4.1、Get

    1、直接查询
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.GET("/", func(c *gin.Context) {
    		// Query 查询 request 的参数,DefaultQuery 同样查询 request 的参数但若未查询到则赋一个默认值
    		//var name = c.Query("name")
    		var name = c.DefaultQuery("name", "Gin")
    		c.String(http.StatusOK, "Hello %s!", name)
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2、绑定到结构体
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.GET("/", func(c *gin.Context) {
    		var person = Person{}
    		err := c.ShouldBind(&person)
    		if err != nil {
    			c.JSON(http.StatusOK, gin.H{
    				"status":  500,
    				"message": "Params Error!",
    			})
                return
    		}
    		c.JSON(http.StatusOK, gin.H{
    			"status": 200,
    			"data":   person,
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    type Person struct {
    	Name string `json:"name" form:"name"`
    	Age  string `json:"age" form:"age"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    4.2、Post

    1、直接查询
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.POST("/", func(c *gin.Context) {
    		// PostForm 查询 request 的参数,DefaultPostForm 同样查询 request 的参数但若未查询到则赋一个默认值
    		var name = c.PostForm("name")
    		var age = c.DefaultPostForm("age", "20")
    		c.String(http.StatusOK, "Hello %s %s!", name, age)
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    2、绑定到结构体(form-data)
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.POST("/", func(c *gin.Context) {
    		var person = Person{}
    		err := c.ShouldBind(&person)
    		if err != nil {
    			c.JSON(http.StatusOK, gin.H{
    				"status":  500,
    			})
                return
    		}
    		c.JSON(http.StatusOK, gin.H{
    			"status":  500,
    			"data": person,
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    type Person struct {
    	Name string `json:"name" form:"name"`
    	Age  string `json:"age" form:"age"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    3、绑定到结构体(json)
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.POST("/json", func(c *gin.Context) {
    		var person = Person{}
    		err := c.ShouldBindJSON(&person)
    		if err != nil {
    			c.JSON(http.StatusOK, gin.H{
    				"status": 500,
    			})
    			return
    		}
    		c.JSON(http.StatusOK, gin.H{
    			"status": 200,
    			"data":   person,
    		})
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    type Person struct {
    	Name string `json:"name" form:"name"`
    	Age  string `json:"age" form:"age"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    5、Restful

    http://127.0.0.1/item/1 查询,GET

    http://127.0.0.1/item 新增,POST

    http://127.0.0.1/item 更新,PUT

    http://127.0.0.1/item/1 删除,DELETE

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	r.GET("/get/:name", func(c *gin.Context) {
    		param := c.Param("name")
    		c.String(http.StatusOK, "Hello get %s!", param)
    	})
    
    	r.POST("/post/:name", func(c *gin.Context) {
    		param := c.Param("name")
    		c.String(http.StatusOK, "Hello post %s!", param)
    	})
    
    	r.PUT("/put/:name", func(c *gin.Context) {
    		param := c.Param("name")
    		c.String(http.StatusOK, "Hello put %s!", param)
    	})
    
    	r.DELETE("/delete/:name", func(c *gin.Context) {
    		param := c.Param("name")
    		c.String(http.StatusOK, "Hello delete %s!", param)
    	})
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    6、路由分组

    6.1、基本使用

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func main() {
    	r := gin.Default()
    
    	group := r.Group("/restful0")
    	{
    		group.GET("/get/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello get %s!", param)
    		})
    
    		group.POST("/post/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello post %s!", param)
    		})
    	}
    
    	group1 := r.Group("/restful1")
    	{
    		group1.PUT("/put/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello put %s!", param)
    		})
    
    		group1.DELETE("/delete/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello delete %s!", param)
    		})
    	}
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    6.2、拆分文件

    package main
    
    import (
    	"ginstudy/router"
    	"github.com/gin-gonic/gin"
    )
    
    func main() {
    	r := gin.Default()
    
    	router.Restful0(r)
    	router.Restful1(r)
    
    	err := r.Run()
    	if err != nil {
    		return
    	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    package router
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func Restful0(r *gin.Engine) {
    	group := r.Group("/restful0")
    	{
    		group.GET("/get/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello get %s!", param)
    		})
    
    		group.POST("/post/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello post %s!", param)
    		})
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    package router
    
    import (
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    func Restful1(r *gin.Engine) {
    	group1 := r.Group("/restful1")
    	{
    		group1.PUT("/put/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello put %s!", param)
    		})
    
    		group1.DELETE("/delete/:name", func(c *gin.Context) {
    			param := c.Param("name")
    			c.String(http.StatusOK, "Hello delete %s!", param)
    		})
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    7、自定义控制器

    7.1、目录结构

    在这里插入图片描述

    7.2、router

    app.go

    package app
    
    import (
        "ginstudy/controller/app"
        "github.com/gin-gonic/gin"
    )
    
    func AppApi(r *gin.Engine) {
        group := r.Group("/app/api")
        appController := app.AppController{}
    
        group.GET("/", appController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    web.go

    package web
    
    import (
        "ginstudy/controller/web"
        "github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
        group := r.Group("/web/api")
        webController := web.WebController{}
    
        group.GET("/", webController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7.3、controller

    app.go

    package app
    
    import (
        "ginstudy/controller"
        "github.com/gin-gonic/gin"
    )
    
    type AppController struct {
        controller.StandardController
    }
    
    func (app AppController) Index(c *gin.Context) {
        //c.String(http.StatusOK, "Hello App Api!")
        app.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    web.go

    package web
    
    import (
        "ginstudy/controller"
        "github.com/gin-gonic/gin"
    )
    
    type WebController struct {
        controller.StandardController
    }
    
    func (web WebController) Index(c *gin.Context) {
        //c.String(http.StatusOK, "Hello Web Api!")
        web.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    standard.go

    package controller
    
    import (
        "github.com/gin-gonic/gin"
        "net/http"
    )
    
    type StandardController struct{}
    
    func (standard StandardController) Success(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
           "status": 200,
        })
    }
    
    func (standard StandardController) Error(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
           "status": 500,
        })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7.4、main

    package main
    
    import (
        "ginstudy/router/app"
        "ginstudy/router/web"
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        r := gin.Default()
    
        app.AppApi(r)
        web.WebApi(r)
    
        err := r.Run()
        if err != nil {
           return
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    7.5、go.mod

    module ginstudy
    
    go 1.20
    
    require (
        dario.cat/mergo v1.0.0 // indirect
        github.com/bep/godartsass v1.2.0 // indirect
        github.com/bep/godartsass/v2 v2.0.0 // indirect
        github.com/bep/golibsass v1.1.1 // indirect
        github.com/bytedance/sonic v1.10.2 // indirect
        github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
        github.com/chenzhuoyu/iasm v0.9.1 // indirect
        github.com/cli/safeexec v1.0.1 // indirect
        github.com/cosmtrek/air v1.49.0 // indirect
        github.com/creack/pty v1.1.20 // indirect
        github.com/fatih/color v1.15.0 // indirect
        github.com/fsnotify/fsnotify v1.7.0 // indirect
        github.com/gabriel-vasile/mimetype v1.4.3 // indirect
        github.com/gin-contrib/sse v0.1.0 // indirect
        github.com/gin-gonic/gin v1.9.1 // indirect
        github.com/go-playground/locales v0.14.1 // indirect
        github.com/go-playground/universal-translator v0.18.1 // indirect
        github.com/go-playground/validator/v10 v10.16.0 // indirect
        github.com/goccy/go-json v0.10.2 // indirect
        github.com/gohugoio/hugo v0.120.3 // indirect
        github.com/json-iterator/go v1.1.12 // indirect
        github.com/klauspost/cpuid/v2 v2.2.5 // indirect
        github.com/leodido/go-urn v1.2.4 // indirect
        github.com/mattn/go-colorable v0.1.13 // indirect
        github.com/mattn/go-isatty v0.0.20 // indirect
        github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
        github.com/modern-go/reflect2 v1.0.2 // indirect
        github.com/pelletier/go-toml v1.9.5 // indirect
        github.com/pelletier/go-toml/v2 v2.1.0 // indirect
        github.com/spf13/afero v1.10.0 // indirect
        github.com/tdewolff/parse/v2 v2.7.4 // indirect
        github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
        github.com/ugorji/go/codec v1.2.11 // indirect
        golang.org/x/arch v0.6.0 // indirect
        golang.org/x/crypto v0.14.0 // indirect
        golang.org/x/net v0.17.0 // indirect
        golang.org/x/sys v0.14.0 // indirect
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/protobuf v1.31.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    8、中间处理程序

    8.1、基本使用

    standard.go

    package controller
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    type StandardController struct{}
    
    func (standard StandardController) Success(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 200,
    	})
    }
    
    func (standard StandardController) Error(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 500,
    	})
    }
    
    func (standard StandardController) Aop(c *gin.Context) {
    	fmt.Println("aop start")
    	// 放行请求
    	c.Next()
    	// 拒绝请求
    	//c.Abort()
    	fmt.Println("aop end")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    web.go

    package web
    
    import (
    	"ginstudy/controller/web"
    	"github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
    	group := r.Group("/web/api")
    	webController := web.WebController{}
    
    	group.GET("/", webController.Aop, webController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    app.go

    package app
    
    import (
        "ginstudy/controller/app"
        "github.com/gin-gonic/gin"
    )
    
    func AppApi(r *gin.Engine) {
        group := r.Group("/app/api")
        appController := app.AppController{}
    
        group.GET("/", appController.Aop, appController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.2、多中间处理程序

    standard.go

    package controller
    
    import (
        "fmt"
        "github.com/gin-gonic/gin"
        "net/http"
    )
    
    type StandardController struct{}
    
    func (standard StandardController) Success(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
           "status": 200,
        })
    }
    
    func (standard StandardController) Error(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
           "status": 500,
        })
    }
    
    func (standard StandardController) Aop1(c *gin.Context) {
        fmt.Println("aop1 start")
        // 放行请求
        c.Next()
        // 拒绝请求
        //c.Abort()
        fmt.Println("aop1 end")
    }
    
    func (standard StandardController) Aop2(c *gin.Context) {
        fmt.Println("aop2 start")
        // 放行请求
        c.Next()
        // 拒绝请求
        //c.Abort()
        fmt.Println("aop2 end")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    web.go

    package web
    
    import (
        "ginstudy/controller/web"
        "github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
        group := r.Group("/web/api")
        webController := web.WebController{}
    
        group.GET("/", webController.Aop1, webController.Aop2, webController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    app.go

    package app
    
    import (
        "ginstudy/controller/app"
        "github.com/gin-gonic/gin"
    )
    
    func AppApi(r *gin.Engine) {
        group := r.Group("/app/api")
        appController := app.AppController{}
    
        group.GET("/", appController.Aop1, appController.Aop2, appController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.3、全局中间处理程序

    路由组也可单独配置中间处理程序!

    main.go

    package main
    
    import (
        "ginstudy/controller"
        "ginstudy/router/app"
        "ginstudy/router/web"
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        standardController := controller.StandardController{}
        r := gin.Default()
        r.Use(standardController.Aop1, standardController.Aop2)
    
        app.AppApi(r)
        web.WebApi(r)
    
        err := r.Run()
        if err != nil {
           return
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    web.go

    package web
    
    import (
        "ginstudy/controller/web"
        "github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
        group := r.Group("/web/api")
        webController := web.WebController{}
    
        group.GET("/", webController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    app.go

    package app
    
    import (
        "ginstudy/controller/app"
        "github.com/gin-gonic/gin"
    )
    
    func AppApi(r *gin.Engine) {
        group := r.Group("/app/api")
        appController := app.AppController{}
    
        group.GET("/", appController.Index)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.4、中间处理程序通信

    standard.go

    package controller
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    type StandardController struct{}
    
    func (standard StandardController) Success(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 200,
    	})
    }
    
    func (standard StandardController) Error(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 500,
    	})
    }
    
    func (standard StandardController) Aop1(c *gin.Context) {
    	fmt.Println("aop1 start")
    	c.Set("name", "xumeng03")
    	// 放行请求
    	c.Next()
    	// 拒绝请求
    	//c.Abort()
    	fmt.Println("aop1 end")
    }
    
    func (standard StandardController) Aop2(c *gin.Context) {
    	fmt.Println("aop2 start")
    	fmt.Println(c.Get("name"))
    	// 放行请求
    	c.Next()
    	// 拒绝请求
    	//c.Abort()
    	fmt.Println("aop2 end")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    8.5、协程中使用中间处理程序

    standard.go

    package controller
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"net/http"
    	"time"
    )
    
    type StandardController struct{}
    
    func (standard StandardController) Success(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 200,
    	})
    }
    
    func (standard StandardController) Error(c *gin.Context) {
    	c.JSON(http.StatusOK, gin.H{
    		"status": 500,
    	})
    }
    
    func (standard StandardController) Aop1(c *gin.Context) {
    	fmt.Println("aop1 start")
    	c.Set("name", "xumeng03")
    	// 放行请求
    	c.Next()
    	// 拒绝请求
    	//c.Abort()
    	fmt.Println("aop1 end")
    }
    
    func (standard StandardController) Aop2(c *gin.Context) {
    	fmt.Println("aop2 start")
    	fmt.Println(c.Get("name"))
    	// 放行请求
    	c.Next()
    	// 拒绝请求
    	//c.Abort()
      
      // 不能直接使用gin.Context,需要使用c.Copy()获取gin.Context的副本
      // c.Copy()只会复制gin.Context结构体中的字段值,并不会复制底层的请求和响应对象
    	cc := c.Copy()
    	go func() {
    		time.Sleep(5 * time.Second)
    		fmt.Println(cc.Request.URL.Path, "被访问了")
    	}()
    	fmt.Println("aop2 end")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    9、自定义模块

    package utils
    
    import "time"
    
    func UnixToDatetime(unix int64) string {
        return time.Unix(unix, 0).Format("2006-01-02 15:04:05")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    10、文件上传

    10.1、单文件上传

    在这里插入图片描述

    router/webgo

    package web
    
    import (
        "ginstudy/controller/web"
        "github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
        group := r.Group("/web/api")
        webController := web.WebController{}
    
        group.GET("/", webController.Index)
        group.POST("/upload", webController.Upload)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    controller/web.go

    package web
    
    import (
    	"fmt"
    	"ginstudy/controller"
    	"github.com/gin-gonic/gin"
    	"path"
    )
    
    type WebController struct {
    	controller.StandardController
    }
    
    func (web WebController) Index(c *gin.Context) {
    	//c.String(http.StatusOK, "Hello Web Api!")
    	fmt.Println("Hello Web Api!")
    	web.Success(c)
    }
    
    func (web WebController) Upload(c *gin.Context) {
    	file, err := c.FormFile("file")
    	if err != nil {
    		fmt.Println("upload error!")
    	}
    	fmt.Println(file.Filename)
    	// 路径为main.go所在目录
    	p := path.Join("static", file.Filename)
    	err = c.SaveUploadedFile(file, p)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	web.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    10.2、多文件上传

    在这里插入图片描述

    router/webgo

    package web
    
    import (
        "ginstudy/controller/web"
        "github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
        group := r.Group("/web/api")
        webController := web.WebController{}
    
        group.GET("/", webController.Index)
        group.POST("/uploads", webController.Uploads)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    controller/web.go

    package web
    
    import (
    	"fmt"
    	"ginstudy/controller"
    	"github.com/gin-gonic/gin"
    	"path"
    )
    
    type WebController struct {
    	controller.StandardController
    }
    
    func (web WebController) Index(c *gin.Context) {
    	//c.String(http.StatusOK, "Hello Web Api!")
    	fmt.Println("Hello Web Api!")
    	web.Success(c)
    }
    
    func (web WebController) Uploads(c *gin.Context) {
    	form, _ := c.MultipartForm()
    	fmt.Println(form)
    	files := form.File["files"]
    	for k, file := range files {
    		fmt.Println(k, file.Filename)
    		p := path.Join("static", file.Filename)
    		err := c.SaveUploadedFile(file, p)
    		if err != nil {
    			return
    		}
    	}
    	web.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    11、Cookie&Session

    11.1、cookie

    package web
    
    import (
    	"fmt"
    	"ginstudy/controller"
    	"github.com/gin-gonic/gin"
    	"path"
    )
    
    type WebController struct {
    	controller.StandardController
    }
    
    func (web WebController) Index(c *gin.Context) {
    	//c.String(http.StatusOK, "Hello Web Api!")
    	fmt.Println("Hello Web Api!")
    	web.Success(c)
    }
    
    func (web WebController) Cookie(c *gin.Context) {
    	// name: cookie 名称
    	// value: cookie 值
    	// maxAge: 存活时间,单位秒(等于0则一直存活,小于0则删除cookie)
    	// path: cookie 可用路径
    	// domain: cookie 可用域名,如需在多个二级域名下共享,设置时需设置为“.bilibili.com”格式
    	// secure: 是否只在 https 启用
    	// httponly: cookie仅后端可操作
    	cookie, err := c.Cookie("GinStudy")
    	if err != nil {
    		fmt.Println(err)
    	}
    	if cookie == "" {
    		c.SetCookie("GinStudy", "xumeng03", 3600, "/", "localhost", false, false)
    	}
    	web.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    11.2、session

    https://github.com/gin-contrib/sessions#redis

    1、安装
    go get github.com/gin-contrib/sessions
    
    • 1
    2、设置存储引擎

    main.go

    package main
    
    import (
    	"ginstudy/router/app"
    	"ginstudy/router/web"
    	"github.com/gin-contrib/sessions"
    	"github.com/gin-contrib/sessions/cookie"
    	"github.com/gin-gonic/gin"
    )
    
    func main() {
    	r := gin.Default()
    
    	// 创建基于cookie的存储引擎
    	store := cookie.NewStore([]byte("Ginstudy"))
    	r.Use(sessions.Sessions("session", store))
    
    	app.AppApi(r)
    	web.WebApi(r)
    
    	err := r.Run()
    	if err != nil {
    		return
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    3、使用session

    router/web.go

    package web
    
    import (
    	"ginstudy/controller/web"
    	"github.com/gin-gonic/gin"
    )
    
    func WebApi(r *gin.Engine) {
    	group := r.Group("/web/api")
    	webController := web.WebController{}
    
    	group.GET("/", webController.Index)
    	group.GET("/session", webController.Session)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    controller/web.go

    package web
    
    import (
    	"fmt"
    	"ginstudy/controller"
    	"github.com/gin-contrib/sessions"
    	"github.com/gin-gonic/gin"
    	"path"
    )
    
    type WebController struct {
    	controller.StandardController
    }
    
    func (web WebController) Index(c *gin.Context) {
    	//c.String(http.StatusOK, "Hello Web Api!")
    	fmt.Println("Hello Web Api!")
    	web.Success(c)
    }
    
    func (web WebController) Session(c *gin.Context) {
    	// 获取session
    	session := sessions.Default(c)
    	name := session.Get("name")
    	if name == nil {
    		session.Set("name", "xumeng03")
    		// 设置值(需要手动保存)
    		session.Save()
    	} else {
    		fmt.Println(name)
    	}
    	web.Success(c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    11.3、分布式session

    待补。。。

  • 相关阅读:
    若要对多态类进行深拷贝,应使用虚函数的clone,而不是公开的拷贝构造赋值
    【10套模拟】【5】
    docker 和 docker-compose的区别
    Leetcode刷题详解——猜数字大小 II
    neo4j下载安装配置步骤
    软件项目管理 6.7.参数估算法
    WebRTC创建客户端
    opencv 基础(持续更新中)
    VMware Horizon 8 运维系列(二)win10设置共享桌面图标
    什么是Jmeter?Jmeter使用的原理步骤是什么?
  • 原文地址:https://blog.csdn.net/gyfghh/article/details/134237395