PostgreSQL数据库timestamp,date类型字段,GO读取格式化
发布时间:2020-12-13 16:43:26 所属栏目:百科 来源:网络整理
导读:go语言从PostgreSQL数据库读取数据, timestamp类型的字段,直接读取后为2018-01-01T15:59:24Z 格式 date类型字段,直接读取为2018-01-01T00:00:00Z 格式 需要做一下处理 定义LocalTime、LocalDate结构体 实现MarshalJSON接口,格式化一下数据 type LocalTim
go语言从PostgreSQL数据库读取数据, 需要做一下处理 type LocalTime time.Time
// MarshalJSON satify the json marshal interface
func (l LocalTime) MarshalJSON() ([]byte,error) {
stamp := fmt.Sprintf(""%s"",time.Time(l).Format("2006-01-02 15:04:05"))
return []byte(stamp),nil
}
type LocalDate time.Time
// MarshalJSON satify the json marshal interface
func (l LocalDate) MarshalJSON() ([]byte,time.Time(l).Format("2006-01-02"))
return []byte(stamp),nil
}
在定义接收数据的结构体时,定义成员类型为LocalTime、LocalDate,可以根据需要格式化出数据 type LogInfo struct {
ID int `form:"id" json:"id" gorm:"id"`
LogName string `form:"log_name" json:"log_name" gorm:"column:log_name"`
CreateTime LocalTime `form:"create_time" json:"create_time" gorm:"column:create_time"`
CreateDate LocalDate `form:"create_date" json:"create_date" gorm:"column:create_date"`
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |