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

发布时间:2021-10-12 10:55:02 作者:iii
来源:亿速云 阅读:377

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

我们在写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框架中利用结构体来获取参数”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 结构体/struct
  2. 如何在golang中使用gin网络框架

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ajax gin

上一篇:ASP.NET中后台注册js脚本怎么用

下一篇:Knative Eventing 0.6 版本新特性有哪些呢

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》