为了账号安全,请及时绑定邮箱和手机立即绑定

需要有关 binary.write 错误的更多输入或信息无效类型 xxx

需要有关 binary.write 错误的更多输入或信息无效类型 xxx

Go
桃花长相依 2023-06-12 15:42:35
我正在尝试将 protobuf *Timestamp.timestamp 写入二进制文件,但我得到的错误是,我invalid type *Timestamp.timestamp已经尝试过但无济于事,任何人都可以指出我的方向吗?谢谢!    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贡献1765条经验 获得超5个赞

谁能指出我的方向?


阅读错误消息。

binary.Write: invalid type *timestamp.Timestamp

binary.Write阅读和的文档timestamp.Timestamp

二进制包

import "encoding/binary"

函数写入

func Write(w io.Writer, order ByteOrder, data interface{}) error

Write 将数据的二进制表示写入 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 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


查看完整回答
反对 回复 2023-06-12
  • 1 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信