我需要从数据库中读取日期,将其转换为某个时间戳并将其转换为 JSON。
我有以下代码:
package usages
import (
"fmt"
"time"
)
type SpecialOffer struct {
PublishedDate jsonTime `gorm:"column:publishing_date" json:"published_date"`
ExpirationDate jsonTime `gorm:"column:expiration_date" json:"expiration_date"`
}
type jsonTime struct {
time.Time
}
func (tt jsonTime) MarshalJSON() ([]byte, error) {
jsonTime := fmt.Sprintf("\"%s\"", tt.Format("20060102"))
return []byte(jsonTime), nil
}
当我像这样运行它时,我收到以下错误:
sql: Scan error on column index 8, name "publishing_date": unsupported Scan, storing driver.Value type time.Time into type *usages.trvTime
而且数据有误:
{"published_date":"00010101","expiration_date":"00010101"}
如果我将SpecialOffer结构更改为 use time.Time,它返回正确,但显然格式错误:
{"published_date":"2020-03-12T00:00:00Z","expiration_date":"2020-06-12T00:00:00Z"}
我究竟做错了什么?
扬帆大鱼
忽然笑
随时随地看视频慕课网APP
相关分类