Documentation
ΒΆ
Overview ΒΆ
Package deepclone provides high-performance deep cloning functionality for Go.
This package is designed for extreme performance optimization, utilizing Go 1.21+ generics, reflection caching, and careful memory management to achieve zero-allocation hot paths where possible.
Basic Usage:
import "github.com/kaptinlin/deepclone"
// Clone any value with deep copying semantics
dst := deepclone.Clone(src)
// For custom types, implement the Cloneable interface for deep cloning
type MyStruct struct {
Name string
Data []int
}
func (m MyStruct) Clone() any {
return MyStruct{
Name: m.Name,
Data: deepclone.Clone(m.Data).([]int), // Deep clone nested data
}
}
Performance Features:
- Zero-allocation hot paths for primitive types
- Reflection result caching for struct types
- Optimized fast paths for common slice and map types
- CPU cache-friendly data access patterns
Supported Types:
- All primitive types (int, string, bool, etc.)
- Slices, maps, and arrays (with deep cloning of elements)
- Pointers and pointer chains (with circular reference detection)
- Structs (with automatic field-by-field deep cloning)
- Interfaces (with concrete type preservation)
- Custom types implementing Cloneable interface
Deep Cloning Semantics:
- All cloning operations perform deep copies by default
- Nested data structures are recursively cloned
- Circular references are safely detected and handled
- Custom types can override default behavior via Cloneable interface
Thread Safety:
- All cloning operations are thread-safe
- Internal caches use concurrent-safe mechanisms
- No global state modifications during cloning
Index ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Clone ΒΆ
func Clone[T any](src T) T
Clone creates a deep copy of the given value. This is the main entry point for the deepclone package.
The function supports all basic Go types and uses optimized paths for common scenarios to achieve maximum performance.
For custom types, implement the Cloneable interface to provide specialized cloning behavior.
Performance characteristics:
- Zero allocation for primitive types
- Optimized paths for slices, maps, and common structs
- Reflection caching for repeated struct types
- Circular reference detection to prevent infinite loops
Usage:
dst := deepclone.Clone(src)
Types ΒΆ
type Cloneable ΒΆ
type Cloneable interface {
Clone() any
}
Cloneable interface allows types to implement custom deep cloning behavior. Types implementing this interface will have their Clone method called instead of using the default reflection-based cloning.
The Clone method should return a deep copy of the receiver. It's the implementer's responsibility to ensure all nested data is properly cloned to maintain complete independence from the original.
Example:
type Document struct {
Title string
Content []byte
Metadata map[string]interface{}
}
func (d Document) Clone() any {
return Document{
Title: d.Title,
Content: deepclone.Clone(d.Content).([]byte),
Metadata: deepclone.Clone(d.Metadata).(map[string]interface{}),
}
}
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Package main demonstrates basic usage of the deepclone library.
|
Package main demonstrates basic usage of the deepclone library. |
|
circular
command
Package main demonstrates handling circular references with the deepclone library.
|
Package main demonstrates handling circular references with the deepclone library. |
|
custom
command
Package main demonstrates custom cloning behavior with the deepclone library.
|
Package main demonstrates custom cloning behavior with the deepclone library. |