deep

package module
v1.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2024 License: Apache-2.0 Imports: 3 Imported by: 27

README

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

Benchmark Iterations Time Bytes Allocated Allocations
BenchmarkCopy_Deep-16 830553 1273 ns/op 1264 B/op 21 allocs/op
BenchmarkCopy_DeepCopy-16 (1) 458072 2466 ns/op 1912 B/op 50 allocs/op
BenchmarkCopy_CopyStructure-16 (2) 149685 7836 ns/op 6392 B/op 168 allocs/op
BenchmarkCopy_Clone-16 (3) 510760 2188 ns/op 1656 B/op 22 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy[T any](src T) (T, error)

Copy creates a deep copy of src. It returns the copy and a nil error in case of success and the zero value for the type and a non-nil error on failure.

func CopySkipUnsupported

func CopySkipUnsupported[T any](src T) (T, error)

CopySkipUnsupported creates a deep copy of src. It returns the copy and a nil errorin case of success and the zero value for the type and a non-nil error on failure. Unsupported types are skipped (the copy will have the zero value for the type) instead of returning an error.

func MustCopy

func MustCopy[T any](src T) T

MustCopy creates a deep copy of src. It returns the copy on success or panics in case of any failure.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL