1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: add go playground demo

This commit is contained in:
dudaodong
2023-08-01 11:01:57 +08:00
parent 72924d4486
commit bb563724c7
9 changed files with 131 additions and 42 deletions

View File

@@ -395,26 +395,31 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/1UI4oQ4WXKM)] [[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha1WithBase64</big>** : return the hmac hash of string use sha1 with base64. - **<big>HmacSha1WithBase64</big>** : return the hmac hash of string use sha1 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)]
[[play](https://go.dev/play/p/47JmmGrnF7B)]
- **<big>HmacSha256</big>** : return the hmac hash of string use sha256. - **<big>HmacSha256</big>** : return the hmac hash of string use sha256.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)] [[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha256WithBase64</big>** : return the hmac hash of string use sha256 with base64. - **<big>HmacSha256WithBase64</big>** : return the hmac hash of string use sha256 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)]
[[play](https://go.dev/play/p/EKbkUvPTLwO)]
- **<big>HmacSha512</big>** : return the hmac hash of string use sha512. - **<big>HmacSha512</big>** : return the hmac hash of string use sha512.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)] [[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>HmacSha512WithBase64</big>** : return the hmac hash of string use sha512 with base64. - **<big>HmacSha512WithBase64</big>** : return the hmac hash of string use sha512 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)]
[[play](https://go.dev/play/p/c6dSe3E2ydU)]
- **<big>Md5Byte</big>** : return the md5 string of byte slice. - **<big>Md5Byte</big>** : return the md5 string of byte slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5Byte)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5Byte)]
[[play](https://go.dev/play/p/suraalH8lyC)] [[play](https://go.dev/play/p/suraalH8lyC)]
- **<big>Md5ByteWithBase64</big>** : return the md5 string of byte slice with base64. - **<big>Md5ByteWithBase64</big>** : return the md5 string of byte slice with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)]
[[play](https://go.dev/play/p/Tcb-Z7LN2ax)]
- **<big>Md5String</big>** : return the md5 value of string. - **<big>Md5String</big>** : return the md5 value of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)] [[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5StringWithBase64</big>** : return the md5 value of string with base64. - **<big>Md5StringWithBase64</big>** : return the md5 value of string with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)]
[[play](https://go.dev/play/p/Lx4gH7Vdr5_y)]
- **<big>Md5File</big>** : return the md5 value of file. - **<big>Md5File</big>** : return the md5 value of file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)]
- **<big>Sha1</big>** : return the sha1 value (SHA-1 hash algorithm) of base64 string. - **<big>Sha1</big>** : 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)] [[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<big>Sha1WithBase64</big>** : return the sha1 value (SHA-1 hash algorithm) of string. - **<big>Sha1WithBase64</big>** : return the sha1 value (SHA-1 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)]
[[play](https://go.dev/play/p/fSyx-Gl2l2-)]
- **<big>Sha256</big>** : return the sha256 value (SHA-256 hash algorithm) of string. - **<big>Sha256</big>** : return the sha256 value (SHA-256 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)] [[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<big>Sha256WithBase64</big>** : return the sha256 value (SHA256 hash algorithm) of base64 string. - **<big>Sha256WithBase64</big>** : return the sha256 value (SHA256 hash algorithm) of base64 string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)]
[[play](https://go.dev/play/p/85IXJHIal1k)]
- **<big>Sha512</big>** : return the sha512 value (SHA-512 hash algorithm) of string. - **<big>Sha512</big>** : return the sha512 value (SHA-512 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)] [[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>Sha512WithBase64</big>** : return the sha512 value (SHA-512 hash algorithm) of base64 string. - **<big>Sha512WithBase64</big>** : return the sha512 value (SHA-512 hash algorithm) of base64 string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)]
[[play](https://go.dev/play/p/q_fY2rA-k5I)]
- **<big>GenerateRsaKey</big>** : create rsa private and public pemo file. - **<big>GenerateRsaKey</big>** : create rsa private and public pemo file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)] [[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)] [[play](https://go.dev/play/p/cupRM5aZOIY)]
- **<big>NowDateOrTime</big>** : returns current datetime with specific format and timezone. - **<big>NowDateOrTime</big>** : returns current datetime with specific format and timezone.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)]
[[play](https://go.dev/play/p/EZ-begEjtT0)]
- **<big>Timestamp</big>** : returns current second timestamp. - **<big>Timestamp</big>** : returns current second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)]
- **<big>TimestampMilli</big>** : returns current mill second timestamp. - **<big>TimestampMilli</big>** : returns current mill second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)]
[[play](https://go.dev/play/p/4gvEusOTu1T)]
- **<big>TimestampMicro</big>** : returns current micro second timestamp. - **<big>TimestampMicro</big>** : returns current micro second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)]
[[play](https://go.dev/play/p/2maANglKHQE)]
- **<big>TimestampNano</big>** : returns current nano second timestamp. - **<big>TimestampNano</big>** : returns current nano second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)]
[[play](https://go.dev/play/p/A9Oq_COrcCF)]
<h3 id="Datastructure"> 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="Datastructure"> 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3>
@@ -826,22 +839,32 @@ import "github.com/duke-git/lancet/v2/maputil"
[[play](https://go.dev/play/p/N9qgYg_Ho6f)] [[play](https://go.dev/play/p/N9qgYg_Ho6f)]
- **<big>HasKey</big>** : checks if map has key or not. - **<big>HasKey</big>** : checks if map has key or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count. - **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Set</big>** : set the value for a key. - **<big>ConcurrentMap_Set</big>** : set the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Get</big>** : get the value stored in the map for a key, or nil if no. - **<big>ConcurrentMap_Get</big>** : 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)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Get)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_GetOrSet</big>** : returns the existing value for the key if present. - **<big>ConcurrentMap_GetOrSet</big>** : returns the existing value for the key if present.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetOrSet)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetOrSet)]
[[play](https://go.dev/play/p/aDcDApOK01a)]
- **<big>ConcurrentMap_Delete</big>** : delete the value for a key. - **<big>ConcurrentMap_Delete</big>** : delete the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)]
[[play](https://go.dev/play/p/uTIJZYhpVMS)]
- **<big>ConcurrentMap_GetAndDelete</big>** :returns the existing value for the key if present and then delete the value for the key. - **<big>ConcurrentMap_GetAndDelete</big>** :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)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetAndDelete)]
[[play](https://go.dev/play/p/ZyxeIXSZUiM)]
- **<big>ConcurrentMap_Has</big>** : checks if map has the value for a key. - **<big>ConcurrentMap_Has</big>** : checks if map has the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Has)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Has)]
[[play](https://go.dev/play/p/C8L4ul9TVwf)]
- **<big>ConcurrentMap_Range</big>** : calls iterator sequentially for each key and value present in each of the shards in the map. - **<big>ConcurrentMap_Range</big>** : 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)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Range)]
[[play](https://go.dev/play/p/iqcy7P8P0Pr)]
<h3 id="Mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="Mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3>
@@ -1295,6 +1318,8 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
- **<big>Join</big>** : join the slice item with specify separator. - **<big>Join</big>** : join the slice item with specify separator.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)]
<h3 id="Stream"> 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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="Stream"> 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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

View File

@@ -389,31 +389,37 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>HmacMd5WithBase64</big>** : 获取字符串 md5 hmac base64 字符串值。 - **<big>HmacMd5WithBase64</big>** : 获取字符串 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_zh-CN.md#HmacMd5WithBase64)]
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5WithBase64)]
- **<big>HmacSha1</big>** : 返回字符串 sha1 hmac 值。 - **<big>HmacSha1</big>** : 返回字符串 sha1 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)]
[[play](https://go.dev/play/p/1UI4oQ4WXKM)] [[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha1WithBase64</big>** : 获取字符串的 sha1 base64 值。 - **<big>HmacSha1WithBase64</big>** : 获取字符串的 sha1 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)]
[[play](https://go.dev/play/p/47JmmGrnF7B)]
- **<big>HmacSha256</big>** : 返回字符串 sha256 hmac 值。 - **<big>HmacSha256</big>** : 返回字符串 sha256 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)] [[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha256WithBase64</big>** : 获取字符串 sha256 hmac base64 值。 - **<big>HmacSha256WithBase64</big>** : 获取字符串 sha256 hmac base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)]
[[play](https://go.dev/play/p/EKbkUvPTLwO)]
- **<big>HmacSha512</big>** : 返回字符串 sha256 hmac 值。 - **<big>HmacSha512</big>** : 返回字符串 sha256 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)] [[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>HmacSha512WithBase64</big>** : 获取字符串 sha512 hmac base64 值。 - **<big>HmacSha512WithBase64</big>** : 获取字符串 sha512 hmac base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)]
[[play](https://go.dev/play/p/c6dSe3E2ydU)]
- **<big>Md5Byte</big>** : 返回 byte slice 的 md5 值. - **<big>Md5Byte</big>** : 返回 byte slice 的 md5 值.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5Byte)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5Byte)]
[[play](https://go.dev/play/p/suraalH8lyC)] [[play](https://go.dev/play/p/suraalH8lyC)]
- **<big>Md5ByteWithBase64</big>** : 获取 byte slice 的 md5 base64 值。 - **<big>Md5ByteWithBase64</big>** : 获取 byte slice 的 md5 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)]
[[play](https://go.dev/play/p/Tcb-Z7LN2ax)]
- **<big>Md5String</big>** : 返回字符串 md5 值。 - **<big>Md5String</big>** : 返回字符串 md5 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)] [[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5StringWithBase64</big>** : 获取字符串 md5 base64 值。 - **<big>Md5StringWithBase64</big>** : 获取字符串 md5 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)]
[[play](https://go.dev/play/p/Lx4gH7Vdr5_y)]
- **<big>Md5File</big>** : 返回文件 md5 值。 - **<big>Md5File</big>** : 返回文件 md5 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)]
- **<big>Sha1</big>** : 返回字符串 sha1 哈希值。 - **<big>Sha1</big>** : 返回字符串 sha1 哈希值。
@@ -421,16 +427,19 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/_m_uoD1deMT)] [[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<big>Sha1WithBase64</big>** : 获取字符串 sha1 base64 值。 - **<big>Sha1WithBase64</big>** : 获取字符串 sha1 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)]
[[play](https://go.dev/play/p/fSyx-Gl2l2-)]
- **<big>Sha256</big>** :返回字符串 sha256 哈希值。 - **<big>Sha256</big>** :返回字符串 sha256 哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)] [[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<big>Sha256WithBase64</big>** : 获取字符串 sha256 base64 值。 - **<big>Sha256WithBase64</big>** : 获取字符串 sha256 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)]
[[play](https://go.dev/play/p/85IXJHIal1k)]
- **<big>Sha512</big>** : 返回字符串 sha512 哈希值。 - **<big>Sha512</big>** : 返回字符串 sha512 哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)] [[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>Sha512WithBase64</big>** : 获取字符串 sha512 base64 值。 - **<big>Sha512WithBase64</big>** : 获取字符串 sha512 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)]
[[play](https://go.dev/play/p/q_fY2rA-k5I)]
- **<big>GenerateRsaKey</big>** : 在当前目录下创建 rsa 私钥文件和公钥文件。 - **<big>GenerateRsaKey</big>** : 在当前目录下创建 rsa 私钥文件和公钥文件。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)] [[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)] [[play](https://go.dev/play/p/cupRM5aZOIY)]
- **<big>NowDateOrTime</big>** : 根据指定的格式和时区返回当前时间字符串。 - **<big>NowDateOrTime</big>** : 根据指定的格式和时区返回当前时间字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)]
[[play](https://go.dev/play/p/EZ-begEjtT0)]
- **<big>Timestamp</big>** : 返回当前秒级时间戳。 - **<big>Timestamp</big>** : 返回当前秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)]
[[play](https://go.dev/play/p/iU5b7Vvjx6x)]
- **<big>TimestampMilli</big>** : 返回当前毫秒级时间戳。 - **<big>TimestampMilli</big>** : 返回当前毫秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)]
[[play](https://go.dev/play/p/4gvEusOTu1T)]
- **<big>TimestampMicro</big>** : 返回当前微秒级时间戳。 - **<big>TimestampMicro</big>** : 返回当前微秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)]
[[play](https://go.dev/play/p/2maANglKHQE)]
- **<big>TimestampNano</big>** : 返回当前纳秒级时间戳。 - **<big>TimestampNano</big>** : 返回当前纳秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)]
[[play](https://go.dev/play/p/A9Oq_COrcCF)]
<h3 id="Datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="Datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -828,22 +843,32 @@ import "github.com/duke-git/lancet/v2/maputil"
[[play](https://go.dev/play/p/N9qgYg_Ho6f)] [[play](https://go.dev/play/p/N9qgYg_Ho6f)]
- **<big>HasKey</big>** : 检查map是否包含某个key。 - **<big>HasKey</big>** : 检查map是否包含某个key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>NewConcurrentMap</big>** : ConcurrentMap协程安全的map结构。 - **<big>NewConcurrentMap</big>** : ConcurrentMap协程安全的map结构。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Set</big>** : 在map中设置key和value。 - **<big>ConcurrentMap_Set</big>** : 在map中设置key和value。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Get</big>** : 根据key获取value, 如果不存在key,返回零值。 - **<big>ConcurrentMap_Get</big>** : 根据key获取value, 如果不存在key,返回零值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_GetOrSet</big>** : 返回键的现有值如果存在否则设置key并返回给定值。 - **<big>ConcurrentMap_GetOrSet</big>** : 返回键的现有值如果存在否则设置key并返回给定值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)]
[[play](https://go.dev/play/p/aDcDApOK01a)]
- **<big>ConcurrentMap_Delete</big>** : 删除key。 - **<big>ConcurrentMap_Delete</big>** : 删除key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)]
[[play](https://go.dev/play/p/uTIJZYhpVMS)]
- **<big>ConcurrentMap_GetAndDelete</big>** :获取key然后删除。 - **<big>ConcurrentMap_GetAndDelete</big>** :获取key然后删除。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)]
[[play](https://go.dev/play/p/ZyxeIXSZUiM)]
- **<big>ConcurrentMap_Has</big>** : 验证是否包含key。 - **<big>ConcurrentMap_Has</big>** : 验证是否包含key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)]
[[play](https://go.dev/play/p/C8L4ul9TVwf)]
- **<big>ConcurrentMap_Range</big>** : 为map中每个键和值顺序调用迭代器。 如果iterator返回false则停止迭代。 - **<big>ConcurrentMap_Range</big>** : 为map中每个键和值顺序调用迭代器。 如果iterator返回false则停止迭代。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)]
[[play](https://go.dev/play/p/iqcy7P8P0Pr)]
<h3 id="Mathutil"> 13. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="Mathutil"> 13. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1297,6 +1322,8 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
- **<big>Join</big>** : 用指定的分隔符链接切片元素。 - **<big>Join</big>** : 用指定的分隔符链接切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)]
<h3 id="Stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="Stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>

View File

@@ -57,7 +57,7 @@ func Md5Byte(data []byte) string {
} }
// Md5ByteWithBase64 return the md5 string of byte slice with base64. // 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 { func Md5ByteWithBase64(data []byte) string {
h := md5.New() h := md5.New()
h.Write(data) h.Write(data)
@@ -153,7 +153,7 @@ func HmacSha512(str, key string) string {
} }
// HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. // 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 { func HmacSha512WithBase64(str, key string) string {
h := hmac.New(sha512.New, []byte(key)) h := hmac.New(sha512.New, []byte(key))
h.Write([]byte(str)) 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. // 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 { func Sha1WithBase64(str string) string {
sha1 := sha1.New() sha1 := sha1.New()
sha1.Write([]byte(str)) sha1.Write([]byte(str))

View File

@@ -298,7 +298,7 @@ func IsWeekend(t time.Time) bool {
} }
// NowDateOrTime return current datetime with specific format and timezone. // 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 { func NowDateOrTime(format string, timezone ...string) string {
tf, ok := timeFormat[strings.ToLower(format)] tf, ok := timeFormat[strings.ToLower(format)]
if !ok { if !ok {
@@ -318,7 +318,7 @@ func NowDateOrTime(format string, timezone ...string) string {
} }
// Timestamp return current second timestamp. // Timestamp return current second timestamp.
// Play: todo // Play: https://go.dev/play/p/iU5b7Vvjx6x
func Timestamp(timezone ...string) int64 { func Timestamp(timezone ...string) int64 {
t := time.Now() t := time.Now()
@@ -335,7 +335,7 @@ func Timestamp(timezone ...string) int64 {
} }
// TimestampMilli return current mill second timestamp. // TimestampMilli return current mill second timestamp.
// Play: todo // Play: https://go.dev/play/p/4gvEusOTu1T
func TimestampMilli(timezone ...string) int64 { func TimestampMilli(timezone ...string) int64 {
t := time.Now() t := time.Now()
@@ -351,7 +351,7 @@ func TimestampMilli(timezone ...string) int64 {
} }
// TimestampMicro return current micro second timestamp. // TimestampMicro return current micro second timestamp.
// Play: todo // Play: https://go.dev/play/p/2maANglKHQE
func TimestampMicro(timezone ...string) int64 { func TimestampMicro(timezone ...string) int64 {
t := time.Now() t := time.Now()
@@ -367,7 +367,7 @@ func TimestampMicro(timezone ...string) int64 {
} }
// TimestampNano return current nano second timestamp. // TimestampNano return current nano second timestamp.
// Play: todo // Play: https://go.dev/play/p/A9Oq_COrcCF
func TimestampNano(timezone ...string) int64 { func TimestampNano(timezone ...string) int64 {
t := time.Now() t := time.Now()

View File

@@ -980,8 +980,8 @@ func main() {
"b": 2, "b": 2,
} }
result1 := HasKey(m, "a") result1 := maputil.HasKey(m, "a")
result2 := HasKey(m, "c") result2 := maputil.HasKey(m, "c")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -1054,12 +1054,16 @@ func main() {
wg1.Wait() wg1.Wait()
for j := 0; j < 5; j++ { var wg2 sync.WaitGroup
go func(n int) { wg2.Add(5)
val, ok := cm.Get(fmt.Sprintf("%d", n)) for j := 0; j < 5; j++ {
fmt.Println(val, ok) go func(n int) {
}(j) val, ok := cm.Get(fmt.Sprintf("%d", n))
} fmt.Println(val, ok)
wg2.Done()
}(j)
}
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1105,12 +1109,16 @@ func main() {
wg1.Wait() wg1.Wait()
for j := 0; j < 5; j++ { var wg2 sync.WaitGroup
go func(n int) { wg2.Add(5)
val, ok := cm.Get(fmt.Sprintf("%d", n)) for j := 0; j < 5; j++ {
fmt.Println(val, ok) go func(n int) {
}(j) val, ok := cm.Get(fmt.Sprintf("%d", n))
} fmt.Println(val, ok)
wg2.Done()
}(j)
}
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1199,13 +1207,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
cm.Delete(fmt.Sprintf("%d", n)) cm.Delete(fmt.Sprintf("%d", n))
wg2.Done() wg2.Done()
}(i) }(j)
} }
wg2.Wait()
} }
``` ```
@@ -1244,7 +1255,8 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
@@ -1252,8 +1264,12 @@ func main() {
_, ok = cm.Get(fmt.Sprintf("%d", n)) _, ok = cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) //false fmt.Println(val, ok) //false
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
@@ -1292,13 +1308,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
ok := cm.Has(fmt.Sprintf("%d", n)) ok := cm.Has(fmt.Sprintf("%d", n))
fmt.Println(ok) // true fmt.Println(ok) // true
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
@@ -1337,7 +1356,7 @@ func main() {
} }
wg1.Wait() wg1.Wait()
cm.Range(func(key string, value int) bool { cm.Range(func(key string, value int) bool {
fmt.Println(value) fmt.Println(value)
return true return true

View File

@@ -977,8 +977,8 @@ func main() {
"b": 2, "b": 2,
} }
result1 := HasKey(m, "a") result1 := maputil.HasKey(m, "a")
result2 := HasKey(m, "c") result2 := maputil.HasKey(m, "c")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -1050,13 +1050,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n)) val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) fmt.Println(val, ok)
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1101,13 +1104,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n)) val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) fmt.Println(val, ok)
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1196,13 +1202,15 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
cm.Delete(fmt.Sprintf("%d", n)) cm.Delete(fmt.Sprintf("%d", n))
wg2.Done() wg2.Done()
}(i) }(i)
} }
wg2.Wait()
} }
``` ```
@@ -1241,7 +1249,8 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
@@ -1249,8 +1258,11 @@ func main() {
_, ok = cm.Get(fmt.Sprintf("%d", n)) _, ok = cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) //false fmt.Println(val, ok) //false
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
@@ -1280,7 +1292,6 @@ func main() {
var wg1 sync.WaitGroup var wg1 sync.WaitGroup
wg1.Add(5) wg1.Add(5)
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
go func(n int) { go func(n int) {
cm.Set(fmt.Sprintf("%d", n), n) cm.Set(fmt.Sprintf("%d", n), n)
@@ -1289,13 +1300,18 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
ok := cm.Has(fmt.Sprintf("%d", n)) ok := cm.Has(fmt.Sprintf("%d", n))
fmt.Println(ok) // true fmt.Println(ok) // true
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```

View File

@@ -19,6 +19,7 @@ type ConcurrentMap[K comparable, V any] struct {
} }
// NewConcurrentMap create a ConcurrentMap with specific shard count. // 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] { func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] {
if shardCount <= 0 { if shardCount <= 0 {
shardCount = defaultShardCount shardCount = defaultShardCount
@@ -38,7 +39,7 @@ func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]
} }
// Set the value for a key. // 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) { func (cm *ConcurrentMap[K, V]) Set(key K, value V) {
shard := cm.getShard(key) 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. // 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) { func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) {
shard := cm.getShard(key) 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. // GetOrSet returns the existing value for the key if present.
// Otherwise, it sets and returns the given value. // 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) { func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) {
shard := cm.getShard(key) 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. // Delete the value for a key.
// Play: todo // Play: https://go.dev/play/p/uTIJZYhpVMS
func (cm *ConcurrentMap[K, V]) Delete(key K) { func (cm *ConcurrentMap[K, V]) Delete(key K) {
shard := cm.getShard(key) 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. // GetAndDelete returns the existing value for the key if present and then delete the value for the key.
// Otherwise, do nothing, just return false // 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) { func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) {
shard := cm.getShard(key) 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. // 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 { func (cm *ConcurrentMap[K, V]) Has(key K) bool {
_, ok := cm.Get(key) _, ok := cm.Get(key)
return ok 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. // 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. // 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) { func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool) {
for shard := range cm.locks { for shard := range cm.locks {
cm.locks[shard].RLock() cm.locks[shard].RLock()

View File

@@ -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") // 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 { func HasKey[K comparable, V any](m map[K]V, key K) bool {
_, haskey := m[key] _, haskey := m[key]
return haskey return haskey

View File

@@ -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. // 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 { func Join[T any](s []T, separator string) string {
str := Map(s, func(_ int, item T) string { str := Map(s, func(_ int, item T) string {
return fmt.Sprint(item) return fmt.Sprint(item)