Releases: golang-design/reflect
Releases · golang-design/reflect
v0.1.1
v0.1.0
First release of golang.design/x/reflect — a generic DeepCopy for Go, the
external implementation of proposal go.dev/issue/51520.
import "golang.design/x/reflect"
dst := reflect.DeepCopy(src) // a fully independent copyFeatures
DeepCopy[T any](src T, opts ...DeepCopyOption) (dst T)recursively copies into
a freshly allocated value: numbers, bools, strings, slices, arrays, maps,
pointers, structs (including unexported fields), and interfaces.- Circular and shared structures are handled, preserving the original pointer
aliasing. - Customization options for singletons and stateful objects (
sync.Mutex,
os.File,net.Conn,js.Value, ...):WithCopyFunc[T](fn)— general per-type escape hatch;Tmay be concrete or an interface.RetainType[T]()— share values of typeTby reference.ZeroType[T]()— substitute a fresh zero value (e.g. an unlocked mutex).DisallowType[T]()— panic when a value of typeTis encountered.DisallowCopyUnexported(),DisallowCopyCircular(),DisallowCopyBidirectionalChan().
Notes
- Requires Go 1.18+ (generics).
- Copying unexported fields relies on the
unsafepackage. - Deep copy is not free — roughly one allocation per copied element; prefer a
hand-written copy on hot paths. Seebench_test.go. - This is a pre-1.0 release: the API may still change.