POPMUISE
谁能指出我的方向?阅读错误消息。binary.Write: invalid type *timestamp.Timestampbinary.Write阅读和的文档timestamp.Timestamp。二进制包import "encoding/binary"函数写入func Write(w io.Writer, order ByteOrder, data interface{}) errorWrite 将数据的二进制表示写入 w。数据必须是固定大小的值或固定大小值的切片,或指向此类数据的指针。布尔值编码为一个字节:1 表示真,0 表示假。写入 w 的字节使用指定的字节顺序进行编码,并从数据的连续字段中读取。编写结构时,将为具有空白 (_) 字段名称的字段写入零值。包时间戳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:"-"`}时间戳表示独立于任何时区或日历的时间点,以 UTC 纪元时间纳秒分辨率表示为秒和秒的分数。它使用 Proleptic Gregorian Calendar 编码,该日历将 Gregorian 日历向后扩展到第一年。它的编码假设所有分钟都是 60 秒长,即闰秒被“涂抹”,因此不需要闰秒表来解释。范围从 0001-01-01T00:00:00Z 到 9999-12-31T23:59:59.999999999Z。通过限制在该范围内,我们确保可以与 RFC 3339 日期字符串相互转换。请参阅 https://www.ietf.org/rfc/rfc3339.txt。正如错误消息所说:*timestamp.Timestamp不是固定大小的值或固定大小值的切片,或指向此类数据的指针。为了确认这一点,注释掉XXX_unrecognized可变大小的字段;没有错误。package mainimport ( "bytes" "encoding/binary" "fmt" "time")// https://github.com/golang/protobuf/blob/master/ptypes/timestamp/timestamp.pb.gotype 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")}游乐场:https://play.golang.org/p/Q5NGnO49Dsc输出:done