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

时间:2019-03-04 05:04:28

标签: go

我正在尝试将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 个答案:

答案 0 :(得分:1)

  

有人可以指出我的方向吗?


阅读错误消息。

binary.Write: invalid type *timestamp.Timestamp

阅读binary.Writetimestamp.Timestamp的文档。

  

Package binary

     

import "encoding/binary"

     

func Write

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

Write将数据的二进制表示形式写入w。数据必须是   固定大小的值或固定大小的值的切片,或指向该大小的指针   数据。布尔值编码为一个字节:1表示true,0表示false。   写入w的字节使用指定的字节顺序编码并读取   来自数据的连续字段。编写结构时,零值   是为具有空白(_)字段名称的字段编写的。

     

package timestamp

import "github.com/golang/protobuf/ptypes/timestamp" 
     

type 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纪元时间的纳秒分辨率。它使用   阳历公历,扩展了阳历   倒退到第一年。假设所有分钟均为60,则进行编码   秒,即“涂抹” leap秒,因此不会出现leap秒   需要表来解释。范围从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