package main

import (
	"github.com/gin-gonic/gin"
	//"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {
	Name  string 'json:"name"'
	Age int 'json:"age"'
	Sex bool 'json:"sex"'
}



func main() {
	r := gin.Default() //携带基础中间件
	r.POST("/testBind", func(context *gin.Context) {
		var p PostParams
		err :=context.ShouldBindJSON(&p)
		if err!=nil{

			context.JSON(200,gin.H{
				"msg":"报错了",
				"data":gin.H{

				},
			})
		}else{
			context.JSON(200,gin.H{
				"msg":"报错了",
				"data ":p,
			})
		}

	})
	//r.GET("/path/:id", func(c *gin.Context) {
	//	id:=c.Param("id")
	//	user:=c.DefaultQuery("user","Luckly")
	//	pwd:=c.Query("pwd")
	//	c.JSON(200, gin.H{
	//		"id":id,
	//		"user":user,
	//		"pwd":pwd,
	//
	//
	//	})
	//})
	//r.POST("/path", func(c *gin.Context) {
	//
	//	pwd:=c.PostForm("pwd")
	//	user:=c.DefaultQuery("user","Luckly")
	//	c.JSON(200, gin.H{
	//	"pwd":pwd,
	//	"user":user,
	//	})
	//})
	//r.PUT("/path", func(c *gin.Context) {
	//	pwd := c.PostForm("pwd")
	//	user := c.DefaultQuery("user", "Luckly")
	//	c.JSON(200, gin.H{
	//		"pwd":  pwd,
	//		"user": user,
	//	})
	//
	//	r.DELETE("/path/:id", func(c *gin.Context) {
	//		c.JSON(200, gin.H{
	//			"message": "pong",
	//			"sucess":  true,
	//		})
	//	})

		r.Run() //yyyyy listen and serve on 0.0.0.0:8080
	}

image-20210418113930470

package main

import (
	"github.com/gin-gonic/gin"
	//"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {
	//Name  string 'json:"name"'
	User string ` json:"user"`
	Age  int    ` json:"age"`
	Sex  bool   ` json:"sex"`
}


func main() {
	r := gin.Default() //携带基础中间件
	r.POST("/testBind", func(context *gin.Context) {
		var p PostParams
		err :=context.ShouldBindJSON(&p)
		if err!=nil{

			context.JSON(200,gin.H{
				"msg":"报错了",
				"data":gin.H{

				},
			})
		}else{
			context.JSON(200,gin.H{
				"msg":"成功",
				"data ":p,
			})
		}

	})
	//r.GET("/path/:id", func(c *gin.Context) {
	//	id:=c.Param("id")
	//	user:=c.DefaultQuery("user","Luckly")
	//	pwd:=c.Query("pwd")
	//	c.JSON(200, gin.H{
	//		"id":id,
	//		"user":user,
	//		"pwd":pwd,
	//
	//
	//	})
	//})
	//r.POST("/path", func(c *gin.Context) {
	//
	//	pwd:=c.PostForm("pwd")
	//	user:=c.DefaultQuery("user","Luckly")
	//	c.JSON(200, gin.H{
	//	"pwd":pwd,
	//	"user":user,
	//	})
	//})
	//r.PUT("/path", func(c *gin.Context) {
	//	pwd := c.PostForm("pwd")
	//	user := c.DefaultQuery("user", "Luckly")
	//	c.JSON(200, gin.H{
	//		"pwd":  pwd,
	//		"user": user,
	//	})
	//
	//	r.DELETE("/path/:id", func(c *gin.Context) {
	//		c.JSON(200, gin.H{
	//			"message": "pong",
	//			"sucess":  true,
	//		})
	//	})

		r.Run() //yyyyy listen and serve on 0.0.0.0:8080
	}
package main

import (
	"github.com/gin-gonic/gin"
	//"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {

	Name string ` json:"name" uri:"name"`
	Age  int    ` json:"age"  uri:"age"`
	Sex  bool   ` json:"sex"  uri:"sex"`
}


func main() {
	r := gin.Default() //携带基础中间件
	r.POST("/testBind/:name/:age/:sex", func(context *gin.Context) {
		var p PostParams
		err :=context.ShouldBindUri(&p)
		if err!=nil{

			context.JSON(200,gin.H{
				"msg":"报错了",
				"data":gin.H{

				},
			})
		}else{
			context.JSON(200,gin.H{
				"msg":"成功",
				"data ":p,
			})
		}

	})
	//r.GET("/path/:id", func(c *gin.Context) {
	//	id:=c.Param("id")
	//	user:=c.DefaultQuery("user","Luckly")
	//	pwd:=c.Query("pwd")
	//	c.JSON(200, gin.H{
	//		"id":id,
	//		"user":user,
	//		"pwd":pwd,
	//
	//
	//	})
	//})
	//r.POST("/path", func(c *gin.Context) {
	//
	//	pwd:=c.PostForm("pwd")
	//	user:=c.DefaultQuery("user","Luckly")
	//	c.JSON(200, gin.H{
	//	"pwd":pwd,
	//	"user":user,
	//	})
	//})
	//r.PUT("/path", func(c *gin.Context) {
	//	pwd := c.PostForm("pwd")
	//	user := c.DefaultQuery("user", "Luckly")
	//	c.JSON(200, gin.H{
	//		"pwd":  pwd,
	//		"user": user,
	//	})
	//
	//	r.DELETE("/path/:id", func(c *gin.Context) {
	//		c.JSON(200, gin.H{
	//			"message": "pong",
	//			"sucess":  true,
	//		})
	//	})

		r.Run() //yyyyy listen and serve on 0.0.0.0:8080
	}
package main

import (
   "github.com/gin-gonic/gin"
   //"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {

   Name string ` json:"name" uri:"name" form:"name"`
   Age  int    ` json:"age"  uri:"age"  form:"age"`
   Sex  bool   ` json:"sex"  uri:"sex "  form:"sex"`
}
//http://localhost:8080/testBind?name=hah&age=18&sex=true

func main() {
   r := gin.Default() //携带基础中间件
   r.POST("/testBind", func(context *gin.Context) {
      var p PostParams
      err :=context.ShouldBindQuery(&p)
      if err!=nil{

         context.JSON(200,gin.H{
            "msg":"报错了",
            "data":gin.H{

            },
         })
      }else{
         context.JSON(200,gin.H{
            "msg":"成功",
            "data ":p,
         })
      }

   })


      r.Run() //yyyyy listen and serve on 0.0.0.0:8080
   }

changy

package main

import (
   "github.com/gin-gonic/gin"
   //"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {

   Name string ` json:"name" binding:"required"`
   Age  int    ` json:"age"  binding:"required"`
   Sex  bool   ` json:"sex"  binding:"required"`
}


func main() {
   r := gin.Default() //携带基础中间件
   r.POST("/testBind", func(context *gin.Context) {
      var p PostParams
      err :=context.ShouldBindJSON(&p)
      if err!=nil{

         context.JSON(200,gin.H{
            "msg":"报错了",
            "data":gin.H{

            },
         })
      }else{
         context.JSON(200,gin.H{
            "msg":"成功",
            "data ":p,
         })
      }

   })


      r.Run() //yyyyy listen and serve on 0.0.0.0:8080
   }
package main

import (
   "github.com/gin-gonic/gin"
   "github.com/gin-gonic/gin/binding"
   "github.com/go-playground/validator/v10"
   //"github.com/smartystreets/goconvey/convey"
)

type  PostParams struct {

   Name string ` json:"name" `
   Age  int    ` json:"age"  binding:"required,mustBig"`  //表单验证
   Sex  bool   ` json:"sex"  `
}

func mustBig (fl validator.FieldLevel)  bool {
if(fl.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(context *gin.Context) {
      var p PostParams

      err :=context.ShouldBindJSON(&p)
      //fmt.Println(err)
      if err!=nil{

         context.JSON(200,gin.H{
            "msg":"报错了",
            "data":gin.H{

            },
         })
      }else{
         context.JSON(200,gin.H{
            "msg":"成功",
            "data ":p,
         })
      }

   })


      r.Run() //yyyyy listen and serve on 0.0.0.0:8080
   }
package main

import (
   "fmt"
   "github.com/gin-gonic/gin"
   "io"
   "os"

   //"github.com/gin-gonic/gin/binding"
   //"github.com/go-playground/validator/v10"
   //"log"
   //"net/http"

   //"github.com/smartystreets/goconvey/convey"
)

func main() {
router := gin.Default()
// 给表单限制上传大小 (默认 32 MiB)
// router.MaxMultipartMemory = 8 << 20  // 8 MiB
router.POST("/upload", func(c *gin.Context) {
// 单文件
file, _ := c.FormFile("file")

//name:=c.PostForm("name") //混合传值
in,_:=file.Open()
defer in.Close()
out,_:=os.Create("./"+file.Filename)
defer  out.Close()
io.Copy(out,in)
c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s",file.Filename))
c.File("./"+file.Filename)

//c.SaveUploadedFile(file,"./"+file.Filename)
//c.JSON(200,gin.H{
//
// "msg":file,
// "name":name,
//})
//log.Println(file.Filename)

// 上传文件到指定的路径
// c.SaveUploadedFile(file, dst)

//c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
router.Run(":8081")
}

多文件

package main

import (
   "fmt"
   "github.com/gin-gonic/gin"
   "io"
   "os"

   //"github.com/gin-gonic/gin/binding"
   //"github.com/go-playground/validator/v10"
   //"log"
   //"net/http"

   //"github.com/smartystreets/goconvey/convey"
)

func main() {
router := gin.Default()
// 给表单限制上传大小 (默认 32 MiB)
// router.MaxMultipartMemory = 8 << 20  // 8 MiB
router.POST("/upload", func(c *gin.Context) {
// 多文件
   form,_=c.MultipartForm()
file, _ := form.File["file"]
fmt.Println(file)


})
router.Run(":8081")
}