gpt4 book ai didi

javascript - go - 从 RethinkDB 获取时间

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

我在 Go 中有一个结构 Quote

type Quote struct{
CreatedAt int64 `gorethink:"createdAt"`
// Other fields...
}

我写从RethinkDB查询数据,成功

result,err:=r.Table("quote").GetAll(ids...).Run(session)

defer result.Close()

if err!=nil{
fmt.Println(err)
}
var quotes []Quote
err=result.All(&quotes)

它确实得到了真实的结果,但是没有记录在 CreatedAt 字段中具有值。我在数据库中用于 createdAt 的时间格式是自纪元 UTC 以来的毫秒数,我打算将它们用作数字之后进行计算

我花时间阅读 GoDocs 并发现:

func (t Time) Unix() int64

所以我认为 int64CreatedAt 的正确类型,但它没有用。我应该怎么办?如何获取时间数据?

time.Time 也不起作用。如果使用 time.Time,则结果始终为 0001-01-01 00:00:00 +0000 UTC(类似地,0 值如果转换为毫秒)

因为当我在 NodeJS 中构建相同的服务器时,我使用了 Date.Now(),在这种情况下我需要寻找一个等效的类型,它返回一个数字供我计算后面的过程

最佳答案

使用 gorethink 时驱动程序将自动与 GO 的本地数据类型相互转换。更改您的结构以使用 time.Time:

type Quote struct{
CreatedAt time.Time `gorethink:"created_at"`
}

有关更多示例,您可以查看相应的 test cases .

关于javascript - go - 从 RethinkDB 获取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150021/

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