假如有如下结构体(同时也对应数据库里的一张表)
type Form struct {
ID int `json:"id"`
IsOk bool `json:"is_ok"`
// 一些其他字段
}
假如我现在要修改这个 Form 的某些值,前端在做逻辑的时候只会向后端提交有修改的值
因为 go bool 默认是 false ,当IsOk=false
的时候我怎么知道这个值是前端传上来的还是是它的默认值呢
同理int
和string
其实也有同样的问题,不过可以用特殊的字符规避掉
大佬们有更换的解决办法吗
1
virusdefender 2021-10-28 17:22:36 +08:00 1
*bool
|
2
Cheivin 2021-10-28 17:22:50 +08:00 1
要不你考虑考虑*bool
|
3
imherer OP |
4
XTTX 2021-10-29 12:40:52 +08:00 1
// UpdateUser defines what information may be provided to modify an existing
// User. All fields are optional so clients can send just the fields they want // changed. It uses pointer fields so we can differentiate between a field that // was not provided and a field that was provided as explicitly blank. Normally // we do not want to use pointers to basic types but we make exceptions around // marshalling/unmarshalling. type UpdateUser struct { Username *string `json:"username"` Email *string `json:"email" validate:"omitempty,email"` Password *string `json:"password"` PasswordConfirm *string `json:"passwordConfirm" validate:"omitempty,eqfield=Password"` } |