Skip to content

Commit a044a19

Browse files
authored
Added example to README
1 parent 39310c7 commit a044a19

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Overpunch
1+
## Go Deep Copy
22

33
[![Build Status](https://travis-ci.org/barkimedes/go-deepcopy.svg?branch=master)](https://travis-ci.org/barkimedes/go-deepcopy) [![codecov](https://codecov.io/gh/barkimedes/go-deepcopy/branch/master/graph/badge.svg)](https://codecov.io/gh/barkimedes/go-deepcopy) [![](https://godoc.org/github.com/nathany/looper?status.svg)](https://godoc.org/github.com/barkimedes/go-deepcopy)
44

@@ -7,3 +7,79 @@ This package is a Golang implementation for creating deep copies of virtually an
77
This is a truly deep copy--every single value behind a pointer, every item in a slice or array, and every key and value in a map are all cloned so nothing is pointing to what it pointed to before.
88

99
To handle circular pointer references (e.g. a pointer to a struct with a pointer field that points back to the original struct), we keep track of a map of pointers that have already been visited. This serves two purposes. First, it keeps us from getting into any kind of infinite loop. Second, it ensures that the code will behave similarly to how it would have on the original struct -- if you expect two values to be pointing at the same value within the copied tree, then they'll both still point to the same thing.
10+
11+
### Sample Program
12+
13+
```go
14+
package main
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/barkimedes/go-deepcopy"
20+
)
21+
22+
type Foo struct {
23+
Bar []string
24+
Baz *Baz
25+
}
26+
27+
type Baz struct {
28+
Qux int
29+
Corgi map[bool]string
30+
Foo *Foo
31+
}
32+
33+
func main() {
34+
x := &Foo{
35+
Bar: []string{"a", "b", "c", "d"},
36+
Baz: &Baz{
37+
Qux: 4,
38+
Corgi: map[bool]string{
39+
false: "nope",
40+
true: "yup",
41+
},
42+
},
43+
}
44+
45+
x.Baz.Foo = x // just for funsies
46+
47+
y, err := deepcopy.Anything(x)
48+
if err != nil {
49+
panic(err)
50+
}
51+
print(x)
52+
fmt.Println()
53+
print(y.(*Foo))
54+
}
55+
56+
func print(x *Foo) {
57+
fmt.Printf("Foo: %p %v\n", x, x)
58+
fmt.Printf("\tFoo.Bar: %p %v\n", x.Bar, x.Bar)
59+
fmt.Printf("\tFoo.Baz: %p %v\n", x.Baz, x.Baz)
60+
fmt.Printf("\t\tFoo.Baz.Qux: %v\n", x.Baz.Qux)
61+
fmt.Printf("\t\tFoo.Baz.Corgi: %p %v\n", x.Baz.Corgi, x.Baz.Corgi)
62+
fmt.Printf("\t\tFoo.Baz.Foo: %p %v\n", x.Baz.Foo, x.Baz.Foo)
63+
}
64+
```
65+
66+
### Sample Output
67+
68+
_Note that the values are all the same, but the addresses are all different.
69+
Note also that circular dependencies are handled--the self-referential Foo remains self-referential within each instance, but different across copies._
70+
71+
```go
72+
Foo: 0xc00000c0a0 &{[a b c d] 0xc00000c0c0}
73+
Foo.Bar: 0xc000016080 [a b c d]
74+
Foo.Baz: 0xc00000c0c0 &{4 map[false:nope true:yup] 0xc00000c0a0}
75+
Foo.Baz.Qux: 4
76+
Foo.Baz.Corgi: 0xc000060180 map[false:nope true:yup]
77+
Foo.Baz.Foo: 0xc00000c0a0 &{[a b c d] 0xc00000c0c0}
78+
79+
Foo: 0xc00000c0e0 &{[a b c d] 0xc00000c160}
80+
Foo.Bar: 0xc0000160c0 [a b c d]
81+
Foo.Baz: 0xc00000c160 &{4 map[false:nope true:yup] 0xc00000c0e0}
82+
Foo.Baz.Qux: 4
83+
Foo.Baz.Corgi: 0xc0000601e0 map[false:nope true:yup]
84+
Foo.Baz.Foo: 0xc00000c0e0 &{[a b c d] 0xc00000c160}
85+
```

0 commit comments

Comments
 (0)