diff --git a/README.md b/README.md index 2ad4878..b20663b 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ import "github.com/duke-git/lancet/convertor" - [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#StructToMap) - [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#EncodeByte) - [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DecodeByte) +- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DeepClone) ### 2. Cryptor package is for data encryption and decryption. diff --git a/README_zh-CN.md b/README_zh-CN.md index c2deec2..cf07a2b 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -82,6 +82,8 @@ import "github.com/duke-git/lancet/convertor" - [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#StructToMap) - [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#EncodeByte) - [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DecodeByte) +- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DeepClone) + ### 2. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。 diff --git a/docs/convertor.md b/docs/convertor.md index 30e309a..926ceaa 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -33,6 +33,7 @@ import ( - [StructToMap](#StructToMap) - [EncodeByte](#EncodeByte) - [DecodeByte](#DecodeByte) +- [DeepClone](#DeepClone)
@@ -443,4 +444,68 @@ func main() { convertor.DecodeByte(byteData, &result) fmt.Println(result) //"abc" } +``` + +### DeepClone + +Creates a deep copy of passed item, can't clone unexported field of struct.
+ +Signature: + +```go +func DeepClone[T any](src T) T +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + type Struct struct { + Str string + Int int + Float float64 + Bool bool + Nil interface{} + unexported string + } + + cases := []interface{}{ + true, + 1, + 0.1, + map[string]int{ + "a": 1, + "b": 2, + }, + &Struct{ + Str: "test", + Int: 1, + Float: 0.1, + Bool: true, + Nil: nil, + // unexported: "can't be cloned", + }, + } + + for _, item := range cases { + cloned := convertor.DeepClone(item) + + isPointerEqual := &cloned == &item + fmt.Println(cloned, isPointerEqual) + } + + // Output: + // true false + // 1 false + // 0.1 false + // map[a:1 b:2] false + // &{test 1 0.1 true创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。
+ +函数签名: + +```go +func DeepClone[T any](src T) T +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + type Struct struct { + Str string + Int int + Float float64 + Bool bool + Nil interface{} + unexported string + } + + cases := []interface{}{ + true, + 1, + 0.1, + map[string]int{ + "a": 1, + "b": 2, + }, + &Struct{ + Str: "test", + Int: 1, + Float: 0.1, + Bool: true, + Nil: nil, + // unexported: "can't be cloned", + }, + } + + for _, item := range cases { + cloned := convertor.DeepClone(item) + + isPointerEqual := &cloned == &item + fmt.Println(cloned, isPointerEqual) + } + + // Output: + // true false + // 1 false + // 0.1 false + // map[a:1 b:2] false + // &{test 1 0.1 true