gpt4 book ai didi

mysql - 在 golang 的 time.time 中从 mysql 检索日期时间

转载 作者:IT王子 更新时间:2023-10-29 01:44:44 26 4
gpt4 key购买 nike

我已经在我的数据库 (mySQL) 中存储了这个数据时间字段,last_activity: 2017-06-12 11:07:09

我正在使用参数 parseTime=True在我的 OpenDB 上

问题是输出是:last activity: {63632862429 0 <nil>}而不是 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)

最佳答案

您必须像这样声明 DateType.String() 方法:

func (t DateType) String() string {
return time.Time(t).String()
}

来自 Language Specification :

The declared type does not inherit any methods bound to the existing type

关于mysql - 在 golang 的 time.time 中从 mysql 检索日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44498738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com