简体   繁体   中英

Golang: How to Convert time.Time to a Protobuf Timestamp?

I have a time.Time variable in Golang 10-30 00:49:07.1236 that needs to be converted to a Go Protobuf timestamp.Timestamp . Any idea on what functions can be used to accomplish this? Or am I looking at this from the wrong angle?

See New and Timestamp.AsTime in timestamppb

These support conversion to/from time.Time and Timestamp

Below code will give current time in timestamppb format

import "google.golang.org/protobuf/types/known/timestamppb"
timeNow := timestamppb.Now()

Below will return time in time.Time format

timeNow.GetCurrentTime().AsTime()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM