[TOC] ## 继承绑定模型 > [demo](https://github.com/gin-gonic/gin#bind-form-data-request-with-custom-struct) ``` 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"` } ``` ## 模型绑定 ``` type User struct { Username string `form:"username" json:"username" binding:"required"` Passwd string `form:"passwd" json:"passwd" binding:"required"` Age int `form:"age" json:"age"` } router.POST("/login", func(c *gin.Context) { var user User var err error err := c.Bind(&user) if err != nil { log.Fatal(err) } c.JSON(http.StatusOK, gin.H{ "user": user.Username, "passwd": user.Passwd, "age": user.Age, }) }) ``` ### 非自动绑定 Bind,BindJSON,BindXML ``` contentType := c.Request.Header.Get("Content-Type") switch contentType { case "application/json": err = c.BindJSON(&user) case "application/x-www-form-urlencoded": err = c.BindWith(&user, binding.Form) } ```