如何在gin框架中利用结构体来获取参数

这篇文章主要介绍“如何在gin框架中利用结构体来获取参数”,在日常操作中,相信很多人在如何在gin框架中利用结构体来获取参数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何在gin框架中利用结构体来获取参数”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

目前成都创新互联公司已为上1000+的企业提供了网站建设、域名、虚拟主机、网站托管维护、企业网站设计、王屋网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

我们在写http请求的时候都会遇到后端的Handler是如何来接受请求的参数,我们在请求的时候有表单请求,ajax请求(参数是json),如下:

http://localhost:8080/bind?name=tim&age=1

在gin框架中我们是怎么利用结构体来获取参数呢?   其实很简单我们直接看代码

engine.GET("/bind", handler.BindHandler)


package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

// 定义结构体
type Member struct {
	Name string `form:"name"`
	Age  int    `form:"age"`
}

func BindHandler(c *gin.Context) {
	m := &Member{}
	c.Bind(m)
	fmt.Println(m)
	c.JSON(200, gin.H{
		"code": "ok",
	})
}

就是利用Bind函数将参数和结构体进行绑定

// Bind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
//     "application/json" --> JSON binding
//     "application/xml"  --> XML binding
// otherwise --> returns an error.
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (c *Context) Bind(obj interface{}) error {
	b := binding.Default(c.Request.Method, c.ContentType())
	return c.MustBindWith(obj, b)
}

Bind的注释可以看出如何绑定和绑定的格式和Content-Type有很大的关系,从源码可以看出有很多类型格式的数据都都可以进行绑定

// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
func (c *Context) BindJSON(obj interface{}) error {
	return c.MustBindWith(obj, binding.JSON)
}

// BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).
func (c *Context) BindXML(obj interface{}) error {
	return c.MustBindWith(obj, binding.XML)
}

// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query).
func (c *Context) BindQuery(obj interface{}) error {
	return c.MustBindWith(obj, binding.Query)
}

// BindYAML is a shortcut for c.MustBindWith(obj, binding.YAML).
func (c *Context) BindYAML(obj interface{}) error {
	return c.MustBindWith(obj, binding.YAML)
}

// BindHeader is a shortcut for c.MustBindWith(obj, binding.Header).
func (c *Context) BindHeader(obj interface{}) error {
	return c.MustBindWith(obj, binding.Header)
}

// BindUri binds the passed struct pointer using binding.Uri.
// It will abort the request with HTTP 400 if any error occurs.
func (c *Context) BindUri(obj interface{}) error {
	if err := c.ShouldBindUri(obj); err != nil {
		c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) // nolint: errcheck
		return err
	}
	return nil
}

到此,关于“如何在gin框架中利用结构体来获取参数”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网页标题:如何在gin框架中利用结构体来获取参数
转载注明:http://bzwzjz.com/article/pgdpss.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 手机网站制作 网站建设改版 高端网站设计推广 高端网站建设 重庆外贸网站建设 网站制作 营销型网站建设 成都网站建设推广 定制网站设计 营销型网站建设 成都网站制作 成都网站制作 网站建设费用 响应式网站设计 成都网站设计 网站建设公司 手机网站建设 成都网站制作 上市集团网站建设 网站建设开发 成都网站建设 成都网站建设公司