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

标签 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")
    }

最佳答案

can anyone point me to some direction?


阅读错误消息。

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 writes the binary representation of data into w. Data must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. Boolean values encode as one byte: 1 for true, and 0 for false. Bytes written to w are encoded using the specified byte order and read from successive fields of the data. When writing structs, zero values are written for fields with blank (_) field names.

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:"-"`
}

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we can convert to and from RFC 3339 date strings. See https://www.ietf.org/rfc/rfc3339.txt.


正如错误消息所示:*timestamp.Timestamp 不是固定大小值或固定大小值的 slice ,也不是指向此类数据的指针。

要确认这一点,请注释掉 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")
}

Playground :https://play.golang.org/p/Q5NGnO49Dsc

输出:

done

关于go - 需要有关binary.write错误的更多输入或信息无效类型xxx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976925/

相关文章:

go - Golang解决hackerrank的“对角线差异”问题

go - 如何解决从openapi go代码生成器获取使用应用程序/x-www-form-urlencoded内容类型的API的验证错误?

go - Golang 上的包导入错误

windows - 在 Windows 中从 Golang 启动一个新的命令窗口

azure - 使用API​​或SDK在Azure中上传模板

go - Go 中当前正在运行的进程列表

go - 为什么 Go 在写入封闭 channel 时会感到 panic ?

去解包数组作为 path.Join 的参数

go - 为什么我在 golang 的类型断言上得到奇怪的结果?

go - 使用嵌入式别名在不同包中的结构上定义方法是否有效?