Go:读写压缩的gob到文件中

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

Go: Reading and writing compressed gob to file

问题

这似乎无法正常工作,我不确定我做错了什么。我试图将一个map转换为gob,将二进制数据压缩并保存到文件中,然后稍后再读取它。

type Object struct {
    mystruct map[string][]scorer
}

type scorer struct {
    category int
    score float64
}

func (t *Object) Load(filename string) error {
    fi, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer fi.Close()

    fz, err := gzip.NewReader(fi)
    if err != nil {
        return err
    }
    defer fz.Close()

    decoder := gob.NewDecoder(fz)
    err = decoder.Decode(&t.mystruct)
    if err != nil {
        return err
    }

    return nil
}

func (t *Object) Save(filename string) error {
    fi, err := os.Create(filename)
    if err != nil {
        return err
    }
    defer fi.Close()

    fz := gzip.NewWriter(fi)
    defer fz.Close()

    encoder := gob.NewEncoder(fz)
    err = encoder.Encode(t.mystruct)
    if err != nil {
        return err
    }

    return nil
}

某些内容被保存到文件中,gzip似乎是有效的,但要么保存了空内容,要么无法再次加载。

我也不确定我是否做得正确,因为我是Go的新手,对于读者和写入器我感到困惑,因为我之前使用的是PHP,对此不太熟悉。

有什么想法吗?

英文:

This does not appear to work correctly and I'm not sure what I'm doing wrong. I'm attempting to convert a map into a gob, gzip the binary and save it to a file, then later read it back.

type Object struct {
mystruct map[string][]scorer
}

type scorer struct {
category int
score float64
}

func (t *Object) Load(filename string) error {
	
	fi, err := os.Open(filename)
	if err !=nil {
		return err
	}
	defer fi.Close()
	
    fz, err := gzip.NewReader(fi)
	if err !=nil {
		return err
	}
	defer fz.Close()
	
	decoder := gob.NewDecoder(fz)
	err = decoder.Decode(&t.mystruct)
	if err !=nil {
		return err
	}
	
	return nil
}

func (t *Object) Save(filename string) error {
	
	fi, err := os.Create(filename)
	if err !=nil {
		return err
	}
	defer fi.Close()
	
	fz := gzip.NewWriter(fi)
	defer fz.Close()
	
	encoder := gob.NewEncoder(fz)
	err = encoder.Encode(t.mystruct)
	if err !=nil {
		return err
	}
	
	return nil
}

Something is saved to a file and the gzip appears to be valid, but it is either saving nothing or not loading it back again.

I'm also not sure if I'm doing this correctly as I'm new to Go and I'm finding it difficult to get my head around the readers and writers, since I'm coming from PHP and not used to that.

Any ideas?

答案1

得分: 10

你的问题与读者和写者无关:你只是无法对未导出的字段进行编码/解码,而你的所有字段都是未导出的(小写)。你需要使用MystructCategoryScore,或者按照http://golang.org/pkg/encoding/gob/#example__encodeDecode中的说明编写自己的BinaryMarshal/BinaryUnmarshal函数。

英文:

Your problem has nothing to do with Readers and Writers: You just cannot encode/decode fields which are un-exported and all your fields are unexported (lowercase). You'll have to use Mystruct, Categoryand Score or write your own BinaryMarshal/BinaryUnmarshal as explained in http://golang.org/pkg/encoding/gob/#example__encodeDecode

huangapple
  • 本文由 发表于 2014年7月25日 17:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/24952422.html
匿名

发表评论

匿名网友

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

确定