From bb563724c78bbff8d2093262ff1f7389d651ac7e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 1 Aug 2023 11:01:57 +0800 Subject: [PATCH] doc: add go playground demo --- README.md | 25 ++++++++++++++++++ README_zh-CN.md | 27 +++++++++++++++++++ cryptor/basic.go | 6 ++--- datetime/datetime.go | 10 +++---- docs/maputil.md | 57 ++++++++++++++++++++++++++-------------- docs/maputil_zh-CN.md | 30 ++++++++++++++++----- maputil/concurrentmap.go | 14 +++++----- maputil/map.go | 2 +- slice/slice.go | 2 +- 9 files changed, 131 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index b7b147e..b237caa 100644 --- a/README.md +++ b/README.md @@ -395,26 +395,31 @@ import "github.com/duke-git/lancet/v2/cryptor" [[play](https://go.dev/play/p/1UI4oQ4WXKM)] - **HmacSha1WithBase64** : return the hmac hash of string use sha1 with base64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)] + [[play](https://go.dev/play/p/47JmmGrnF7B)] - **HmacSha256** : return the hmac hash of string use sha256. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)] [[play](https://go.dev/play/p/HhpwXxFhhC0)] - **HmacSha256WithBase64** : return the hmac hash of string use sha256 with base64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)] + [[play](https://go.dev/play/p/EKbkUvPTLwO)] - **HmacSha512** : return the hmac hash of string use sha512. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)] [[play](https://go.dev/play/p/59Od6m4A0Ud)] - **HmacSha512WithBase64** : return the hmac hash of string use sha512 with base64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)] + [[play](https://go.dev/play/p/c6dSe3E2ydU)] - **Md5Byte** : return the md5 string of byte slice. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5Byte)] [[play](https://go.dev/play/p/suraalH8lyC)] - **Md5ByteWithBase64** : return the md5 string of byte slice with base64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)] + [[play](https://go.dev/play/p/Tcb-Z7LN2ax)] - **Md5String** : return the md5 value of string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)] [[play](https://go.dev/play/p/1bLcVetbTOI)] - **Md5StringWithBase64** : return the md5 value of string with base64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)] + [[play](https://go.dev/play/p/Lx4gH7Vdr5_y)] - **Md5File** : return the md5 value of file. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)] - **Sha1** : return the sha1 value (SHA-1 hash algorithm) of base64 string. @@ -422,16 +427,19 @@ import "github.com/duke-git/lancet/v2/cryptor" [[play](https://go.dev/play/p/_m_uoD1deMT)] - **Sha1WithBase64** : return the sha1 value (SHA-1 hash algorithm) of string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)] + [[play](https://go.dev/play/p/fSyx-Gl2l2-)] - **Sha256** : return the sha256 value (SHA-256 hash algorithm) of string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256)] [[play](https://go.dev/play/p/tU9tfBMIAr1)] - **Sha256WithBase64** : return the sha256 value (SHA256 hash algorithm) of base64 string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)] + [[play](https://go.dev/play/p/85IXJHIal1k)] - **Sha512** : return the sha512 value (SHA-512 hash algorithm) of string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512)] [[play](https://go.dev/play/p/3WsvLYZxsHa)] - **Sha512WithBase64** : return the sha512 value (SHA-512 hash algorithm) of base64 string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)] + [[play](https://go.dev/play/p/q_fY2rA-k5I)] - **GenerateRsaKey** : create rsa private and public pemo file. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)] [[play](https://go.dev/play/p/zutRHrDqs0X)] @@ -563,14 +571,19 @@ import "github.com/duke-git/lancet/v2/datetime" [[play](https://go.dev/play/p/cupRM5aZOIY)] - **NowDateOrTime** : returns current datetime with specific format and timezone. [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)] + [[play](https://go.dev/play/p/EZ-begEjtT0)] - **Timestamp** : returns current second timestamp. [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)] - **TimestampMilli** : returns current mill second timestamp. [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)] + [[play](https://go.dev/play/p/4gvEusOTu1T)] - **TimestampMicro** : returns current micro second timestamp. [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)] + [[play](https://go.dev/play/p/2maANglKHQE)] - **TimestampNano** : returns current nano second timestamp. [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)] + [[play](https://go.dev/play/p/A9Oq_COrcCF)] +

8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.        index

@@ -826,22 +839,32 @@ import "github.com/duke-git/lancet/v2/maputil" [[play](https://go.dev/play/p/N9qgYg_Ho6f)] - **HasKey** : checks if map has key or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)] + [[play](https://go.dev/play/p/isZZHOsDhFc)] - **NewConcurrentMap** : creates a ConcurrentMap with specific shard count. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_Set** : set the value for a key. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_Get** : get the value stored in the map for a key, or nil if no. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Get)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_GetOrSet** : returns the existing value for the key if present. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetOrSet)] + [[play](https://go.dev/play/p/aDcDApOK01a)] - **ConcurrentMap_Delete** : delete the value for a key. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)] + [[play](https://go.dev/play/p/uTIJZYhpVMS)] - **ConcurrentMap_GetAndDelete** :returns the existing value for the key if present and then delete the value for the key. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetAndDelete)] + [[play](https://go.dev/play/p/ZyxeIXSZUiM)] - **ConcurrentMap_Has** : checks if map has the value for a key. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Has)] + [[play](https://go.dev/play/p/C8L4ul9TVwf)] - **ConcurrentMap_Range** : calls iterator sequentially for each key and value present in each of the shards in the map. [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Range)] + [[play](https://go.dev/play/p/iqcy7P8P0Pr)] +

13. Mathutil package implements some functions for math calculation.        index

@@ -1295,6 +1318,8 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/uXod2LWD1Kg)] - **Join** : join the slice item with specify separator. [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)] + [[play](https://go.dev/play/p/huKzqwNDD7V)] +

19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited.        index

diff --git a/README_zh-CN.md b/README_zh-CN.md index 08666bb..5603096 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -389,31 +389,37 @@ import "github.com/duke-git/lancet/v2/cryptor" [[play](https://go.dev/play/p/uef0q1fz53I)] - **HmacMd5WithBase64** : 获取字符串 md5 hmac base64 字符串值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5WithBase64)] + [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5WithBase64)] - **HmacSha1** : 返回字符串 sha1 hmac 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)] [[play](https://go.dev/play/p/1UI4oQ4WXKM)] - **HmacSha1WithBase64** : 获取字符串的 sha1 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)] + [[play](https://go.dev/play/p/47JmmGrnF7B)] - **HmacSha256** : 返回字符串 sha256 hmac 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)] [[play](https://go.dev/play/p/HhpwXxFhhC0)] - **HmacSha256WithBase64** : 获取字符串 sha256 hmac base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)] + [[play](https://go.dev/play/p/EKbkUvPTLwO)] - **HmacSha512** : 返回字符串 sha256 hmac 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)] [[play](https://go.dev/play/p/59Od6m4A0Ud)] - **HmacSha512WithBase64** : 获取字符串 sha512 hmac base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)] + [[play](https://go.dev/play/p/c6dSe3E2ydU)] - **Md5Byte** : 返回 byte slice 的 md5 值. [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5Byte)] [[play](https://go.dev/play/p/suraalH8lyC)] - **Md5ByteWithBase64** : 获取 byte slice 的 md5 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)] + [[play](https://go.dev/play/p/Tcb-Z7LN2ax)] - **Md5String** : 返回字符串 md5 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)] [[play](https://go.dev/play/p/1bLcVetbTOI)] - **Md5StringWithBase64** : 获取字符串 md5 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)] + [[play](https://go.dev/play/p/Lx4gH7Vdr5_y)] - **Md5File** : 返回文件 md5 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)] - **Sha1** : 返回字符串 sha1 哈希值。 @@ -421,16 +427,19 @@ import "github.com/duke-git/lancet/v2/cryptor" [[play](https://go.dev/play/p/_m_uoD1deMT)] - **Sha1WithBase64** : 获取字符串 sha1 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)] + [[play](https://go.dev/play/p/fSyx-Gl2l2-)] - **Sha256** :返回字符串 sha256 哈希值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)] [[play](https://go.dev/play/p/tU9tfBMIAr1)] - **Sha256WithBase64** : 获取字符串 sha256 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)] + [[play](https://go.dev/play/p/85IXJHIal1k)] - **Sha512** : 返回字符串 sha512 哈希值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)] [[play](https://go.dev/play/p/3WsvLYZxsHa)] - **Sha512WithBase64** : 获取字符串 sha512 base64 值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)] + [[play](https://go.dev/play/p/q_fY2rA-k5I)] - **GenerateRsaKey** : 在当前目录下创建 rsa 私钥文件和公钥文件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)] [[play](https://go.dev/play/p/zutRHrDqs0X)] @@ -565,14 +574,20 @@ import "github.com/duke-git/lancet/v2/datetime" [[play](https://go.dev/play/p/cupRM5aZOIY)] - **NowDateOrTime** : 根据指定的格式和时区返回当前时间字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)] + [[play](https://go.dev/play/p/EZ-begEjtT0)] - **Timestamp** : 返回当前秒级时间戳。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)] + [[play](https://go.dev/play/p/iU5b7Vvjx6x)] - **TimestampMilli** : 返回当前毫秒级时间戳。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)] + [[play](https://go.dev/play/p/4gvEusOTu1T)] - **TimestampMicro** : 返回当前微秒级时间戳。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)] + [[play](https://go.dev/play/p/2maANglKHQE)] - **TimestampNano** : 返回当前纳秒级时间戳。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)] + [[play](https://go.dev/play/p/A9Oq_COrcCF)] +

8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph。       回到目录

@@ -828,22 +843,32 @@ import "github.com/duke-git/lancet/v2/maputil" [[play](https://go.dev/play/p/N9qgYg_Ho6f)] - **HasKey** : 检查map是否包含某个key。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)] + [[play](https://go.dev/play/p/isZZHOsDhFc)] - **NewConcurrentMap** : ConcurrentMap协程安全的map结构。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_Set** : 在map中设置key和value。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_Get** : 根据key获取value, 如果不存在key,返回零值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)] + [[play](https://go.dev/play/p/3PenTPETJT0)] - **ConcurrentMap_GetOrSet** : 返回键的现有值(如果存在),否则,设置key并返回给定值。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)] + [[play](https://go.dev/play/p/aDcDApOK01a)] - **ConcurrentMap_Delete** : 删除key。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)] + [[play](https://go.dev/play/p/uTIJZYhpVMS)] - **ConcurrentMap_GetAndDelete** :获取key,然后删除。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)] + [[play](https://go.dev/play/p/ZyxeIXSZUiM)] - **ConcurrentMap_Has** : 验证是否包含key。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)] + [[play](https://go.dev/play/p/C8L4ul9TVwf)] - **ConcurrentMap_Range** : 为map中每个键和值顺序调用迭代器。 如果iterator返回false,则停止迭代。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)] + [[play](https://go.dev/play/p/iqcy7P8P0Pr)] +

13. mathutil 包实现了一些数学计算的函数。       回到目录

@@ -1297,6 +1322,8 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/uXod2LWD1Kg)] - **Join** : 用指定的分隔符链接切片元素。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)] + [[play](https://go.dev/play/p/huKzqwNDD7V)] +

19. stream 流,该包仅验证简单的 stream 实现,功能有限。       回到目录

diff --git a/cryptor/basic.go b/cryptor/basic.go index 0c1dfea..c0f5b7e 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -57,7 +57,7 @@ func Md5Byte(data []byte) string { } // Md5ByteWithBase64 return the md5 string of byte slice with base64. -// Play: https://go.dev/play/p/CkN9hYKGeAy +// Play: https://go.dev/play/p/Tcb-Z7LN2ax func Md5ByteWithBase64(data []byte) string { h := md5.New() h.Write(data) @@ -153,7 +153,7 @@ func HmacSha512(str, key string) string { } // HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. -// Play: https://go.dev/play/p/61wBBOKO-GH +// Play: https://go.dev/play/p/c6dSe3E2ydU func HmacSha512WithBase64(str, key string) string { h := hmac.New(sha512.New, []byte(key)) h.Write([]byte(str)) @@ -169,7 +169,7 @@ func Sha1(str string) string { } // Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string. -// Play: todo +// Play: https://go.dev/play/p/fSyx-Gl2l2- func Sha1WithBase64(str string) string { sha1 := sha1.New() sha1.Write([]byte(str)) diff --git a/datetime/datetime.go b/datetime/datetime.go index 261b742..db80649 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -298,7 +298,7 @@ func IsWeekend(t time.Time) bool { } // NowDateOrTime return current datetime with specific format and timezone. -// Play: todo +// Play: https://go.dev/play/p/EZ-begEjtT0 func NowDateOrTime(format string, timezone ...string) string { tf, ok := timeFormat[strings.ToLower(format)] if !ok { @@ -318,7 +318,7 @@ func NowDateOrTime(format string, timezone ...string) string { } // Timestamp return current second timestamp. -// Play: todo +// Play: https://go.dev/play/p/iU5b7Vvjx6x func Timestamp(timezone ...string) int64 { t := time.Now() @@ -335,7 +335,7 @@ func Timestamp(timezone ...string) int64 { } // TimestampMilli return current mill second timestamp. -// Play: todo +// Play: https://go.dev/play/p/4gvEusOTu1T func TimestampMilli(timezone ...string) int64 { t := time.Now() @@ -351,7 +351,7 @@ func TimestampMilli(timezone ...string) int64 { } // TimestampMicro return current micro second timestamp. -// Play: todo +// Play: https://go.dev/play/p/2maANglKHQE func TimestampMicro(timezone ...string) int64 { t := time.Now() @@ -367,7 +367,7 @@ func TimestampMicro(timezone ...string) int64 { } // TimestampNano return current nano second timestamp. -// Play: todo +// Play: https://go.dev/play/p/A9Oq_COrcCF func TimestampNano(timezone ...string) int64 { t := time.Now() diff --git a/docs/maputil.md b/docs/maputil.md index 956be9b..5e9b79e 100644 --- a/docs/maputil.md +++ b/docs/maputil.md @@ -980,8 +980,8 @@ func main() { "b": 2, } - result1 := HasKey(m, "a") - result2 := HasKey(m, "c") + result1 := maputil.HasKey(m, "a") + result2 := maputil.HasKey(m, "c") fmt.Println(result1) fmt.Println(result2) @@ -1054,12 +1054,16 @@ func main() { wg1.Wait() - for j := 0; j < 5; j++ { - go func(n int) { - val, ok := cm.Get(fmt.Sprintf("%d", n)) - fmt.Println(val, ok) - }(j) - } + var wg2 sync.WaitGroup + wg2.Add(5) + for j := 0; j < 5; j++ { + go func(n int) { + val, ok := cm.Get(fmt.Sprintf("%d", n)) + fmt.Println(val, ok) + wg2.Done() + }(j) + } + wg2.Wait() // output: (order may change) // 1 true @@ -1105,12 +1109,16 @@ func main() { wg1.Wait() - for j := 0; j < 5; j++ { - go func(n int) { - val, ok := cm.Get(fmt.Sprintf("%d", n)) - fmt.Println(val, ok) - }(j) - } + var wg2 sync.WaitGroup + wg2.Add(5) + for j := 0; j < 5; j++ { + go func(n int) { + val, ok := cm.Get(fmt.Sprintf("%d", n)) + fmt.Println(val, ok) + wg2.Done() + }(j) + } + wg2.Wait() // output: (order may change) // 1 true @@ -1199,13 +1207,16 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { cm.Delete(fmt.Sprintf("%d", n)) wg2.Done() - }(i) + }(j) } + + wg2.Wait() } ``` @@ -1244,7 +1255,8 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) @@ -1252,8 +1264,12 @@ func main() { _, ok = cm.Get(fmt.Sprintf("%d", n)) fmt.Println(val, ok) //false + + wg2.Done() }(j) } + + wg2.Wait() } ``` @@ -1292,13 +1308,16 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { ok := cm.Has(fmt.Sprintf("%d", n)) fmt.Println(ok) // true + wg2.Done() }(j) } + wg2.Wait() } ``` @@ -1337,7 +1356,7 @@ func main() { } wg1.Wait() - + cm.Range(func(key string, value int) bool { fmt.Println(value) return true diff --git a/docs/maputil_zh-CN.md b/docs/maputil_zh-CN.md index 63f57cc..d814538 100644 --- a/docs/maputil_zh-CN.md +++ b/docs/maputil_zh-CN.md @@ -977,8 +977,8 @@ func main() { "b": 2, } - result1 := HasKey(m, "a") - result2 := HasKey(m, "c") + result1 := maputil.HasKey(m, "a") + result2 := maputil.HasKey(m, "c") fmt.Println(result1) fmt.Println(result2) @@ -1050,13 +1050,16 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { val, ok := cm.Get(fmt.Sprintf("%d", n)) fmt.Println(val, ok) + wg2.Done() }(j) } + wg2.Wait() // output: (order may change) // 1 true @@ -1101,13 +1104,16 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { val, ok := cm.Get(fmt.Sprintf("%d", n)) fmt.Println(val, ok) + wg2.Done() }(j) } + wg2.Wait() // output: (order may change) // 1 true @@ -1196,13 +1202,15 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { cm.Delete(fmt.Sprintf("%d", n)) wg2.Done() }(i) } + wg2.Wait() } ``` @@ -1241,7 +1249,8 @@ func main() { } wg1.Wait() - + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) @@ -1249,8 +1258,11 @@ func main() { _, ok = cm.Get(fmt.Sprintf("%d", n)) fmt.Println(val, ok) //false + + wg2.Done() }(j) } + wg2.Wait() } ``` @@ -1280,7 +1292,6 @@ func main() { var wg1 sync.WaitGroup wg1.Add(5) - for i := 0; i < 5; i++ { go func(n int) { cm.Set(fmt.Sprintf("%d", n), n) @@ -1289,13 +1300,18 @@ func main() { } wg1.Wait() + var wg2 sync.WaitGroup + wg2.Add(5) for j := 0; j < 5; j++ { go func(n int) { ok := cm.Has(fmt.Sprintf("%d", n)) fmt.Println(ok) // true + + wg2.Done() }(j) } + wg2.Wait() } ``` diff --git a/maputil/concurrentmap.go b/maputil/concurrentmap.go index 633370d..e229f5a 100644 --- a/maputil/concurrentmap.go +++ b/maputil/concurrentmap.go @@ -19,6 +19,7 @@ type ConcurrentMap[K comparable, V any] struct { } // NewConcurrentMap create a ConcurrentMap with specific shard count. +// Play: https://go.dev/play/p/3PenTPETJT0 func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] { if shardCount <= 0 { shardCount = defaultShardCount @@ -38,7 +39,7 @@ func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] } // Set the value for a key. -// Play: todo +// Play: https://go.dev/play/p/3PenTPETJT0 func (cm *ConcurrentMap[K, V]) Set(key K, value V) { shard := cm.getShard(key) @@ -49,7 +50,7 @@ func (cm *ConcurrentMap[K, V]) Set(key K, value V) { } // Get the value stored in the map for a key, or nil if no. -// Play: todo +// Play: https://go.dev/play/p/3PenTPETJT0 func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) { shard := cm.getShard(key) @@ -62,7 +63,7 @@ func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) { // GetOrSet returns the existing value for the key if present. // Otherwise, it sets and returns the given value. -// Play: todo +// Play: https://go.dev/play/p/aDcDApOK01a func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) { shard := cm.getShard(key) @@ -87,7 +88,7 @@ func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) { } // Delete the value for a key. -// Play: todo +// Play: https://go.dev/play/p/uTIJZYhpVMS func (cm *ConcurrentMap[K, V]) Delete(key K) { shard := cm.getShard(key) @@ -98,7 +99,7 @@ func (cm *ConcurrentMap[K, V]) Delete(key K) { // GetAndDelete returns the existing value for the key if present and then delete the value for the key. // Otherwise, do nothing, just return false -// Play: todo +// Play: https://go.dev/play/p/ZyxeIXSZUiM func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) { shard := cm.getShard(key) @@ -114,7 +115,7 @@ func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) { } // Has checks if map has the value for a key. -// Play: todo +// Play: https://go.dev/play/p/C8L4ul9TVwf func (cm *ConcurrentMap[K, V]) Has(key K) bool { _, ok := cm.Get(key) return ok @@ -122,6 +123,7 @@ func (cm *ConcurrentMap[K, V]) Has(key K) bool { // Range calls iterator sequentially for each key and value present in each of the shards in the map. // If iterator returns false, range stops the iteration. +// Play: https://go.dev/play/p/iqcy7P8P0Pr func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool) { for shard := range cm.locks { cm.locks[shard].RLock() diff --git a/maputil/map.go b/maputil/map.go index e0c39f6..a445121 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -298,7 +298,7 @@ func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value // fmt.Println("map has key baz") // } // -// Play: todo +// Play: https://go.dev/play/p/isZZHOsDhFc func HasKey[K comparable, V any](m map[K]V, key K) bool { _, haskey := m[key] return haskey diff --git a/slice/slice.go b/slice/slice.go index 2cb3aff..f1fe871 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1191,7 +1191,7 @@ func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T { } // Join the slice item with specify separator. -// Play: todo +// Play: https://go.dev/play/p/huKzqwNDD7V func Join[T any](s []T, separator string) string { str := Map(s, func(_ int, item T) string { return fmt.Sprint(item)