如皋市建设局网站在哪,网站制作方案,网站建设电话邀约话术,天津 网站备案官方文档地址#xff08;中文#xff09;#xff1a;https://gin-gonic.com/zh-cn/docs/ 注#xff1a;没用过Gin的读者强烈建议先阅读第一节#xff1a;Gin操作指南#xff1a;开山篇。 本节继续演示POST绑定#xff0c;包括将request-body绑定到不同的结构体中#x…官方文档地址中文https://gin-gonic.com/zh-cn/docs/ 注没用过Gin的读者强烈建议先阅读第一节Gin操作指南开山篇。 本节继续演示POST绑定包括将request-body绑定到不同的结构体中映射查询字符串或表单参数上传文件 Query和post-form。 目录 一、将request-body绑定到不同的结构体中二、映射查询字符串或表单参数三、上传文件四、Query和post-form 一、将request-body绑定到不同的结构体中
package mainimport (net/httpgithub.com/gin-gonic/gin
)// 定义表单结构体 formA包含一个必需字段 Foo
type formA struct {Foo string json:foo xml:foo binding:required // 绑定 JSON 和 XML 的字段 foo
}// 定义表单结构体 formB包含一个必需字段 Bar
type formB struct {Bar string json:bar xml:bar binding:required // 绑定 JSON 和 XML 的字段 bar
}// 处理请求的函数
func SomeHandler(c *gin.Context) {objA : formA{} // 创建 formA 的实例objB : formB{} // 创建 formB 的实例// c.ShouldBind 使用了 c.Request.Body不可重用。if errA : c.ShouldBind(objA); errA nil {// 如果绑定成功返回表单A的成功信息c.String(http.StatusOK, the body should be formA)} else if errB : c.ShouldBind(objB); errB nil {// 因为现在 c.Request.Body 是 EOF所以这里会报错。c.String(http.StatusOK, the body should be formB)} else {// 处理绑定错误c.String(http.StatusBadRequest, Invalid input)}
}func main() {router : gin.Default()router.POST(/some-handler, SomeHandler) // 注册路由router.Run(:8080) // 启动服务器
}注意if分支中的注释效果如下 要想多次绑定可以使用 c.ShouldBindBodyWith.
func SomeHandler(c *gin.Context) { objA : formA{} objB : formB{} // 读取 c.Request.Body 并将结果存入上下文。 if errA : c.ShouldBindBodyWith(objA, binding.JSON); errA nil { c.String(http.StatusOK, the body should be formA) // 这时, 复用存储在上下文中的 body。 } else if errB : c.ShouldBindBodyWith(objB, binding.JSON); errB nil { c.String(http.StatusOK, the body should be formB JSON) // 可以接受其他格式 } else if errB2 : c.ShouldBindBodyWith(objB, binding.XML); errB2 nil { c.String(http.StatusOK, the body should be formB XML) } else { … } } c.ShouldBindBodyWith 会在绑定之前将 body 存储到上下文中。 这会对性能造成轻微影响如果调用一次就能完成绑定的话那就不要用这个方法。 只有某些格式需要此功能如 JSON, XML, MsgPack, ProtoBuf。 对于其他格式, 如 Query, Form, FormPost, FormMultipart 可以多次调用 c.ShouldBind() 而不会造成任任何性能损失 (详见 #1341)。
二、映射查询字符串或表单参数
package mainimport (fmt // 导入格式化I/O库github.com/gin-gonic/gin // 导入Gin框架
)func main() {router : gin.Default() // 创建默认的Gin路由// 设置处理POST请求的路由router.POST(/post, func(c *gin.Context) {// c.QueryMap(ids) 用于映射查询字符串中的 ids 参数// 例如POST /post?ids[a]1234ids[b]hello// 这个方法会将查询参数转换为一个mapkey为参数名value为参数值ids : c.QueryMap(ids)// c.PostFormMap(names) 用于映射表单数据中的 names 参数// 例如names[first]thinkerounames[second]tianou// 这个方法会将表单数据转换为一个mapnames : c.PostFormMap(names)// 打印解析后的 ids 和 names 的值// ids: map[b:hello a:1234], names: map[second:tianou first:thinkerou]fmt.Printf(ids: %v; names: %v, ids, names)})router.Run(:8080) // 启动服务器并监听8080端口
}打开postman输入http://localhost:8080/post?ids[a]1234ids[b]hello 然后在body中添加key和value注意names[]作为一个key效果如图
三、上传文件
在”上传文件”目录下建立两个文件夹demo01和demo02demo01用于单文件上传
package mainimport (fmtlognet/httpgithub.com/gin-gonic/gin
)func main() {router : gin.Default()// 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)// 将内存限制设置为 8 MiB这意味着如果上传的文件超过这个大小// 将会返回错误确保服务器不会因为大文件上传而耗尽内存router.MaxMultipartMemory 8 20 // 8 MiB// 定义一个 POST 路由处理文件上传router.POST(/upload, func(c *gin.Context) {// 从请求中获取文件file 是表单字段的名称file, err : c.FormFile(file)if err ! nil {// 处理文件获取错误返回状态码 400 和错误信息c.String(http.StatusBadRequest, Error retrieving the file)return}// 打印文件名到服务器日志log.Println(file.Filename)// 设置文件保存的目标路径文件将保存在当前目录下dst : ./ file.Filename// 上传文件至指定的完整文件路径if err : c.SaveUploadedFile(file, dst); err ! nil {// 如果文件保存失败返回状态码 500 和错误信息c.String(http.StatusInternalServerError, Unable to save the file)return}// 返回成功信息告知用户文件已上传c.String(http.StatusOK, fmt.Sprintf(%s uploaded!, file.Filename))})// 启动服务器监听 8080 端口router.Run(:8080)
}打开postman输入http://loaclhost:8080/upload在body中填充form-datakeyfile类型为File然后选择文件选好后点击send效果如下 多文件上传
package mainimport (fmtlognet/httpgithub.com/gin-gonic/gin
)func main() {router : gin.Default()// 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)// 将内存限制设置为 8 MiB这意味着如果上传的文件总大小超过这个限制// 将返回错误确保服务器不会因为大文件上传而耗尽内存router.MaxMultipartMemory 8 20 // 8 MiB// 定义一个 POST 路由用于处理文件上传router.POST(/upload, func(c *gin.Context) {// Multipart form// 从请求中获取 multipart 表单数据form, err : c.MultipartForm()if err ! nil {// 如果获取 multipart 表单数据失败返回状态码 400 和错误信息c.String(http.StatusBadRequest, Failed to get multipart form)return}// 获取上传的文件upload[] 是表单字段的名称files : form.File[upload[]]// 遍历每个文件for _, file : range files {// 打印文件名到服务器日志log.Println(file.Filename)// 设置文件保存的目标路径文件将保存在当前目录下dst : ./ file.Filename// 上传文件至指定目录if err : c.SaveUploadedFile(file, dst); err ! nil {// 如果文件保存失败返回状态码 500 和错误信息c.String(http.StatusInternalServerError, Unable to save the file)return}}// 返回成功信息告知用户上传的文件数量c.String(http.StatusOK, fmt.Sprintf(%d files uploaded!, len(files)))})// 启动服务器监听 8080 端口router.Run(:8080)
} 四、Query和post-form
package mainimport (fmtgithub.com/gin-gonic/gin
)func main() {// 创建一个新的 Gin 路由引擎router : gin.Default()// 定义一个 POST 路由用于处理 POST 请求router.POST(/post, func(c *gin.Context) {// 从查询字符串中获取 id 参数id : c.Query(id)// 从查询字符串中获取 page 参数如果未提供则返回默认值 0page : c.DefaultQuery(page, 0)// 从 POST 请求的表单数据中获取 name 参数name : c.PostForm(name)// 从 POST 请求的表单数据中获取 message 参数message : c.PostForm(message)// 打印获取到的参数值到标准输出fmt.Printf(id: %s; page: %s; name: %s; message: %s\n, id, page, name, message)})// 启动 HTTP 服务器监听 8080 端口router.Run(:8080)
}效果