Gorm: 怎么支持 time.Time 的别名

Created on 10 May 2018  ·  5Comments  ·  Source: go-gorm/gorm

我想重新定义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只支持标准的内置类型吗?

gorm_v1

Most helpful comment

All 5 comments

同有疑问,希望官方尽快fix

请问你们找到解决办法了没?

用万能的interface{} 来表示时间的字段吧

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  ·  3Comments

Quentin-M picture Quentin-M  ·  3Comments

youtwo123 picture youtwo123  ·  3Comments

koalacxr picture koalacxr  ·  3Comments

sredxny picture sredxny  ·  3Comments