Skip to content

Releases: golang-design/reflect

v0.1.1

17 Jun 14:42
a22c79e

Choose a tag to compare

What's Changed

  • Fix when map values that are set as nil deleted after deepcopy by @khlopkov in #13

New Contributors

Full Changelog: v0.1.0...v0.1.1

v0.1.0

07 Jun 09:00
7a32213

Choose a tag to compare

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 copy

Features

  • 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; T may be concrete or an interface.
    • RetainType[T]() — share values of type T by reference.
    • ZeroType[T]() — substitute a fresh zero value (e.g. an unlocked mutex).
    • DisallowType[T]() — panic when a value of type T is encountered.
    • DisallowCopyUnexported(), DisallowCopyCircular(), DisallowCopyBidirectionalChan().

Notes

  • Requires Go 1.18+ (generics).
  • Copying unexported fields relies on the unsafe package.
  • Deep copy is not free — roughly one allocation per copied element; prefer a
    hand-written copy on hot paths. See bench_test.go.
  • This is a pre-1.0 release: the API may still change.