亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 Protobuf 的時間戳庫與 Go 的時間庫一起使用

如何將 Protobuf 的時間戳庫與 Go 的時間庫一起使用

Go
郎朗坤 2023-03-29 15:14:13
Import fromgithub.com/golang/protobuf/ptypes/timestamp提供了 Protobuf 的本機時間戳實現,可以在您的 protobuf 定義中使用來表示時間。仔細查看timestamp.pb.go所提供的文件,它生成了struct如下內容: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:"-"`}里面有一些注釋示例,timestamp.pb.go但我不是很理解。time 在 go 的庫中使用它。我不確定我應該如何設置Timestamp. 我假設這兩種類型之間的“轉換”并不困難,但我對 Go 和 protobuf 不陌生。任何幫助,將不勝感激。
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您必須手動將其轉換為 time.Time。

對于非指針值:

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

對于指針值:

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


查看完整回答
反對 回復 2023-03-29
  • 1 回答
  • 0 關注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號