There's a bug here, namely when you're trying to copy slices.

The following assertion will fail.

var slice1, slice2 []*int
slice1 = make([]*int, 0)
Clone(slice1, &slice2)
assert.Equal(slice1, slice2)


On Sunday, May 1, 2011 at 10:32:06 AM UTC-7, Zhai wrote:
>
> I use Gob to Clone data,it's simple, and  work  well 
> except when data has cyclic value. like this :
>
> package main
>
> import (
> "fmt"
> "gob"
> "reflect"
> "bytes"
> )
> const Level = 12
> type Tree struct {
> Item int
> Left,Right *Tree
> }
>
> type List struct {
> Item int
> Next *List
> }
>
> func NewTree(item,level int) *Tree {
> if level <= 1 {
> return &Tree{Item:item}
> }
> return  &Tree{item, NewTree(2*item-1,level-1), NewTree(2*item,level-1) }
> }
> func Clone(a,b interface{}) {
> buff := new(bytes.Buffer)
> enc := gob.NewEncoder(buff)
> dec := gob.NewDecoder(buff)
> enc.Encode(a)
> dec.Decode(b)
> }
> func main() {
> var a,b *Tree
> a = NewTree(1,Level)
> Clone(a,&b)
> fmt.Println("a DeepEqual b:",reflect.DeepEqual(a,b))
> var p,q *List
> p = &List{100,nil}
> p.Next = p
>
> q = &List{100,nil}
> // Clone(p,&q)                 //don't uncommet, it will  loop forever
>
> fmt.Println("p DeepEqual q:",reflect.DeepEqual(p,q))
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to