mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 06:02:27 +08:00
doc: update go playground demo
This commit is contained in:
19
README.md
19
README.md
@@ -272,7 +272,7 @@ import "github.com/duke-git/lancet/v2/convertor"
|
||||
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
||||
- **<big>CopyProperties</big>** : 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)]
|
||||
- **<big>IsZipFile</big>** : 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)]
|
||||
- **<big>FileSize</big>** : 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)]
|
||||
- **<big>MTime</big>** : 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)]
|
||||
- **<big>Sha</big>** : return file sha value.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Sha)]
|
||||
[[play](https://go.dev/play/p/VfEEcO2MJYf)]
|
||||
- **<big>ReadCsvFile</big>** : 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)]
|
||||
- **<big>IsPingConnected</big>** : 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)]
|
||||
- **<big>IsTelnetConnected</big>** : 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)]
|
||||
- **<big>FindBy</big>** : 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)]
|
||||
- **<big>FindLast<sup>deprecated</sup></big>** : 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)]
|
||||
- **<big>FindLastBy</big>** : 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)]
|
||||
- **<big>Flatten</big>** : 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)]
|
||||
- **<big>StringToBytes</big>** : 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)]
|
||||
- **<big>BytesToString</big>** : 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)]
|
||||
- **<big>IsBlank</big>** : 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)]
|
||||
- **<big>HasPrefixAny</big>** : 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)]
|
||||
- **<big>HasSuffixAny</big>** : 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)]
|
||||
- **<big>IndexOffset</big>** : 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.
|
||||
|
||||
|
||||
@@ -271,7 +271,7 @@ import "github.com/duke-git/lancet/v2/convertor"
|
||||
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
||||
- **<big>CopyProperties</big>** : 拷贝不同结构体之间的同名字段。
|
||||
[[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)]
|
||||
- **<big>IsZipFile</big>** : 判断文件是否是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)]
|
||||
- **<big>FileSize</big>** : 返回文件字节大小。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileSize)]
|
||||
[[play](https://go.dev/play/p/H9Z05uD-Jjc)]
|
||||
- **<big>MTime</big>** : 返回文件修改时间(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)]
|
||||
- **<big>Sha</big>** : 返回文件sha值。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Sha)]
|
||||
[[play](https://go.dev/play/p/VfEEcO2MJYf)]
|
||||
- **<big>ReadCsvFile</big>** : 读取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)]
|
||||
- **<big>IsPingConnected</big>** : 检查能否ping通主机。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPingConnected)]
|
||||
[[play](https://go.dev/play/p/q8OzTijsA87)]
|
||||
- **<big>IsTelnetConnected</big>** : 检查能否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)]
|
||||
- **<big>FindBy</big>** : 遍历切片的元素,返回第一个通过 predicate 函数真值测试的元素。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindBy)]
|
||||
[[play](https://go.dev/play/p/n1lysBYl-GB)]
|
||||
- **<big>FindLast<sup>deprecated</sup></big>** : 从头到尾遍历 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)]
|
||||
- **<big>FindLastBy</big>** : 从遍历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)]
|
||||
- **<big>Flatten</big>** : 将多维切片展平一层。
|
||||
[[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)]
|
||||
- **<big>StringToBytes</big>** : 在不分配内存的情况下将字符串转换为字节片。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#StringToBytes)]
|
||||
[[play](https://go.dev/play/p/7OyFBrf9AxA)]
|
||||
- **<big>BytesToString</big>** : 在不分配内存的情况下将字节切片转换为字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BytesToString)]
|
||||
[[play](https://go.dev/play/p/6c68HRvJecH)]
|
||||
- **<big>IsBlank</big>** : 检查字符串是否为空格或空。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsBlank)]
|
||||
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
||||
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasPrefixAny)]
|
||||
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||
- **<big>HasSuffixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个结尾。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasSuffixAny)]
|
||||
[[play](https://go.dev/play/p/sKWpCQdOVkx)]
|
||||
- **<big>IndexOffset</big>** : 将字符串偏移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 的相关函数。
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -522,7 +522,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
isZip := IsZipFile("./zipfile.zip")
|
||||
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||
fmt.Println(isZip)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -522,7 +522,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
isZip := IsZipFile("./zipfile.zip")
|
||||
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||
fmt.Println(isZip)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user