2018-08-30 02:05
浏览 331


Import from gives a native timestamp implementation by Protobuf, could be used inside of your protobuf definitions to represent time. A closer look at the timestamp.pb.go file provided looks like it generated some struct like this:

type Timestamp struct {
    Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
    Nanos                int32    `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`

There are some commented examples inside the timestamp.pb.go but I'm not understanding it very well.

To use it in relation with go's time library. I'm not sure how I'm suppose to set the fields inside of Timestamp. I assuming "conversion" between the two types aren't difficult, but I'm quit new to Go and protobuf. Any help would be appreciated.

图片转代码服务由CSDN问答提供 功能建议导入给出了本机时间戳 Protobuf的实现可用于您的protobuf定义中以表示时间。 仔细查看提供的 timestamp.pb.go 文件,看起来它生成了如下的 struct

  type 时间戳记结构{
 Seconds int64`protobuf:“ varint,1,opt,name = seconds,proto3” json:“ seconds,omitempty”`
 Nanos int32`protobuf:“ varint,2,opt,name = nanos,proto3  “ json:” nanos,omitempty“`
 XXX_NoUnkeyedLiteral struct {}`json:”-“`n XXX_unrecognized [] byte`json:”-“`
 XXX_sizecache int32`json:”-“`
} \  n   

timestamp.pb.go 内部有一些已注释的示例,但我对此不太了解。 \ n

要将其与go的 time 相关联图书馆。 我不确定如何在 Timestamp 中设置字段。 我假设这两种类型之间的“转换”并不困难,但是我对Go和protobuf不熟悉。 任何帮助将不胜感激。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyan8929 2018-08-30 05:18

    You have to convert this manually into time.Time.

    For non-pointer values:

    if !u.Timestamp.IsZero() {
        timestamp, _ := ptypes.TimestampProto(u.Timestamp)
        up.Timestamp = timestamp

    For pointer values:

    if u.Timestamp != nil {
        timestamp, _ := ptypes.TimestampProto(*u.Timestamp)
        up.Timestamp = timestamp
    点赞 打赏 评论

相关推荐 更多相似问题