首頁 > 軟體

go Gin安裝及初始化操作範例

2022-04-15 16:00:24

第一步初始化

1.新建資料夾(專案名)

2.終端進入該專案,敲 go mod init gin-class 產生go.mode檔案

3.golang開啟,Go Modules設定GOPROXY=https://goproxy.io

4.新建mian.go,下面程式碼貼上進去

5.執行

package main
import "github.com/gin-gonic/gin"
func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

修改啟動埠

# 預設是8080埠
r.Run(":8888")

get請求url取引數

//get請求   "/get/123?user=jeff&pwd=admin"
r.GET("/get/:id", func(c *gin.Context) {
		id := c.Param("id")
		user := c.DefaultQuery("user","jeff")
		pwd := c.Query("pwd")
		c.JSON(200, gin.H{
			"message": "hell gyy",
			"id":id,
			"user":user,
			"pwd":pwd,
		})
	})

Param:取“?”之前的引數

Query:取“?”之後的引數

DefaultQuery:優先取key值,沒有就用預設值

Post請求獲取form引數

r.POST("/post", func(c *gin.Context) {
		user := c.DefaultPostForm("user","jeff")
		pwd := c.PostForm("pwd")
		c.JSON(200, gin.H{
			"message": "hell gyy",
			"user":user,
			"pwd":pwd,
		})
	})

DefaultPostForm :優先取key值,沒有就用預設值

PostForm:取key

模型繫結ShouldBind

ShouldBindJSON

package main
import "github.com/gin-gonic/gin"
type PostParams struct {
	Name string `json:"name"`
	Age int `json:"age"`
	Sex bool `json:"sex"`
}
func main() {
	r.POST("/testBind", func(c *gin.Context) {
		p := PostParams{}
		err := c.ShouldBindJSON(&p)
		if err != nil{
			c.JSON(400,gin.H{
				"msg":"出錯!",
				"data":gin.H{},
			})
		}else{
			c.JSON(200,gin.H{
				"msg":"success",
				"data":p,
			})
		}
		c.JSON(200, gin.H{
		})
	})
	r.Run(":8888") // listen and serve on 0.0.0.0:8080
}

ShouldBinduri

package main
import "github.com/gin-gonic/gin"
func main() {
	r := gin.Default()  //攜帶中介軟體啟動路由
	r.POST("/testBind/:name/:age/:sex", func(c *gin.Context) {
		p := PostParams{}
		err := c.ShouldBindUri(&p)
		if err != nil{
			c.JSON(400,gin.H{
				"msg":"出錯!",
				"data":gin.H{},
			})
		}else{
			c.JSON(200,gin.H{
				"msg":"success",
				"data":p,
			})
		}
		c.JSON(200, gin.H{
		})
	})
	r.Run(":8888") // listen and serve on 0.0.0.0:8080
}
type PostParams struct {
	Name string `json:"name" uri:"name"`
	Age int `json:"age" uri:"age"`
	Sex bool `json:"sex" uri:"ses"`
}

ShouldBindQuery

package main
import "github.com/gin-gonic/gin"
func main() {
	r := gin.Default()  //攜帶中介軟體啟動路由
	r.POST("/testBind", func(c *gin.Context) {
		p := PostParams{}
		err := c.ShouldBindQuery(&p)
		if err != nil{
			c.JSON(400,gin.H{
				"msg":"出錯!",
				"data":gin.H{},
			})
		}else{
			c.JSON(200,gin.H{
				"msg":"success",
				"data":p,
			})
		}
		c.JSON(200, gin.H{
		})
	})
	r.Run(":8888") // listen and serve on 0.0.0.0:8080
}
type PostParams struct {
	Name string `json:"name" uri:"name" form:"name"`
	Age int `json:"age" uri:"age" form:"age"`
	Sex bool `json:"sex" uri:"ses" form:"sex"`
}

自定義驗證器,表單驗證

binding:"required,mustBig"

package main
import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)
type PostParams struct {
	Name string `json:"name" uri:"name" form:"name"`
	Age int `json:"age" uri:"age" form:"age" binding:"required,mustBig"`
	Sex bool `json:"sex" uri:"ses" form:"sex"`
}
// 過濾年齡小於18
func mustBig(f1 validator.FieldLevel) bool {
	if f1.Field().Interface().(int) <=18{
		return false
	}
	return true
}
func main() {
	r := gin.Default()  //攜帶中介軟體啟動路由
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("mustBig", mustBig)
	}
	r.POST("/testBind", func(c *gin.Context) {
		p := PostParams{}
		err := c.ShouldBindJSON(&p)
		if err != nil{
			c.JSON(400,gin.H{
				"msg":"小於18歲了!",
				"data":gin.H{},
			})
		}else{
			c.JSON(200,gin.H{
				"msg":"success",
				"data":p,
			})
		}
		c.JSON(200, gin.H{
		})
	})
	r.Run(":8888") // listen and serve on 0.0.0.0:8080
}

以上就是gin安裝及初始化操作範例的詳細內容,更多關於gin安裝及初始化的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com