Golang中的时间戳

huangapple go评论77阅读模式
英文:

Timestamps in Golang

问题

尝试让我的应用程序中的时间戳方法起作用: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"`
} 

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

相关问题
我也不知道如何使用这个包。我是否需要创建一个新的User模型,类似于这样?

u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
英文:

Trying to get this approach to timestamps working in my application: https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go

Here it is:

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()
}

and the article that goes with it: https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f

However, I'm getting the following error:

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

My relevant code looks like this:

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

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

Can anyone see what I'm doing wrong?

Related question
I can't see how to implement this package either. Do I create a new User model something like this?

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

答案1

得分: 6

你的代码中有一个拼写错误。你不能对非指针进行解引用,所以你需要将GetBSON方法改为指针接收器(或者你可以删除对t的间接引用,因为方法不会改变t的值)。

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

要内联设置*Timestamp值,你需要有一个*time.Time来进行转换。

now := time.Now()
u := User{
    Name:      "Bob",
    CreatedAt: (*Timestamp)(&now),
}

构造函数和辅助函数(如New()Now())也可能对此有帮助。

英文:

Your code has a typo. You can't dereference a non-pointer, so you need to make GetBSON a pointer receiver (or you could remove the indirects to t, since the value of t isn't changed by the method).

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

To set a *Timestamp value inline, you need to have a *time.Time to convert.

now := time.Now()
u := User{
	Name:      "Bob",
	CreatedAt: (*Timestamp)(&now),
}

Constructor and a helper functions like New() and Now() may come in handy for this as well.

答案2

得分: 0

你不能引用一个不是指针变量的间接引用。

var a int = 3         // a = 3
var A *int = &a       // A = 0x10436184
fmt.Println(*A == a)  // true,两者都等于3
fmt.Println(*&a == a) // true,两者都等于3
fmt.Println(*a)       // a的间接引用无效(类型为int)

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

看一下错误发生的地方:

func (t Timestamp) GetBSON() (interface{}, error) {
        // t是一个变量类型为Timestamp,而不是*Timestamp(指针类型)

        // 因此,除非t是一个指针变量,并且你试图取消引用它以获取Timestamp值,否则这是不可能的
        if time.Time(*t).IsZero() {
                return nil, nil
        }
        // 这也是如此
        return time.Time(*t), nil
}
英文:

You cannot refer to an indirection of something that is not a pointer variable.

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)

Thus, you can not reference the address of a with *a.

Looking at where the error happens:

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
}

huangapple
  • 本文由 发表于 2015年8月15日 01:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32015364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定