亲宝软件园·资讯

展开

gin安装及初始化

Jeff的技术栈 人气:6

第一步初始化

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
}

加载全部内容

相关教程
猜你喜欢
用户评论