1 回答

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個贊
誰能指出我的方向?
閱讀錯誤消息。
binary.Write:?invalid?type?*timestamp.Timestamp
binary.Write
閱讀和的文檔timestamp.Timestamp
。
二進(jìn)制包
import "encoding/binary"
函數(shù)寫入
func?Write(w?io.Writer,?order?ByteOrder,?data?interface{})?errorWrite 將數(shù)據(jù)的二進(jìn)制表示寫入 w。數(shù)據(jù)必須是固定大小的值或固定大小值的切片,或指向此類數(shù)據(jù)的指針。布爾值編碼為一個字節(jié):1 表示真,0 表示假。寫入 w 的字節(jié)使用指定的字節(jié)順序進(jìn)行編碼,并從數(shù)據(jù)的連續(xù)字段中讀取。編寫結(jié)構(gòu)時,將為具有空白 (_) 字段名稱的字段寫入零值。
包時間戳
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:"-"`
}
時間戳表示獨(dú)立于任何時區(qū)或日歷的時間點(diǎn),以 UTC 紀(jì)元時間納秒分辨率表示為秒和秒的分?jǐn)?shù)。它使用 Proleptic Gregorian Calendar 編碼,該日歷將 Gregorian 日歷向后擴(kuò)展到第一年。它的編碼假設(shè)所有分鐘都是 60 秒長,即閏秒被“涂抹”,因此不需要閏秒表來解釋。范圍從 0001-01-01T00:00:00Z 到 9999-12-31T23:59:59.999999999Z。通過限制在該范圍內(nèi),我們確??梢耘c RFC 3339 日期字符串相互轉(zhuǎn)換。請參閱 https://www.ietf.org/rfc/rfc3339.txt。
正如錯誤消息所說:*timestamp.Timestamp不是固定大小的值或固定大小值的切片,或指向此類數(shù)據(jù)的指針。
為了確認(rèn)這一點(diǎn),注釋掉XXX_unrecognized可變大小的字段;沒有錯誤。
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")
}
游樂場:https://play.golang.org/p/Q5NGnO49Dsc
輸出:
done
- 1 回答
- 0 關(guān)注
- 205 瀏覽
添加回答
舉報