clone-go

Deep clone for Go, standard library only.

clone-go copies a value and everything it points to: slices, maps, pointers and nested structs. One generic function, no dependencies beyond reflect, and a second function for graphs with cycles.

go get github.com/pixie-sh/clone-go

Go 1.25+. MIT licensed. The full contract is in the README.

The problem

Assigning a struct copies the struct, not what it points to.

Slices, maps and pointers inside a copied struct still refer to the original data. Change one and you change both, usually somewhere far from where the copy was made.

A plain copy

orig := &User{Name: "Alice", Tags: []string{"admin"}}

c := *orig
c.Tags[0] = "guest"

fmt.Println(orig.Tags[0]) // guest

The two values share one backing array.

With clone-go

orig := &User{Name: "Alice", Tags: []string{"admin"}}

c := clone.Clone(orig)
c.Tags[0] = "guest"

fmt.Println(orig.Tags[0]) // admin

The clone owns its own slices, maps and pointers.

Nothing else in your module graph

The library imports only the standard library. The benchmark harness lives in a separate sub-module, so its dependencies never reach yours.

Typed in, typed out

Clone[T] takes a *T and returns a *T. There are no casts at the call site.

Why we wrote it

A third-party clone module kept breaking builds of core-go with checksum mismatches. Deep clone is small enough to own.

How it works

Two functions and one interface.

Clone

Walks the value with reflect and deep-copies exported pointers, slices, maps, structs, arrays and interfaces. For graphs without cycles.

CloneSlowly

The same walk with a visited map, so self-references and mutual pointers keep their shape in the copy.

Cloneable[T]

Give a type its own Clone() *T method and Clone calls it instead of using reflect.

Safe to call concurrently

No shared state between calls. CloneSlowly allocates its visited map per call.

time.Time round-trips

Scalar-like structs are copied whole, so wall clock, monotonic reading and location are preserved.

nil in, nil out

A nil pointer returns a typed nil *T.

Usage

The three cases.

import clone "github.com/pixie-sh/clone-go"

type User struct {
    Name    string
    Tags    []string
    Meta    map[string]string
    Created time.Time
}

orig := &User{
    Name:    "Alice",
    Tags:    []string{"admin"},
    Meta:    map[string]string{"plan": "pro"},
    Created: time.Now(),
}

c := clone.Clone(orig)
c.Name = "Bob"
c.Tags[0] = "guest"
c.Meta["plan"] = "free"

fmt.Println(orig.Name, orig.Tags[0], orig.Meta["plan"]) // Alice admin pro
fmt.Println(c.Created.Equal(orig.Created))              // true
type Foo struct{ Bar string }

// *Foo implements clone.Cloneable[Foo].
func (f *Foo) Clone() *Foo {
    c := *f
    return &c
}

// Clone finds the method and calls it; reflect is skipped.
c := clone.Clone(&Foo{Bar: "hello"})
type Node struct {
    Data int
    Self *Node
}

n := &Node{Data: 1}
n.Self = n

c := clone.CloneSlowly(n)

fmt.Println(c.Self == c) // true: the cycle points at the copy
fmt.Println(c != n)      // true: a fresh allocation

Clone does not detect cycles and will not return on a cyclic graph. CloneSlowly does not consult Cloneable[T].

Contract

What is deep-cloned, and what is shared.

FieldWhat happens
Exported pointer, slice, map, struct, interface, arrayDeep-cloned, recursively.
Exported scalarCopied.
Unexported fieldShallow-copied. Pointer-like data in it is shared with the original.
func, chan, unsafe.PointerShallow-copied.
sync.Mutex and other no-copy typesNot handled. Do not clone a struct that holds a live mutex.
Benchmark

Next to huandu/go-clone.

Apple M5 Pro, Go 1.24.4, medians of three runs. Rerun it with go test -bench=. -benchmem in the benchmarks sub-module.

Workloadhuandu ns/opclone-go ns/ophuandu allocsclone-go allocs
Simple struct, 2 fields534711
map[string]*T5897451827
User: slice, map, two time.Time3957111218
Slice of 100 items5,2145,1631033
Binary tree, 511 nodes38,34340,473511511
Self-cycle (CloneSlowly)2256941

clone-go is faster on small structs and on cycles, level on large slices with far fewer allocations, and up to 1.8 times slower on structs that mix slices and maps. A per-type field cache to close that gap is on the roadmap.

Open source at Pixie

One of a set that works together.

These libraries run in our own services every day. Use one, or all of them.