本文介绍了无法解码gob数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的结构类型,我正在编码。但是,在解码数据时,我正在做一些根本性错误。每当我尝试解码它时,我都会遇到EOF恐慌错误。

//将地图编码为gob。将gob保存到磁盘。从磁盘读取gob。将gob解码为另一张地图。
package main

  import(
fmt
encoding / gob
字节
io / ioutil


类型hashinfo结构{
fname字符串
散列字符串
}



func main(){

thing:= [] hashinfo {
{rahul,test},
{boya,test2},
}

m:= new(bytes.Buffer)
enc:= gob.NewEncoder(m)
enc.Encode(thing)
err:= ioutil.WriteFile(gob_data,m.Bytes(),0600)
if err!= nil {
panic(err)
}
fmt.Printf(刚刚储存的gob with%v \\\
,东西)

$ b $,err:= ioutil.ReadFile(gob_data)
if err!= nil {
fmt.Printf(can not read file)
panic(err)
}
p:= bytes.NewBuffer(n)
dec:= gob.NewDecoder(p)
e:= [] hashinfo {}
err = dec.Decode(& e)
if err!= nil {
fmt.Printf(can not decode)
panic(err)

fmt.Printf(从文件中读取gob并显示:%v \ n,e)
}

为了解码gobject,我创建了e:= [] hashinfo {}对象。我在那里做错了什么?

解决方案

您在中键入hashinfo 不会被导出并且不能被反序列化。尝试使用 Fname 哈希


I have a simple struct type which I am encoding. However, I am doing something fundamentally wrong while decoding the data. Every time I try to decode it, I get EOF panic error.

//Encoding a map to a gob. Save the gob to disk. Read the gob from disk. Decode the gob into another map.package main

import (
        "fmt"
        "encoding/gob"
        "bytes"
        "io/ioutil"
)

type hashinfo struct {
  fname string 
  hash string
}



func main() {

        thing := []hashinfo{
            {"rahul","test"},
            {"boya","test2"},
        }

        m := new(bytes.Buffer) 
        enc := gob.NewEncoder(m)
        enc.Encode(thing)
        err := ioutil.WriteFile("gob_data", m.Bytes(), 0600) 
        if err != nil {
                panic(err)
        }
        fmt.Printf("just saved gob with %v\n", thing)


        n,err := ioutil.ReadFile("gob_data")
        if err != nil {
                fmt.Printf("cannot read file")
                panic(err)
        } 
        p := bytes.NewBuffer(n) 
        dec := gob.NewDecoder(p)
        e := []hashinfo{}
        err = dec.Decode(&e)
        if err != nil {
                fmt.Printf("cannot decode")
                panic(err)
        } 
        fmt.Printf("just read gob from file and it's showing: %v\n", e)
}

I have created e := []hashinfo{} object in order to decode gobject. Am I doing something wrong there ?

解决方案

Your fields in type hashinfo are not exported and cannot be deserialized. Try with Fname and Hash.

这篇关于无法解码gob数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:52