使用gob来递归地打包定义的结构体。

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

use gob to package recursively defined structs

问题

我主要使用Python,但也在尝试使用Go语言。我写了以下代码,用于在Python中非常简单的操作,希望在Go中也能实现。

package main

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

type Order struct {
    Text string
    User *User
}

type User struct {
    Text  string
    Order *Order
}

func main() {
    o := Order{}
    u := User{}
    o.Text = "order text"
    u.Text = "user text"

    // commenting this section prevents stack overflow
    o.User = &u
    u.Order = &o
    fmt.Println("o.u.text:", o.User.Text, "u.o.text:", u.Order.Text)
    // end section

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

    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 := Order{}
    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)
}

如你所见,这里有两个自定义结构体,每个结构体都包含对另一个结构体的引用,形成了递归关系。当我尝试使用gob将其中一个结构体打包到文件中时,代码可以编译通过,但会导致堆栈溢出错误。我猜测这是由于递归引起的。根据我的经验,pickle可以处理这样的情况而不会出错。我做错了什么?

英文:

I mostly use Python, but am playing around with Go. I wrote the following to do something that is quite simple in python, and im hoping it can be accomplished in Go as well.

package main
import (
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
)
type Order struct {
Text string
User *User
}
type User struct {
Text  string
Order *Order
}
func main() {
o := Order{}
u := User{}
o.Text = "order text"
u.Text = "user text"
// commenting this section prevents stack overflow
o.User = &u
u.Order = &o
fmt.Println("o.u.text:", o.User.Text, "u.o.text:", u.Order.Text)
// end section
m := new(bytes.Buffer)
enc := gob.NewEncoder(m)
enc.Encode(o)
err := ioutil.WriteFile("gob_data", m.Bytes(), 0600)
if err != nil {
panic(err)
}
fmt.Printf("just saved gob with %v\n", o)
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 := Order{}
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)
}

As you can see, there are two custom structs, each containing a reference to the other, recursively. When I try to package one up into a file using gob, it compiles, but i get a stack overflow, I am assuming this is caused by the recursion. In my experience, pickle handles things like this without a gasp. What am I doing wrong?

答案1

得分: 6

截至目前,encoding/gob包在处理递归值方面存在问题。链接

>递归类型可以正常工作,但是递归值(具有循环的数据)存在问题。这可能会改变。

在此问题得到解决之前,你可以选择不使用循环数据,或者采用其他方法进行序列化。

英文:

As of now, the encoding/gob package doesn't work with recursive values:

>Recursive types work fine, but recursive values (data with cycles) are problematic. This may change.

Until this is changed, you'll have to either not use cyclic data, or use a different approach to serialisation.

huangapple
  • 本文由 发表于 2014年11月12日 22:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/26889287.html
匿名

发表评论

匿名网友

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

确定