Skip to content

Repository files navigation

deep

Support for doing deep copies of (almost all) Go types.

This is a from scratch implementation of the ideas from https://github.com/barkimedes/go-deepcopy (which, unfortunatelly appears to be dead) but it is faster, simpler to use for callers and supports copying unexported fields.

It should support most Go types. Specificaly, it does not support functions, channels and unsafe.Pointers unless they are nil. Also it might have weird interactions with structs that include any synchronization primitives (mutexes, for example. They should still be copied but if they are usable after that is left as an exercise to the reader).

One possible usage scenario would be, for example, to negate the use of the deepcopy-gen tool described in Kubernetes code generation. For any type T, the DeepEqual method can be implemented more or less like this:

func (t* T) DeepCopy() *T {
return deep.MustCopy(t) // This would panic on errors.
}

Benchmarks

BenchmarkIterationsTimeBytes AllocatedAllocations
BenchmarkCopy_Deep-168305531273 ns/op1264 B/op21 allocs/op
BenchmarkCopy_DeepCopy-16 (1)4580722466 ns/op1912 B/op50 allocs/op
BenchmarkCopy_CopyStructure-16 (2)1496857836 ns/op6392 B/op168 allocs/op
BenchmarkCopy_Clone-16 (3)5107602188 ns/op1656 B/op22 allocs/op

(1) https://github.com/barkimedes/go-deepcopy (does not support unexported fields)

(2) https://github.com/mitchellh/copystructure (does not support cycles)

(3) https://github.com/huandu/go-clone

About

Support for doing deep copies of (almost all) Go types.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages