This is a creation in Article, where the information may have evolved or changed.
Deep replication can be reflect done based on the reflection mechanism of the package, but it is tedious to write all the way back.
The simplest approach is to implement deep replication of objects based on serialization and deserialization:
Func deepcopy (DST, src interface{})Error{varbuf bytes. Bufferif Err: = Gob. Newencoder (&BUF).Encode(SRC);Err! = Nil {return Err}returnGob. Newdecoder (bytes. Newbuffer (BUF. Bytes ())).Decode(DST)}
The simple combination of gob and bytes.buffer is done. Of course, the bottom of Gob is also based on reflect packages.
Serialize in memory, deserialize object entities to complete deep copies of object entities this is the idiom for most languages to implement deep copies of objects.
Add: Golang is completely by value, so the normal assignment is a value copy, of course, if the type inside a nested pointer, is also a copy of the pointer value, there will be two types of variables within the internal part is shared.