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 }
- 1 回答
- 0 關注
- 282 瀏覽
添加回答
舉報
0/150
提交
取消