1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 03:02:28 +08:00

feat: add DeepClone

This commit is contained in:
dudaodong
2023-02-10 15:50:28 +08:00
parent 888381a06c
commit 4b196a72b1
4 changed files with 320 additions and 0 deletions

View File

@@ -324,3 +324,19 @@ func DecodeByte(data []byte, target any) error {
decoder := gob.NewDecoder(buffer)
return decoder.Decode(target)
}
// DeepClone creates a deep copy of passed item.
// can't clone unexported field of struct
// Play: todo
func DeepClone[T any](src T) T {
c := cloner{
ptrs: map[reflect.Type]map[uintptr]reflect.Value{},
}
result := c.clone(reflect.ValueOf(src))
if result.Kind() == reflect.Invalid {
var zeroValue T
return zeroValue
}
return result.Interface().(T)
}