微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL数据库timestamp,date类型字段,GO读取格式化

go语言从Postgresql数据库读取数据,
timestamp类型的字段,直接读取后为2018-01-01T15:59:24Z 格式
date类型字段,直接读取为2018-01-01T00:00:00Z 格式

要做一下处理
定义LocalTime、LocalDate结构体
实现MarshalJSON接口,格式化一下数据

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"`
}

原文地址:https://www.jb51.cc/postgresql/193307.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐