网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在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://bjjierui.cn/article/pgdpss.html

其他资讯