gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:07:30 27 4
gpt4 key购买 nike

我正在尝试将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com