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/

相关文章:

google-chrome - GoDoc 标识符搜索浏览器界面

go - 使用 Go Modules 时外部基准测试的最佳实践

go - 将 int96 时间戳从 Parquet 转换到 golang

go - Goroutine概念:使用函数字面量的直接调用与调用

go - Unix时间戳格式转换

go - 使用通配符匹配删除 s3 中的对象

json - 如何为 POST 请求创建 json 负载?

pointers - Go 在哪里定义 p.Field(与 (*p).Field 相对)是有效语法,即使 p 是指向结构的指针?

go - 如何更新多对多关系

pointers - Golang自动参照,并非总能获得值的地址