examples/

directory
v0.2.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: MIT

README

DeepClone Examples

This directory contains various examples demonstrating different use cases of the DeepClone library.

Available Examples

1. Basic Examples (basic/)

Demonstrates fundamental cloning operations:

  • Primitive types (int, string, bool)
  • Collections (slices, maps)
  • Pointers and pointer chains
  • Complex nested structures
  • Custom cloning with Cloner interface

Run:

cd basic && go run main.go
2. Circular Reference Examples (circular/)

Shows how DeepClone handles circular references safely:

  • Simple circular references between nodes
  • Self-referencing structures
  • Safe cloning without infinite loops

Run:

cd circular && go run main.go
3. Custom Cloning Examples (custom/)

Demonstrates custom cloning behavior via the Cloner interface:

  • Reconstructing private mutable state
  • Preserving logical values
  • Proving source and clone storage independence

Run:

cd custom && go run main.go

Running All Examples

To run all examples at once:

# From the examples directory
for dir in */; do
    if [ -f "$dir/main.go" ]; then
        echo "=== Running $dir ==="
        cd "$dir" && go run main.go && cd ..
        echo ""
    fi
done

Key Learning Points

  1. Zero-allocation paths: Primitive types are cloned without memory allocation
  2. Deep independence: Supported cloned data is independent from the original
  3. Circular safety: Circular references are handled without infinite loops
  4. Error honesty: Unsupported runtime state is reported by Clone
  5. Custom behavior: Implement Cloner for equivalent reconstruction of private state

Requirements

  • Go 1.26.5 or later
  • DeepClone library installed (go get github.com/kaptinlin/deepclone)

Directories

Path Synopsis
Package main demonstrates basic usage of the deepclone library.
Package main demonstrates basic usage of the deepclone library.
Package main demonstrates handling circular references with the deepclone library.
Package main demonstrates handling circular references with the deepclone library.
Package main demonstrates custom cloning behavior with the deepclone library.
Package main demonstrates custom cloning behavior with the deepclone library.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL