Golang中的时间戳

5

我正在尝试在我的应用程序中使用这种时间戳方法:https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go

这就是它:

package timestamp

import (
    "fmt"
    "labix.org/v2/mgo/bson"
    "strconv"
    "time"
)

type Timestamp time.Time

func (t *Timestamp) MarshalJSON() ([]byte, error) {
    ts := time.Time(*t).Unix()
    stamp := fmt.Sprint(ts)

    return []byte(stamp), nil
}

func (t *Timestamp) UnmarshalJSON(b []byte) error {
    ts, err := strconv.Atoi(string(b))
    if err != nil {
        return err
    }

    *t = Timestamp(time.Unix(int64(ts), 0))

    return nil
}

func (t Timestamp) GetBSON() (interface{}, error) {
    if time.Time(*t).IsZero() {
        return nil, nil
    }

    return time.Time(*t), nil
}

func (t *Timestamp) SetBSON(raw bson.Raw) error {
    var tm time.Time

    if err := raw.Unmarshal(&tm); err != nil {
        return err
    }

    *t = Timestamp(tm)

    return nil
}

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

我需要翻译与之相关的文章:https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f

然而,我遇到了以下错误:

core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)                                                                                                                                                     
core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)

我的相关代码如下:

import (
    "github.com/path/to/timestamp"
)

type User struct {
    Name        string
    Created_at  *timestamp.Timestamp  `bson:"created_at,omitempty" json:"created_at,omitempty"`
} 

有人能看出我做错了什么吗?

相关问题 我也不知道如何实现这个包。我要创建一个新的用户模型,类似于这样吗?

u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
2个回答

6

你的代码存在拼写错误。你不能解引用一个非指针类型,所以你需要将GetBSON变成指针接收者(或者你可以移除对t的间接引用,因为该方法不会改变t的值)。

func (t *Timestamp) GetBSON() (interface{}, error) {

要内联设置*Timestamp值,您需要有一个*time.Time进行转换。
now := time.Now()
u := User{
    Name:      "Bob",
    CreatedAt: (*Timestamp)(&now),
}

构造函数和辅助函数如New()Now()也可能会很有用。


这正是我所想的,但如果你查看 GH 上的修订记录,那么代码的作者对此进行了特定更改。请在此处查看最新修订版本:https://gist.github.com/bsphere/8369aca6dde3e7b4392c/revisions - tommyd456
你知道在创建新用户时如何实现这个包吗? - tommyd456
1
这个更改没有意义,因为它不再是有效的Go代码。通过编译可以轻松验证。我不会对博客文章中的随机代码块抱有太多信心。 - JimB
我不知道你所说的“在创建新用户时实现此包”的意思。 - JimB
我只是在测试它 - 我所说的实现是如何创建一个带有时间戳值的created_at字段的新用户。 - tommyd456
@tommyd456:啊,我明白了。添加了一个内联创建*Timestamp的示例。 - JimB

0

您不能引用非指针变量的间接引用。

var a int = 3         // a = 3
var A *int = &a       // A = 0x10436184
fmt.Println(*A == a)  // true, both equals 3
fmt.Println(*&a == a) // true, both equals 3
fmt.Println(*a)       // invalid indirect of a (type int)

因此,您不能使用*a引用a的地址。

看一下错误发生的位置:

func (t Timestamp) GetBSON() (interface{}, error) {
        // t is a variable type Timestamp, not type *Timestamp (pointer)

        // so this is not possible at all, unless t is a pointer variable
        // and you're trying to dereference it to get the Timestamp value
        if time.Time(*t).IsZero() {
                return nil, nil
        }
        // so is this
        return time.Time(*t), nil
}

2
你不能获取函数返回值的地址(&time.Now()),也不能获取转换的地址(&timestamp.Timestamp(time.Now()))。这行代码在Go语言中是无效的。 - JimB
@JimB 你说得对。我太匆忙了。那部分已经删除,谢谢。 - Pandemonium

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接