Categorygithub.com/xieyuschen/deepcopy
repositorypackage
1.0.1
Repository: https://github.com/xieyuschen/deepcopy.git
Documentation: pkg.go.dev

# README

DeepCopy

example workflow codecov

It's fully tested and has 100% test coverage, feel free to use it!

The repo is original at mohae/deepcopy. As it hasn't been maintained for a long time, it's maintained here with bug fix and new features.

Expected Behaviors in DeepCopy Library

This topic lists some expected behaviors in DeepCopy library here. Deep copy circular references will return error instead of fatal stack overflow.

TypeExpected Behavior
Primitive TypesCopy by the value
StructConstruct a new struct, deep copy each field according to the respective behaviors
Slice/ArrayConstruct a new slice/array, deep copy underlying types according to the respective behaviors
InterfaceConstruct a new interface, deep copy underlying types according to the respective behaviors
MapConstruct a new map, deep copy underlying types according to the respective behaviors
ChannelChannel will be shared between source and copied.
FunctionFunction will be shared betwwen source and copied. Beware of the variables captured by your function when you do deep copy
PointerConstruct a new object with the type under the pointer according to the respective behaviors and then return the a pointer to the new object
time.TimeA new time.Time object will be constructed with the shared *time.Location pointer inside it
Unexposed FieldsUnexposed fields are not supported in reflect, as a result, it cannot copy the inside status.
Accroding to this, states of objects cannot be conserved and some of objects cannot be used at all.
For example, a mutex will be deep copied like a value because all internal states are lost during deep copy. A *os.File cannot be used at all.

Acknowledge

There was a deep copy proposal in Go but got declined due to no consensus. The brief of arguments are listed below.

  • The deep copy should be a library, instead of go language itself.
    "ianlancetaylor:... the best way forward here is going to be to write the version of the function that you think we need. And that implementation can live anywhere..."

  • The behaviors of deep copy for circular/recursive data structure.

  • The behaviors of deep copy for stateful structures such as *os.File, mutex, channel and so on.

  • ...

As the deep copy function shouldn't belong to go language, and it's useful in some cases, the library came into being.

Contribute

This repo will be maintained in the long term even though the original author start to maintain the original repo. I will try to fix any unexpected bug and add some features if necessary. Glad to receive issues and PRs.

Thank Joel Scoble, Mathieu Champlon, Damien Neil, Sergey Cherepanov for their previous work.