mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
Compare commits
26 Commits
0d0848ac67
...
v2.2.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72924d4486 | ||
|
|
231f8b04b4 | ||
|
|
daa932fee3 | ||
|
|
1b0691f1d5 | ||
|
|
0b6a00bd99 | ||
|
|
4ab98664bb | ||
|
|
31eb5f4d1f | ||
|
|
0ae5d17a06 | ||
|
|
4558d7a3c2 | ||
|
|
4715301240 | ||
|
|
1c58ee23f1 | ||
|
|
2a303d5e4b | ||
|
|
326e7881a6 | ||
|
|
31e8b12674 | ||
|
|
a0431d9435 | ||
|
|
0456b65cc7 | ||
|
|
989b4dd791 | ||
|
|
a76b02fbba | ||
|
|
2d7747738a | ||
|
|
fe0cb04137 | ||
|
|
fe0264f628 | ||
|
|
65396cee32 | ||
|
|
544702b460 | ||
|
|
d8936cdcb5 | ||
|
|
d4b97d6b20 | ||
|
|
5caa14c838 |
60
README.md
60
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -38,7 +38,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.0. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.1. </b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
||||||
@@ -388,32 +388,50 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
- **<big>HmacMd5</big>** : return the md5 hmac hash of string.
|
- **<big>HmacMd5</big>** : return the md5 hmac hash of string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5)]
|
||||||
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
||||||
|
- **<big>HmacMd5WithBase64</big>** : return the md5 hmac hash of base64 string.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5WithBase64)]
|
||||||
- **<big>HmacSha1</big>** : return the hmac hash of string use sha1.
|
- **<big>HmacSha1</big>** : return the hmac hash of string use sha1.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1)]
|
||||||
[[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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)]
|
||||||
- **<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 string.
|
- **<big>Sha1</big>** : return the sha1 value (SHA-1 hash algorithm) of base64 string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1)]
|
||||||
[[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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)]
|
||||||
- **<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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)]
|
||||||
- **<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)]
|
||||||
@@ -543,6 +561,16 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>IsWeekend</big>** : checks if passed time is weekend or not.
|
- **<big>IsWeekend</big>** : checks if passed time is weekend or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#IsWeekend)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#IsWeekend)]
|
||||||
[[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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)]
|
||||||
|
- **<big>Timestamp</big>** : returns current second timestamp.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)]
|
||||||
|
- **<big>TimestampMilli</big>** : returns current mill second timestamp.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)]
|
||||||
|
- **<big>TimestampMicro</big>** : returns current micro second timestamp.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)]
|
||||||
|
- **<big>TimestampNano</big>** : returns current nano second timestamp.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)]
|
||||||
|
|
||||||
<h3 id="Datastructure"> 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. <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. <a href="#index">index</span></h3>
|
||||||
|
|
||||||
@@ -796,6 +824,24 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
- **<big>IsDisjoint</big>** : check two map are disjoint if they have no keys in common.
|
- **<big>IsDisjoint</big>** : check two map are disjoint if they have no keys in common.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)]
|
||||||
[[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.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)]
|
||||||
|
- **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)]
|
||||||
|
- **<big>ConcurrentMap_Set</big>** : set the value for a key.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)]
|
||||||
|
- **<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)]
|
||||||
|
- **<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)]
|
||||||
|
- **<big>ConcurrentMap_Delete</big>** : delete the value for a key.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)]
|
||||||
|
- **<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)]
|
||||||
|
- **<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)]
|
||||||
|
- **<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)]
|
||||||
|
|
||||||
<h3 id="Mathutil"> 13. Mathutil package implements some functions for math calculation. <a href="#index">index</span></h3>
|
<h3 id="Mathutil"> 13. Mathutil package implements some functions for math calculation. <a href="#index">index</span></h3>
|
||||||
|
|
||||||
@@ -968,6 +1014,12 @@ import "github.com/duke-git/lancet/v2/pointer"
|
|||||||
- **<big>Unwrap</big>** : return the value from the pointer.
|
- **<big>Unwrap</big>** : return the value from the pointer.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#Unwrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#Unwrap)]
|
||||||
[[play](https://go.dev/play/p/cgeu3g7cjWb)]
|
[[play](https://go.dev/play/p/cgeu3g7cjWb)]
|
||||||
|
- **<big>UnwarpOr</big>** : UnwarpOr returns the value from the pointer or fallback if the pointer is nil.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#UnwrapOr)]
|
||||||
|
[[play](https://go.dev/play/p/mmNaLC38W8C)]
|
||||||
|
- **<big>UnwarpOrDefault</big>** : UnwarpOrDefault returns the value from the pointer or the default value if the pointer is nil.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#UnwrapOrDefault)]
|
||||||
|
[[play](https://go.dev/play/p/ZnGIHf8_o4E)]
|
||||||
|
|
||||||
<h3 id="Random"> 16. Random package implements some basic functions to generate random int and string. <a href="#index">index</a></h3>
|
<h3 id="Random"> 16. Random package implements some basic functions to generate random int and string. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
@@ -1241,6 +1293,8 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- **<big>KeyBy</big>** : converts a slice to a map based on a callback function.
|
- **<big>KeyBy</big>** : converts a slice to a map based on a callback function.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)]
|
||||||
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
||||||
|
- **<big>Join</big>** : join the slice item with specify separator.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)]
|
||||||
|
|
||||||
<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. <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. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -37,7 +37,7 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.0。</b>
|
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.1。</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
@@ -387,32 +387,50 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
- **<big>HmacMd5</big>** : 返回字符串 md5 hmac 值。
|
- **<big>HmacMd5</big>** : 返回字符串 md5 hmac 值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5)]
|
||||||
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
[[play](https://go.dev/play/p/uef0q1fz53I)]
|
||||||
|
- **<big>HmacMd5WithBase64</big>** : 获取字符串 md5 hmac base64 字符串值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)]
|
||||||
- **<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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)]
|
||||||
- **<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>Md5Byte</big>** : 返回byte slice的md5值.
|
- **<big>HmacSha512WithBase64</big>** : 获取字符串 sha512 hmac base64 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)]
|
||||||
|
- **<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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)]
|
||||||
- **<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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)]
|
||||||
- **<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 哈希值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1)]
|
||||||
[[play](https://go.dev/play/p/_m_uoD1deMT)]
|
[[play](https://go.dev/play/p/_m_uoD1deMT)]
|
||||||
|
- **<big>Sha1WithBase64</big>** : 获取字符串 sha1 base64 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)]
|
||||||
- **<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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)]
|
||||||
- **<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 值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)]
|
||||||
- **<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)]
|
||||||
@@ -545,6 +563,16 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>IsWeekend</big>** : 判断日期是否是周末。
|
- **<big>IsWeekend</big>** : 判断日期是否是周末。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#IsWeekend)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#IsWeekend)]
|
||||||
[[play](https://go.dev/play/p/cupRM5aZOIY)]
|
[[play](https://go.dev/play/p/cupRM5aZOIY)]
|
||||||
|
- **<big>NowDateOrTime</big>** : 根据指定的格式和时区返回当前时间字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)]
|
||||||
|
- **<big>Timestamp</big>** : 返回当前秒级时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)]
|
||||||
|
- **<big>TimestampMilli</big>** : 返回当前毫秒级时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)]
|
||||||
|
- **<big>TimestampMicro</big>** : 返回当前微秒级时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)]
|
||||||
|
- **<big>TimestampNano</big>** : 返回当前纳秒级时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)]
|
||||||
|
|
||||||
<h3 id="Datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph。 <a href="#index">回到目录</a></h3>
|
<h3 id="Datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -778,26 +806,44 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)]
|
||||||
[[play](https://go.dev/play/p/CBKdUc5FTW6)]
|
[[play](https://go.dev/play/p/CBKdUc5FTW6)]
|
||||||
- **<big>ValuesBy</big>** : 创建一个切片,其元素是每个 map 的 value 调用 mapper 函数的结果。
|
- **<big>ValuesBy</big>** : 创建一个切片,其元素是每个 map 的 value 调用 mapper 函数的结果。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#ValuesBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ValuesBy)]
|
||||||
[[play](https://go.dev/play/p/sg9-oRidh8f)]
|
[[play](https://go.dev/play/p/sg9-oRidh8f)]
|
||||||
- **<big>MapKeys</big>** : 操作 map 的每个 key,然后转为新的 map。
|
- **<big>MapKeys</big>** : 操作 map 的每个 key,然后转为新的 map。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#MapKeys)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#MapKeys)]
|
||||||
[[play](https://go.dev/play/p/8scDxWeBDKd)]
|
[[play](https://go.dev/play/p/8scDxWeBDKd)]
|
||||||
- **<big>MapValues</big>** : 操作 map 的每个 value,然后转为新的 map。
|
- **<big>MapValues</big>** : 操作 map 的每个 value,然后转为新的 map。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#MapValues)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#MapValues)]
|
||||||
[[play](https://go.dev/play/p/g92aY3fc7Iw)]
|
[[play](https://go.dev/play/p/g92aY3fc7Iw)]
|
||||||
- **<big>Entries</big>** : 将 map 转换为键/值对切片。
|
- **<big>Entries</big>** : 将 map 转换为键/值对切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#Entries)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Entries)]
|
||||||
[[play](https://go.dev/play/p/Ltb11LNcElY)]
|
[[play](https://go.dev/play/p/Ltb11LNcElY)]
|
||||||
- **<big>FromEntries</big>** : 基于键/值对的切片创建 map。
|
- **<big>FromEntries</big>** : 基于键/值对的切片创建 map。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#FromEntries)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#FromEntries)]
|
||||||
[[play](https://go.dev/play/p/fTdu4sCNjQO)]
|
[[play](https://go.dev/play/p/fTdu4sCNjQO)]
|
||||||
- **<big>Transform</big>** : 将 map 转换为其他类型的 map。
|
- **<big>Transform</big>** : 将 map 转换为其他类型的 map。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN#Transform)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Transform)]
|
||||||
[[play](https://go.dev/play/p/P6ovfToM3zj)]
|
[[play](https://go.dev/play/p/P6ovfToM3zj)]
|
||||||
- **<big>IsDisjoint</big>** : 验证两个 map 是否具有不同的 key。
|
- **<big>IsDisjoint</big>** : 验证两个 map 是否具有不同的 key。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)]
|
||||||
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
||||||
|
- **<big>HasKey</big>** : 检查map是否包含某个key。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)]
|
||||||
|
- **<big>NewConcurrentMap</big>** : ConcurrentMap协程安全的map结构。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)]
|
||||||
|
- **<big>ConcurrentMap_Set</big>** : 在map中设置key和value。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)]
|
||||||
|
- **<big>ConcurrentMap_Get</big>** : 根据key获取value, 如果不存在key,返回零值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)]
|
||||||
|
- **<big>ConcurrentMap_GetOrSet</big>** : 返回键的现有值(如果存在),否则,设置key并返回给定值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)]
|
||||||
|
- **<big>ConcurrentMap_Delete</big>** : 删除key。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)]
|
||||||
|
- **<big>ConcurrentMap_GetAndDelete</big>** :获取key,然后删除。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)]
|
||||||
|
- **<big>ConcurrentMap_Has</big>** : 验证是否包含key。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)]
|
||||||
|
- **<big>ConcurrentMap_Range</big>** : 为map中每个键和值顺序调用迭代器。 如果iterator返回false,则停止迭代。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)]
|
||||||
|
|
||||||
<h3 id="Mathutil"> 13. mathutil 包实现了一些数学计算的函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="Mathutil"> 13. mathutil 包实现了一些数学计算的函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -969,7 +1015,13 @@ import "github.com/duke-git/lancet/v2/pointer"
|
|||||||
[[play](https://go.dev/play/p/HFd70x4DrMj)]
|
[[play](https://go.dev/play/p/HFd70x4DrMj)]
|
||||||
- **<big>Unwrap</big>** : 返回传入指针指向的值。
|
- **<big>Unwrap</big>** : 返回传入指针指向的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#Unwrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#Unwrap)]
|
||||||
[[play](https://go.dev/play/p/cgeu3g7cjWb)]
|
[[play](https://go.dev/play/p/cgeu3g7cjWb)
|
||||||
|
- **<big>UnwarpOr</big>** : 返回指针的值,如果指针为零值,则返回 fallback。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#UnwrapOr)]
|
||||||
|
[[play](https://go.dev/play/p/mmNaLC38W8C)]
|
||||||
|
- **<big>UnwarpOrDefault</big>** : 返回指针的值,如果指针为零值,则返回相应零值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#UnwrapOrDefault)]
|
||||||
|
[[play](https://go.dev/play/p/ZnGIHf8_o4E)]
|
||||||
|
|
||||||
<h3 id="Random"> 16. random 随机数生成器包,可以生成随机[]bytes, int, string。 <a href="#index">回到目录</a></h3>
|
<h3 id="Random"> 16. random 随机数生成器包,可以生成随机[]bytes, int, string。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -1243,6 +1295,8 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- **<big>KeyBy</big>** :将切片每个元素调用函数后转为 map。
|
- **<big>KeyBy</big>** :将切片每个元素调用函数后转为 map。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
|
||||||
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
||||||
|
- **<big>Join</big>** : 用指定的分隔符链接切片元素。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)]
|
||||||
|
|
||||||
<h3 id="Stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。 <a href="#index">回到目录</a></h3>
|
<h3 id="Stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -1538,7 +1592,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip2</big>** : 创建一个 Tuple2 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip2</big>** : 创建一个 Tuple2 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip2)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip2)]
|
||||||
[[play](https://go.dev/play/p/4ncWJJ77Xio)]
|
[[play](https://go.dev/play/p/4ncWJJ77Xio)]
|
||||||
- **<big>Unzip2</big>** : 根据传入的Tuple2切片,创建一组和Tuple2元素相对应的切片。
|
- **<big>Unzip2</big>** : 根据传入的 Tuple2 切片,创建一组和 Tuple2 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip2)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip2)]
|
||||||
[[play](https://go.dev/play/p/KBecr60feXb)]
|
[[play](https://go.dev/play/p/KBecr60feXb)]
|
||||||
- **<big>Tuple3</big>** : 3 元元组
|
- **<big>Tuple3</big>** : 3 元元组
|
||||||
@@ -1550,7 +1604,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip3</big>** : 创建一个 Tuple3 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip3</big>** : 创建一个 Tuple3 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip3)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip3)]
|
||||||
[[play](https://go.dev/play/p/97NgmsTILfu)]
|
[[play](https://go.dev/play/p/97NgmsTILfu)]
|
||||||
- **<big>Unzip3</big>** : 根据传入的Tuple3切片,创建一组和Tuple3元素相对应的切片。
|
- **<big>Unzip3</big>** : 根据传入的 Tuple3 切片,创建一组和 Tuple3 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip3)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip3)]
|
||||||
[[play](https://go.dev/play/p/bba4cpAa7KO)]
|
[[play](https://go.dev/play/p/bba4cpAa7KO)]
|
||||||
- **<big>Tuple4</big>** : 4 元元组
|
- **<big>Tuple4</big>** : 4 元元组
|
||||||
@@ -1562,7 +1616,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip4</big>** : 创建一个 Tuple4 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip4</big>** : 创建一个 Tuple4 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip4)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip4)]
|
||||||
[[play](https://go.dev/play/p/PEmTYVK5hL4)]
|
[[play](https://go.dev/play/p/PEmTYVK5hL4)]
|
||||||
- **<big>Unzip4</big>** : 根据传入的Tuple4切片,创建一组和Tuple4元素相对应的切片。
|
- **<big>Unzip4</big>** : 根据传入的 Tuple4 切片,创建一组和 Tuple4 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip4)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip4)]
|
||||||
[[play](https://go.dev/play/p/rb8z4gyYSRN)]
|
[[play](https://go.dev/play/p/rb8z4gyYSRN)]
|
||||||
- **<big>Tuple5</big>** : 5 元元组
|
- **<big>Tuple5</big>** : 5 元元组
|
||||||
@@ -1574,7 +1628,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip5</big>** : 创建一个 Tuple5 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip5</big>** : 创建一个 Tuple5 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip5)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip5)]
|
||||||
[[play](https://go.dev/play/p/fCAAJLMfBIP)]
|
[[play](https://go.dev/play/p/fCAAJLMfBIP)]
|
||||||
- **<big>Unzip5</big>** : 根据传入的Tuple5切片,创建一组和Tuple5元素相对应的切片。
|
- **<big>Unzip5</big>** : 根据传入的 Tuple5 切片,创建一组和 Tuple5 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip5)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip5)]
|
||||||
[[play](https://go.dev/play/p/gyl6vKfhqPb)]
|
[[play](https://go.dev/play/p/gyl6vKfhqPb)]
|
||||||
- **<big>Tuple6</big>** : 6 元元组
|
- **<big>Tuple6</big>** : 6 元元组
|
||||||
@@ -1586,7 +1640,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip6</big>** : 创建一个 Tuple6 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip6</big>** : 创建一个 Tuple6 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip6)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip6)]
|
||||||
[[play](https://go.dev/play/p/oWPrnUYuFHo)]
|
[[play](https://go.dev/play/p/oWPrnUYuFHo)]
|
||||||
- **<big>Unzip6</big>** : 根据传入的Tuple6切片,创建一组和Tuple6元素相对应的切片。
|
- **<big>Unzip6</big>** : 根据传入的 Tuple6 切片,创建一组和 Tuple6 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip6)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip6)]
|
||||||
[[play](https://go.dev/play/p/l41XFqCyh5E)]
|
[[play](https://go.dev/play/p/l41XFqCyh5E)]
|
||||||
- **<big>Tuple7</big>** : 7 元元组
|
- **<big>Tuple7</big>** : 7 元元组
|
||||||
@@ -1598,7 +1652,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip7</big>** : 创建一个 Tuple7 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip7</big>** : 创建一个 Tuple7 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip7)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip7)]
|
||||||
[[play](https://go.dev/play/p/WUJuo897Egf)]
|
[[play](https://go.dev/play/p/WUJuo897Egf)]
|
||||||
- **<big>Unzip7</big>** : 根据传入的Tuple7切片,创建一组和Tuple7元素相对应的切片。
|
- **<big>Unzip7</big>** : 根据传入的 Tuple7 切片,创建一组和 Tuple7 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip7)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip7)]
|
||||||
[[play](https://go.dev/play/p/hws_P1Fr2j3)]
|
[[play](https://go.dev/play/p/hws_P1Fr2j3)]
|
||||||
- **<big>Tuple8</big>** : 8 元元组
|
- **<big>Tuple8</big>** : 8 元元组
|
||||||
@@ -1610,7 +1664,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip8</big>** : 创建一个 Tuple8 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip8</big>** : 创建一个 Tuple8 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip8)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip8)]
|
||||||
[[play](https://go.dev/play/p/8V9jWkuJfaQ)]
|
[[play](https://go.dev/play/p/8V9jWkuJfaQ)]
|
||||||
- **<big>Unzip8</big>** : 根据传入的Tuple8切片,创建一组和Tuple8元素相对应的切片。
|
- **<big>Unzip8</big>** : 根据传入的 Tuple8 切片,创建一组和 Tuple8 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip8)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip8)]
|
||||||
[[play](https://go.dev/play/p/1SndOwGsZB4)]
|
[[play](https://go.dev/play/p/1SndOwGsZB4)]
|
||||||
- **<big>Tuple9</big>** : 9 元元组
|
- **<big>Tuple9</big>** : 9 元元组
|
||||||
@@ -1622,7 +1676,7 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip9</big>** : 创建一个 Tuple9 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip9</big>** : 创建一个 Tuple9 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip9)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip9)]
|
||||||
[[play](https://go.dev/play/p/cgsL15QYnfz)]
|
[[play](https://go.dev/play/p/cgsL15QYnfz)]
|
||||||
- **<big>Unzip9</big>** : 根据传入的Tuple9切片,创建一组和Tuple9元素相对应的切片。
|
- **<big>Unzip9</big>** : 根据传入的 Tuple9 切片,创建一组和 Tuple9 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip9)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip9)]
|
||||||
[[play](https://go.dev/play/p/91-BU_KURSA)]
|
[[play](https://go.dev/play/p/91-BU_KURSA)]
|
||||||
- **<big>Tuple10</big>** : 10 元元组
|
- **<big>Tuple10</big>** : 10 元元组
|
||||||
@@ -1634,11 +1688,10 @@ import "github.com/duke-git/lancet/v2/tuple"
|
|||||||
- **<big>Zip10</big>** : 创建一个 Tuple10 元组切片, 其中元组的元素和传入切片元素相对应。
|
- **<big>Zip10</big>** : 创建一个 Tuple10 元组切片, 其中元组的元素和传入切片元素相对应。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip10)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Zip10)]
|
||||||
[[play](https://go.dev/play/p/YSR-2cXnrY4)]
|
[[play](https://go.dev/play/p/YSR-2cXnrY4)]
|
||||||
- **<big>Unzip10</big>** : 根据传入的Tuple10切片,创建一组和Tuple10元素相对应的切片。
|
- **<big>Unzip10</big>** : 根据传入的 Tuple10 切片,创建一组和 Tuple10 元素相对应的切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip10)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple_zh-CN.md#Unzip10)]
|
||||||
[[play](https://go.dev/play/p/-taQB6Wfre_z)]
|
[[play](https://go.dev/play/p/-taQB6Wfre_z)]
|
||||||
|
|
||||||
|
|
||||||
<h3 id="Validator"> 24. validator 验证器包,包含常用字符串格式验证函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="Validator"> 24. validator 验证器包,包含常用字符串格式验证函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
100
cryptor/basic.go
100
cryptor/basic.go
@@ -40,6 +40,14 @@ func Md5String(s string) string {
|
|||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Md5StringWithBase64 return the md5 value of string with base64.
|
||||||
|
// Play: https://go.dev/play/p/Lx4gH7Vdr5_y
|
||||||
|
func Md5StringWithBase64(s string) string {
|
||||||
|
h := md5.New()
|
||||||
|
h.Write([]byte(s))
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
// Md5Byte return the md5 string of byte slice.
|
// Md5Byte return the md5 string of byte slice.
|
||||||
// Play: https://go.dev/play/p/suraalH8lyC
|
// Play: https://go.dev/play/p/suraalH8lyC
|
||||||
func Md5Byte(data []byte) string {
|
func Md5Byte(data []byte) string {
|
||||||
@@ -48,6 +56,14 @@ func Md5Byte(data []byte) string {
|
|||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Md5ByteWithBase64 return the md5 string of byte slice with base64.
|
||||||
|
// Play: https://go.dev/play/p/CkN9hYKGeAy
|
||||||
|
func Md5ByteWithBase64(data []byte) string {
|
||||||
|
h := md5.New()
|
||||||
|
h.Write(data)
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
// Md5File return the md5 value of file.
|
// Md5File return the md5 value of file.
|
||||||
func Md5File(filename string) (string, error) {
|
func Md5File(filename string) (string, error) {
|
||||||
if fileInfo, err := os.Stat(filename); err != nil {
|
if fileInfo, err := os.Stat(filename); err != nil {
|
||||||
@@ -82,56 +98,112 @@ func Md5File(filename string) (string, error) {
|
|||||||
|
|
||||||
// HmacMd5 return the hmac hash of string use md5.
|
// HmacMd5 return the hmac hash of string use md5.
|
||||||
// Play: https://go.dev/play/p/uef0q1fz53I
|
// Play: https://go.dev/play/p/uef0q1fz53I
|
||||||
func HmacMd5(data, key string) string {
|
func HmacMd5(str, key string) string {
|
||||||
|
h := hmac.New(md5.New, []byte(key))
|
||||||
|
h.Write([]byte(str))
|
||||||
|
return hex.EncodeToString(h.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// HmacMd5WithBase64 return the hmac hash of string use md5 with base64.
|
||||||
|
// todo
|
||||||
|
func HmacMd5WithBase64(data, key string) string {
|
||||||
h := hmac.New(md5.New, []byte(key))
|
h := hmac.New(md5.New, []byte(key))
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(data))
|
||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
return base64.StdEncoding.EncodeToString(h.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// HmacSha1 return the hmac hash of string use sha1.
|
// HmacSha1 return the hmac hash of string use sha1.
|
||||||
// Play: https://go.dev/play/p/1UI4oQ4WXKM
|
// Play: https://go.dev/play/p/1UI4oQ4WXKM
|
||||||
func HmacSha1(data, key string) string {
|
func HmacSha1(str, key string) string {
|
||||||
h := hmac.New(sha1.New, []byte(key))
|
h := hmac.New(sha1.New, []byte(key))
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(str))
|
||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
return hex.EncodeToString(h.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HmacSha1WithBase64 return the hmac hash of string use sha1 with base64.
|
||||||
|
// Play: https://go.dev/play/p/47JmmGrnF7B
|
||||||
|
func HmacSha1WithBase64(str, key string) string {
|
||||||
|
h := hmac.New(sha1.New, []byte(key))
|
||||||
|
h.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
// HmacSha256 return the hmac hash of string use sha256.
|
// HmacSha256 return the hmac hash of string use sha256.
|
||||||
// Play: https://go.dev/play/p/HhpwXxFhhC0
|
// Play: https://go.dev/play/p/HhpwXxFhhC0
|
||||||
func HmacSha256(data, key string) string {
|
func HmacSha256(str, key string) string {
|
||||||
h := hmac.New(sha256.New, []byte(key))
|
h := hmac.New(sha256.New, []byte(key))
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(str))
|
||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
return hex.EncodeToString(h.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HmacSha256WithBase64 return the hmac hash of string use sha256 with base64.
|
||||||
|
// Play: https://go.dev/play/p/EKbkUvPTLwO
|
||||||
|
func HmacSha256WithBase64(str, key string) string {
|
||||||
|
h := hmac.New(sha256.New, []byte(key))
|
||||||
|
h.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
// HmacSha512 return the hmac hash of string use sha512.
|
// HmacSha512 return the hmac hash of string use sha512.
|
||||||
// Play: https://go.dev/play/p/59Od6m4A0Ud
|
// Play: https://go.dev/play/p/59Od6m4A0Ud
|
||||||
func HmacSha512(data, key string) string {
|
func HmacSha512(str, key string) string {
|
||||||
h := hmac.New(sha512.New, []byte(key))
|
h := hmac.New(sha512.New, []byte(key))
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(str))
|
||||||
return hex.EncodeToString(h.Sum([]byte("")))
|
return hex.EncodeToString(h.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HmacSha512WithBase64 return the hmac hash of string use sha512 with base64.
|
||||||
|
// Play: https://go.dev/play/p/61wBBOKO-GH
|
||||||
|
func HmacSha512WithBase64(str, key string) string {
|
||||||
|
h := hmac.New(sha512.New, []byte(key))
|
||||||
|
h.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(h.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
// Sha1 return the sha1 value (SHA-1 hash algorithm) of string.
|
// Sha1 return the sha1 value (SHA-1 hash algorithm) of string.
|
||||||
// Play: https://go.dev/play/p/_m_uoD1deMT
|
// Play: https://go.dev/play/p/_m_uoD1deMT
|
||||||
func Sha1(data string) string {
|
func Sha1(str string) string {
|
||||||
sha1 := sha1.New()
|
sha1 := sha1.New()
|
||||||
sha1.Write([]byte(data))
|
sha1.Write([]byte(str))
|
||||||
return hex.EncodeToString(sha1.Sum([]byte("")))
|
return hex.EncodeToString(sha1.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string.
|
||||||
|
// Play: todo
|
||||||
|
func Sha1WithBase64(str string) string {
|
||||||
|
sha1 := sha1.New()
|
||||||
|
sha1.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(sha1.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
// Sha256 return the sha256 value (SHA256 hash algorithm) of string.
|
// Sha256 return the sha256 value (SHA256 hash algorithm) of string.
|
||||||
// Play: https://go.dev/play/p/tU9tfBMIAr1
|
// Play: https://go.dev/play/p/tU9tfBMIAr1
|
||||||
func Sha256(data string) string {
|
func Sha256(str string) string {
|
||||||
sha256 := sha256.New()
|
sha256 := sha256.New()
|
||||||
sha256.Write([]byte(data))
|
sha256.Write([]byte(str))
|
||||||
return hex.EncodeToString(sha256.Sum([]byte("")))
|
return hex.EncodeToString(sha256.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sha256WithBase64 return the sha256 value (SHA256 hash algorithm) of base64 string.
|
||||||
|
// Play: https://go.dev/play/p/85IXJHIal1k
|
||||||
|
func Sha256WithBase64(str string) string {
|
||||||
|
sha256 := sha256.New()
|
||||||
|
sha256.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(sha256.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
// Sha512 return the sha512 value (SHA512 hash algorithm) of string.
|
// Sha512 return the sha512 value (SHA512 hash algorithm) of string.
|
||||||
// Play: https://go.dev/play/p/3WsvLYZxsHa
|
// Play: https://go.dev/play/p/3WsvLYZxsHa
|
||||||
func Sha512(data string) string {
|
func Sha512(str string) string {
|
||||||
sha512 := sha512.New()
|
sha512 := sha512.New()
|
||||||
sha512.Write([]byte(data))
|
sha512.Write([]byte(str))
|
||||||
return hex.EncodeToString(sha512.Sum([]byte("")))
|
return hex.EncodeToString(sha512.Sum([]byte("")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sha512WithBase64 return the sha512 value (SHA512 hash algorithm) of base64 string.
|
||||||
|
// Play: https://go.dev/play/p/q_fY2rA-k5I
|
||||||
|
func Sha512WithBase64(str string) string {
|
||||||
|
sha512 := sha512.New()
|
||||||
|
sha512.Write([]byte(str))
|
||||||
|
return base64.StdEncoding.EncodeToString(sha512.Sum([]byte("")))
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,13 @@ func TestMd5String(t *testing.T) {
|
|||||||
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMd5StringWithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestMd5StringWithBase64")
|
||||||
|
assert.Equal("XUFAKrxLKna5cZ2REBfFkg==", Md5StringWithBase64("hello"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMd5Byte(t *testing.T) {
|
func TestMd5Byte(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -35,6 +42,13 @@ func TestMd5Byte(t *testing.T) {
|
|||||||
assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data))
|
assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMd5ByteWithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestMd5ByteWithBase64")
|
||||||
|
assert.Equal("XUFAKrxLKna5cZ2REBfFkg==", Md5ByteWithBase64([]byte("hello")))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMd5File(t *testing.T) {
|
func TestMd5File(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -51,6 +65,13 @@ func TestHmacMd5(t *testing.T) {
|
|||||||
assert.Equal("5f4c9faaff0a1ad3007d9ddc06abe36d", HmacMd5("hello world", "12345"))
|
assert.Equal("5f4c9faaff0a1ad3007d9ddc06abe36d", HmacMd5("hello world", "12345"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacMd5WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacMd5WithBase64")
|
||||||
|
assert.Equal("6DQwbquJLYclJdSRinpjmg==", HmacMd5WithBase64("hello", "12345"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHmacSha1(t *testing.T) {
|
func TestHmacSha1(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -63,18 +84,42 @@ func TestHmacSha1(t *testing.T) {
|
|||||||
assert.Equal(expected, hmacSha1)
|
assert.Equal(expected, hmacSha1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacSha1WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
s := "hello"
|
||||||
|
key := "12345"
|
||||||
|
hmacSha1 := HmacSha1WithBase64(s, key)
|
||||||
|
expected := "XGqdsMzLkuNu0DI/0Jt/k23prOA="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacSha1")
|
||||||
|
assert.Equal(expected, hmacSha1)
|
||||||
|
}
|
||||||
|
|
||||||
func TestHmacSha256(t *testing.T) {
|
func TestHmacSha256(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
s := "hello world"
|
str := "hello world"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
hmacSha256 := HmacSha256(s, key)
|
hmacSha256 := HmacSha256(str, key)
|
||||||
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
expected := "9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8"
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestHmacSha256")
|
assert := internal.NewAssert(t, "TestHmacSha256")
|
||||||
assert.Equal(expected, hmacSha256)
|
assert.Equal(expected, hmacSha256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacSha256WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
hms := HmacSha256WithBase64(str, key)
|
||||||
|
expected := "MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacSha256WithBase64")
|
||||||
|
assert.Equal(expected, hms)
|
||||||
|
}
|
||||||
|
|
||||||
func TestHmacSha512(t *testing.T) {
|
func TestHmacSha512(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -87,6 +132,18 @@ func TestHmacSha512(t *testing.T) {
|
|||||||
assert.Equal(expected, hmacSha512)
|
assert.Equal(expected, hmacSha512)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHmacSha512WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
hms := HmacSha512WithBase64(str, key)
|
||||||
|
expected := "3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A=="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestHmacSha512WithBase64")
|
||||||
|
assert.Equal(expected, hms)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha1(t *testing.T) {
|
func TestSha1(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -98,6 +155,16 @@ func TestSha1(t *testing.T) {
|
|||||||
assert.Equal(expected, sha1)
|
assert.Equal(expected, sha1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha1WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha1WithBase64("hello")
|
||||||
|
expected := "qvTGHdzF6KLavt4PO0gs2a6pQ00="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha1WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha256(t *testing.T) {
|
func TestSha256(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -109,6 +176,16 @@ func TestSha256(t *testing.T) {
|
|||||||
assert.Equal(expected, sha256)
|
assert.Equal(expected, sha256)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha256WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha256WithBase64("hello")
|
||||||
|
expected := "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha256WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha512(t *testing.T) {
|
func TestSha512(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -119,3 +196,13 @@ func TestSha512(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSha512")
|
assert := internal.NewAssert(t, "TestSha512")
|
||||||
assert.Equal(expected, sha512)
|
assert.Equal(expected, sha512)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSha512WithBase64(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
str := Sha512WithBase64("hello")
|
||||||
|
expected := "m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw=="
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestSha512WithBase64")
|
||||||
|
assert.Equal(expected, str)
|
||||||
|
}
|
||||||
|
|||||||
@@ -322,49 +322,89 @@ func ExampleHmacMd5() {
|
|||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := HmacMd5(str, key)
|
hms := HmacMd5(str, key)
|
||||||
|
|
||||||
fmt.Println(hms)
|
fmt.Println(hms)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// e834306eab892d872525d4918a7a639a
|
// e834306eab892d872525d4918a7a639a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleHmacMd5WithBase64() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := HmacMd5WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 6DQwbquJLYclJdSRinpjmg==
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleHmacSha1() {
|
func ExampleHmacSha1() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := HmacSha1(str, key)
|
hms := HmacSha1(str, key)
|
||||||
|
|
||||||
fmt.Println(hms)
|
fmt.Println(hms)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleHmacSha1WithBase64() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := HmacSha1WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XGqdsMzLkuNu0DI/0Jt/k23prOA=
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleHmacSha256() {
|
func ExampleHmacSha256() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := HmacSha256(str, key)
|
hms := HmacSha256(str, key)
|
||||||
|
|
||||||
fmt.Println(hms)
|
fmt.Println(hms)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
|
// 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleHmacSha256WithBase64() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := HmacSha256WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleHmacSha512() {
|
func ExampleHmacSha512() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := HmacSha512(str, key)
|
hms := HmacSha512(str, key)
|
||||||
|
|
||||||
fmt.Println(hms)
|
fmt.Println(hms)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
|
// dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleHmacSha512WithBase64() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := HmacSha512WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleMd5String() {
|
func ExampleMd5String() {
|
||||||
md5Str := Md5String("hello")
|
md5Str := Md5String("hello")
|
||||||
fmt.Println(md5Str)
|
fmt.Println(md5Str)
|
||||||
@@ -373,6 +413,14 @@ func ExampleMd5String() {
|
|||||||
// 5d41402abc4b2a76b9719d911017c592
|
// 5d41402abc4b2a76b9719d911017c592
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleMd5StringWithBase64() {
|
||||||
|
md5Str := Md5StringWithBase64("hello")
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleMd5Byte() {
|
func ExampleMd5Byte() {
|
||||||
md5Str := Md5Byte([]byte{'a'})
|
md5Str := Md5Byte([]byte{'a'})
|
||||||
fmt.Println(md5Str)
|
fmt.Println(md5Str)
|
||||||
@@ -380,6 +428,15 @@ func ExampleMd5Byte() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 0cc175b9c0f1b6a831c399e269772661
|
// 0cc175b9c0f1b6a831c399e269772661
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleMd5ByteWithBase64() {
|
||||||
|
md5Str := Md5ByteWithBase64([]byte("hello"))
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleSha1() {
|
func ExampleSha1() {
|
||||||
result := Sha1("hello")
|
result := Sha1("hello")
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
@@ -388,6 +445,14 @@ func ExampleSha1() {
|
|||||||
// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
|
// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleSha1WithBase64() {
|
||||||
|
result := Sha1WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// qvTGHdzF6KLavt4PO0gs2a6pQ00=
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleSha256() {
|
func ExampleSha256() {
|
||||||
result := Sha256("hello")
|
result := Sha256("hello")
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
@@ -396,6 +461,14 @@ func ExampleSha256() {
|
|||||||
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
|
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleSha256WithBase64() {
|
||||||
|
result := Sha256WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleSha512() {
|
func ExampleSha512() {
|
||||||
result := Sha512("hello")
|
result := Sha512("hello")
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
@@ -403,3 +476,11 @@ func ExampleSha512() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
|
// 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleSha512WithBase64() {
|
||||||
|
result := Sha512WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// Package datetime implements some functions to format date and time.
|
// Package datetime implements some functions to format date and time.
|
||||||
// Note:
|
// Note:
|
||||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
// 1. `format` param in FormatTimeToStr function should be as flow (case no sensitive):
|
||||||
// "yyyy-mm-dd hh:mm:ss"
|
// "yyyy-mm-dd hh:mm:ss"
|
||||||
// "yyyy-mm-dd hh:mm"
|
// "yyyy-mm-dd hh:mm"
|
||||||
// "yyyy-mm-dd hh"
|
// "yyyy-mm-dd hh"
|
||||||
@@ -18,14 +18,19 @@
|
|||||||
// "yyyy/mm"
|
// "yyyy/mm"
|
||||||
// "mm/dd"
|
// "mm/dd"
|
||||||
// "dd/mm/yy hh:mm:ss"
|
// "dd/mm/yy hh:mm:ss"
|
||||||
|
// "yyyymmdd"
|
||||||
|
// "mmddyy"
|
||||||
// "yyyy"
|
// "yyyy"
|
||||||
|
// "yy"
|
||||||
// "mm"
|
// "mm"
|
||||||
// "hh:mm:ss"
|
// "hh:mm:ss"
|
||||||
|
// "hh:mm"
|
||||||
// "mm:ss"
|
// "mm:ss"
|
||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,9 +52,13 @@ func init() {
|
|||||||
"yyyy/mm": "2006/01",
|
"yyyy/mm": "2006/01",
|
||||||
"mm/dd": "01/02",
|
"mm/dd": "01/02",
|
||||||
"dd/mm/yy hh:mm:ss": "02/01/06 15:04:05",
|
"dd/mm/yy hh:mm:ss": "02/01/06 15:04:05",
|
||||||
|
"yyyymmdd": "20060102",
|
||||||
|
"mmddyy": "010206",
|
||||||
"yyyy": "2006",
|
"yyyy": "2006",
|
||||||
|
"yy": "06",
|
||||||
"mm": "01",
|
"mm": "01",
|
||||||
"hh:mm:ss": "15:04:05",
|
"hh:mm:ss": "15:04:05",
|
||||||
|
"hh:mm": "15:04",
|
||||||
"mm:ss": "04:05",
|
"mm:ss": "04:05",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,19 +133,40 @@ func GetNightTimestamp() int64 {
|
|||||||
|
|
||||||
// FormatTimeToStr convert time to string.
|
// FormatTimeToStr convert time to string.
|
||||||
// Play: https://go.dev/play/p/_Ia7M8H_OvE
|
// Play: https://go.dev/play/p/_Ia7M8H_OvE
|
||||||
func FormatTimeToStr(t time.Time, format string) string {
|
func FormatTimeToStr(t time.Time, format string, timezone ...string) string {
|
||||||
return t.Format(timeFormat[format])
|
tf, ok := timeFormat[strings.ToLower(format)]
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return t.In(loc).Format(tf)
|
||||||
|
}
|
||||||
|
return t.Format(tf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatStrToTime convert string to time.
|
// FormatStrToTime convert string to time.
|
||||||
// Play: https://go.dev/play/p/1h9FwdU8ql4
|
// Play: https://go.dev/play/p/1h9FwdU8ql4
|
||||||
func FormatStrToTime(str, format string) (time.Time, error) {
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) {
|
||||||
v, ok := timeFormat[format]
|
tf, ok := timeFormat[strings.ToLower(format)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return time.Time{}, fmt.Errorf("format %s not found", format)
|
return time.Time{}, fmt.Errorf("format %s not support", format)
|
||||||
}
|
}
|
||||||
|
|
||||||
return time.Parse(v, str)
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.ParseInLocation(tf, str, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Parse(tf, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfMinute return beginning minute time of day.
|
// BeginOfMinute return beginning minute time of day.
|
||||||
@@ -262,6 +292,92 @@ func DayOfYear(t time.Time) int {
|
|||||||
|
|
||||||
// IsWeekend checks if passed time is weekend or not.
|
// IsWeekend checks if passed time is weekend or not.
|
||||||
// Play: https://go.dev/play/p/cupRM5aZOIY
|
// Play: https://go.dev/play/p/cupRM5aZOIY
|
||||||
|
// Deprecated Use '== Weekday' instead
|
||||||
func IsWeekend(t time.Time) bool {
|
func IsWeekend(t time.Time) bool {
|
||||||
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
|
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NowDateOrTime return current datetime with specific format and timezone.
|
||||||
|
// Play: todo
|
||||||
|
func NowDateOrTime(format string, timezone ...string) string {
|
||||||
|
tf, ok := timeFormat[strings.ToLower(format)]
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Now().In(loc).Format(tf)
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Now().Format(tf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timestamp return current second timestamp.
|
||||||
|
// Play: todo
|
||||||
|
func Timestamp(timezone ...string) int64 {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
t = t.In(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimestampMilli return current mill second timestamp.
|
||||||
|
// Play: todo
|
||||||
|
func TimestampMilli(timezone ...string) int64 {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
t = t.In(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(time.Nanosecond) * t.UnixNano() / int64(time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimestampMicro return current micro second timestamp.
|
||||||
|
// Play: todo
|
||||||
|
func TimestampMicro(timezone ...string) int64 {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
t = t.In(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(time.Nanosecond) * t.UnixNano() / int64(time.Microsecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimestampNano return current nano second timestamp.
|
||||||
|
// Play: todo
|
||||||
|
func TimestampNano(timezone ...string) int64 {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
if timezone != nil && timezone[0] != "" {
|
||||||
|
loc, err := time.LoadLocation(timezone[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
t = t.In(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.UnixNano()
|
||||||
|
}
|
||||||
|
|||||||
@@ -147,6 +147,9 @@ func TestFormatTimeToStr(t *testing.T) {
|
|||||||
actual := FormatTimeToStr(datetime, cases[i])
|
actual := FormatTimeToStr(datetime, cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ds := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
t.Log(ds)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormatStrToTime(t *testing.T) {
|
func TestFormatStrToTime(t *testing.T) {
|
||||||
@@ -163,19 +166,23 @@ func TestFormatStrToTime(t *testing.T) {
|
|||||||
"dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss",
|
"dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss",
|
||||||
"yyyy/mm"}
|
"yyyy/mm"}
|
||||||
|
|
||||||
datetimeStr := []string{
|
expected := []string{
|
||||||
"2021-01-02 16:04:08", "2021-01-02",
|
"2021-01-02 16:04:08", "2021-01-02",
|
||||||
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
||||||
"2021/01"}
|
"2021/01"}
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, err := FormatStrToTime(datetimeStr[i], cases[i])
|
actual, err := FormatStrToTime(expected[i], cases[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
expected, _ := time.Parse(formats[i], datetimeStr[i])
|
expected, _ := time.Parse(formats[i], expected[i])
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
estTime, err := FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
t.Log(estTime)
|
||||||
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBeginOfMinute(t *testing.T) {
|
func TestBeginOfMinute(t *testing.T) {
|
||||||
@@ -368,3 +375,38 @@ func TestIsWeekend(t *testing.T) {
|
|||||||
result2 := IsWeekend(date2)
|
result2 := IsWeekend(date2)
|
||||||
assert.Equal(false, result2)
|
assert.Equal(false, result2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNowDateOrTime(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
formats := []string{
|
||||||
|
"yyyy-mm-dd hh:mm:ss",
|
||||||
|
"yyyy-mm-dd",
|
||||||
|
"dd-mm-yy hh:mm:ss",
|
||||||
|
"yyyy/mm/dd hh:mm:ss",
|
||||||
|
"hh:mm:ss",
|
||||||
|
"yyyy/mm",
|
||||||
|
"yyyy-mm-dd hh",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(formats); i++ {
|
||||||
|
result := NowDateOrTime(formats[i], "UTC")
|
||||||
|
t.Log(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimestamp(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ts1 := Timestamp()
|
||||||
|
t.Log(ts1)
|
||||||
|
|
||||||
|
ts2 := TimestampMilli()
|
||||||
|
t.Log(ts2)
|
||||||
|
|
||||||
|
ts3 := TimestampMicro()
|
||||||
|
t.Log(ts3)
|
||||||
|
|
||||||
|
ts4 := TimestampNano()
|
||||||
|
t.Log(ts4)
|
||||||
|
}
|
||||||
|
|||||||
302
docs/cryptor.md
302
docs/cryptor.md
@@ -6,7 +6,7 @@ Package cryptor contains some functions for data encryption and decryption. Supp
|
|||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go](https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go)
|
- [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -44,15 +44,24 @@ import (
|
|||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
|
- [HmacMd5WithBase64](#HmacMd5WithBase64)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
|
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
|
- [HmacSha256WithBase64](#HmacSha256WithBase64)
|
||||||
- [HmacSha512](#HmacSha512)
|
- [HmacSha512](#HmacSha512)
|
||||||
|
- [HmacSha512WithBase64](#HmacSha512WithBase64)
|
||||||
- [Md5String](#Md5String)
|
- [Md5String](#Md5String)
|
||||||
|
- [Md5StringWithBase64](#Md5StringWithBase64)
|
||||||
- [Md5Byte](#Md5Byte)
|
- [Md5Byte](#Md5Byte)
|
||||||
|
- [Md5ByteWithBase64](#Md5ByteWithBase64)
|
||||||
- [Md5File](#Md5File)
|
- [Md5File](#Md5File)
|
||||||
- [Sha1](#Sha1)
|
- [Sha1](#Sha1)
|
||||||
|
- [Sha1WithBase64](#Sha1WithBase64)
|
||||||
- [Sha256](#Sha256)
|
- [Sha256](#Sha256)
|
||||||
|
- [Sha256WithBase64](#Sha256WithBase64)
|
||||||
- [Sha512](#Sha512)
|
- [Sha512](#Sha512)
|
||||||
|
- [Sha512WithBase64](#Sha512WithBase64)
|
||||||
- [GenerateRsaKey](#GenerateRsaKey)
|
- [GenerateRsaKey](#GenerateRsaKey)
|
||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
@@ -730,7 +739,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacMd5(data, key string) string
|
func HmacMd5(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -744,7 +753,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := cryptor.HmacMd5(str, key)
|
hms := cryptor.HmacMd5(str, key)
|
||||||
@@ -754,6 +763,39 @@ func main() {
|
|||||||
// e834306eab892d872525d4918a7a639a
|
// e834306eab892d872525d4918a7a639a
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacMd5WithBase64">HmacMd5WithBase64</span>
|
||||||
|
|
||||||
|
<p>Get the md5 hmac hash of base64 string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacMd5WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacMd5WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 6DQwbquJLYclJdSRinpjmg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 hmac hash of string.</p>
|
<p>Get the sha1 hmac hash of string.</p>
|
||||||
@@ -761,7 +803,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha1(data, key string) string
|
func HmacSha1(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -785,6 +827,39 @@ func main() {
|
|||||||
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha1WithBase64">HmacSha1WithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the hmac hash of string use sha1 with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha1WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha1WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XGqdsMzLkuNu0DI/0Jt/k23prOA=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 hmac hash of string</p>
|
<p>Get the sha256 hmac hash of string</p>
|
||||||
@@ -792,7 +867,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha256(data, key string) string
|
func HmacSha256(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -817,6 +892,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha256WithBase64">HmacSha256WithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the hmac hash of string use sha256 with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha256WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha256WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 hmac hash of string.</p>
|
<p>Get the sha512 hmac hash of string.</p>
|
||||||
@@ -824,7 +931,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha512(data, key string) string
|
func HmacSha512(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -849,6 +956,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha512WithBase64">HmacSha512WithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the hmac hash of string use sha512 with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha512WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha512WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
@@ -881,6 +1020,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Md5StringWithBase64">Md5StringWithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the md5 value of string with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Md5StringWithBase64(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
md5Str := cryptor.Md5StringWithBase64("hello")
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Md5Byte">Md5Byte</span>
|
### <span id="Md5Byte">Md5Byte</span>
|
||||||
|
|
||||||
<p>Return the md5 string of byte slice.</p>
|
<p>Return the md5 string of byte slice.</p>
|
||||||
@@ -910,6 +1078,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Md5ByteWithBase64">Md5ByteWithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the md5 string of byte slice with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Md5ByteWithBase64(data []byte) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
md5Str := cryptor.Md5ByteWithBase64([]byte("hello"))
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>Get the md5 value of file.</p>
|
<p>Get the md5 value of file.</p>
|
||||||
@@ -943,7 +1140,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha1(data string) string
|
func Sha1(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -967,6 +1164,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha1WithBase64">Sha1WithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the sha1 value (SHA-1 hash algorithm) of base64 string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha1WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha1WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// qvTGHdzF6KLavt4PO0gs2a6pQ00=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 value of string.</p>
|
<p>Get the sha256 value of string.</p>
|
||||||
@@ -974,7 +1200,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha256(data string) string
|
func Sha256(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -998,6 +1224,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha256WithBase64">Sha256WithBase64</span>
|
||||||
|
|
||||||
|
<p>Return the sha256 value (SHA256 hash algorithm) of base64 string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha256WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha256WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 value of string.</p>
|
<p>Get the sha512 value of string.</p>
|
||||||
@@ -1005,7 +1260,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha512(data string) string
|
func Sha512(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -1029,6 +1284,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha512WithBase64">Sha512WithBase64</span>
|
||||||
|
|
||||||
|
<p>Get the sha512 value of string with base64.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha512WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha512WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>Create the rsa public and private key file in current directory.</p>
|
<p>Create the rsa public and private key file in current directory.</p>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64
|
|||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go](https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go)
|
- [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -41,15 +41,24 @@ import (
|
|||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
|
- [HmacMd5WithBase64](#HmacMd5WithBase64)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
|
- [HmacSha1WithBase64](#HmacSha1WithBase64)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
|
- [HmacSha256WithBase64](#HmacSha256WithBase64)
|
||||||
- [HmacSha512](#HmacSha512)
|
- [HmacSha512](#HmacSha512)
|
||||||
|
- [HmacSha512WithBase64](#HmacSha512WithBase64)
|
||||||
- [Md5String](#Md5String)
|
- [Md5String](#Md5String)
|
||||||
|
- [Md5StringWithBase64](#Md5StringWithBase64)
|
||||||
- [Md5Byte](#Md5Byte)
|
- [Md5Byte](#Md5Byte)
|
||||||
|
- [Md5ByteWithBase64](#Md5ByteWithBase64)
|
||||||
- [Md5File](#Md5File)
|
- [Md5File](#Md5File)
|
||||||
- [Sha1](#Sha1)
|
- [Sha1](#Sha1)
|
||||||
|
- [Sha1WithBase64](#Sha1WithBase64)
|
||||||
- [Sha256](#Sha256)
|
- [Sha256](#Sha256)
|
||||||
|
- [Sha256WithBase64](#Sha256WithBase64)
|
||||||
- [Sha512](#Sha512)
|
- [Sha512](#Sha512)
|
||||||
|
- [Sha512WithBase64](#Sha512WithBase64)
|
||||||
- [GenerateRsaKey](#GenerateRsaKey)
|
- [GenerateRsaKey](#GenerateRsaKey)
|
||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
@@ -729,7 +738,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacMd5(data, key string) string
|
func HmacMd5(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -743,7 +752,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := "hello"
|
str := "hello"
|
||||||
key := "12345"
|
key := "12345"
|
||||||
|
|
||||||
hms := cryptor.HmacMd5(str, key)
|
hms := cryptor.HmacMd5(str, key)
|
||||||
@@ -753,14 +762,46 @@ func main() {
|
|||||||
// e834306eab892d872525d4918a7a639a
|
// e834306eab892d872525d4918a7a639a
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
|
||||||
|
|
||||||
<p>获取字符串sha1 hmac值。</p>
|
### <span id="HmacMd5WithBase64">HmacMd5WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串md5 hmac base64字符串值。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha1(data, key string) string
|
func HmacMd5WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacMd5WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 6DQwbquJLYclJdSRinpjmg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
|
<p>获取字符串的sha1 hmac值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha1(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -784,6 +825,40 @@ func main() {
|
|||||||
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha1WithBase64">HmacSha1WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串的sha1 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha1WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha1WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XGqdsMzLkuNu0DI/0Jt/k23prOA=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256 hmac值。</p>
|
<p>获取字符串sha256 hmac值。</p>
|
||||||
@@ -791,7 +866,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha256(data, key string) string
|
func HmacSha256(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -816,6 +891,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha256WithBase64">HmacSha256WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串sha256 hmac base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha256WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha256WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512 hmac值。</p>
|
<p>获取字符串sha512 hmac值。</p>
|
||||||
@@ -823,7 +930,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HmacSha512(data, key string) string
|
func HmacSha512(str, key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -848,6 +955,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="HmacSha512WithBase64">HmacSha512WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串sha512 hmac base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HmacSha512WithBase64(str, key string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello"
|
||||||
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha512WithBase64(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
@@ -856,7 +995,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Md5String(s string) string
|
func Md5String(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -880,9 +1019,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Md5StringWithBase64">Md5StringWithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串md5 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Md5StringWithBase64(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
md5Str := cryptor.Md5StringWithBase64("hello")
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Md5Byte">Md5Byte</span>
|
### <span id="Md5Byte">Md5Byte</span>
|
||||||
|
|
||||||
<p>获取byte slice的md5至。</p>
|
<p>获取byte slice的md5值。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -909,6 +1077,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Md5ByteWithBase64">Md5ByteWithBase64</span>
|
||||||
|
|
||||||
|
<p>获取byte slice的md5 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Md5ByteWithBase64(data []byte) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
md5Str := cryptor.Md5ByteWithBase64([]byte("hello"))
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// XUFAKrxLKna5cZ2REBfFkg==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>获取文件md5值。</p>
|
<p>获取文件md5值。</p>
|
||||||
@@ -942,7 +1139,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha1(data string) string
|
func Sha1(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -966,6 +1163,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha1WithBase64">Sha1WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串sha1 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha1WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha1WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// qvTGHdzF6KLavt4PO0gs2a6pQ00=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256值。</p>
|
<p>获取字符串sha256值。</p>
|
||||||
@@ -973,7 +1199,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha256(data string) string
|
func Sha256(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -997,6 +1223,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha256WithBase64">Sha256WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串sha256 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha256WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha256WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512值。</p>
|
<p>获取字符串sha512值。</p>
|
||||||
@@ -1004,7 +1259,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Sha512(data string) string
|
func Sha512(str string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -1028,6 +1283,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Sha512WithBase64">Sha512WithBase64</span>
|
||||||
|
|
||||||
|
<p>获取字符串sha512 base64值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha512WithBase64(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/cryptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := cryptor.Sha512WithBase64("hello")
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>在当前目录下创建rsa私钥文件和公钥文件。</p>
|
<p>在当前目录下创建rsa私钥文件和公钥文件。</p>
|
||||||
|
|||||||
179
docs/datetime.md
179
docs/datetime.md
@@ -59,7 +59,12 @@ import (
|
|||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
- [DayOfYear](#DayOfYear)
|
- [DayOfYear](#DayOfYear)
|
||||||
- [IsWeekend](#IsWeekend)
|
- [IsWeekend<sup>deprecated</sup>](#IsWeekend)
|
||||||
|
- [NowDateOrTime](#NowDateOrTime)
|
||||||
|
- [Timestamp](#Timestamp)
|
||||||
|
- [TimestampMilli](#TimestampMilli)
|
||||||
|
- [TimestampMicro](#TimestampMicro)
|
||||||
|
- [TimestampNano](#TimestampNano)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -67,7 +72,7 @@ import (
|
|||||||
|
|
||||||
## Note:
|
## Note:
|
||||||
|
|
||||||
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
|
1. In below functions, the `format` string param should be one of flows value (case no sensitive):
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
@@ -78,14 +83,18 @@ import (
|
|||||||
- dd-mm-yy hh:mm:ss
|
- dd-mm-yy hh:mm:ss
|
||||||
- yyyy/mm/dd hh:mm:ss
|
- yyyy/mm/dd hh:mm:ss
|
||||||
- yyyy/mm/dd hh:mm
|
- yyyy/mm/dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy/mm/dd hh
|
||||||
- yyyy/mm/dd
|
- yyyy/mm/dd
|
||||||
- yyyy/mm
|
- yyyy/mm
|
||||||
- mm/dd
|
- mm/dd
|
||||||
- dd/mm/yy hh:mm:ss
|
- dd/mm/yy hh:mm:ss
|
||||||
|
- yyyymmdd
|
||||||
|
- mmddyy
|
||||||
- yyyy
|
- yyyy
|
||||||
|
- yy
|
||||||
- mm
|
- mm
|
||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
|
- hh:mm
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
@@ -838,7 +847,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string, timezone ...string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -877,7 +886,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -1185,7 +1194,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BetweenSeconds">BetweenSeconds</span>
|
### <span id="BetweenSeconds">BetweenSeconds</span>
|
||||||
|
|
||||||
<p>Return the number of seconds between two times.</p>
|
<p>Return the number of seconds between two times.</p>
|
||||||
@@ -1223,7 +1231,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="DayOfYear">DayOfYear</span>
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
<p>Returns which day of the year the parameter date `t` is.</p>
|
<p>Returns which day of the year the parameter date `t` is.</p>
|
||||||
@@ -1265,8 +1272,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend(Deprecated, Use '== Weekday' instead)</span>
|
||||||
### <span id="IsWeekend">IsWeekend</span>
|
|
||||||
|
|
||||||
<p>Checks if passed time is weekend or not.</p>
|
<p>Checks if passed time is weekend or not.</p>
|
||||||
|
|
||||||
@@ -1305,3 +1311,158 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="NowDateOrTime">NowDateOrTime</span>
|
||||||
|
|
||||||
|
<p>Return current datetime with specific format and timezone.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NowDateOrTime(format string, timezone ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
||||||
|
|
||||||
|
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-07-26 15:01:30
|
||||||
|
// 2023-07-26 02:01:30
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Timestamp">Timestamp</span>
|
||||||
|
|
||||||
|
<p>Return current second timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Timestamp(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.Timestamp()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="TimestampMilli">TimestampMilli</span>
|
||||||
|
|
||||||
|
<p>Return current mill second timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampMilli(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampMilli()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TimestampMicro">TimestampMicro</span>
|
||||||
|
|
||||||
|
<p>Return current micro second timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampMicro(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampMicro()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331784
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TimestampNano">TimestampNano</span>
|
||||||
|
|
||||||
|
<p>Return current nano second timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampNano(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampNano()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331788000
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -58,8 +58,12 @@ import (
|
|||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
- [DayOfYear](#DayOfYear)
|
- [DayOfYear](#DayOfYear)
|
||||||
- [IsWeekend](#IsWeekend)
|
- [IsWeekend<sup>deprecated</sup>](#IsWeekend)
|
||||||
|
- [NowDateOrTime](#NowDateOrTime)
|
||||||
|
- [Timestamp](#Timestamp)
|
||||||
|
- [TimestampMilli](#TimestampMilli)
|
||||||
|
- [TimestampMicro](#TimestampMicro)
|
||||||
|
- [TimestampNano](#TimestampNano)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -67,7 +71,7 @@ import (
|
|||||||
|
|
||||||
## 注:
|
## 注:
|
||||||
|
|
||||||
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
1. 函数中`format`参数值需要传以下值之一 (忽略大小写):
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
@@ -78,14 +82,18 @@ import (
|
|||||||
- dd-mm-yy hh:mm:ss
|
- dd-mm-yy hh:mm:ss
|
||||||
- yyyy/mm/dd hh:mm:ss
|
- yyyy/mm/dd hh:mm:ss
|
||||||
- yyyy/mm/dd hh:mm
|
- yyyy/mm/dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy/mm/dd hh
|
||||||
- yyyy/mm/dd
|
- yyyy/mm/dd
|
||||||
- yyyy/mm
|
- yyyy/mm
|
||||||
- mm/dd
|
- mm/dd
|
||||||
- dd/mm/yy hh:mm:ss
|
- dd/mm/yy hh:mm:ss
|
||||||
|
- yyyymmdd
|
||||||
|
- mmddyy
|
||||||
- yyyy
|
- yyyy
|
||||||
|
- yy
|
||||||
- mm
|
- mm
|
||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
|
- hh:mm
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
@@ -838,7 +846,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string, timezone ...string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -877,7 +885,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -1263,7 +1271,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsWeekend">IsWeekend</span>
|
### <span id="IsWeekend">IsWeekend(已废弃, 使用 '== Weekday')</span>
|
||||||
|
|
||||||
<p>判断日期是否是周末。</p>
|
<p>判断日期是否是周末。</p>
|
||||||
|
|
||||||
@@ -1302,3 +1310,158 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="NowDateOrTime">NowDateOrTime</span>
|
||||||
|
|
||||||
|
<p>根据指定的格式和时区返回当前时间字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NowDateOrTime(format string, timezone ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
||||||
|
|
||||||
|
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-07-26 15:01:30
|
||||||
|
// 2023-07-26 02:01:30
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Timestamp">Timestamp</span>
|
||||||
|
|
||||||
|
<p>返回当前秒级时间戳。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Timestamp(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.Timestamp()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="TimestampMilli">TimestampMilli</span>
|
||||||
|
|
||||||
|
<p>返回当前毫秒级时间戳。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampMilli(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampMilli()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TimestampMicro">TimestampMicro</span>
|
||||||
|
|
||||||
|
<p>返回当前微秒级时间戳。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampMicro(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampMicro()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331784
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TimestampNano">TimestampNano</span>
|
||||||
|
|
||||||
|
<p>返回当前纳秒级时间戳。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TimestampNano(timezone ...string) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ts := datetime.TimestampNano()
|
||||||
|
|
||||||
|
fmt.Println(ts)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1690363051331788000
|
||||||
|
}
|
||||||
|
```
|
||||||
362
docs/maputil.md
362
docs/maputil.md
@@ -44,12 +44,19 @@ import (
|
|||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
- [IsDisjoint](#IsDisjoint)
|
- [IsDisjoint](#IsDisjoint)
|
||||||
- [HasKey](#HasKey)
|
- [HasKey](#HasKey)
|
||||||
|
- [NewConcurrentMap](#NewConcurrentMap)
|
||||||
|
- [ConcurrentMap_Get](#ConcurrentMap_Get)
|
||||||
|
- [ConcurrentMap_Set](#ConcurrentMap_Set)
|
||||||
|
- [ConcurrentMap_GetOrSet](#ConcurrentMap_GetOrSet)
|
||||||
|
- [ConcurrentMap_Delete](#ConcurrentMap_Delete)
|
||||||
|
- [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete)
|
||||||
|
- [ConcurrentMap_Has](#ConcurrentMap_Has)
|
||||||
|
- [ConcurrentMap_Range](#ConcurrentMap_Range)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="MapTo">MapTo</span>
|
### <span id="MapTo">MapTo</span>
|
||||||
|
|
||||||
<p>Rry to map any interface to struct or base type.</p>
|
<p>Rry to map any interface to struct or base type.</p>
|
||||||
@@ -984,3 +991,356 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="NewConcurrentMap">NewConcurrentMap</span>
|
||||||
|
|
||||||
|
<p>ConcurrentMap is like map, but is safe for concurrent use by multiple goroutines.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
// NewConcurrentMap create a ConcurrentMap with specific shard count.
|
||||||
|
func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// create a ConcurrentMap whose key type is string, value type is int
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Set">ConcurrentMap_Set</span>
|
||||||
|
|
||||||
|
<p>Set the value for a key.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Set(key K, value V)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 true
|
||||||
|
// 3 true
|
||||||
|
// 2 true
|
||||||
|
// 0 true
|
||||||
|
// 4 true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Get">ConcurrentMap_Get</span>
|
||||||
|
|
||||||
|
<p>Get the value stored in the map for a key, or nil if no.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 true
|
||||||
|
// 3 true
|
||||||
|
// 2 true
|
||||||
|
// 0 true
|
||||||
|
// 4 true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_GetOrSet">ConcurrentMap_GetOrSet</span>
|
||||||
|
|
||||||
|
<p>Returns the existing value for the key if present. Otherwise, it sets and returns the given value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetOrSet(fmt.Sprintf("%d", n), n)
|
||||||
|
fmt.Println(val, ok)
|
||||||
|
wg.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 false
|
||||||
|
// 3 false
|
||||||
|
// 2 false
|
||||||
|
// 0 false
|
||||||
|
// 4 false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Delete">ConcurrentMap_Delete</span>
|
||||||
|
|
||||||
|
<p>Delete the value for a key.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Delete(key K)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Delete(fmt.Sprintf("%d", n))
|
||||||
|
wg2.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span>
|
||||||
|
|
||||||
|
<p>Returns the existing value for the key if present and then delete the value for the key. Otherwise, do nothing, just return false.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(val, ok) //n, true
|
||||||
|
|
||||||
|
_, ok = cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(val, ok) //false
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span>
|
||||||
|
|
||||||
|
<p>Checks if map has the value for a key.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Has(key K) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
ok := cm.Has(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Range">ConcurrentMap_Range</span>
|
||||||
|
|
||||||
|
<p>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.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
cm.Range(func(key string, value int) bool {
|
||||||
|
fmt.Println(value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ import (
|
|||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
- [IsDisjoint](#IsDisjoint)
|
- [IsDisjoint](#IsDisjoint)
|
||||||
- [HasKey](#HasKey)
|
- [HasKey](#HasKey)
|
||||||
|
- [NewConcurrentMap](#NewConcurrentMap)
|
||||||
|
- [ConcurrentMap_Get](#ConcurrentMap_Get)
|
||||||
|
- [ConcurrentMap_Set](#ConcurrentMap_Set)
|
||||||
|
- [ConcurrentMap_GetOrSet](#ConcurrentMap_GetOrSet)
|
||||||
|
- [ConcurrentMap_Delete](#ConcurrentMap_Delete)
|
||||||
|
- [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete)
|
||||||
|
- [ConcurrentMap_Has](#ConcurrentMap_Has)
|
||||||
|
- [ConcurrentMap_Range](#ConcurrentMap_Range)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -979,3 +988,357 @@ func main() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="NewConcurrentMap">NewConcurrentMap</span>
|
||||||
|
|
||||||
|
<p>ConcurrentMap协程安全的map结构。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
// NewConcurrentMap create a ConcurrentMap with specific shard count.
|
||||||
|
func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// create a ConcurrentMap whose key type is string, value type is int
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Set">ConcurrentMap_Set</span>
|
||||||
|
|
||||||
|
<p>在map中设置key和value。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Set(key K, value V)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 true
|
||||||
|
// 3 true
|
||||||
|
// 2 true
|
||||||
|
// 0 true
|
||||||
|
// 4 true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Get">ConcurrentMap_Get</span>
|
||||||
|
|
||||||
|
<p>根据key获取value, 如果不存在key,返回零值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 true
|
||||||
|
// 3 true
|
||||||
|
// 2 true
|
||||||
|
// 0 true
|
||||||
|
// 4 true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_GetOrSet">ConcurrentMap_GetOrSet</span>
|
||||||
|
|
||||||
|
<p>返回键的现有值(如果存在),否则,设置key并返回给定值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetOrSet(fmt.Sprintf("%d", n), n)
|
||||||
|
fmt.Println(val, ok)
|
||||||
|
wg.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// output: (order may change)
|
||||||
|
// 1 false
|
||||||
|
// 3 false
|
||||||
|
// 2 false
|
||||||
|
// 0 false
|
||||||
|
// 4 false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Delete">ConcurrentMap_Delete</span>
|
||||||
|
|
||||||
|
<p>删除key。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Delete(key K)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Delete(fmt.Sprintf("%d", n))
|
||||||
|
wg2.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span>
|
||||||
|
|
||||||
|
<p>获取key,然后删除。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(val, ok) //n, true
|
||||||
|
|
||||||
|
_, ok = cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(val, ok) //false
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span>
|
||||||
|
|
||||||
|
<p>验证是否包含key。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Has(key K) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
ok := cm.Has(fmt.Sprintf("%d", n))
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ConcurrentMap_Range">ConcurrentMap_Range</span>
|
||||||
|
|
||||||
|
<p>为map中每个键和值顺序调用迭代器。 如果iterator返回false,则停止迭代。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>实例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cm := maputil.NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
|
||||||
|
cm.Range(func(key string, value int) bool {
|
||||||
|
fmt.Println(value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import (
|
|||||||
|
|
||||||
- [Of](#Of)
|
- [Of](#Of)
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
|
- [UnwarpOr](#UnwarpOr)
|
||||||
|
- [UnwarpOrDefault](#UnwarpOrDefault)
|
||||||
- [ExtractPointer](#ExtractPointer)
|
- [ExtractPointer](#ExtractPointer)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -100,6 +102,97 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UnwarpOr">UnwarpOr</span>
|
||||||
|
|
||||||
|
<p>Returns the value from the pointer or fallback if the pointer is nil.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
```go
|
||||||
|
UnwarpOr[T any](p *T, fallback T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := pointer.UnwarpOr(&a, 456)
|
||||||
|
result2 := pointer.UnwarpOr(&b, "abc")
|
||||||
|
result3 := pointer.UnwarpOr(c, 456)
|
||||||
|
result4 := pointer.UnwarpOr(d, "def")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 456
|
||||||
|
// def
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UnwarpOrDefault">UnwarpOrDefault</span>
|
||||||
|
|
||||||
|
<p>Returns the value from the pointer or the default value if the pointer is nil.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
```go
|
||||||
|
UnwarpOrDefault[T any](p *T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := pointer.UnwarpOrDefault(&a)
|
||||||
|
result2 := pointer.UnwarpOrDefault(&b)
|
||||||
|
result3 := pointer.UnwarpOrDefault(c)
|
||||||
|
result4 := pointer.UnwarpOrDefault(d)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 0
|
||||||
|
//
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExtractPointer">ExtractPointer</span>
|
### <span id="ExtractPointer">ExtractPointer</span>
|
||||||
|
|
||||||
<p>Returns the underlying value by the given interface type</p>
|
<p>Returns the underlying value by the given interface type</p>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Pointer
|
# Pointer
|
||||||
|
|
||||||
pointer包支持一些指针类型的操作。
|
pointer 包支持一些指针类型的操作。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -25,6 +25,8 @@ import (
|
|||||||
- [Of](#Of)
|
- [Of](#Of)
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [ExtractPointer](#ExtractPointer)
|
- [ExtractPointer](#ExtractPointer)
|
||||||
|
- [UnwarpOr](#UnwarpOr)
|
||||||
|
- [UnwarpOrDefault](#UnwarpOrDefault)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -63,7 +65,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unwrap">Unwrap</span>
|
### <span id="Unwrap">Unwrap</span>
|
||||||
|
|
||||||
<p>返回传入指针指向的值。</p>
|
<p>返回传入指针指向的值。</p>
|
||||||
@@ -133,4 +134,94 @@ func main() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UnwarpOr">UnwarpOr</span>
|
||||||
|
|
||||||
|
<p>返回指针的值,如果指针为零值,则返回fallback。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
UnwarpOr[T any](p *T, fallback T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := pointer.UnwarpOr(&a, 456)
|
||||||
|
result2 := pointer.UnwarpOr(&b, "abc")
|
||||||
|
result3 := pointer.UnwarpOr(c, 456)
|
||||||
|
result4 := pointer.UnwarpOr(d, "def")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 456
|
||||||
|
// def
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="UnwarpOrDefault">UnwarpOrDefault</span>
|
||||||
|
|
||||||
|
<p>返回指针的值,如果指针为零值,则返回相应零值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
UnwarpOrDefault[T any](p *T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/pointer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := pointer.UnwarpOrDefault(&a)
|
||||||
|
result2 := pointer.UnwarpOrDefault(&b)
|
||||||
|
result3 := pointer.UnwarpOrDefault(c)
|
||||||
|
result4 := pointer.UnwarpOrDefault(d)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 0
|
||||||
|
//
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
154
maputil/concurrentmap.go
Normal file
154
maputil/concurrentmap.go
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package maputil includes some functions to manipulate map.
|
||||||
|
package maputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultShardCount = 32
|
||||||
|
|
||||||
|
// ConcurrentMap is like map, but is safe for concurrent use by multiple goroutines.
|
||||||
|
type ConcurrentMap[K comparable, V any] struct {
|
||||||
|
shardCount uint64
|
||||||
|
locks []sync.RWMutex
|
||||||
|
maps []map[K]V
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewConcurrentMap create a ConcurrentMap with specific shard count.
|
||||||
|
func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] {
|
||||||
|
if shardCount <= 0 {
|
||||||
|
shardCount = defaultShardCount
|
||||||
|
}
|
||||||
|
|
||||||
|
cm := &ConcurrentMap[K, V]{
|
||||||
|
shardCount: uint64(shardCount),
|
||||||
|
locks: make([]sync.RWMutex, shardCount),
|
||||||
|
maps: make([]map[K]V, shardCount),
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range cm.maps {
|
||||||
|
cm.maps[i] = make(map[K]V)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cm
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the value for a key.
|
||||||
|
// Play: todo
|
||||||
|
func (cm *ConcurrentMap[K, V]) Set(key K, value V) {
|
||||||
|
shard := cm.getShard(key)
|
||||||
|
|
||||||
|
cm.locks[shard].Lock()
|
||||||
|
cm.maps[shard][key] = value
|
||||||
|
|
||||||
|
cm.locks[shard].Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the value stored in the map for a key, or nil if no.
|
||||||
|
// Play: todo
|
||||||
|
func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) {
|
||||||
|
shard := cm.getShard(key)
|
||||||
|
|
||||||
|
cm.locks[shard].RLock()
|
||||||
|
value, ok := cm.maps[shard][key]
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
|
||||||
|
return value, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOrSet returns the existing value for the key if present.
|
||||||
|
// Otherwise, it sets and returns the given value.
|
||||||
|
// Play: todo
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) {
|
||||||
|
shard := cm.getShard(key)
|
||||||
|
|
||||||
|
cm.locks[shard].RLock()
|
||||||
|
if actual, ok := cm.maps[shard][key]; ok {
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
return actual, ok
|
||||||
|
}
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
|
||||||
|
// lock again
|
||||||
|
cm.locks[shard].Lock()
|
||||||
|
if actual, ok = cm.maps[shard][key]; ok {
|
||||||
|
cm.locks[shard].Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cm.maps[shard][key] = value
|
||||||
|
cm.locks[shard].Unlock()
|
||||||
|
|
||||||
|
return value, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the value for a key.
|
||||||
|
// Play: todo
|
||||||
|
func (cm *ConcurrentMap[K, V]) Delete(key K) {
|
||||||
|
shard := cm.getShard(key)
|
||||||
|
|
||||||
|
cm.locks[shard].Lock()
|
||||||
|
delete(cm.maps[shard], key)
|
||||||
|
cm.locks[shard].Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) {
|
||||||
|
shard := cm.getShard(key)
|
||||||
|
|
||||||
|
cm.locks[shard].RLock()
|
||||||
|
if actual, ok = cm.maps[shard][key]; ok {
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
cm.Delete(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
|
||||||
|
return actual, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has checks if map has the value for a key.
|
||||||
|
// Play: todo
|
||||||
|
func (cm *ConcurrentMap[K, V]) Has(key K) bool {
|
||||||
|
_, ok := cm.Get(key)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool) {
|
||||||
|
for shard := range cm.locks {
|
||||||
|
cm.locks[shard].RLock()
|
||||||
|
|
||||||
|
for k, v := range cm.maps[shard] {
|
||||||
|
if !iterator(k, v) {
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cm.locks[shard].RUnlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getShard get shard by a key.
|
||||||
|
func (cm *ConcurrentMap[K, V]) getShard(key K) uint64 {
|
||||||
|
hash := fnv32(fmt.Sprintf("%v", key))
|
||||||
|
return uint64(hash) % cm.shardCount
|
||||||
|
}
|
||||||
|
|
||||||
|
func fnv32(key string) uint32 {
|
||||||
|
hash := uint32(2166136261)
|
||||||
|
const prime32 = uint32(16777619)
|
||||||
|
keyLength := len(key)
|
||||||
|
for i := 0; i < keyLength; i++ {
|
||||||
|
hash *= prime32
|
||||||
|
hash ^= uint32(key[i])
|
||||||
|
}
|
||||||
|
return hash
|
||||||
|
}
|
||||||
165
maputil/concurrentmap_test.go
Normal file
165
maputil/concurrentmap_test.go
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
package maputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConcurrentMap_Set_Get(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_Set_Get")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
for j := 0; j < 10; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(n, val)
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMap_GetOrSet(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_GetOrSet")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(5)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetOrSet(fmt.Sprintf("%d", n), n)
|
||||||
|
assert.Equal(n, val)
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
wg.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
for j := 0; j < 5; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(n, val)
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMap_Delete(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_Delete")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
var wg2 sync.WaitGroup
|
||||||
|
wg2.Add(10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Delete(fmt.Sprintf("%d", n))
|
||||||
|
wg2.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg2.Wait()
|
||||||
|
|
||||||
|
for j := 0; j < 10; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
_, ok := cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMap_GetAndDelete(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_GetAndDelete")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
for j := 0; j < 10; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(n, val)
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
|
||||||
|
_, ok = cm.Get(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMap_Has(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_Has")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
for j := 0; j < 10; j++ {
|
||||||
|
go func(n int) {
|
||||||
|
ok := cm.Has(fmt.Sprintf("%d", n))
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMap_Range(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestConcurrentMap_Range")
|
||||||
|
|
||||||
|
cm := NewConcurrentMap[string, int](100)
|
||||||
|
|
||||||
|
var wg1 sync.WaitGroup
|
||||||
|
wg1.Add(10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func(n int) {
|
||||||
|
cm.Set(fmt.Sprintf("%d", n), n)
|
||||||
|
wg1.Done()
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg1.Wait()
|
||||||
|
|
||||||
|
cm.Range(func(key string, value int) bool {
|
||||||
|
assert.Equal(key, fmt.Sprintf("%d", value))
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -301,9 +301,5 @@ func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value
|
|||||||
// Play: todo
|
// Play: todo
|
||||||
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]
|
||||||
if haskey {
|
return haskey
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,26 @@ func Unwrap[T any](p *T) T {
|
|||||||
return *p
|
return *p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnwarpOr returns the value from the pointer or fallback if the pointer is nil.
|
||||||
|
// Play: https://go.dev/play/p/mmNaLC38W8C
|
||||||
|
func UnwarpOr[T any](p *T, fallback T) T {
|
||||||
|
if p == nil {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
return *p
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnwarpOrDefault returns the value from the pointer or the default value if the pointer is nil.
|
||||||
|
// Play: https://go.dev/play/p/ZnGIHf8_o4E
|
||||||
|
func UnwarpOrDefault[T any](p *T) T {
|
||||||
|
var v T
|
||||||
|
|
||||||
|
if p == nil {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return *p
|
||||||
|
}
|
||||||
|
|
||||||
// ExtractPointer returns the underlying value by the given interface type
|
// ExtractPointer returns the underlying value by the given interface type
|
||||||
// Play: https://go.dev/play/p/D7HFjeWU2ZP
|
// Play: https://go.dev/play/p/D7HFjeWU2ZP
|
||||||
func ExtractPointer(value any) any {
|
func ExtractPointer(value any) any {
|
||||||
|
|||||||
@@ -29,6 +29,54 @@ func ExampleUnwrap() {
|
|||||||
// abc
|
// abc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleUnwarpOr() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := UnwarpOr(&a, 456)
|
||||||
|
result2 := UnwarpOr(&b, "abc")
|
||||||
|
result3 := UnwarpOr(c, 456)
|
||||||
|
result4 := UnwarpOr(d, "def")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 456
|
||||||
|
// def
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUnwarpOrDefault() {
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
result1 := UnwarpOrDefault(&a)
|
||||||
|
result2 := UnwarpOrDefault(&b)
|
||||||
|
result3 := UnwarpOrDefault(c)
|
||||||
|
result4 := UnwarpOrDefault(d)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// abc
|
||||||
|
// 0
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleExtractPointer() {
|
func ExampleExtractPointer() {
|
||||||
a := 1
|
a := 1
|
||||||
b := &a
|
b := &a
|
||||||
|
|||||||
@@ -30,6 +30,40 @@ func TestUnwrap(t *testing.T) {
|
|||||||
assert.Equal(b, Unwrap(&b))
|
assert.Equal(b, Unwrap(&b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnwarpOr(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestUnwarpOr")
|
||||||
|
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
assert.Equal(a, UnwarpOr(&a, 456))
|
||||||
|
assert.Equal(b, UnwarpOr(&b, "abc"))
|
||||||
|
assert.Equal(456, UnwarpOr(c, 456))
|
||||||
|
assert.Equal("def", UnwarpOr(d, "def"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnwarpOrDefault(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestUnwarpOrDefault")
|
||||||
|
|
||||||
|
a := 123
|
||||||
|
b := "abc"
|
||||||
|
|
||||||
|
var c *int
|
||||||
|
var d *string
|
||||||
|
|
||||||
|
assert.Equal(a, UnwarpOrDefault(&a))
|
||||||
|
assert.Equal(b, UnwarpOrDefault(&b))
|
||||||
|
assert.Equal(0, UnwarpOrDefault(c))
|
||||||
|
assert.Equal("", UnwarpOrDefault(d))
|
||||||
|
}
|
||||||
|
|
||||||
func TestExtractPointer(t *testing.T) {
|
func TestExtractPointer(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user