go - 从 RethinkDB 获取时间

go - Get time from RethinkDB

本文关键字:获取 取时间 RethinkDB go      更新时间:2023-09-26

我在 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

所以我认为int64是适合CreatedAt的类型,但它没有用。我该怎么办?如何获取时间数据?

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

有关更多示例,您可以查看相应的测试用例。

我设法自己找到了答案。在此条件下使用 true 类型float64以便获取的结果与数据库浏览器中的结果匹配(例如,1458184908597毫秒)