Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 需要有关二进制文件的更多输入或信息。写入错误无效类型xxx_Go - Fatal编程技术网

Go 需要有关二进制文件的更多输入或信息。写入错误无效类型xxx

Go 需要有关二进制文件的更多输入或信息。写入错误无效类型xxx,go,Go,我正在尝试将protobuf*Timestamp.Timestamp写入二进制文件,我得到的错误是无效类型*Timestamp.Timestamp,我尝试了无效,有人能告诉我一些方向吗?谢谢 package main import ( "bytes" "encoding/binary" "fmt" google_protobuf "github.com/golang/protobuf/ptypes/timestam

我正在尝试将protobuf*Timestamp.Timestamp写入二进制文件,我得到的错误是
无效类型*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")
    }
谁能给我指个方向吗


阅读错误消息


阅读
二进制文件的文档。编写
时间戳。时间戳

导入“编码/二进制”

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

时间戳表示独立于任何时区或时间的时间点 日历,以秒和秒的分数表示 UTC历元时间的纳秒分辨率。它是使用 公历前公历,是公历的延续 回到第一年。假设所有分钟数均为60,则对其进行编码 秒长,即闰秒被“涂抹”,因此没有闰秒 需要表格进行解释。范围为0001-01-01T00:00:00Z 至9999-12-31T23:59:59.999999Z。通过限制在该范围内,我们 确保我们可以在RFC 3339日期字符串之间进行转换。看见


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

要确认这一点,请注释掉
XXX\u未识别的
可变大小字段;没有错误

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")
}
游乐场:

输出:

done

请尝试在没有引用运算符“&”的情况下使用ts。我想你是在写指针,而不是时间戳。@Seaskyways:这不是问题。这是一个很好的答案,谢谢!但是为了扩展它,我想除了注释掉[]字节之外,我没有办法处理它,对吗?既然我不想修改原始代码(源代码)?
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:"-"`
}
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")
}
done