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

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...