需要有关二进制的更多输入或信息.WRITE错误无效类型XXX



我正在尝试编写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")
    }

有人可以将我指向某个方向吗?


阅读错误消息。

binary.Write: invalid type *timestamp.Timestamp

阅读binary.Writetimestamp.Timestamp的文档。

包二进制

import "encoding/binary"

func写

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

写将数据的二进制表示形式写入w。数据必须是 固定尺寸的值或固定尺寸值的切片,或指向此类的指针 数据。布尔值编码为一个字节:1对于true,为false为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时期时间的纳秒分辨率。它是使用 延长格里高利日历 向后到第一年。它是编码的,假设所有分钟都是60 秒长,即leap秒被"涂抹" 需要表进行解释。范围从0001-01-01T00:00:00z起 到9999-12-31T23:59:59.99999999999Z。通过限制到该范围,我们 确保我们可以转换为RFC 3339日期字符串。看 https://www.ietf.org/rfc/rfc33399.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

最新更新