diff --git a/docs/en/api/packages/convertor.md b/docs/en/api/packages/convertor.md index 33ffa9d7..10ab38dd 100644 --- a/docs/en/api/packages/convertor.md +++ b/docs/en/api/packages/convertor.md @@ -571,6 +571,7 @@ func main() { } ``` + ### EncodeByte
Encode data to byte slice.
@@ -610,7 +611,7 @@ func main() { func DecodeByte(data []byte, target any) error ``` -Example: +Example:[Run](https://go.dev/play/p/zI6xsmuQRbn) ```go package main @@ -636,69 +637,6 @@ func main() { } ``` -### 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:[Run](https://go.dev/play/p/j4DP5dquxnk) - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/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 trueConverts reflect value to its interface type.
- -Signature: - -```go -func ToInterface(v reflect.Value) (value interface{}, ok bool) -``` - -Example:[Run](https://go.dev/play/p/syqw0-WG7Xd) - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/convertor" -) - -func main() { - val := reflect.ValueOf("abc") - iVal, ok := convertor.ToInterface(val) - - fmt.Printf("%T\n", iVal) - fmt.Printf("%v\n", iVal) - fmt.Println(ok) - - // Output: - // string - // abc - // true -} -``` - ### Utf8ToGbkConverts utf8 encoding data to GBK encoding data.
@@ -883,7 +786,7 @@ func main() { ### ToStdBase64 -Convert a value to a string encoded in standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
+Convert a value to a string encoded in standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.
Signature: @@ -953,9 +856,11 @@ func main() { ``` + + ### ToUrlBase64 -Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
+Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.
Signature: @@ -1024,7 +929,7 @@ func main() { ### ToRawStdBase64 -Convert a value to a string encoded in raw standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
+Convert a value to a string encoded in raw standard Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.
Signature: @@ -1088,7 +993,7 @@ func main() { ### ToRawUrlBase64 -Convert a value to a string encoded in raw url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.
+Convert a value to a string encoded in raw url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.
Signature: @@ -1148,4 +1053,67 @@ func main() { // dHJ1ZQ // ZXJy } +``` + +### 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:[Run](https://go.dev/play/p/j4DP5dquxnk) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/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, + }, + } + + 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 trueConverts reflect value to its interface type.
+ +Signature: + +```go +func ToInterface(v reflect.Value) (value interface{}, ok bool) +``` + +Example:[Run](https://go.dev/play/p/syqw0-WG7Xd) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + val := reflect.ValueOf("abc") + iVal, ok := convertor.ToInterface(val) + + fmt.Printf("%T\n", iVal) + fmt.Printf("%v\n", iVal) + fmt.Println(ok) + + // Output: + // string + // abc + // true +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 743b5fa4..0607a6cd 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -93,6 +93,7 @@ import ( - [KeyBy](#KeyBy) - [Join](#Join) - [Partition](#Partition) +- [SetToDefaultIf](#SetToDefaultIf)