第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要有關(guān) binary.write 錯誤的更多輸入或信息無效類型 xxx

需要有關(guān) binary.write 錯誤的更多輸入或信息無效類型 xxx

Go
桃花長相依 2023-06-12 15:42:35
我正在嘗試將 protobuf *Timestamp.timestamp 寫入二進(jìn)制文件,但我得到的錯誤是,我invalid type *Timestamp.timestamp已經(jīng)嘗試過但無濟(jì)于事,任何人都可以指出我的方向嗎?謝謝!    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")    }
查看完整描述

1 回答

?
POPMUISE

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{})?error

Write 將數(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


查看完整回答
反對 回復(fù) 2023-06-12
  • 1 回答
  • 0 關(guān)注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號