文檔地址:https://www.kancloud.cn/shuangdeyu/gin_book/949411

一,請求類型

GET / POST

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/pathG", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world GET")
	})
	("/pathP", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world POST")
	})
	r.Run(":9099") // listen and serve on 0.0.0.0:8080
}

Go

返回:

Get:hello world GET

POST:hello world POST


二, 傳參/接參形式

1,/:params形式 uri

GET模式支持,POST不支持

r.GET("/pathG/:uid", func(c *gin.Context) {
		uid := c.Param("uid")
		c.String(http.StatusOK, "hello world GET %s", uid)
	})

Go

結果:

Go之Gin框架(基礎)_基礎

2,?k=v形式

GET模式支持, POST不支持

r.GET("/pathG/:uid", func(c *gin.Context) {
		uid := c.Param("uid")
		name := c.Query("name")
		c.String(http.StatusOK, "hello world GET uid=%s ,name=%s", uid, name)
	})

Go

結果:

Go之Gin框架(基礎)_Gin_02

擴展:

①,Query → DefaultQuery : 如果客户端沒有傳值,使用默認值

r.GET("/pathG/:uid", func(c *gin.Context) {
		uid := c.Param("uid")
		name := c.DefaultQuery("name", "kayer")
		c.String(http.StatusOK, "hello world GET uid=%s ,name=%s", uid, name)
	})

Go

結果:

Go之Gin框架(基礎)_基礎_03

3, Form表單形式

r.POST("/pathP", func(c *gin.Context) {
		uid := c.PostForm("uid")
		name := c.DefaultPostForm("name", "kayer") //如果前端沒有傳name參數, 則name的值為:kayer

		c.String(http.StatusOK, "hello world POST uid=%s ,name=%s", uid, name)
	})

Go

結果:

Go之Gin框架(基礎)_Go_04

三,bind

1, 結構體,用於保存傳參值

type TestGinBindParams struct {
	Name string `form:"name" json:"name"`
	Age  int    `form:"age" json:"age"`
	Sex  bool   `form:"sex" json:"sex"`
}

Go

2,go代碼

r.GET("/testBind", func(c *gin.Context) {
		var params TestGinBindParams
		err := c.ShouldBindQuery(¶ms)
		if err != nil {
			c.String(http.StatusBadRequest, "status:11")
		} else {
			fmt.Println(params)
			b, _ := json.Marshal(params)
			fmt.Println(string(b))
			c.String(http.StatusOK, "hello world GET %s", string(b))
		}
	})
	r.POST("/testPost", func(c *gin.Context) {
		var params TestGinBindParams
		err := c.ShouldBindQuery(¶ms)
		if err != nil {
			c.String(http.StatusBadRequest, "status:11")
		} else {
			fmt.Println(params)
			b, _ := json.Marshal(params)
			fmt.Println(string(b))
			c.String(http.StatusOK, "hello world Post %s", string(b))
		}
	})

Go

結果:

Go之Gin框架(基礎)_Gin_05

Go之Gin框架(基礎)_基礎_06

擴展: 自定義驗證器


type TestGinBindParams struct {
	Name string `form:"name" json:"name" binding:"required,mustBig"` //binding:"required" 前端必傳 mustBig: 自定義驗證器
	Age  int    `form:"age" json:"age"`
	Sex  bool   `form:"sex" json:"sex"`
}

func mustBig(fl validator.FieldLevel) bool {
	i := fl.Top().Interface()
	if v, ok := i.(*TestGinBindParams); ok {
		return v.Age >= 18
	}
	return false
}

Go


r := gin.Default()
	//註冊自定義驗證器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("mustBig", mustBig)
	}

	r.GET("/pathG/:uid", func(c *gin.Context) {
		uid := c.Param("uid")
		name := c.DefaultQuery("name", "kayer")
		c.String(http.StatusOK, "hello world GET uid=%s ,name=%s", uid, name)
	})

Go

結果:

Go之Gin框架(基礎)_Go_07

Go之Gin框架(基礎)_Gin_08


全部代碼

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	json "github.com/goccy/go-json" // 別名保持 json,後面零改動
	"net/http"
)

type TestGinBindParams struct {
	Name string `form:"name" json:"name" binding:"required,mustBig"` //binding:"required" 前端必傳 mustBig: 自定義驗證器
	Age  int    `form:"age" json:"age"`
	Sex  bool   `form:"sex" json:"sex"`
}

func mustBig(fl validator.FieldLevel) bool {
	i := fl.Top().Interface()
	if v, ok := i.(*TestGinBindParams); ok {
		return v.Age >= 18
	}
	return false
}

func main() {
	r := gin.Default()
	//註冊自定義驗證器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("mustBig", mustBig)
	}

	r.GET("/pathG/:uid", func(c *gin.Context) {
		uid := c.Param("uid")
		name := c.DefaultQuery("name", "kayer")
		c.String(http.StatusOK, "hello world GET uid=%s ,name=%s", uid, name)
	})
	r.GET("/testBind", func(c *gin.Context) {
		var params TestGinBindParams
		err := c.ShouldBindQuery(¶ms)
		if err != nil {
			c.String(http.StatusBadRequest, "status:11")
		} else {
			fmt.Println(params)
			b, _ := json.Marshal(params)
			fmt.Println(string(b))
			c.String(http.StatusOK, "hello world GET %s", string(b))
		}
	})
	r.POST("/testPost", func(c *gin.Context) {
		var params TestGinBindParams
		err := c.ShouldBindQuery(¶ms)
		if err != nil {
			c.String(http.StatusBadRequest, "status:11")
		} else {
			fmt.Println(params)
			b, _ := json.Marshal(params)
			fmt.Println(string(b))
			c.String(http.StatusOK, "hello world Post %s", string(b))
		}
	})
	r.Run(":9099") // listen and serve on 0.0.0.0:8080
}