新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

gin使用自定义结构实现绑定表单数据-创新互联

今天就跟大家聊聊有关gin使用自定义结构实现绑定表单数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联主要从事网站设计制作、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务石棉,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

以下示例使用自定义结构

type StructA struct {
  FieldA string `form:"field_a"`
}
 
type StructB struct {
  NestedStruct StructA
  FieldB string `form:"field_b"`
}
 
type StructC struct {
  NestedStructPointer *StructA
  FieldC string `form:"field_c"`
}
 
type StructD struct {
  NestedAnonyStruct struct {
    FieldX string `form:"field_x"`
  }
  FieldD string `form:"field_d"`
}
 
func GetDataB(c *gin.Context) {
  var b StructB
  c.Bind(&b)
  c.JSON(200, gin.H{
    "a": b.NestedStruct,
    "b": b.FieldB,
  })
}
 
func GetDataC(c *gin.Context) {
  var b StructC
  c.Bind(&b)
  c.JSON(200, gin.H{
    "a": b.NestedStructPointer,
    "c": b.FieldC,
  })
}
 
func GetDataD(c *gin.Context) {
  var b StructD
  c.Bind(&b)
  c.JSON(200, gin.H{
    "x": b.NestedAnonyStruct,
    "d": b.FieldD,
  })
}
 
func main() {
  r := gin.Default()
  r.GET("/getb", GetDataB)
  r.GET("/getc", GetDataC)
  r.GET("/getd", GetDataD)
 
  r.Run()
}

当前文章:gin使用自定义结构实现绑定表单数据-创新互联
转载来源:http://www.wjwzjz.com/article/dhhjhj.html
在线咨询
服务热线
服务热线:028-86922220
TOP