【发布时间】:2021-07-10 10:28:25
【问题描述】:
我正在寻找一种从 GORM 模型中动态获取字段大小的方法。
一个明显的用例是,当您想要验证对象中保存的数据不超过 GORM 模型的定义并因此不超过 DB 列大小时。
type MyStruct struct {
field1 string `gorm:"size:10"`
}
func main(){
s := MyStruct {field1: "abcdefghijklmnopqrstuvwxyz"} // len 26 > 10
// if len(s.field1) < ...
db.Create(&s) // This will panic
}
有没有办法在运行时获取值10?
【问题讨论】: