package model import ( "encoding/json" "time" ) type Order struct { ID int OrderNo string UserID int Amount float64 User User `gorm:"foreignKey:UserID;references:ID"` OrderProducts []OrderProduct `gorm:"foreignKey:OrderNo;references:OrderNo"` CreatedAt time.Time UpdatedAt time.Time } func (Order) TableName() string { return "orders" } func (o Order) MarshalJSON() ([]byte, error) { type Alias Order return json.Marshal(&struct { CreatedAt string UpdatedAt string *Alias }{ CreatedAt: o.CreatedAt.Format("2006-01-02 15:04:05"), UpdatedAt: o.UpdatedAt.Format("2006-01-02 15:04:05"), Alias: (*Alias)(&o), }) }