在golang的time.time中从mysql检索datetime
我已经在我的数据库(mySQL)中存储了这个datatime字段,最后的活动:在golang的time.time中从mysql检索datetime,mysql,go,Mysql,Go,我已经在我的数据库(mySQL)中存储了这个datatime字段,最后的活动:2017-06-12 11:07:09 我在我的OpenDB上使用paramparseTime=True 问题是输出是:last activity:{63632862429 0} 而不是2017-06-12 11:07:09 我做错了什么 谢谢 type DateType time.Time type User struct { LastActivity DateType } func (stUser *
2017-06-12 11:07:09
我在我的OpenDB上使用paramparseTime=True
问题是输出是:last activity:{63632862429 0}
而不是2017-06-12 11:07:09
我做错了什么
谢谢
type DateType time.Time
type User struct {
LastActivity DateType
}
func (stUser *User) GetUserDataByLogin(login string) {
db := OpenDB()
defer db.Close()
// Test the connection to the database
err := db.Ping()
checkErr(err)
err = db.QueryRow("SELECT last_activity FROM users WHERE login = ?", login).Scan(&stUser.LastActivity)
if err != nil {
if err == sql.ErrNoRows {
// there were no rows, but otherwise no error occurred
} else {
log.Fatal(err)
}
}
fmt.Println("last activity:", stUser.LastActivity)
}
时间。时间是一种可为空的类型。您必须预见到这一点,并使用类似pq包和NullTime
类型的东西,而不是time.time
。
cf:
pq.NullTime
是一种结构:
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
如果Valid
为True
则在Time
中会得到一个结果您必须声明DateType.String()
方法,如下所示:
func (t DateType) String() string {
return time.Time(t).String()
}
发件人:
声明的类型不继承任何绑定到现有类型的方法