gob解码器尝试解码为非指针类型。

huangapple go评论65阅读模式
英文:

gob decoder attempting to decode into a non-pointer

问题

在我的Go程序中,我正在使用gob对[]byte数据进行编码:

buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
buf.Reset()
enc.Encode(data)

但是在我尝试解码时,出现了'gob解码器尝试解码到非指针'的错误:

buf := new(bytes.Buffer)
d := gob.NewDecoder(buf)
d.Decode(data)
log.Printf("%s", d)

请注意,以上是翻译的代码部分,不包括其他内容。

英文:

In my Go program I am encoding []byte data with gob

buf := new(bytes.Buffer)
	enc := gob.NewEncoder(buf)
		//data is []byte
		buf.Reset()
		enc.Encode(data)

but getting 'gob decoder attempting to decode into a non-pointer' when I am trying to decoded

buf := new(bytes.Buffer)
	d := gob.NewDecoder(buf)
		d.Decode(data)
		log.Printf("%s", d)

答案1

得分: 2

Gob要求你传递一个解码的指针。

在你的情况下,你可以这样做:

    d.Decode(&data)

原因是,它可能需要修改切片(例如:使其更大,以适应解码后的数组)。

英文:

Gob requires you to pass a pointer to decode.

In your case, you would do:

    d.Decode(&data)

reason being, it may have to modify the slice (ie: to make it bigger, to fit the decoded array)

huangapple
  • 本文由 发表于 2017年3月5日 01:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/42598913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定