diff --git a/README.md b/README.md index f3be2ca..3d1fcc5 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,7 @@ import "github.com/duke-git/lancet/v2/convertor" [[play](https://go.dev/play/p/j4DP5dquxnk)] - **CopyProperties** : copies each field from the source struct into the destination struct. [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#CopyProperties)] - [[play](https://go.dev/play/p/FOVY3XJL-6B)] + [[play](https://go.dev/play/p/oZujoB5Sgg5)] ### 6. Cryptor package is for data encryption and decryption. @@ -567,14 +567,20 @@ import "github.com/duke-git/lancet/v2/fileutil" [[play](https://go.dev/play/p/s74a9iBGcSw)] - **IsZipFile** : checks if file is zip file or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsZipFile)] + [[play](https://go.dev/play/p/9M0g2j_uF_e)] - **FileSize** : return file size in bytes. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#FileSize)] + [[play](https://go.dev/play/p/H9Z05uD-Jjc)] - **MTime** : return file modified time(unix timestamp). [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#MTime)] + [[play](https://go.dev/play/p/s_Tl7lZoAaY)] - **Sha** : return file sha value. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Sha)] + [[play](https://go.dev/play/p/VfEEcO2MJYf)] - **ReadCsvFile** : read file content into slice. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ReadCsvFile)] + [[play](https://go.dev/play/p/OExTkhGEd3_u)] + ### 10. Formatter contains some functions for data formatting. @@ -845,8 +851,10 @@ import "github.com/duke-git/lancet/v2/netutil" [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#UploadFile)] - **IsPingConnected** : checks if can ping the specified host or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPingConnected)] + [[play](https://go.dev/play/p/q8OzTijsA87)] - **IsTelnetConnected** : checks if can if can telnet the specified host or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsTelnetConnected)] + [[play](https://go.dev/play/p/yiLCGtQv_ZG)] ### 15. Random package implements some basic functions to generate random int and string. @@ -984,11 +992,13 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/CBKeBoHVLgq)] - **FindBy** : iterates over elements of slice, returning the first one that passes a truth test on predicate function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindBy)] + [[play](https://go.dev/play/p/n1lysBYl-GB)] - **FindLastdeprecated** : return the last item that passes a truth test on predicate function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)] [[play](https://go.dev/play/p/FFDPV_j7URd)] - **FindLastBy** : iterates over elements of slice, returning the last one that passes a truth test on predicate function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLastBy)] + [[play](https://go.dev/play/p/8iqomzyCl_s)] - **Flatten** : flattens slice one level. [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)] [[play](https://go.dev/play/p/hYa3cBEevtm)] @@ -1314,16 +1324,23 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/og47F5x_jTZ)] - **StringToBytes** : converts a string to byte slice without a memory allocation. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#StringToBytes)] + [[play](https://go.dev/play/p/7OyFBrf9AxA)] - **BytesToString** : converts a byte slice to string without a memory allocation. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BytesToString)] + [[play](https://go.dev/play/p/6c68HRvJecH)] - **IsBlank** : checks if a string is whitespace or empty. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsBlank)] + [[play](https://go.dev/play/p/6zXRH_c0Qd3)] - **HasPrefixAny** : checks if a string starts with any of an array of specified strings. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasPrefixAny)] + [[play](https://go.dev/play/p/8UUTl2C5slo)] - **HasSuffixAny** : checks if a string ends with any of an array of specified strings. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasSuffixAny)] + [[play](https://go.dev/play/p/sKWpCQdOVkx)] - **IndexOffset** : returns the index of the first instance of substr in string after offsetting the string by `idxFrom`. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IndexOffset)] + [[play](https://go.dev/play/p/qZo4lV2fomB)] + ### 21. System package contain some functions about os, runtime, shell command. diff --git a/README_zh-CN.md b/README_zh-CN.md index 968d97f..4c2d6cf 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -271,7 +271,7 @@ import "github.com/duke-git/lancet/v2/convertor" [[play](https://go.dev/play/p/j4DP5dquxnk)] - **CopyProperties** : 拷贝不同结构体之间的同名字段。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#CopyProperties)] - [[play](https://go.dev/play/p/FOVY3XJL-6B)] + [[play](https://go.dev/play/p/oZujoB5Sgg5)] ### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。 @@ -566,14 +566,20 @@ import "github.com/duke-git/lancet/v2/fileutil" [[play](https://go.dev/play/p/s74a9iBGcSw)] - **IsZipFile** : 判断文件是否是zip压缩文件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsZipFile)] + [[play](https://go.dev/play/p/9M0g2j_uF_e)] - **FileSize** : 返回文件字节大小。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileSize)] + [[play](https://go.dev/play/p/H9Z05uD-Jjc)] - **MTime** : 返回文件修改时间(unix timestamp)。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#MTime)] + [[play](https://go.dev/play/p/s_Tl7lZoAaY)] - **Sha** : 返回文件sha值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Sha)] + [[play](https://go.dev/play/p/VfEEcO2MJYf)] - **ReadCsvFile** : 读取csv文件内容到切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ReadCsvFile)] + [[play](https://go.dev/play/p/OExTkhGEd3_u)] + ### 10. formatter 格式化器包含一些数据格式化处理方法。 @@ -844,8 +850,10 @@ import "github.com/duke-git/lancet/v2/netutil" [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#UploadFile)] - **IsPingConnected** : 检查能否ping通主机。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPingConnected)] + [[play](https://go.dev/play/p/q8OzTijsA87)] - **IsTelnetConnected** : 检查能否telnet到主机。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsTelnetConnected)] + [[play](https://go.dev/play/p/yiLCGtQv_ZG)] ### 15. random 随机数生成器包,可以生成随机[]bytes, int, string。 @@ -983,11 +991,13 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/CBKeBoHVLgq)] - **FindBy** : 遍历切片的元素,返回第一个通过 predicate 函数真值测试的元素。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindBy)] + [[play](https://go.dev/play/p/n1lysBYl-GB)] - **FindLastdeprecated** : 从头到尾遍历 slice 的元素,返回最后一个通过 predicate 函数真值测试的元素。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)] [[play](https://go.dev/play/p/FFDPV_j7URd)] - **FindLastBy** : 从遍历slice的元素,返回最后一个通过predicate函数真值测试的元素。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLastBy)] + [[play](https://go.dev/play/p/8iqomzyCl_s)] - **Flatten** : 将多维切片展平一层。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)] [[play](https://go.dev/play/p/hYa3cBEevtm)] @@ -1316,16 +1326,22 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/og47F5x_jTZ)] - **StringToBytes** : 在不分配内存的情况下将字符串转换为字节片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#StringToBytes)] + [[play](https://go.dev/play/p/7OyFBrf9AxA)] - **BytesToString** : 在不分配内存的情况下将字节切片转换为字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BytesToString)] + [[play](https://go.dev/play/p/6c68HRvJecH)] - **IsBlank** : 检查字符串是否为空格或空。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsBlank)] + [[play](https://go.dev/play/p/6zXRH_c0Qd3)] - **HasPrefixAny** : 检查字符串是否以指定字符串数组中的任何一个开头。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasPrefixAny)] + [[play](https://go.dev/play/p/8UUTl2C5slo)] - **HasSuffixAny** : 检查字符串是否以指定字符串数组中的任何一个结尾。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasSuffixAny)] + [[play](https://go.dev/play/p/sKWpCQdOVkx)] - **IndexOffset** : 将字符串偏移idxFrom后,返回字符串中第一个 substr 实例的索引。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IndexOffset)] + [[play](https://go.dev/play/p/qZo4lV2fomB)] ### 21. system 包含 os, runtime, shell command 的相关函数。 diff --git a/convertor/convertor.go b/convertor/convertor.go index 2035a49..56f6280 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -321,7 +321,7 @@ func DeepClone[T any](src T) T { // CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers. // use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct. -// Play: todo +// Play: https://go.dev/play/p/oZujoB5Sgg5 func CopyProperties[T, U any](dst T, src U) error { dstType, srcType := reflect.TypeOf(dst), reflect.TypeOf(src) diff --git a/docs/convertor.md b/docs/convertor.md index 527ae78..0ce83aa 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -757,7 +757,11 @@ func main() { indicatorVO := IndicatorVO{} - CopyProperties(&indicatorVO, indicator) + err := convertor.CopyProperties(&indicatorVO, indicator) + + if err != nil { + return + } fmt.Println(indicatorVO.Id) fmt.Println(indicatorVO.Ip) diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 8d38744..40bb7a6 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -756,7 +756,11 @@ func main() { indicatorVO := IndicatorVO{} - CopyProperties(&indicatorVO, indicator) + err := convertor.CopyProperties(&indicatorVO, indicator) + + if err != nil { + return + } fmt.Println(indicatorVO.Id) fmt.Println(indicatorVO.Ip) diff --git a/docs/fileutil.md b/docs/fileutil.md index e61d485..5901210 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -522,7 +522,7 @@ import ( ) func main() { - isZip := IsZipFile("./zipfile.zip") + isZip := fileutil.IsZipFile("./zipfile.zip") fmt.Println(isZip) } ``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 3852549..091f9b2 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -522,7 +522,7 @@ import ( ) func main() { - isZip := IsZipFile("./zipfile.zip") + isZip := fileutil.IsZipFile("./zipfile.zip") fmt.Println(isZip) } ``` diff --git a/docs/netutil.md b/docs/netutil.md index 16892e3..f37e7eb 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -922,7 +922,7 @@ import ( ) func main() { - err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") + err := netutil.DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") fmt.Println(err) } diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index cc16537..dda5a29 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -924,7 +924,7 @@ import ( ) func main() { - err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") + err := netutil.DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") fmt.Println(err) } diff --git a/docs/strutil.md b/docs/strutil.md index 65c6120..331cff0 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -968,6 +968,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // hello world // 你好😄 @@ -998,6 +999,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // [97 98 99] // true @@ -1027,6 +1029,7 @@ func main() { result := strutil.BytesToString(bytes) fmt.Println(result) + // Output: // abc } @@ -1058,6 +1061,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + // Output: // true // true @@ -1089,6 +1093,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // true // false @@ -1119,6 +1124,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // true // false @@ -1157,6 +1163,7 @@ func main() { fmt.Println(result3) fmt.Println(result4) fmt.Println(result5) + // Output: // 12 // 1 diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index f640b48..d96c746 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -968,6 +968,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // hello world // 你好😄 @@ -1027,6 +1028,7 @@ func main() { result := strutil.BytesToString(bytes) fmt.Println(result) + // Output: // abc } @@ -1058,6 +1060,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + // Output: // true // true @@ -1089,6 +1092,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // true // false @@ -1119,6 +1123,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) + // Output: // true // false @@ -1157,6 +1162,7 @@ func main() { fmt.Println(result3) fmt.Println(result4) fmt.Println(result5) + // Output: // 12 // 1 diff --git a/fileutil/file.go b/fileutil/file.go index 174a46e..357b459 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -180,7 +180,7 @@ func ListFileNames(path string) ([]string, error) { } // IsZipFile checks if file is zip or not. -// Play: todo +// Play: https://go.dev/play/p/9M0g2j_uF_e func IsZipFile(filepath string) bool { f, err := os.Open(filepath) if err != nil { @@ -383,7 +383,7 @@ func CurrentPath() string { } // FileSize returns file size in bytes. -// Play: todo +// Play: https://go.dev/play/p/H9Z05uD-Jjc func FileSize(path string) (int64, error) { f, err := os.Stat(path) if err != nil { @@ -393,7 +393,7 @@ func FileSize(path string) (int64, error) { } // MTime returns file modified time. -// Play: todo +// Play: https://go.dev/play/p/s_Tl7lZoAaY func MTime(filepath string) (int64, error) { f, err := os.Stat(filepath) if err != nil { @@ -403,7 +403,7 @@ func MTime(filepath string) (int64, error) { } // MTime returns file sha value, param `shaType` should be 1, 256 or 512. -// Play: todo +// Play: https://go.dev/play/p/VfEEcO2MJYf func Sha(filepath string, shaType ...int) (string, error) { file, err := os.Open(filepath) if err != nil { @@ -437,7 +437,7 @@ func Sha(filepath string, shaType ...int) (string, error) { } // ReadCsvFile read file content into slice. -// Play: todo +// Play: https://go.dev/play/p/OExTkhGEd3_u func ReadCsvFile(filepath string) ([][]string, error) { f, err := os.Open(filepath) if err != nil { diff --git a/netutil/net.go b/netutil/net.go index e9e40f8..307ba00 100644 --- a/netutil/net.go +++ b/netutil/net.go @@ -250,7 +250,7 @@ func DownloadFile(filepath string, url string) error { } // IsPingConnected checks if can ping specified host or not. -// Play: todo +// Play: https://go.dev/play/p/q8OzTijsA87 func IsPingConnected(host string) bool { cmd := exec.Command("ping", host, "-c", "4", "-W", "6") @@ -266,7 +266,7 @@ func IsPingConnected(host string) bool { } // IsTelnetConnected checks if can telnet specified host or not. -// Play: todo +// Play: https://go.dev/play/p/yiLCGtQv_ZG func IsTelnetConnected(host string, port string) bool { adder := host + ":" + port conn, err := net.DialTimeout("tcp", adder, 5*time.Second) diff --git a/slice/slice.go b/slice/slice.go index 73c23ff..68cd366 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -338,7 +338,7 @@ func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, boo // FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function. // If return T is nil or zero value then no items matched the predicate func. // In contrast to Find or FindLast, its return value no longer requires dereferencing -// Play: todo +// Play: https://go.dev/play/p/n1lysBYl-GB func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) { index := -1 @@ -359,7 +359,7 @@ func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok b // FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function. // If return T is nil or zero value then no items matched the predicate func. // In contrast to Find or FindLast, its return value no longer requires dereferencing -// Play: todo +// Play: https://go.dev/play/p/8iqomzyCl_s func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) { index := -1 diff --git a/strutil/string.go b/strutil/string.go index f7982b0..3ea2f65 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -377,7 +377,7 @@ func RemoveNonPrintable(str string) string { } // StringToBytes converts a string to byte slice without a memory allocation. -// Play: todo +// Play: https://go.dev/play/p/7OyFBrf9AxA func StringToBytes(str string) (b []byte) { sh := *(*reflect.StringHeader)(unsafe.Pointer(&str)) bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) @@ -386,13 +386,13 @@ func StringToBytes(str string) (b []byte) { } // BytesToString converts a byte slice to string without a memory allocation. -// Play: todo +// Play: https://go.dev/play/p/6c68HRvJecH func BytesToString(bytes []byte) string { return *(*string)(unsafe.Pointer(&bytes)) } // IsBlank checks if a string is whitespace, empty. -// Play: todo +// Play: https://go.dev/play/p/6zXRH_c0Qd3 func IsBlank(str string) bool { if len(str) == 0 { return true @@ -408,7 +408,7 @@ func IsBlank(str string) bool { } // HasPrefixAny check if a string starts with any of an array of specified strings. -// Play: todo +// Play: https://go.dev/play/p/8UUTl2C5slo func HasPrefixAny(str string, prefixes []string) bool { if len(str) == 0 || len(prefixes) == 0 { return false @@ -422,7 +422,7 @@ func HasPrefixAny(str string, prefixes []string) bool { } // HasSuffixAny check if a string ends with any of an array of specified strings. -// Play: todo +// Play: https://go.dev/play/p/sKWpCQdOVkx func HasSuffixAny(str string, suffixes []string) bool { if len(str) == 0 || len(suffixes) == 0 { return false @@ -437,7 +437,7 @@ func HasSuffixAny(str string, suffixes []string) bool { // IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, // or -1 if substr is not present in string. -// Play: todo +// Play: https://go.dev/play/p/qZo4lV2fomB func IndexOffset(str string, substr string, idxFrom int) int { if idxFrom > len(str)-1 || idxFrom < 0 { return -1