gpt4 book ai didi

go - 马提尼绑定(bind) "cannot return value obtained from unexported field or method"

转载 作者:IT王子 更新时间:2023-10-29 02:24:07 25 4
gpt4 key购买 nike

我有以下路线:

m.Post("/users", binding.Bind(models.User{}), func(user models.User, r render.Render)

当我尝试执行 Post 请求时收到以下错误消息:

"PANIC: reflect.Value.Interface: cannot return value obtained from unexported field or method"

type User struct {
id int
UUID string `json:"uuid"`
Username string `json:"userName" form:"userName" binding:"required"`
Firstname string `json:"firstName" form:"Firstname" binding:"required`
Lastname string `json:"lastName" form:"Lastname" binding:"required`
Email string `json:"email" form:"Email" binding:"required`
IsActive bool `json:"isActive"`
DateJoined time.Time `json:"dateJoined"`
}

有人知道吗?

最佳答案

我现在知道问题了。

问题是用户结构的属性“id”。如果“id”重命名为“Id”,绑定(bind)过程将完美运行。

使用标记 form:"-" 可以忽略结构属性。

type User struct {
id int `form:"-"`
UUID string `json:"uuid"`
Username string `json:"userName" form:"userName" binding:"required"`
Firstname string `json:"firstName" form:"Firstname" binding:"required`
Lastname string `json:"lastName" form:"Lastname" binding:"required`
Email string `json:"email" form:"Email" binding:"required`
IsActive bool `json:"isActive"`
DateJoined time.Time `json:"dateJoined"`
}

关于go - 马提尼绑定(bind) "cannot return value obtained from unexported field or method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com