Clone any Go data structure deeply and thoroughly.
https://pkg.go.dev/github.com/huandu/go-clone β HEALTH ? 90 / Healthy
Clone any Go data structure deeply and thoroughly.
https://pkg.go.dev/github.com/huandu/go-clone β// readme
Package clone provides functions to deep clone any Go data. It also provides a wrapper to protect a pointer from any unexpected mutation.
For users who use Go 1.18+, itβs recommended to import github.com/huandu/go-clone/generic for generic APIs and arena support.
Clone/Slowly can clone unexported fields and βno-copyβ structs as well. Use this feature wisely.
Use go get to install this package.
go get github.com/huandu/go-clone
Clone and SlowlyIf we want to clone any Go value, use Clone.
t := &T{...}
v := clone.Clone(t).(*T)
reflect.DeepEqual(t, v) // true
For the sake of performance, Clone doesnβt deal with values containing pointer cycles.
If we need to clone such values, use Slowly instead.
type ListNode struct {
Data int
Next *ListNode
}
node1 := &ListNode{
Data: 1,
}
node2 := &ListNode{
Data: 2,
}
node3 := &ListNode{
Data: 3,
}
node1.Next = node2
node2.Next = node3
node3.Next = node1
// We must use `Slowly` to clone a circular linked list.
node := Slowly(node1).(*ListNode)
for i := 0; i < 10; i++ {β¦