-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
请求body为formdata,对应接收结构体存在嵌套继承时,参数绑定不上 #4
Comments
这是 Go 匿名嵌套 type Student struct {
person // 这个结构体必须是首字母小写
School string `json:"school" form:"school"`
Grade int `json:"grade" form:"grade"`
}
解决这个问题有以下几个方法: 1、将
|
上面的解释不周,对于 匿名嵌套 struct 的名字可见性 的描述部分有误,正确的是:当匿名 struct 自身的 对于 |
@xmx 已经修复。更新 版本到 // go.mod
module mypackage
require github.com/xgfone/ship/v5 v5.1.1
go 1.11 // main.go
package main
import (
"fmt"
"net/http"
"github.com/xgfone/ship/v5"
)
type person struct {
Name string `json:"name" form:"name"`
Age int `json:"age" form:"age"`
}
type Student struct {
person // 这个结构体必须是首字母小写
School string `json:"school" form:"school"`
Grade int `json:"grade" form:"grade"`
}
func Demo(c *ship.Context) error {
var stu Student
if err := c.Bind(&stu); err != nil {
return ship.ErrBadRequest.New(err)
}
fmt.Println(stu.Name) // OK
fmt.Println(stu.Age) // OK
fmt.Println(stu.School) // OK
fmt.Println(stu.Grade) // OK
return c.JSON(http.StatusOK, stu)
}
func main() {
router := ship.Default()
router.Route("/demo").POST(Demo)
ship.StartServer(":9999", router)
// $ go run main.go
//
// 请求 body 格式是 FormData:
// $ curl -X POST -F "name=小明" -F "age=9" -F "school=新民小学" -F "grade=3" http://localhost:9999/demo
//
// 响应结果:
// {"name":"小明","age":9,"school":"新民小学","grade":3}
} |
速度! |
go version:1.17.3
ship version:5.1.0
复现代码:
The text was updated successfully, but these errors were encountered: