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.
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.
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.
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.
The library imports only the standard library. The benchmark harness lives in a separate sub-module, so its dependencies never reach yours.
Clone[T] takes a *T and returns a *T. There are no casts at the call site.
A third-party clone module kept breaking builds of core-go with checksum mismatches. Deep clone is small enough to own.
Walks the value with reflect and deep-copies exported pointers, slices, maps, structs, arrays and interfaces. For graphs without cycles.
The same walk with a visited map, so self-references and mutual pointers keep their shape in the copy.
Give a type its own Clone() *T method and Clone calls it instead of using reflect.
No shared state between calls. CloneSlowly allocates its visited map per call.
Scalar-like structs are copied whole, so wall clock, monotonic reading and location are preserved.
A nil pointer returns a typed nil *T.
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)) // truetype 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 allocationClone does not detect cycles and will not return on a cyclic graph. CloneSlowly does not consult Cloneable[T].
| Field | What happens |
|---|---|
| Exported pointer, slice, map, struct, interface, array | Deep-cloned, recursively. |
| Exported scalar | Copied. |
| Unexported field | Shallow-copied. Pointer-like data in it is shared with the original. |
func, chan, unsafe.Pointer | Shallow-copied. |
sync.Mutex and other no-copy types | Not handled. Do not clone a struct that holds a live mutex. |
Apple M5 Pro, Go 1.24.4, medians of three runs. Rerun it with go test -bench=. -benchmem in the benchmarks sub-module.
| Workload | huandu ns/op | clone-go ns/op | huandu allocs | clone-go allocs |
|---|---|---|---|---|
| Simple struct, 2 fields | 53 | 47 | 1 | 1 |
map[string]*T | 589 | 745 | 18 | 27 |
User: slice, map, two time.Time | 395 | 711 | 12 | 18 |
| Slice of 100 items | 5,214 | 5,163 | 103 | 3 |
| Binary tree, 511 nodes | 38,343 | 40,473 | 511 | 511 |
Self-cycle (CloneSlowly) | 225 | 69 | 4 | 1 |
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.
These libraries run in our own services every day. Use one, or all of them.