Documentation
¶
Overview ¶
Package clone provides Clone and CloneSlowly: stdlib-only, reflect-based deep clone for Go values, with an optional Cloneable[T] escape hatch for types that want to provide their own fast path.
Contract ¶
- Exported pointer / slice / map / struct / interface / array fields are deep-cloned recursively.
- Scalar exported fields and ALL unexported fields are shallow-copied via a whole-struct reflect.Value.Set. This means time.Time and similar scalar-like structs round-trip correctly (wall + monotonic clock + location pointer all preserved), but structs whose UNEXPORTED fields hold pointer-like data will share those values with the original.
- Clone does NOT support pointer cycles; calling it on a cyclic graph will infinite-loop. Use CloneSlowly for cycles.
- sync.Mutex / sync.WaitGroup and similar "no-copy" types are not given special handling: their unexported state would be shallow-shared. Do not clone structs containing live mutexes.
Performance ¶
Stdlib-only reflect; no unsafe, no third-party dependencies on the production side. Roughly competitive with reflect+unsafe libraries like huandu/go-clone for typical workloads — faster on simple structs and on the cycle path, similar on slices and trees, ~1.8× slower on structs with several pointer-typed exported fields. See benchmarks/ for numbers.
Escape hatch ¶
For hot paths where reflect overhead is unacceptable, types may implement Cloneable[T]:
type Foo struct{ Bar string }
func (f *Foo) Clone() *Foo { c := *f; return &c }
Clone[Foo] will invoke f.Clone() and skip the reflect path entirely. CloneSlowly does NOT consult Cloneable[T] — it always uses reflect with cycle detection — so a Foo passed to CloneSlowly takes the reflect path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
func Clone[T any](o *T) *T
Clone creates a deep copy of o.
Strategy: stdlib reflect-only deep walk (no unsafe, no third-party). Exported pointer / slice / map / struct / interface / array fields are recursively cloned. Scalar fields and ALL unexported fields are shallow-copied via a whole-struct reflect.Value.Set, which carries unexported data along without bypassing reflect's exported-flag check.
Implications:
- time.Time and similar scalar-like structs round-trip correctly: their unexported wall/ext/loc fields are preserved by the parent struct's whole-value Set.
- Structs whose UNEXPORTED fields hold pointer / slice / map data will have those values shallow-shared with the original, NOT deep-cloned. Mutations to such shared values would be visible to both. Callers who require full deep semantics for unexported fields must keep the "all fields must be exported" contract documented here.
Pointer cycles are NOT supported by Clone: calling Clone on a cyclic graph will infinite-loop. Use CloneSlowly for cycles.
If o == nil, Clone returns nil (typed nil *T).
If *T satisfies Cloneable[T], the type's Clone method is called and the reflect path is skipped. Implement Cloneable[T] when you need a custom fast path, want to avoid reflect for performance, or need behavior that the reflect walker cannot express.
func CloneSlowly ¶
func CloneSlowly[T any](o *T) *T
CloneSlowly creates a deep copy of o, supporting pointer cycles via a visited-pointer map.
Use this function when the object graph may contain self-references or mutual cycles between pointers (e.g. a doubly-linked list, parent-back pointers, mutually-referencing structs). The visited map is allocated per call, so concurrent callers do not share state.
Unlike Clone, CloneSlowly does NOT consult Cloneable[T]: the reflect path always runs. This preserves the historical behavior split between the two functions.
If o == nil, CloneSlowly returns nil (typed nil *T).
Types ¶
type Cloneable ¶
type Cloneable[T any] interface { // Clone creates a deep copy of the object. Clone() *T }
Cloneable defines the interface for types that provide their own deep clone.
When Clone is called on a value whose type satisfies Cloneable[T], the type's own Clone method is invoked instead of the default reflect-based deep copy. CloneSlowly does NOT consult this interface: it always uses the reflect path (with cycle detection). This preserves the historical behavior split between Clone (fast path with escape hatch) and CloneSlowly (cycle-aware deep copy).