我想重新定义golang 标准库里面的time.Time 格式化,定义之后发现gorm 新增数据的时候会忽略我定义的字段。
//-----------1
type JsonTime time.Time
func (this JsonTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02 15:04:05"))
return []byte(stamp), nil
}
func (ct *JsonTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
ctv, _ := time.Parse("2006-01-02", s)
*ct = JsonTime(ctv)
return
}
//-----------2
type XXX struct{
Cretime JsonTime
}
是 gorm只支持标准的内置类型吗?
同有疑问,希望官方尽快fix
请问你们找到解决办法了没?
用万能的interface{} 来表示时间的字段吧
我找到了解决办法https://github.com/jinzhu/gorm/issues/1611#issuecomment-329654638
This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you
Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog
Most helpful comment
我找到了解决办法https://github.com/jinzhu/gorm/issues/1611#issuecomment-329654638