登录
首页 >  Golang >  Go教程

goGin安装及初始化操作示例

来源:脚本之家

时间:2023-01-01 11:15:41 159浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《goGin安装及初始化操作示例》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下初始化、gin安装,希望所有认真读完的童鞋们,都有实质性的提高。

第一步初始化

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) 

以上就是《goGin安装及初始化操作示例》的详细内容,更多关于golang的资料请关注golang学习网公众号!

声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>