欢迎来到代码驿站!

Golang

当前位置:首页 > 脚本语言 > Golang

go Gin安装及初始化操作示例

时间:2022-07-25 10:52:43|栏目:Golang|点击:

第一步初始化

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
}

上一篇:Go语言中的指针运算实例分析

栏    目:Golang

下一篇:一文搞懂Go Exec 僵尸与孤儿进程

本文标题:go Gin安装及初始化操作示例

本文地址:http://www.codeinn.net/misctech/208823.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有