drhib62644 2015-08-14 17:11
浏览 33
已采纳

戈朗的时间戳

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())}
  • 写回答

2条回答 默认 最新

  • dragonsun00000 2015-08-14 17:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置