Friday 15 April 2011

go - use gob to package recursively defined structs -



go - use gob to package recursively defined structs -

i utilize python, playing around go. wrote next quite simple in python, , im hoping can accomplished in go 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 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 %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 file , it's showing: %v\n", e) }

as can see, there 2 custom structs, each containing reference other, recursively. when seek bundle 1 file using gob, compiles, stack overflow, assuming caused recursion. in experience, pickle handles things without gasp. doing wrong?

as of now, encoding/gob bundle doesn't work recursive values:

recursive types work fine, recursive values (data cycles) problematic. may change.

until changed, you'll have either not utilize cyclic data, or utilize different approach serialisation.

go recursive-datastructures gob

No comments:

Post a Comment