Deepcopy: Difference between revisions
Content deleted Content added
→{{header|Go}}: language change. built in error type. |
|||
Line 128: | Line 128: | ||
&{1 one [117 110 105 116] map[1:true]} |
&{1 one [117 110 105 116] map[1:true]} |
||
</pre> |
</pre> |
||
If you need a generalized deep copy, one can be cobbled with the gob package, which does type safe serialization |
If you need a generalized deep copy, one can be cobbled with an os.Pipe and the gob package, which does type safe serialization. The deepcopy function shown below works on arbitrary data with a few limitations. It handles data types with recursive or cyclic definitions, but does not handle cycles in the data itself. For example, it handles a linked list, but not a ring data structure. Another limitiation is that struct fields must be exported. (That is, fields must start with an upper case letter. This makes the field visible outside the package.) |
||
<lang go>package main |
<lang go>package main |
||
Line 138: | Line 138: | ||
// capability requested by task |
// capability requested by task |
||
func deepcopy(dst, src interface{}) |
func deepcopy(dst, src interface{}) error { |
||
r, w, err := os.Pipe() |
r, w, err := os.Pipe() |
||
if err != nil { |
if err != nil { |
||
Line 155: | Line 155: | ||
type link struct { |
type link struct { |
||
Value string |
Value string |
||
Next *link |
Next *link |
||
} |
} |
||