I'm trying to write protobuf *Timestamp.timestamp to binary, and the error I got is invalid type *Timestamp.timestamp
and I've tried to no avail, can anyone point me to some direction? thanks!
package main
import (
"bytes"
"encoding/binary"
"fmt"
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
"time"
)
func main() {
buff := new(bytes.Buffer)
ts := &google_protobuf.Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
}
err := binary.Write(buff, binary.LittleEndian, ts)
if err != nil {
panic(err)
}
fmt.Println("done")
}
can anyone point me to some direction?
Read the error message.
binary.Write: invalid type *timestamp.Timestamp
Read the documentation for binary.Write
and timestamp.Timestamp
.
import "encoding/binary"
func Write(w io.Writer, order ByteOrder, data interface{}) error
Write writes the binary representation of data into w. Data must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. Boolean values encode as one byte: 1 for true, and 0 for false. Bytes written to w are encoded using the specified byte order and read from successive fields of the data. When writing structs, zero values are written for fields with blank (_) field names.
import "github.com/golang/protobuf/ptypes/timestamp"
type Timestamp struct { // Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"` // Non-negative fractions of a second at nanosecond resolution. Negative // second values with fractions must still have non-negative nanos values // that count forward in time. Must be from 0 to 999,999,999 // inclusive. Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we can convert to and from RFC 3339 date strings. See https://www.ietf.org/rfc/rfc3339.txt.
As the error message says: *timestamp.Timestamp
is not a fixed-size value or a slice of fixed-size values, or a pointer to such data.
To confirm that, comment out the XXX_unrecognized
variable-sized field; there is no error.
package main
import (
"bytes"
"encoding/binary"
"fmt"
"time"
)
// https://github.com/golang/protobuf/blob/master/ptypes/timestamp/timestamp.pb.go
type Timestamp struct {
// Represents seconds of UTC time since Unix epoch
// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
// 9999-12-31T23:59:59Z inclusive.
Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
// Non-negative fractions of a second at nanosecond resolution. Negative
// second values with fractions must still have non-negative nanos values
// that count forward in time. Must be from 0 to 999,999,999
// inclusive.
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
// XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func main() {
buff := new(bytes.Buffer)
ts := &Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
}
err := binary.Write(buff, binary.LittleEndian, ts)
if err != nil {
panic(err)
}
fmt.Println("done")
}
Playground: https://play.golang.org/p/Q5NGnO49Dsc
Output:
done