1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

..

31 Commits

Author SHA1 Message Date
dudaodong
5b6def5ff0 doc: update style link 2023-08-27 11:50:44 +08:00
dudaodong
33d99ad7d4 doc: add demo 2023-08-27 11:43:48 +08:00
dudaodong
8bd31d39c3 docs: update doc 2023-08-27 11:41:29 +08:00
dudaodong
a418549525 doc: update meta url 2023-08-25 17:43:10 +08:00
dudaodong
1f849efe1c doc: update doc for datetime fileutil formatter function package 2023-08-25 14:31:49 +08:00
dudaodong
8ad374bb21 doc: update doc for datastructure package 2023-08-25 11:05:33 +08:00
dudaodong
a69d886565 doc: add go playground demo 2023-08-25 10:49:16 +08:00
dudaodong
cf58542b4a doc: add new doc for convertor and cryptor 2023-08-25 10:47:16 +08:00
dudaodong
51f166d1d9 doc: update doc styles 2023-08-24 19:27:47 +08:00
dudaodong
fbeb031b40 doc: update doc 2023-08-24 19:07:24 +08:00
dudaodong
095cfc0aab doc: add new site for doc 2023-08-24 17:47:03 +08:00
ggymm
e66ab154bc fix: 覆盖写入字符串到文件问题 (#128) 2023-08-24 16:36:44 +08:00
dudaodong
91bc1a512c test: update TestZipFolder 2023-08-22 10:49:46 +08:00
dudaodong
8753255026 doc: update link in readme 2023-08-22 10:43:52 +08:00
dudaodong
9cf1f13fec doc: update link in readme 2023-08-22 10:39:37 +08:00
dudaodong
5d78bae4bb doc: update link in readme 2023-08-22 10:34:16 +08:00
dudaodong
27777eecc0 doc: update copyonwritelist doc 2023-08-22 10:26:39 +08:00
dudaodong
1a7e0e8792 doc: add copyonwritelist 2023-08-22 10:25:21 +08:00
dudaodong
02b7aa8f33 update version 2023-08-22 10:18:21 +08:00
dudaodong
f188d3d08f doc: add doc for InDelta 2023-08-21 11:05:11 +08:00
dudaodong
0c924b859e Merge branch 'main' into v2 2023-08-21 11:00:23 +08:00
warpmatrix
c5a5a07462 feat: InDelta (#127) 2023-08-15 09:53:16 +08:00
dudaodong
1ff5dd6df0 Merge branch 'main' into v2 2023-08-03 15:06:15 +08:00
Faucherwind
b5f86a488c feat: CopyOnWriteList adds more functions such as ForEach, Sort... (#126)
* feat: CopyOnWriteList adds more functions such as ForEach, Sort

* feat: CopyOnWriteList adds more functions such as ForEach, Sort
2023-08-03 15:05:30 +08:00
dudaodong
1390b7a964 fix: fix bug of Zip 2023-08-03 10:45:48 +08:00
dudaodong
c3e28a9fc0 fix: fix bug of Zip 2023-08-02 20:17:55 +08:00
dudaodong
e924429d6e fix: fix test case TestConcurrentMap_GetAndDelete 2023-08-02 11:33:27 +08:00
dudaodong
7079b1f704 format doc 2023-08-02 11:29:51 +08:00
Faucherwind
40cad365c0 feat: add CopyOnWriteList. A thread-safe list. (#125)
* fix: use loop to get value . On Get() and Contains()

* fix: Use reflect. DeepEqual() determines whether the keys are equal

* feat: add CopyOnWriteList. A thread-safe list.

* fix: fix failed unit test

* feat: add Equal() in CopyOnWriteList.

fix: update some function`s  names

* doc: add copyonwritelist.md and copyonwritelist_zh-CN.md
2023-08-02 11:10:10 +08:00
Faucherwind
6386ab908d fix: use loop to get value . On Get() and Contains() (#124)
* fix: use loop to get value . On Get() and Contains()

* fix: Use reflect. DeepEqual() determines whether the keys are equal
2023-08-01 11:14:45 +08:00
dudaodong
bb563724c7 doc: add go playground demo 2023-08-01 11:01:57 +08:00
105 changed files with 4718 additions and 770 deletions

5
.gitignore vendored
View File

@@ -6,6 +6,9 @@ fileutil/*.txt
fileutil/*.zip fileutil/*.zip
fileutil/*.link fileutil/*.link
fileutil/unzip/* fileutil/unzip/*
fileutil/tempdir/*
slice/testdata/* slice/testdata/*
cryptor/*.pem cryptor/*.pem
test test
docs/node_modules
docs/.vitepress/cache

136
README.md
View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.2.4-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.2.5-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -75,33 +75,33 @@ func main() {
### <span id="index">Index<span> ### <span id="index">Index<span>
- [Algorithm](#Algorithm) - [Algorithm](#user-content-algorithm)
- [Compare](#Compare) - [Compare](#user-content-compare)
- [Concurrency](#Concurrency) - [Concurrency](#user-content-concurrency)
- [Condition](#Condition) - [Condition](#user-content-condition)
- [Convertor](#Convertor) - [Convertor](#user-content-convertor)
- [Cryptor](#Cryptor) - [Cryptor](#user-content-cryptor)
- [Datetime](#Datetime) - [Datetime](#user-content-datetime)
- [Datastructure](#Datastructure) - [Datastructure](#user-content-datastructure)
- [Fileutil](#Fileutil) - [Fileutil](#user-content-fileutil)
- [Formatter](#Formatter) - [Formatter](#user-content-formatter)
- [Function](#Function) - [Function](#user-content-function)
- [Maputil](#Maputil) - [Maputil](#user-content-maputil)
- [Mathutil](#Mathutil) - [Mathutil](#user-content-mathutil)
- [Netutil](#Netutil) - [Netutil](#user-content-netutil)
- [Pointer](#Pointer) - [Pointer](#user-content-pointer)
- [Random](#Random) - [Random](#user-content-random)
- [Retry](#Retry) - [Retry](#user-content-retry)
- [Slice](#Slice) - [Slice](#user-content-slice)
- [Stream](#Stream) - [Stream](#user-content-stream)
- [Structs](#Structs) - [Structs](#user-content-structs)
- [Strutil](#Strutil) - [Strutil](#user-content-strutil)
- [System](#System) - [System](#user-content-system)
- [Tuple](#Tuple) - [Tuple](#user-content-tuple)
- [Validator](#Validator) - [Validator](#user-content-validator)
- [Xerror](#Xerror) - [Xerror](#user-content-xerror)
<h3 id="Algorithm"> 1. Algorithm package implements some basic algorithm. eg. sort, search. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span> </h3> <h3 id="algorithm">1. Algorithm package implements some basic algorithm. eg. sort, search. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/algorithm" import "github.com/duke-git/lancet/v2/algorithm"
@@ -146,7 +146,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)] [[play](https://go.dev/play/p/-EZjgOURufP)]
<h3 id="Compare"> 2. Compare package provides a lightweight comparison function on any type. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span> </h3> <h3 id="compare"> 2. Compare package provides a lightweight comparison function on any type. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a> </h3>
```go ```go
import "github.com/duke-git/lancet/v2/compare" import "github.com/duke-git/lancet/v2/compare"
@@ -172,8 +172,10 @@ import "github.com/duke-git/lancet/v2/compare"
- **<big>GreaterOrEqual</big>** : Checks if value `left` less greater or equal than value `right`. - **<big>GreaterOrEqual</big>** : Checks if value `left` less greater or equal than value `right`.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterOrEqual)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterOrEqual)]
[[play](https://go.dev/play/p/vx8mP0U8DFk)] [[play](https://go.dev/play/p/vx8mP0U8DFk)]
- **<big>InDelta</big>** : Checks if two values are equal or not within a delta.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#InDelta)]
<h3 id="Concurrency"> 3. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span> </h3> <h3 id="concurrency"> 3. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a> </h3>
```go ```go
import "github.com/duke-git/lancet/v2/concurrency" import "github.com/duke-git/lancet/v2/concurrency"
@@ -212,7 +214,7 @@ import "github.com/duke-git/lancet/v2/concurrency"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)]
[[play](https://go.dev/play/p/3TQPKnCirrP)] [[play](https://go.dev/play/p/3TQPKnCirrP)]
<h3 id="Condition"> 4. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span> </h3> <h3 id="condition"> 4. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a> </h3>
```go ```go
import "github.com/duke-git/lancet/v2/condition" import "github.com/duke-git/lancet/v2/condition"
@@ -245,7 +247,7 @@ import "github.com/duke-git/lancet/v2/condition"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#TernaryOperator)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#TernaryOperator)]
[[play](https://go.dev/play/p/ElllPZY0guT)] [[play](https://go.dev/play/p/ElllPZY0guT)]
<h3 id="Convertor"> 5. Convertor package contains some functions for data convertion. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span> </h3> <h3 id="convertor"> 5. Convertor package contains some functions for data convertion. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a> </h3>
```go ```go
import "github.com/duke-git/lancet/v2/convertor" import "github.com/duke-git/lancet/v2/convertor"
@@ -317,7 +319,7 @@ import "github.com/duke-git/lancet/v2/convertor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#GbkToUtf8)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#GbkToUtf8)]
[[play](https://go.dev/play/p/OphmHCN_9u8)] [[play](https://go.dev/play/p/OphmHCN_9u8)]
<h3 id="Cryptor"> 6. Cryptor package is for data encryption and decryption.</span> &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="cryptor"> 6. Cryptor package is for data encryption and decryption.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/cryptor" import "github.com/duke-git/lancet/v2/cryptor"
@@ -395,26 +397,31 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/1UI4oQ4WXKM)] [[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha1WithBase64</big>** : return the hmac hash of string use sha1 with base64. - **<big>HmacSha1WithBase64</big>** : return the hmac hash of string use sha1 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1WithBase64)]
[[play](https://go.dev/play/p/47JmmGrnF7B)]
- **<big>HmacSha256</big>** : return the hmac hash of string use sha256. - **<big>HmacSha256</big>** : return the hmac hash of string use sha256.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)] [[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha256WithBase64</big>** : return the hmac hash of string use sha256 with base64. - **<big>HmacSha256WithBase64</big>** : return the hmac hash of string use sha256 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256WithBase64)]
[[play](https://go.dev/play/p/EKbkUvPTLwO)]
- **<big>HmacSha512</big>** : return the hmac hash of string use sha512. - **<big>HmacSha512</big>** : return the hmac hash of string use sha512.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)] [[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>HmacSha512WithBase64</big>** : return the hmac hash of string use sha512 with base64. - **<big>HmacSha512WithBase64</big>** : return the hmac hash of string use sha512 with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512WithBase64)]
[[play](https://go.dev/play/p/c6dSe3E2ydU)]
- **<big>Md5Byte</big>** : return the md5 string of byte slice. - **<big>Md5Byte</big>** : return the md5 string of byte slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5Byte)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5Byte)]
[[play](https://go.dev/play/p/suraalH8lyC)] [[play](https://go.dev/play/p/suraalH8lyC)]
- **<big>Md5ByteWithBase64</big>** : return the md5 string of byte slice with base64. - **<big>Md5ByteWithBase64</big>** : return the md5 string of byte slice with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5ByteWithBase64)]
[[play](https://go.dev/play/p/Tcb-Z7LN2ax)]
- **<big>Md5String</big>** : return the md5 value of string. - **<big>Md5String</big>** : return the md5 value of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)] [[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5StringWithBase64</big>** : return the md5 value of string with base64. - **<big>Md5StringWithBase64</big>** : return the md5 value of string with base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5StringWithBase64)]
[[play](https://go.dev/play/p/Lx4gH7Vdr5_y)]
- **<big>Md5File</big>** : return the md5 value of file. - **<big>Md5File</big>** : return the md5 value of file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)]
- **<big>Sha1</big>** : return the sha1 value (SHA-1 hash algorithm) of base64 string. - **<big>Sha1</big>** : return the sha1 value (SHA-1 hash algorithm) of base64 string.
@@ -422,16 +429,19 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/_m_uoD1deMT)] [[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<big>Sha1WithBase64</big>** : return the sha1 value (SHA-1 hash algorithm) of string. - **<big>Sha1WithBase64</big>** : return the sha1 value (SHA-1 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1WithBase64)]
[[play](https://go.dev/play/p/fSyx-Gl2l2-)]
- **<big>Sha256</big>** : return the sha256 value (SHA-256 hash algorithm) of string. - **<big>Sha256</big>** : return the sha256 value (SHA-256 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)] [[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<big>Sha256WithBase64</big>** : return the sha256 value (SHA256 hash algorithm) of base64 string. - **<big>Sha256WithBase64</big>** : return the sha256 value (SHA256 hash algorithm) of base64 string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256WithBase64)]
[[play](https://go.dev/play/p/85IXJHIal1k)]
- **<big>Sha512</big>** : return the sha512 value (SHA-512 hash algorithm) of string. - **<big>Sha512</big>** : return the sha512 value (SHA-512 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)] [[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>Sha512WithBase64</big>** : return the sha512 value (SHA-512 hash algorithm) of base64 string. - **<big>Sha512WithBase64</big>** : return the sha512 value (SHA-512 hash algorithm) of base64 string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512WithBase64)]
[[play](https://go.dev/play/p/q_fY2rA-k5I)]
- **<big>GenerateRsaKey</big>** : create rsa private and public pemo file. - **<big>GenerateRsaKey</big>** : create rsa private and public pemo file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)] [[play](https://go.dev/play/p/zutRHrDqs0X)]
@@ -442,7 +452,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
<h3 id="Datetime"> 7. Datetime package supports date and time format and compare. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="datetime"> 7. Datetime package supports date and time format and compare. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/datetime" import "github.com/duke-git/lancet/v2/datetime"
@@ -563,19 +573,24 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/cupRM5aZOIY)] [[play](https://go.dev/play/p/cupRM5aZOIY)]
- **<big>NowDateOrTime</big>** : returns current datetime with specific format and timezone. - **<big>NowDateOrTime</big>** : returns current datetime with specific format and timezone.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NowDateOrTime)]
[[play](https://go.dev/play/p/EZ-begEjtT0)]
- **<big>Timestamp</big>** : returns current second timestamp. - **<big>Timestamp</big>** : returns current second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#Timestamp)]
- **<big>TimestampMilli</big>** : returns current mill second timestamp. - **<big>TimestampMilli</big>** : returns current mill second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMilli)]
[[play](https://go.dev/play/p/4gvEusOTu1T)]
- **<big>TimestampMicro</big>** : returns current micro second timestamp. - **<big>TimestampMicro</big>** : returns current micro second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampMicro)]
[[play](https://go.dev/play/p/2maANglKHQE)]
- **<big>TimestampNano</big>** : returns current nano second timestamp. - **<big>TimestampNano</big>** : returns current nano second timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#TimestampNano)]
[[play](https://go.dev/play/p/A9Oq_COrcCF)]
<h3 id="Datastructure"> 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="datastructure"> 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import list "github.com/duke-git/lancet/v2/datastructure/list" import list "github.com/duke-git/lancet/v2/datastructure/list"
import copyonwritelist "github.com/duke-git/lancet/v2/datastructure/copyonwritelist"
import link "github.com/duke-git/lancet/v2/datastructure/link" import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack" import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue" import queue "github.com/duke-git/lancet/v2/datastructure/queue"
@@ -589,6 +604,8 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
- **<big>List</big>** : a linear table, implemented with slice. - **<big>List</big>** : a linear table, implemented with slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)]
- **<big>CopyOnWriteList</big>** : a thread-safe list implementation that uses go slicing as its base.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/copyonwritelist.md)]
- **<big>Link</big>** : link list structure, contains singly link and doubly link. - **<big>Link</big>** : link list structure, contains singly link and doubly link.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link.md)]
- **<big>Stack</big>** : stack structure(fifo), contains array stack and link stack. - **<big>Stack</big>** : stack structure(fifo), contains array stack and link stack.
@@ -604,7 +621,7 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
- **<big>Hashmap</big>** : hash map structure. - **<big>Hashmap</big>** : hash map structure.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)]
<h3 id="Fileutil"> 9. Fileutil package implements some basic functions for file operations. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="fileutil"> 9. Fileutil package implements some basic functions for file operations. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/fileutil" import "github.com/duke-git/lancet/v2/fileutil"
@@ -687,7 +704,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteStringToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteStringToFile)]
[[play](https://go.dev/play/p/GhLS6d8lH_g)] [[play](https://go.dev/play/p/GhLS6d8lH_g)]
<h3 id="Formatter"> 10. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="formatter"> 10. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/formatter" import "github.com/duke-git/lancet/v2/formatter"
@@ -717,7 +734,7 @@ import "github.com/duke-git/lancet/v2/formatter"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#ParseBinaryBytes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)] [[play](https://go.dev/play/p/69v1tTT62x8)]
<h3 id="Function"> 11. Function package can control the flow of function execution and support part of functional programming.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="function"> 11. Function package can control the flow of function execution and support part of functional programming.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/function" import "github.com/duke-git/lancet/v2/function"
@@ -753,7 +770,7 @@ import "github.com/duke-git/lancet/v2/function"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
<h3 id="Maputil"> 12. Maputil package includes some functions to manipulate map.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="maputil"> 12. Maputil package includes some functions to manipulate map.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -824,26 +841,35 @@ 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. - **<big>HasKey</big>** : checks if map has key or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#HasKey)]
[[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count. - **<big>NewConcurrentMap</big>** : creates a ConcurrentMap with specific shard count.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#NewConcurrentMap)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Set</big>** : set the value for a key. - **<big>ConcurrentMap_Set</big>** : set the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Set)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Get</big>** : get the value stored in the map for a key, or nil if no. - **<big>ConcurrentMap_Get</big>** : get the value stored in the map for a key, or nil if no.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Get)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Get)]
[[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_GetOrSet</big>** : returns the existing value for the key if present. - **<big>ConcurrentMap_GetOrSet</big>** : returns the existing value for the key if present.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetOrSet)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetOrSet)]
[[play](https://go.dev/play/p/aDcDApOK01a)]
- **<big>ConcurrentMap_Delete</big>** : delete the value for a key. - **<big>ConcurrentMap_Delete</big>** : delete the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Delete)]
- **<big>ConcurrentMap_GetAndDelete</big>** :returns the existing value for the key if present and then delete the value for the key. [[play](https://go.dev/play/p/uTIJZYhpVMS)]
- **<big>ConcurrentMap_GetAndDelete</big>** :returns the existing value for the key if present and then delete the value for the key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetAndDelete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_GetAndDelete)]
[[play](https://go.dev/play/p/ZyxeIXSZUiM)]
- **<big>ConcurrentMap_Has</big>** : checks if map has the value for a key. - **<big>ConcurrentMap_Has</big>** : checks if map has the value for a key.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Has)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Has)]
[[play](https://go.dev/play/p/C8L4ul9TVwf)]
- **<big>ConcurrentMap_Range</big>** : calls iterator sequentially for each key and value present in each of the shards in the map. - **<big>ConcurrentMap_Range</big>** : calls iterator sequentially for each key and value present in each of the shards in the map.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ConcurrentMap_Range)]
[[play](https://go.dev/play/p/iqcy7P8P0Pr)]
<h3 id="Mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</span></h3> <h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -923,8 +949,11 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>Sum</big>** : return sum of passed numbers. - **<big>Sum</big>** : return sum of passed numbers.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Sum)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Sum)]
[[play](https://go.dev/play/p/1To2ImAMJA7)] [[play](https://go.dev/play/p/1To2ImAMJA7)]
- **<big>Abs</big>** : returns the absolute value of param nubmer.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Sum)]
[[play](https://go.dev/play/p/fsyBh1Os-1d)]
<h3 id="Netutil"> 14. Netutil package contains functions to get net information and send http request. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="netutil"> 14. Netutil package contains functions to get net information and send http request. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/netutil" import "github.com/duke-git/lancet/v2/netutil"
@@ -997,7 +1026,7 @@ import "github.com/duke-git/lancet/v2/netutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsTelnetConnected)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsTelnetConnected)]
[[play](https://go.dev/play/p/yiLCGtQv_ZG)] [[play](https://go.dev/play/p/yiLCGtQv_ZG)]
<h3 id="Pointer"> 15. Pointer package contains some util functions to operate go pointer. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="pointer"> 15. Pointer package contains some util functions to operate go pointer. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/pointer" import "github.com/duke-git/lancet/v2/pointer"
@@ -1021,7 +1050,7 @@ import "github.com/duke-git/lancet/v2/pointer"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#UnwrapOrDefault)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer.md#UnwrapOrDefault)]
[[play](https://go.dev/play/p/ZnGIHf8_o4E)] [[play](https://go.dev/play/p/ZnGIHf8_o4E)]
<h3 id="Random"> 16. Random package implements some basic functions to generate random int and string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="random"> 16. Random package implements some basic functions to generate random int and string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/random" import "github.com/duke-git/lancet/v2/random"
@@ -1057,7 +1086,7 @@ import "github.com/duke-git/lancet/v2/random"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUniqueIntSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUniqueIntSlice)]
[[play](https://go.dev/play/p/uBkRSOz73Ec)] [[play](https://go.dev/play/p/uBkRSOz73Ec)]
<h3 id="Retry"> 17. Retry package is for executing a function repeatedly until it was successful or canceled by the context. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="retry"> 17. Retry package is for executing a function repeatedly until it was successful or canceled by the context. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/retry" import "github.com/duke-git/lancet/v2/retry"
@@ -1081,7 +1110,7 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)] [[play](https://go.dev/play/p/ssfVeU2SwLO)]
<h3 id="Slice"> 18. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="slice"> 18. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/slice" import "github.com/duke-git/lancet/v2/slice"
@@ -1295,8 +1324,9 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
- **<big>Join</big>** : join the slice item with specify separator. - **<big>Join</big>** : join the slice item with specify separator.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)]
<h3 id="Stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/stream" import "github.com/duke-git/lancet/v2/stream"
@@ -1383,7 +1413,7 @@ import "github.com/duke-git/lancet/v2/stream"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#ToSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#ToSlice)]
[[play](https://go.dev/play/p/jI6_iZZuVFE)] [[play](https://go.dev/play/p/jI6_iZZuVFE)]
<h3 id="Structs"> 20. Structs package provides several high level functions to manipulate struct, tag, and field. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="structs"> 20. Structs package provides several high level functions to manipulate struct, tag, and field. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/structs" import "github.com/duke-git/lancet/v2/structs"
@@ -1416,7 +1446,7 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>IsSlice</big>** : check if the field is a slice - **<big>IsSlice</big>** : check if the field is a slice
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field.md#IsSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field.md#IsSlice)]
<h3 id="Strutil"> 21. Strutil package contains some functions to manipulate string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
@@ -1534,7 +1564,7 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>RemoveWhiteSpace</big>** : remove whitespace characters from a string. - **<big>RemoveWhiteSpace</big>** : remove whitespace characters from a string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveWhiteSpace)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#RemoveWhiteSpace)]
<h3 id="System"> 22. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="system"> 22. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/system" import "github.com/duke-git/lancet/v2/system"
@@ -1570,7 +1600,7 @@ import "github.com/duke-git/lancet/v2/system"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)]
[[play](https://go.dev/play/p/ml-_XH3gJbW)] [[play](https://go.dev/play/p/ml-_XH3gJbW)]
<h3 id="Tuple"> 23. Tuple package implements tuple data type and some operations on it. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="tuple"> 23. Tuple package implements tuple data type and some operations on it. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/tuple" import "github.com/duke-git/lancet/v2/tuple"
@@ -1687,7 +1717,7 @@ import "github.com/duke-git/lancet/v2/tuple"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple.md#Unzip10)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/tuple.md#Unzip10)]
[[play](https://go.dev/play/p/-taQB6Wfre_z)] [[play](https://go.dev/play/p/-taQB6Wfre_z)]
<h3 id="Validator"> 24. Validator package contains some functions for data validation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="validator"> 24. Validator package contains some functions for data validation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/validator" import "github.com/duke-git/lancet/v2/validator"
@@ -1795,7 +1825,7 @@ import "github.com/duke-git/lancet/v2/validator"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)] [[play](https://go.dev/play/p/Pe1FE2gdtTP)]
<h3 id="Xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/xerror" import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.2.4-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.2.5-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -74,33 +74,33 @@ func main() {
### <span id="index">目录<span> ### <span id="index">目录<span>
- [Algorithm](#Algorithm) - [Algorithm](#user-content-algorithm)
- [Compare](#Compare) - [Compare](#user-content-compare)
- [Concurrency](#Concurrency) - [Concurrency](#user-content-concurrency)
- [Condition](#Condition) - [Condition](#user-content-condition)
- [Convertor](#Convertor) - [Convertor](#user-content-convertor)
- [Cryptor](#Cryptor) - [Cryptor](#user-content-cryptor)
- [Datetime](#Datetime) - [Datetime](#user-content-datetime)
- [Datastructure](#Datastructure) - [Datastructure](#user-content-datastructure)
- [Fileutil](#Fileutil) - [Fileutil](#user-content-fileutil)
- [Formatter](#Formatter) - [Formatter](#user-content-formatter)
- [Function](#Function) - [Function](#user-content-function)
- [Maputil](#Maputil) - [Maputil](#user-content-maputil)
- [Mathutil](#Mathutil) - [Mathutil](#user-content-mathutil)
- [Netutil](#Netutil) - [Netutil](#user-content-netutil)
- [Pointer](#Pointer) - [Pointer](#user-content-pointer)
- [Random](#Random) - [Random](#user-content-random)
- [Retry](#Retry) - [Retry](#user-content-retry)
- [Slice](#Slice) - [Slice](#user-content-slice)
- [Stream](#Stream) - [Stream](#user-content-stream)
- [Structs](#Structs) - [Structs](#user-content-structs)
- [Strutil](#Strutil) - [Strutil](#user-content-strutil)
- [System](#System) - [System](#user-content-system)
- [Tuple](#Tuple) - [Tuple](#user-content-tuple)
- [Validator](#Validator) - [Validator](#user-content-validator)
- [Xerror](#Xerror) - [Xerror](#user-content-xerror)
<h3 id="Algorithm"> 1. algorithm 包实现一些基本查找和排序算法。 &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="algorithm"> 1. algorithm 包实现一些基本查找和排序算法。 &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/algorithm" import "github.com/duke-git/lancet/v2/algorithm"
@@ -145,7 +145,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)] [[play](https://go.dev/play/p/-EZjgOURufP)]
<h3 id="Compare"> 2. compare 包提供几个轻量级的类型比较函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="compare"> 2. compare 包提供几个轻量级的类型比较函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/compare" import "github.com/duke-git/lancet/v2/compare"
@@ -171,8 +171,10 @@ import "github.com/duke-git/lancet/v2/compare"
- **<big>GreaterOrEqual</big>** : 验证参数`left`的值是否大于或等于参数`right`的值。 - **<big>GreaterOrEqual</big>** : 验证参数`left`的值是否大于或等于参数`right`的值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterOrEqual)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterOrEqual)]
[[play](https://go.dev/play/p/vx8mP0U8DFk)] [[play](https://go.dev/play/p/vx8mP0U8DFk)]
- **<big>InDelta</big>** : 检查增量内两个值是否相等。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#InDelta)]
<h3 id="Concurrency"> 3. concurrency 包含一些支持并发编程的功能。例如goroutine, channel, async 等。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="concurrency"> 3. concurrency 包含一些支持并发编程的功能。例如goroutine, channel, async 等。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/concurrency" import "github.com/duke-git/lancet/v2/concurrency"
@@ -211,7 +213,7 @@ import "github.com/duke-git/lancet/v2/concurrency"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
[[play](https://go.dev/play/p/3TQPKnCirrP)] [[play](https://go.dev/play/p/3TQPKnCirrP)]
<h3 id="Condition"> 4. condition 包含一些用于条件判断的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="condition"> 4. condition 包含一些用于条件判断的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/condition" import "github.com/duke-git/lancet/v2/condition"
@@ -244,7 +246,7 @@ import "github.com/duke-git/lancet/v2/condition"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#TernaryOperator)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#TernaryOperator)]
[[play](https://go.dev/play/p/ElllPZY0guT)] [[play](https://go.dev/play/p/ElllPZY0guT)]
<h3 id="Convertor"> 5. convertor 转换器包支持一些常见的数据类型转换。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="convertor"> 5. convertor 转换器包支持一些常见的数据类型转换。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/convertor" import "github.com/duke-git/lancet/v2/convertor"
@@ -316,7 +318,7 @@ import "github.com/duke-git/lancet/v2/convertor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#GbkToUtf8)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#GbkToUtf8)]
[[play](https://go.dev/play/p/OphmHCN_9u8)] [[play](https://go.dev/play/p/OphmHCN_9u8)]
<h3 id="Cryptor"> 6. cryptor 加密包支持数据加密和解密,获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="cryptor"> 6. cryptor 加密包支持数据加密和解密,获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/cryptor" import "github.com/duke-git/lancet/v2/cryptor"
@@ -389,31 +391,37 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>HmacMd5WithBase64</big>** : 获取字符串 md5 hmac base64 字符串值。 - **<big>HmacMd5WithBase64</big>** : 获取字符串 md5 hmac base64 字符串值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5WithBase64)]
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5WithBase64)]
- **<big>HmacSha1</big>** : 返回字符串 sha1 hmac 值。 - **<big>HmacSha1</big>** : 返回字符串 sha1 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)]
[[play](https://go.dev/play/p/1UI4oQ4WXKM)] [[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha1WithBase64</big>** : 获取字符串的 sha1 base64 值。 - **<big>HmacSha1WithBase64</big>** : 获取字符串的 sha1 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1WithBase64)]
[[play](https://go.dev/play/p/47JmmGrnF7B)]
- **<big>HmacSha256</big>** : 返回字符串 sha256 hmac 值。 - **<big>HmacSha256</big>** : 返回字符串 sha256 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)] [[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha256WithBase64</big>** : 获取字符串 sha256 hmac base64 值。 - **<big>HmacSha256WithBase64</big>** : 获取字符串 sha256 hmac base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256WithBase64)]
[[play](https://go.dev/play/p/EKbkUvPTLwO)]
- **<big>HmacSha512</big>** : 返回字符串 sha256 hmac 值。 - **<big>HmacSha512</big>** : 返回字符串 sha256 hmac 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)] [[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>HmacSha512WithBase64</big>** : 获取字符串 sha512 hmac base64 值。 - **<big>HmacSha512WithBase64</big>** : 获取字符串 sha512 hmac base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512WithBase64)]
[[play](https://go.dev/play/p/c6dSe3E2ydU)]
- **<big>Md5Byte</big>** : 返回 byte slice 的 md5 值. - **<big>Md5Byte</big>** : 返回 byte slice 的 md5 值.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5Byte)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5Byte)]
[[play](https://go.dev/play/p/suraalH8lyC)] [[play](https://go.dev/play/p/suraalH8lyC)]
- **<big>Md5ByteWithBase64</big>** : 获取 byte slice 的 md5 base64 值。 - **<big>Md5ByteWithBase64</big>** : 获取 byte slice 的 md5 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5ByteWithBase64)]
[[play](https://go.dev/play/p/Tcb-Z7LN2ax)]
- **<big>Md5String</big>** : 返回字符串 md5 值。 - **<big>Md5String</big>** : 返回字符串 md5 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)] [[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5StringWithBase64</big>** : 获取字符串 md5 base64 值。 - **<big>Md5StringWithBase64</big>** : 获取字符串 md5 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5StringWithBase64)]
[[play](https://go.dev/play/p/Lx4gH7Vdr5_y)]
- **<big>Md5File</big>** : 返回文件 md5 值。 - **<big>Md5File</big>** : 返回文件 md5 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)]
- **<big>Sha1</big>** : 返回字符串 sha1 哈希值。 - **<big>Sha1</big>** : 返回字符串 sha1 哈希值。
@@ -421,16 +429,19 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[play](https://go.dev/play/p/_m_uoD1deMT)] [[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<big>Sha1WithBase64</big>** : 获取字符串 sha1 base64 值。 - **<big>Sha1WithBase64</big>** : 获取字符串 sha1 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1WithBase64)]
[[play](https://go.dev/play/p/fSyx-Gl2l2-)]
- **<big>Sha256</big>** :返回字符串 sha256 哈希值。 - **<big>Sha256</big>** :返回字符串 sha256 哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)] [[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<big>Sha256WithBase64</big>** : 获取字符串 sha256 base64 值。 - **<big>Sha256WithBase64</big>** : 获取字符串 sha256 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256WithBase64)]
[[play](https://go.dev/play/p/85IXJHIal1k)]
- **<big>Sha512</big>** : 返回字符串 sha512 哈希值。 - **<big>Sha512</big>** : 返回字符串 sha512 哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)] [[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>Sha512WithBase64</big>** : 获取字符串 sha512 base64 值。 - **<big>Sha512WithBase64</big>** : 获取字符串 sha512 base64 值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512WithBase64)]
[[play](https://go.dev/play/p/q_fY2rA-k5I)]
- **<big>GenerateRsaKey</big>** : 在当前目录下创建 rsa 私钥文件和公钥文件。 - **<big>GenerateRsaKey</big>** : 在当前目录下创建 rsa 私钥文件和公钥文件。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)] [[play](https://go.dev/play/p/zutRHrDqs0X)]
@@ -441,7 +452,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)] [[play](https://go.dev/play/p/uef0q1fz53I)]
<h3 id="Datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="datetime"> 7. datetime 日期时间处理包,格式化日期,比较日期。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/datetime" import "github.com/duke-git/lancet/v2/datetime"
@@ -565,19 +576,25 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/cupRM5aZOIY)] [[play](https://go.dev/play/p/cupRM5aZOIY)]
- **<big>NowDateOrTime</big>** : 根据指定的格式和时区返回当前时间字符串。 - **<big>NowDateOrTime</big>** : 根据指定的格式和时区返回当前时间字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NowDateOrTime)]
[[play](https://go.dev/play/p/EZ-begEjtT0)]
- **<big>Timestamp</big>** : 返回当前秒级时间戳。 - **<big>Timestamp</big>** : 返回当前秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#Timestamp)]
[[play](https://go.dev/play/p/iU5b7Vvjx6x)]
- **<big>TimestampMilli</big>** : 返回当前毫秒级时间戳。 - **<big>TimestampMilli</big>** : 返回当前毫秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMilli)]
[[play](https://go.dev/play/p/4gvEusOTu1T)]
- **<big>TimestampMicro</big>** : 返回当前微秒级时间戳。 - **<big>TimestampMicro</big>** : 返回当前微秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampMicro)]
[[play](https://go.dev/play/p/2maANglKHQE)]
- **<big>TimestampNano</big>** : 返回当前纳秒级时间戳。 - **<big>TimestampNano</big>** : 返回当前纳秒级时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#TimestampNano)]
[[play](https://go.dev/play/p/A9Oq_COrcCF)]
<h3 id="Datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="datastructure"> 8. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import list "github.com/duke-git/lancet/v2/datastructure/list" import list "github.com/duke-git/lancet/v2/datastructure/list"
import copyonwritelist "github.com/duke-git/lancet/v2/datastructure/copyonwritelist"
import link "github.com/duke-git/lancet/v2/datastructure/link" import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack" import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue" import queue "github.com/duke-git/lancet/v2/datastructure/queue"
@@ -591,6 +608,8 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
- **<big>List</big>** : 线性表结构, 用切片实现。 - **<big>List</big>** : 线性表结构, 用切片实现。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)]
- **<big>CopyOnWriteList</big>** : 是一个线程安全的 List 实现,底层使用 go 切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/copyonwritelist_zh-CN.md)]
- **<big>Link</big>** : 链表解构, 包括单链表和双向链表。 - **<big>Link</big>** : 链表解构, 包括单链表和双向链表。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link_zh-CN.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link_zh-CN.md)]
- **<big>Stack</big>** : 栈结构(fifo), 包括数组栈和链表栈。 - **<big>Stack</big>** : 栈结构(fifo), 包括数组栈和链表栈。
@@ -606,7 +625,7 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
- **<big>Hashmap</big>** : 哈希映射。 - **<big>Hashmap</big>** : 哈希映射。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)]
<h3 id="Fileutil"> 9. fileutil 包含文件基本操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="fileutil"> 9. fileutil 包含文件基本操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/fileutil" import "github.com/duke-git/lancet/v2/fileutil"
@@ -689,7 +708,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteStringToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteStringToFile)]
[[play](https://go.dev/play/p/GhLS6d8lH_g)] [[play](https://go.dev/play/p/GhLS6d8lH_g)]
<h3 id="Formatter"> 10. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="formatter"> 10. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/formatter" import "github.com/duke-git/lancet/v2/formatter"
@@ -719,7 +738,7 @@ import "github.com/duke-git/lancet/v2/formatter"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#ParseBinaryBytes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)] [[play](https://go.dev/play/p/69v1tTT62x8)]
<h3 id="Function"> 11. function 函数包控制函数执行流程,包含部分函数式编程。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="function"> 11. function 函数包控制函数执行流程,包含部分函数式编程。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/function" import "github.com/duke-git/lancet/v2/function"
@@ -755,7 +774,7 @@ import "github.com/duke-git/lancet/v2/function"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
<h3 id="Maputil"> 12. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="maputil"> 12. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -826,26 +845,35 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<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。 - **<big>HasKey</big>** : 检查 map 是否包含某个 key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#HasKey)]
- **<big>NewConcurrentMap</big>** : ConcurrentMap协程安全的map结构。 [[play](https://go.dev/play/p/isZZHOsDhFc)]
- **<big>NewConcurrentMap</big>** : ConcurrentMap 协程安全的 map 结构。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#NewConcurrentMap)]
- **<big>ConcurrentMap_Set</big>** : 在map中设置key和value。 [[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Set</big>** : 在 map 中设置 key 和 value。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Set)]
- **<big>ConcurrentMap_Get</big>** : 根据key获取value, 如果不存在key,返回零值。 [[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_Get</big>** : 根据 key 获取 value, 如果不存在 key,返回零值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Get)]
- **<big>ConcurrentMap_GetOrSet</big>** : 返回键的现有值如果存在否则设置key并返回给定值。 [[play](https://go.dev/play/p/3PenTPETJT0)]
- **<big>ConcurrentMap_GetOrSet</big>** : 返回键的现有值(如果存在),否则,设置 key 并返回给定值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetOrSet)]
- **<big>ConcurrentMap_Delete</big>** : 删除key。 [[play](https://go.dev/play/p/aDcDApOK01a)]
- **<big>ConcurrentMap_Delete</big>** : 删除 key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Delete)]
- **<big>ConcurrentMap_GetAndDelete</big>** :获取key然后删除。 [[play](https://go.dev/play/p/uTIJZYhpVMS)]
- **<big>ConcurrentMap_GetAndDelete</big>** :获取 key然后删除。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_GetAndDelete)]
- **<big>ConcurrentMap_Has</big>** : 验证是否包含key。 [[play](https://go.dev/play/p/ZyxeIXSZUiM)]
- **<big>ConcurrentMap_Has</big>** : 验证是否包含 key。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Has)]
- **<big>ConcurrentMap_Range</big>** : 为map中每个键和值顺序调用迭代器。 如果iterator返回false则停止迭代。 [[play](https://go.dev/play/p/C8L4ul9TVwf)]
- **<big>ConcurrentMap_Range</big>** : 为 map 中每个键和值顺序调用迭代器。 如果 iterator 返回 false则停止迭代。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ConcurrentMap_Range)]
[[play](https://go.dev/play/p/iqcy7P8P0Pr)]
<h3 id="Mathutil"> 13. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="mathutil"> 13. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -925,8 +953,11 @@ import "github.com/duke-git/lancet/v2/mathutil"
- **<big>Sum</big>** : 求传入参数之和。 - **<big>Sum</big>** : 求传入参数之和。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Sum)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Sum)]
[[play](https://go.dev/play/p/1To2ImAMJA7)] [[play](https://go.dev/play/p/1To2ImAMJA7)]
- **<big>Abs</big>** : 求绝对值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Sum)]
[[play](https://go.dev/play/p/fsyBh1Os-1d)]
<h3 id="Netutil"> 14. netutil 网络包支持获取 ip 地址,发送 http 请求。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="netutil"> 14. netutil 网络包支持获取 ip 地址,发送 http 请求。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/netutil" import "github.com/duke-git/lancet/v2/netutil"
@@ -999,7 +1030,7 @@ import "github.com/duke-git/lancet/v2/netutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsTelnetConnected)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsTelnetConnected)]
[[play](https://go.dev/play/p/yiLCGtQv_ZG)] [[play](https://go.dev/play/p/yiLCGtQv_ZG)]
<h3 id="Pointer"> 15. pointer 包支持一些指针类型的操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="pointer"> 15. pointer 包支持一些指针类型的操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/pointer" import "github.com/duke-git/lancet/v2/pointer"
@@ -1023,7 +1054,7 @@ import "github.com/duke-git/lancet/v2/pointer"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#UnwrapOrDefault)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/pointer_zh-CN.md#UnwrapOrDefault)]
[[play](https://go.dev/play/p/ZnGIHf8_o4E)] [[play](https://go.dev/play/p/ZnGIHf8_o4E)]
<h3 id="Random"> 16. random 随机数生成器包,可以生成随机[]bytes, int, string。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="random"> 16. random 随机数生成器包,可以生成随机[]bytes, int, string。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/random" import "github.com/duke-git/lancet/v2/random"
@@ -1059,7 +1090,7 @@ import "github.com/duke-git/lancet/v2/random"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUniqueIntSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUniqueIntSlice)]
[[play](https://go.dev/play/p/uBkRSOz73Ec)] [[play](https://go.dev/play/p/uBkRSOz73Ec)]
<h3 id="Retry"> 17. retry 重试执行函数直到函数运行成功或被 context cancel。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="retry"> 17. retry 重试执行函数直到函数运行成功或被 context cancel。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/retry" import "github.com/duke-git/lancet/v2/retry"
@@ -1083,7 +1114,7 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)] [[play](https://go.dev/play/p/ssfVeU2SwLO)]
<h3 id="Slice"> 18. slice 包含操作切片的方法集合。</span>&nbsp; &nbsp; &nbsp; &nbsp; [回到目录](#index) <h3 id="slice"> 18. slice 包含操作切片的方法集合。&nbsp; &nbsp; &nbsp; &nbsp; [回到目录](#index)
```go ```go
import "github.com/duke-git/lancet/v2/slice" import "github.com/duke-git/lancet/v2/slice"
@@ -1297,8 +1328,9 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/uXod2LWD1Kg)] [[play](https://go.dev/play/p/uXod2LWD1Kg)]
- **<big>Join</big>** : 用指定的分隔符链接切片元素。 - **<big>Join</big>** : 用指定的分隔符链接切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Join)]
[[play](https://go.dev/play/p/huKzqwNDD7V)]
<h3 id="Stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="stream"> 19. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/stream" import "github.com/duke-git/lancet/v2/stream"
@@ -1385,7 +1417,7 @@ import "github.com/duke-git/lancet/v2/stream"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#ToSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#ToSlice)]
[[play](https://go.dev/play/p/jI6_iZZuVFE)] [[play](https://go.dev/play/p/jI6_iZZuVFE)]
<h3 id="Structs"> 20. structs 提供操作 struct, tag, field 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="structs"> 20. structs 提供操作 struct, tag, field 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/structs" import "github.com/duke-git/lancet/v2/structs"
@@ -1420,7 +1452,7 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>IsSlice</big>** : 判断属性是否是切片。 - **<big>IsSlice</big>** : 判断属性是否是切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/structs/field_zh-CN.md#IsSlice)]
<h3 id="Strutil"> 21. strutil 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="strutil"> 21. strutil 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
@@ -1539,7 +1571,7 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>RemoveWhiteSpace</big>** : 删除字符串中的空格。 - **<big>RemoveWhiteSpace</big>** : 删除字符串中的空格。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveWhiteSpace)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#RemoveWhiteSpace)]
<h3 id="System"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="system"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/system" import "github.com/duke-git/lancet/v2/system"
@@ -1575,7 +1607,7 @@ import "github.com/duke-git/lancet/v2/system"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)]
[[play](https://go.dev/play/p/ml-_XH3gJbW)] [[play](https://go.dev/play/p/ml-_XH3gJbW)]
<h3 id="Tuple"> 23. Tuple 包实现一个元组数据类型。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="tuple"> 23. Tuple 包实现一个元组数据类型。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/tuple" import "github.com/duke-git/lancet/v2/tuple"
@@ -1692,7 +1724,7 @@ import "github.com/duke-git/lancet/v2/tuple"
[[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 验证器包,包含常用字符串格式验证函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="validator"> 24. validator 验证器包,包含常用字符串格式验证函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/validator" import "github.com/duke-git/lancet/v2/validator"
@@ -1800,7 +1832,7 @@ import "github.com/duke-git/lancet/v2/validator"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsPrintable)]
[[play](https://go.dev/play/p/Pe1FE2gdtTP)] [[play](https://go.dev/play/p/Pe1FE2gdtTP)]
<h3 id="Xerror"> 25. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="xerror"> 25. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/xerror" import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -10,6 +10,8 @@ import (
"time" "time"
"github.com/duke-git/lancet/v2/convertor" "github.com/duke-git/lancet/v2/convertor"
"github.com/duke-git/lancet/v2/mathutil"
"golang.org/x/exp/constraints"
) )
// operator type // operator type
@@ -62,3 +64,9 @@ func LessOrEqual(left, right any) bool {
func GreaterOrEqual(left, right any) bool { func GreaterOrEqual(left, right any) bool {
return compareValue(greaterOrEqual, left, right) return compareValue(greaterOrEqual, left, right)
} }
// InDelta checks if two values are equal or not within a delta.
// Play: https://go.dev/play/p/TuDdcNtMkjo
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool {
return float64(mathutil.Abs(left-right)) <= delta
}

View File

@@ -168,3 +168,29 @@ func ExampleGreaterOrEqual() {
// false // false
// false // false
} }
func ExampleInDelta() {
result1 := InDelta(1, 1, 0)
result2 := InDelta(1, 2, 0)
result3 := InDelta(2.0/3.0, 0.66667, 0.001)
result4 := InDelta(2.0/3.0, 0.0, 0.001)
result5 := InDelta(float64(74.96)-float64(20.48), 54.48, 0)
result6 := InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
// Output:
// true
// false
// true
// false
// false
// true
}

View File

@@ -138,3 +138,19 @@ func TestGreaterOrEqual(t *testing.T) {
assert.Equal(false, GreaterOrEqual(int64(2), 1)) assert.Equal(false, GreaterOrEqual(int64(2), 1))
assert.Equal(false, GreaterOrEqual("b", "c")) assert.Equal(false, GreaterOrEqual("b", "c"))
} }
func TestInDelta(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestInDelta")
assert.Equal(true, InDelta(1, 1, 0))
assert.Equal(false, InDelta(1, 2, 0))
assert.Equal(true, InDelta(2.0/3.0, 0.66667, 0.001))
assert.Equal(false, InDelta(2.0/3.0, 0.0, 0.001))
assert.Equal(false, InDelta(float64(74.96)-float64(20.48), 54.48, 0))
assert.Equal(true, InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14))
assert.Equal(false, InDelta(float64(float32(80.45)), float64(80.45), 0))
assert.Equal(true, InDelta(float64(float32(80.45)), float64(80.45), 1e-5))
}

View File

@@ -57,7 +57,7 @@ func Md5Byte(data []byte) string {
} }
// Md5ByteWithBase64 return the md5 string of byte slice with base64. // Md5ByteWithBase64 return the md5 string of byte slice with base64.
// Play: https://go.dev/play/p/CkN9hYKGeAy // Play: https://go.dev/play/p/Tcb-Z7LN2ax
func Md5ByteWithBase64(data []byte) string { func Md5ByteWithBase64(data []byte) string {
h := md5.New() h := md5.New()
h.Write(data) h.Write(data)
@@ -105,7 +105,7 @@ func HmacMd5(str, key string) string {
} }
// HmacMd5WithBase64 return the hmac hash of string use md5 with base64. // HmacMd5WithBase64 return the hmac hash of string use md5 with base64.
// todo // https://go.dev/play/p/UY0ng2AefFC
func HmacMd5WithBase64(data, key string) string { 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))
@@ -153,7 +153,7 @@ func HmacSha512(str, key string) string {
} }
// HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. // HmacSha512WithBase64 return the hmac hash of string use sha512 with base64.
// Play: https://go.dev/play/p/61wBBOKO-GH // Play: https://go.dev/play/p/c6dSe3E2ydU
func HmacSha512WithBase64(str, key string) string { func HmacSha512WithBase64(str, key string) string {
h := hmac.New(sha512.New, []byte(key)) h := hmac.New(sha512.New, []byte(key))
h.Write([]byte(str)) h.Write([]byte(str))
@@ -169,7 +169,7 @@ func Sha1(str string) string {
} }
// Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string. // Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string.
// Play: todo // Play: https://go.dev/play/p/fSyx-Gl2l2-
func Sha1WithBase64(str string) string { func Sha1WithBase64(str string) string {
sha1 := sha1.New() sha1 := sha1.New()
sha1.Write([]byte(str)) sha1.Write([]byte(str))

View File

@@ -7,6 +7,7 @@ package datastructure
import ( import (
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"reflect"
) )
var defaultMapCapacity uint64 = 1 << 10 var defaultMapCapacity uint64 = 1 << 10
@@ -45,13 +46,24 @@ func NewHashMapWithCapacity(size, capacity uint64) *HashMap {
func (hm *HashMap) Get(key any) any { func (hm *HashMap) Get(key any) any {
hashValue := hm.hash(key) hashValue := hm.hash(key)
node := hm.table[hashValue] node := hm.table[hashValue]
if node != nil { for node != nil {
return node.value if reflect.DeepEqual(node.key, key) {
return node.value
}
node = node.next
} }
return nil return nil
} }
// GetOrDefault return the value of given key in hashmap, if not found return default value
func (hm *HashMap) GetOrDefault(key any, defaultValue any) any {
value := hm.Get(key)
if value == nil {
return defaultValue
}
return value
}
// Put new key value in hashmap // Put new key value in hashmap
func (hm *HashMap) Put(key any, value any) { func (hm *HashMap) Put(key any, value any) {
hm.putValue(hm.hash(key), key, value) hm.putValue(hm.hash(key), key, value)
@@ -90,7 +102,13 @@ func (hm *HashMap) Delete(key any) {
// Contains checks if given key is in hashmap or not // Contains checks if given key is in hashmap or not
func (hm *HashMap) Contains(key any) bool { func (hm *HashMap) Contains(key any) bool {
node := hm.table[hm.hash(key)] node := hm.table[hm.hash(key)]
return node != nil for node != nil {
if reflect.DeepEqual(node.key, key) {
return true
}
node = node.next
}
return false
} }
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order) // Iterate executes iteratee funcation for every key and value pair of hashmap (random order)

View File

@@ -74,3 +74,34 @@ func TestHashMap_KeysValues(t *testing.T) {
assert.Equal(3, len(values)) assert.Equal(3, len(values))
assert.Equal(3, len(keys)) assert.Equal(3, len(keys))
} }
func TestHashMap_Keys(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHashMap_Keys")
hm := NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
keys := hm.Keys()
assert.Equal(3, len(keys))
}
func TestHashMap_GetOrDefault(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHashMap_GetOrDefault")
hm := NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
assert.Equal(1, hm.GetOrDefault("a", 5))
assert.Equal(5, hm.GetOrDefault("d", 5))
}

View File

@@ -0,0 +1,350 @@
package datastructure
import (
"reflect"
"sort"
"sync"
)
type CopyOnWriteList[T any] struct {
data []T
lock sync.Locker
}
// NewCopyOnWriteList Creates an empty list.
func NewCopyOnWriteList[T any](data []T) *CopyOnWriteList[T] {
return &CopyOnWriteList[T]{data: data, lock: &sync.RWMutex{}}
}
func (c *CopyOnWriteList[T]) getList() []T {
return c.data
}
func (c *CopyOnWriteList[T]) setList(data []T) {
c.data = data
}
// Size returns the number of elements in this list.
func (c *CopyOnWriteList[T]) Size() int {
return len(c.getList())
}
// IsEmpty returns true if this list contains no elements.
func (c *CopyOnWriteList[T]) IsEmpty() bool {
return c.Size() == 0
}
// Contain returns true if this list contains the specified element.
func (c *CopyOnWriteList[T]) Contain(e T) bool {
list := c.getList()
return indexOf(e, list, 0, c.Size()) >= 0
}
// ValueOf returns the index of the first occurrence of the specified element in this list, or null if this list does not contain the element.
func (c *CopyOnWriteList[T]) ValueOf(index int) (*T, bool) {
list := c.getList()
if index < 0 || index >= len(c.data) {
return nil, false
}
return get(list, index), true
}
// IndexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
func (c *CopyOnWriteList[T]) IndexOf(e T) int {
list := c.getList()
return indexOf(e, list, 0, c.Size())
}
// indexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
// start the start position of the search (inclusive)
// end the end position of the search (exclusive)
func indexOf[T any](o T, e []T, start int, end int) int {
if start >= end {
return -1
}
for i := start; i < end; i++ {
if reflect.DeepEqual(e[i], o) {
return i
}
}
return -1
}
// LastIndexOf returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
func (c *CopyOnWriteList[T]) LastIndexOf(e T) int {
list := c.getList()
return lastIndexOf(e, list, 0, c.Size())
}
// lastIndexOf returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
// start the start position of the search (inclusive)
// end the end position of the search (exclusive)
func lastIndexOf[T any](o T, e []T, start int, end int) int {
if start >= end {
return -1
}
for i := end - 1; i >= start; i-- {
if reflect.DeepEqual(e[i], o) {
return i
}
}
return -1
}
// get returns the element at the specified position in this list.
func get[T any](o []T, index int) *T {
return &o[index]
}
// Get returns the element at the specified position in this list.
func (c *CopyOnWriteList[T]) Get(index int) *T {
list := c.getList()
if index < 0 || index >= len(list) {
return nil
}
return get(list, index)
}
func (c *CopyOnWriteList[T]) set(index int, e T) (oldValue *T) {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
oldValue = get(list, index)
if reflect.DeepEqual(oldValue, e) {
c.setList(list)
} else {
newList := make([]T, len(list))
copy(newList, list)
newList[index] = e
c.setList(newList)
}
return
}
// Set replaces the element at the specified position in this list with the specified element.
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool) {
list := c.getList()
if index < 0 || index >= len(list) {
return oldValue, false
}
return c.set(index, e), true
}
// Add appends the specified element to the end of this list.
func (c *CopyOnWriteList[T]) Add(e T) bool {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
newList := make([]T, len(list)+1)
copy(newList, list)
newList[len(list)] = e
c.setList(newList)
return true
}
// AddAll appends all the elements in the specified collection to the end of this list
func (c *CopyOnWriteList[T]) AddAll(e []T) bool {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
newList := make([]T, len(list)+len(e))
copy(newList, list)
copy(newList[len(list):], e)
c.setList(newList)
return true
}
// AddByIndex inserts the specified element at the specified position in this list.
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
length := len(list)
if index < 0 || index > length {
return false
}
var newList []T
var numMove = length - index
if numMove == 0 {
newList = make([]T, length+1)
copy(newList, list)
} else {
newList = make([]T, length+1)
copy(newList, list[:index])
copy(newList[index+1:], list[index:])
}
newList[index] = e
c.setList(newList)
return true
}
// delete removes the element at the specified position in this list.
func (c *CopyOnWriteList[T]) delete(index int) *T {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
length := len(list)
oldValue := get(list, index)
numMove := length - index - 1
var newList []T
if numMove == 0 {
newList = make([]T, length-1)
copy(newList, list[:index])
} else {
newList = make([]T, length-1)
copy(newList, list[:index])
copy(newList[index:], list[index+1:])
}
c.setList(newList)
return oldValue
}
// DeleteAt removes the element at the specified position in this list.
func (c *CopyOnWriteList[T]) DeleteAt(index int) (*T, bool) {
list := c.getList()
if index < 0 || index >= len(list) {
return nil, false
}
return c.delete(index), true
}
// DeleteBy removes the first occurrence of the specified element from this list, if it is present.
func (c *CopyOnWriteList[T]) DeleteBy(o T) (*T, bool) {
list := c.getList()
index := indexOf(o, list, 0, len(list))
if index == -1 {
return nil, false
}
return c.delete(index), true
}
// DeleteRange removes from this list all the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
// left close and right open
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int) {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
length := len(list)
if start < 0 || end > length || start > end {
return
}
var newList []T
numMove := length - end
if numMove == 0 {
newList = make([]T, length-(end-start))
copy(newList, list[:start])
} else {
newList = make([]T, length-(end-start))
copy(newList, list[:start])
copy(newList[start:], list[end:])
}
c.setList(newList)
}
// DeleteIf removes all the elements of this collection that satisfy the given predicate.
func (c *CopyOnWriteList[T]) DeleteIf(f func(T) bool) {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
length := len(list)
var newList []T
for i := 0; i < length; i++ {
if !f(list[i]) {
newList = append(newList, list[i])
}
}
c.setList(newList)
}
// Equal returns true if the specified object is equal to this list.
func (c *CopyOnWriteList[T]) Equal(other *[]T) bool {
if other == nil {
return false
}
if c.Size() != len(*other) {
return false
}
list := c.getList()
otherList := NewCopyOnWriteList(*other).getList()
for i := 0; i < len(list); i++ {
if !reflect.DeepEqual(list[i], otherList[i]) {
return false
}
}
return true
}
// Clear removes all the elements from this list.
func (c *CopyOnWriteList[T]) Clear() {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
list = make([]T, 0)
c.setList(list)
}
// Merge a tow list to one, change the list
func (c *CopyOnWriteList[T]) Merge(other []T) {
lock := c.lock
lock.Lock()
defer lock.Unlock()
list := c.getList()
list = append(list, other...)
c.setList(list)
}
// ForEach performs the given action for each element of the Iterable until all elements have been processed
// or the action throws an exception.
func (c *CopyOnWriteList[T]) ForEach(f func(T)) {
list := c.getList()
for i := 0; i < len(list); i++ {
f(list[i])
}
}
// Sort sorts this list according to the order induced by the specified Comparator.
func (c *CopyOnWriteList[T]) Sort(compare func(o1 T, o2 T) bool) {
lock := c.lock
lock.Lock()
list := c.getList()
sort.Slice(list, func(i, j int) bool {
return compare(list[i], list[j])
})
c.setList(list)
}
func (c *CopyOnWriteList[T]) SubList(start int, end int) (newList []T) {
lock := c.lock
lock.Lock()
list := c.getList()
length := len(list)
defer lock.Unlock()
if start < 0 || end > length || start > end {
return []T{}
}
newList = make([]T, end-start)
copy(newList, list[start:end])
c.setList(newList)
return
}

View File

@@ -0,0 +1,235 @@
package datastructure
import (
"github.com/duke-git/lancet/v2/internal"
"testing"
)
func TestCopyOnWriteList_ValueOf(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_IndexOf")
of, ok := list.ValueOf(3)
assert.Equal(4, *of)
assert.Equal(true, ok)
_, ok = list.ValueOf(6)
assert.Equal(false, ok)
}
func TestCopyOnWriteList_Contain(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_Contains")
assert.Equal(true, list.Contain(3))
}
func TestCopyOnWriteList_IsEmpty(t *testing.T) {
list := NewCopyOnWriteList([]int{})
assert := internal.NewAssert(t, "CopyOnWriteList_IsEmpty")
assert.Equal(true, list.IsEmpty())
}
func TestCopyOnWriteList_Size(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_size")
assert.Equal(5, list.Size())
}
func TestCopyOnWriteList_GetList(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_GetList")
assert.Equal([]int{1, 2, 3, 4, 5}, list.getList())
}
func TestCopyOnWriteList_Get(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_Get")
i := list.Get(2)
assert.Equal(3, *i)
}
func TestCopyOnWriteList_Set(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_Set")
list.Set(2, 6)
assert.Equal(6, list.getList()[2])
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.Set(0, 6)
assert.Equal(6, list.getList()[0])
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.Set(0, 1)
assert.Equal(1, list.getList()[0])
}
func TestCopyOnWriteList_Add(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_Add")
list.Add(6)
assert.Equal([]int{1, 2, 3, 4, 5, 6}, list.getList())
}
func TestCopyOnWriteList_AddAll(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_AddAll")
list.AddAll([]int{6, 7, 8})
assert.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8}, list.getList())
}
func TestCopyOnWriteList_AddByIndex(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_AddByIndex")
list.AddByIndex(2, 6)
assert.Equal([]int{1, 2, 6, 3, 4, 5}, list.getList())
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.AddByIndex(0, 6)
assert.Equal([]int{6, 1, 2, 3, 4, 5}, list.getList())
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.AddByIndex(5, 6)
assert.Equal([]int{1, 2, 3, 4, 5, 6}, list.getList())
}
func TestCopyOnWriteList_DeleteAt2(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_RemoveByIndex")
list.DeleteAt(2)
assert.Equal([]int{1, 2, 4, 5}, list.getList())
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.DeleteAt(4)
assert.Equal([]int{1, 2, 3, 4}, list.getList())
}
func TestCopyOnWriteList_RemoveByValue(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_RemoveByValue")
list.DeleteBy(3)
assert.Equal([]int{1, 2, 4, 5}, list.getList())
}
func TestCopyOnWriteList_DeleteRange(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
assert := internal.NewAssert(t, "CopyOnWriteList_RemoveRange")
list.DeleteRange(1, 3)
assert.Equal([]int{1, 4, 5}, list.getList())
list = NewCopyOnWriteList([]int{1, 2, 3, 4, 5})
list.DeleteRange(0, 5)
assert.Equal([]int{}, list.getList())
}
func TestCopyOnWriteList_LastIndexOf(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3})
assert := internal.NewAssert(t, "CopyOnWriteList_LastIndexOf")
assert.Equal(5, list.LastIndexOf(3))
}
func TestCopyOnWriteList_DeleteAt(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3})
assert := internal.NewAssert(t, "CopyOnWriteList_DeleteAt")
list.DeleteAt(2)
assert.Equal([]int{1, 2, 4, 5, 3}, list.getList())
}
func TestCopyOnWriteList_DeleteBy(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3})
assert := internal.NewAssert(t, "CopyOnWriteList_DeleteBy")
list.DeleteBy(3)
assert.Equal([]int{1, 2, 4, 5, 3}, list.getList())
}
func TestCopyOnWriteList_DeleteIf(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3, 6})
assert := internal.NewAssert(t, "CopyOnWriteList_DeleteIf")
list.DeleteIf(func(i int) bool {
return i%2 == 0
})
assert.Equal([]int{1, 3, 5, 3}, list.getList())
}
func TestCopyOnWriteList_Equal(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3, 6})
assert := internal.NewAssert(t, "CopyOnWriteList_Equal")
assert.Equal(true, list.Equal(&[]int{1, 2, 3, 4, 5, 3, 6}))
}
func TestCopyOnWriteList_ForEach(t *testing.T) {
testList := make([]int, 0)
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3, 6})
assert := internal.NewAssert(t, "CopyOnWriteList_ForEach")
list.ForEach(func(i int) {
testList = append(testList, i)
})
assert.Equal([]int{1, 2, 3, 4, 5, 3, 6}, testList)
list.ForEach(func(i int) {
list.Add(i)
})
assert.Equal([]int{1, 2, 3, 4, 5, 3, 6, 1, 2, 3, 4, 5, 3, 6}, list.getList())
}
func TestCopyOnWriteList_Clear(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 2, 3, 4, 5, 3, 6})
assert := internal.NewAssert(t, "CopyOnWriteList_Clear")
list.Clear()
assert.Equal([]int{}, list.getList())
}
func TestCopyOnWriteList_Merge(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 3, 5, 7, 9})
assert := internal.NewAssert(t, "CopyOnWriteList_Merge")
list.Merge([]int{2, 4, 6, 8, 10})
assert.Equal([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10}, list.getList())
}
func TestCopyOnWriteList_Sort(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
assert := internal.NewAssert(t, "CopyOnWriteList_Sort")
list.Sort(func(i, j int) bool {
return i < j
})
assert.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, list.getList())
}
func TestCopyOnWriteList_IndexOf(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
assert := internal.NewAssert(t, "CopyOnWriteList_IndexOf")
assert.Equal(0, list.IndexOf(1))
assert.Equal(9, list.IndexOf(10))
assert.Equal(-1, list.IndexOf(11))
}
func TestCopyOnWriteList_SubList(t *testing.T) {
list := NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
assert := internal.NewAssert(t, "CopyOnWriteList_SubList")
list = NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
subList := list.SubList(1, 3)
assert.Equal([]int{3, 5}, subList)
list = NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
subList = list.SubList(1, 1)
assert.Equal([]int{}, subList)
list = NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
assert.Equal(10, list.Size())
subList = list.SubList(1, 10)
assert.Equal([]int{3, 5, 7, 9, 2, 4, 6, 8, 10}, subList)
list = NewCopyOnWriteList([]int{1, 3, 5, 7, 9, 2, 4, 6, 8, 10})
subList = list.SubList(11, 1)
assert.Equal([]int{}, subList)
}

View File

@@ -271,7 +271,7 @@ func (l *List[T]) Reverse() {
} }
} }
// Unique remove duplicate items in list. // Unique delete duplicate items in list.
func (l *List[T]) Unique() { func (l *List[T]) Unique() {
data := l.data data := l.data
size := len(data) size := len(data)
@@ -294,7 +294,7 @@ func (l *List[T]) Unique() {
l.data = uniqueData l.data = uniqueData
} }
// Union creates a new list contain all element in list l and other, remove duplicate element. // Union creates a new list contain all element in list l and other, delete duplicate element.
func (l *List[T]) Union(other *List[T]) *List[T] { func (l *List[T]) Union(other *List[T]) *List[T] {
result := NewList([]T{}) result := NewList([]T{})

View File

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

89
docs/.vitepress/common.ts Normal file
View File

@@ -0,0 +1,89 @@
import { defineConfig, HeadConfig } from 'vitepress'
export const META_IMAGE = '/lancet_logo.png'
export const isProduction = process.env.NETLIFY && process.env.CONTEXT === 'production'
if (process.env.NETLIFY) {
console.log('Netlify build', process.env.CONTEXT)
}
const productionHead: HeadConfig[] = [
[
'script',
{
src: 'https://unpkg.com/thesemetrics@latest',
async: '',
type: 'text/javascript',
},
],
]
const rControl = /[\u0000-\u001f]/g
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’<>,.?/]+/g
const rCombining = /[\u0300-\u036F]/g
/**
* Default slugification function
*/
export const slugify = (str: string): string =>
str
.normalize('NFKD')
// Remove accents
.replace(rCombining, '')
// Remove control characters
.replace(rControl, '')
// Replace special characters
.replace(rSpecial, '-')
// ensure it doesn't start with a number
.replace(/^(\d)/, '_$1')
export const commonConfig = defineConfig({
title: 'Lancet',
appearance: 'dark',
markdown: {
theme: {
dark: 'dracula-soft',
light: 'vitesse-light',
},
attrs: {
leftDelimiter: '%{',
rightDelimiter: '}%',
},
anchor: {
slugify,
},
},
head: [
// ['link', { rel: 'icon', type: 'image/svg+xml', href: '/logo.svg' }],
['link', { rel: 'icon', type: 'image/png', href: '/lancet_logo_mini.png' }],
['meta', { name: 'theme-color', content: '#5f67ee' }],
['meta', { name: 'og:type', content: 'website' }],
['meta', { name: 'og:locale', content: 'zh' }],
...(isProduction ? productionHead : []),
],
themeConfig: {
logo: { src: '/lancet_logo_mini.png', width: 24, height: 24 },
outline: [2, 3],
search: {
provider: 'local',
},
socialLinks: [
{
icon: 'github',
link: 'https://github.com/duke-git/lancet',
},
],
footer: {
copyright: 'Copyright © 2023-present Duke Du',
message: 'Released under the MIT License.',
},
},
})

View File

@@ -0,0 +1,14 @@
import { defineConfig } from 'vitepress'
import { commonConfig } from './common'
import { zhConfig } from './zh'
import { enConfig } from './en'
// https://vitepress.dev/reference/site-config
export default defineConfig({
...commonConfig,
locales: {
root: { label: '简体中文', lang: 'zh-CN', link: '/', ...zhConfig },
en: { label: 'English', lang: 'en-US', link: '/en/', ...enConfig },
},
})

102
docs/.vitepress/en.ts Normal file
View File

@@ -0,0 +1,102 @@
import type { DefaultTheme, LocaleSpecificConfig } from 'vitepress'
export const META_URL = 'https://golancet.dev'
export const META_TITLE = 'Lancet'
export const META_DESCRIPTION = 'A powerful util function library of Go'
export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
description: META_DESCRIPTION,
head: [
['meta', { property: 'og:url', content: META_URL }],
['meta', { property: 'og:description', content: META_DESCRIPTION }],
['meta', { property: 'twitter:url', content: META_URL }],
['meta', { property: 'twitter:title', content: META_TITLE }],
['meta', { property: 'twitter:description', content: META_DESCRIPTION }],
],
themeConfig: {
nav: [
{
text: 'Home',
link: '/en/',
activeMatch: '^/en/',
},
{
text: 'Guide',
link: '/en/guide/introduction',
activeMatch: '^/en/guide/',
},
{ text: 'API', link: '/en/api/overview', activeMatch: '^/en/api/' },
{
text: 'Links',
items: [
{
text: 'Releaselog',
link: 'https://github.com/duke-git/lancet/releases',
},
],
},
],
sidebar: {
'/en/': [
{
text: 'Introduction',
collapsed: false,
items: [
{
text: 'What is Lancet',
link: '/en/guide/introduction',
},
{
text: 'getting started',
link: '/en/guide/getting_started',
},
],
},
],
'/en/api/': [
{
text: 'overview',
items: [{ text: 'overview of API', link: '/en/api/overview' }],
},
{
text: 'packages',
collapsed: false,
items: [
{ text: 'algorithm', link: '/en/api/packages/algorithm' },
{ text: 'compare', link: '/en/api/packages/compare' },
{ text: 'concurrency', link: '/en/api/packages/concurrency' },
{ text: 'condition', link: '/en/api/packages/condition' },
{ text: 'convertor', link: '/en/api/packages/convertor' },
{ text: 'cryptor', link: '/en/api/packages/cryptor' },
{
text: 'datastructure',
collapsed: true,
items: [
{ text: 'list', link: '/en/api/packages/datastructure/list' },
{ text: 'safelist', link: '/en/api/packages/datastructure/copyonwritelist' },
{ text: 'link', link: '/en/api/packages/datastructure/link' },
{ text: 'stack', link: '/en/api/packages/datastructure/stack' },
{ text: 'queue', link: '/en/api/packages/datastructure/queue' },
{ text: 'heap', link: '/en/api/packages/datastructure/heap' },
{ text: 'tree', link: '/en/api/packages/datastructure/tree' },
{ text: 'set', link: '/en/api/packages/datastructure/set' },
{ text: 'hashmap', link: '/en/api/packages/datastructure/hashmap' },
],
},
{ text: 'datetime', link: '/en/api/packages/datetime' },
{ text: 'fileutil', link: '/en/api/packages/fileutil' },
{ text: 'formatter', link: '/en/api/packages/formatter' },
{ text: 'function', link: '/en/api/packages/function' },
{ text: 'mathutil', link: '/en/api/packages/mathutil' },
{ text: 'maputil', link: '/en/api/packages/maputil' },
{ text: 'netutil', link: '/en/api/packages/netutil' },
{ text: 'pointer', link: '/en/api/packages/pointer' },
],
},
],
},
},
}

115
docs/.vitepress/zh.ts Normal file
View File

@@ -0,0 +1,115 @@
import type { DefaultTheme, LocaleSpecificConfig } from 'vitepress'
export const META_URL = 'https://lancet.dev'
export const META_TITLE = 'Lancet'
export const META_DESCRIPTION = '一个强大的Go语言工具函数库'
export const zhConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
description: META_DESCRIPTION,
head: [
['meta', { property: 'og:url', content: META_URL }],
['meta', { property: 'og:description', content: META_DESCRIPTION }],
['meta', { property: 'twitter:url', content: META_URL }],
['meta', { property: 'twitter:title', content: META_TITLE }],
['meta', { property: 'twitter:description', content: META_DESCRIPTION }],
],
themeConfig: {
outline: {
label: '本页内容',
},
docFooter: {
prev: '上一页',
next: '下一页',
},
nav: [
{
text: '首页',
link: '/',
activeMatch: '^/',
},
{
text: '指南',
link: '/guide/introduction',
activeMatch: '^/guide/',
},
{ text: 'API', link: '/api/overview', activeMatch: '^/api/' },
{
text: '相关链接',
items: [
{
text: '更新日志',
link: 'https://github.com/duke-git/lancet/releases',
},
],
},
],
sidebar: {
'/': [
{
text: '介绍',
collapsed: true,
items: [
{
text: 'Lancet是什么',
link: '/guide/introduction',
},
{
text: '开始',
link: '/guide/getting_started',
},
],
},
],
'/api/': [
{
text: '概览',
items: [{ text: 'API概述', link: '/api/overview' }],
},
{
text: 'API文档',
collapsed: false,
items: [
{ text: '算法', link: '/api/packages/algorithm' },
{ text: '比较器', link: '/api/packages/compare' },
{ text: '并发处理', link: '/api/packages/concurrency' },
{ text: '条件判断', link: '/api/packages/condition' },
{ text: '类型转换', link: '/api/packages/convertor' },
{ text: '加密&解密', link: '/api/packages/cryptor' },
{
text: '数据结构',
collapsed: true,
items: [
{ text: '线性表', link: '/api/packages/datastructure/list' },
{
text: '线性表(线程安全)',
link: '/api/packages/datastructure/copyonwritelist',
},
{ text: '链表', link: '/api/packages/datastructure/link' },
{ text: '栈', link: '/api/packages/datastructure/stack' },
{ text: '队列', link: '/api/packages/datastructure/queue' },
{ text: '堆', link: '/api/packages/datastructure/heap' },
{ text: '树', link: '/api/packages/datastructure/tree' },
{ text: '集合', link: '/api/packages/datastructure/set' },
{ text: 'HashMap', link: '/api/packages/datastructure/hashmap' },
],
},
{ text: '日期&时间', link: '/api/packages/datetime' },
{ text: '文件', link: '/api/packages/fileutil' },
{ text: '格式化工具', link: '/api/packages/formatter' },
{ text: '函数', link: '/api/packages/function' },
{ text: '数学工具', link: '/api/packages/mathutil' },
{ text: 'Map', link: '/api/packages/maputil' },
{ text: '网络', link: '/api/packages/netutil' },
{ text: '指针', link: '/api/packages/pointer' },
],
},
],
},
},
}

69
docs/api/overview.md Normal file
View File

@@ -0,0 +1,69 @@
---
outline: deep
---
# API概述
<b>lancet柳叶刀是一个强大、全面、高效、可复用的go语言工具函数库。包含25个包超过600个工具函数。功能涵盖字符串处理、切片处理、网络、并发、加解密、文件处理、时间/日期、流处理、迭代器等等。</b>
<style>
.package-title {
color: black;
font-size: 18px;
text-align: center;
font-weight: bold;
}
.package-container {
font-size: 16px;
border: 1px dashed;
padding: 10px;
text-align: center;
}
.package-cell {
height: 40px;
width: 140px;
display: inline-block;
vertical-align: middle;
line-height: 40px;
background: #ecefff;
border: 1px solid;
margin-right: 10px;
margin-bottom: 10px;
border-radius: 6px;
font-weight: bold;
}
</style>
<div>
<p class="package-title">lancet功能模块</p>
<div class="package-container">
<div class="package-cell">algorithm</div>
<div class="package-cell">compare</div>
<div class="package-cell">concurrency</div>
<div class="package-cell">condition</div>
<div class="package-cell">convertor</div>
<div class="package-cell">cryptor</div>
<div class="package-cell">datastructure</div>
<div class="package-cell">datetime</div>
<div class="package-cell">fileutil</div>
<div class="package-cell">formatter</div>
<div class="package-cell">function</div>
<div class="package-cell">iterator</div>
<div class="package-cell">maputil</div>
<div class="package-cell">mathutil</div>
<div class="package-cell">netutil</div>
<div class="package-cell">pointer</div>
<div class="package-cell">random</div>
<div class="package-cell">retry</div>
<div class="package-cell">slice</div>
<div class="package-cell">stream</div>
<div class="package-cell">structs</div>
<div class="package-cell">strutil</div>
<div class="package-cell">system</div>
<div class="package-cell">tuple</div>
<div class="package-cell">validator</div>
<div class="package-cell">xerror</div>
</div>
</div>

View File

@@ -39,6 +39,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="BubbleSort">BubbleSort</span> ### <span id="BubbleSort">BubbleSort</span>
@@ -51,7 +53,7 @@ import (
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
```go ```go
package main package main
@@ -99,7 +101,7 @@ func main() {
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
```go ```go
package main package main
@@ -162,7 +164,7 @@ func main() {
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/oXovbkekayS)</span></b>
```go ```go
package main package main
@@ -210,7 +212,7 @@ func main() {
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/3ibkszpJEu3)</span></b>
```go ```go
package main package main
@@ -258,7 +260,7 @@ func main() {
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
```go ```go
package main package main
@@ -306,7 +308,7 @@ func main() {
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
```go ```go
package main package main
@@ -354,7 +356,7 @@ func main() {
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ydinn9YzUJn)</span></b>
```go ```go
package main package main
@@ -402,7 +404,7 @@ func main() {
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
```go ```go
package main package main
@@ -451,7 +453,7 @@ func main() {
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/t6MeGiUSN47)</span></b>
```go ```go
package main package main
@@ -502,7 +504,7 @@ func main() {
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
```go ```go
package main package main
@@ -553,7 +555,7 @@ func main() {
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
```go ```go
package main package main
@@ -596,7 +598,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int func (l *LRUCache[K, V]) Len() int
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/-EZjgOURufP)</span></b>
```go ```go
package main package main

View File

@@ -30,10 +30,14 @@ import (
- [GreaterThan](#GreaterThan) - [GreaterThan](#GreaterThan)
- [LessOrEqual](#LessOrEqual) - [LessOrEqual](#LessOrEqual)
- [GreaterOrEqual](#GreaterOrEqual) - [GreaterOrEqual](#GreaterOrEqual)
- [InDelta](#InDelta)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation <link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档
### <span id="Equal">Equal</span> ### <span id="Equal">Equal</span>
@@ -45,7 +49,7 @@ import (
func Equal(left, right any) bool func Equal(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/wmVxR-to4lz)</span></b>
```go ```go
package main package main
@@ -94,7 +98,7 @@ func main() {
func EqualValue(left, right any) bool func EqualValue(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/fxnna_LLD9u)</span></b>
```go ```go
package main package main
@@ -133,7 +137,7 @@ func main() {
func LessThan(left, right any) bool func LessThan(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
```go ```go
package main package main
@@ -182,7 +186,7 @@ func main() {
func GreaterThan(left, right any) bool func GreaterThan(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
```go ```go
package main package main
@@ -234,7 +238,7 @@ func main() {
func LessOrEqual(left, right any) bool func LessOrEqual(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/e4T_scwoQzp)</span></b>
```go ```go
package main package main
@@ -283,7 +287,7 @@ func main() {
func GreaterOrEqual(left, right any) bool func GreaterOrEqual(left, right any) bool
``` ```
<b>示例:</b> <b>示例: <span class="run-container">[运行](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
```go ```go
package main package main
@@ -324,3 +328,50 @@ func main() {
// false // false
} }
``` ```
### <span id="InDelta">InDelta</span>
<p>检查增量内两个值是否相等。</p>
<b>函数签名:</b>
```go
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool
```
<b>示例: <span class="run-container">[运行](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/compare"
)
func main() {
result1 := InDelta(1, 1, 0)
result2 := InDelta(1, 2, 0)
result3 := InDelta(2.0/3.0, 0.66667, 0.001)
result4 := InDelta(2.0/3.0, 0.0, 0.001)
result5 := InDelta(float64(74.96)-float64(20.48), 54.48, 0)
result6 := InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
// Output:
// true
// false
// true
// false
// false
// true
}
```

View File

@@ -33,11 +33,15 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### Channel ### Channel
### <span id="NewChannel">NewChannel</span> ### <span id="NewChannel">NewChannel</span>
<p>返回一个Channel指针实例</p> <p>返回一个Channel指针实例</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -46,7 +50,7 @@ import (
type Channel[T any] struct type Channel[T any] struct
func NewChannel[T any]() *Channel[T] func NewChannel[T any]() *Channel[T]
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/7aB4KyMMp9A)</span></b>
```go ```go
package main package main
@@ -70,7 +74,7 @@ func main() {
```go ```go
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/qmWSy1NVF-Y)</span></b>
```go ```go
package main package main
@@ -122,7 +126,7 @@ func main() {
```go ```go
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/2VYFMexEvTm)</span></b>
```go ```go
package main package main
@@ -161,7 +165,7 @@ func main() {
```go ```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/7aB4KyMMp9A)</span></b>
```go ```go
package main package main
@@ -199,7 +203,7 @@ func main() {
```go ```go
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/k5N_ALVmYjE)</span></b>
```go ```go
package main package main
@@ -238,7 +242,7 @@ func main() {
```go ```go
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/4J1zAWttP85)</span></b>
```go ```go
package main package main
@@ -279,7 +283,7 @@ func main() {
```go ```go
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T func (c *Channel[T]) Or(channels ...<-chan T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Wqz9rwioPww)</span></b>
```go ```go
package main package main
@@ -322,7 +326,7 @@ func main() {
```go ```go
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/lm_GoS6aDjo)</span></b>
```go ```go
package main package main
@@ -360,7 +364,7 @@ func main() {
```go ```go
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9Utt-1pDr2J)</span></b>
```go ```go
package main package main
@@ -406,7 +410,7 @@ func main() {
```go ```go
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/3TQPKnCirrP)</span></b>
```go ```go
package main package main

View File

@@ -18,7 +18,7 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## 目录
- [Bool](#Bool) - [Bool](#Bool)
- [And](#And) - [And](#And)
@@ -31,7 +31,9 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 <link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档
### <span id="Bool">Bool</span> ### <span id="Bool">Bool</span>
<p>返回传入参数的bool值.<br/> <p>返回传入参数的bool值.<br/>
@@ -45,7 +47,7 @@ slices和map的length大于0时返回true否则返回false<br/>
```go ```go
func Bool[T any](value T) bool func Bool[T any](value T) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ETzeDJRSvhm)</span></b>
```go ```go
package main package main
@@ -109,7 +111,7 @@ func main() {
```go ```go
func And[T, U any](a T, b U) bool func And[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/W1SSUmt6pvr)</span></b>
```go ```go
package main package main
@@ -135,7 +137,7 @@ func main() {
```go ```go
func Or[T, U any](a T, b U) bool func Or[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/UlQTxHaeEkq)</span></b>
```go ```go
package main package main
@@ -161,7 +163,7 @@ func main() {
```go ```go
func Xor[T, U any](a T, b U) bool func Xor[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/gObZrW7ZbG8)</span></b>
```go ```go
package main package main
@@ -187,7 +189,7 @@ func main() {
```go ```go
func Nor[T, U any](a T, b U) bool func Nor[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/g2j08F_zZky)</span></b>
```go ```go
package main package main
@@ -213,7 +215,7 @@ func main() {
```go ```go
func Xnor[T, U any](a T, b U) bool func Xnor[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/OuDB9g51643)</span></b>
```go ```go
package main package main
@@ -239,7 +241,7 @@ func main() {
```go ```go
func Nand[T, U any](a T, b U) bool func Nand[T, U any](a T, b U) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/vSRMLxLIbq8)</span></b>
```go ```go
package main package main
@@ -265,7 +267,7 @@ func main() {
```go ```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ElllPZY0guT)</span></b>
```go ```go
package main package main

View File

@@ -46,6 +46,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="ColorHexToRGB">ColorHexToRGB</span> ### <span id="ColorHexToRGB">ColorHexToRGB</span>
@@ -58,7 +60,7 @@ import (
func ColorHexToRGB(colorHex string) (red, green, blue int) func ColorHexToRGB(colorHex string) (red, green, blue int)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/o7_ft-JCJBV)</span></b>
```go ```go
package main package main
@@ -89,7 +91,7 @@ func main() {
func ColorRGBToHex(red, green, blue int) string func ColorRGBToHex(red, green, blue int) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nzKS2Ro87J1)</span></b>
```go ```go
package main package main
@@ -122,7 +124,7 @@ func main() {
func ToBool(s string) (bool, error) func ToBool(s string) (bool, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ARht2WnGdIN)</span></b>
```go ```go
package main package main
@@ -163,7 +165,7 @@ func main() {
func ToBytes(data any) ([]byte, error) func ToBytes(data any) ([]byte, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/fAMXYFDvOvr)</span></b>
```go ```go
package main package main
@@ -196,7 +198,7 @@ func main() {
func ToChar(s string) []string func ToChar(s string) []string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/JJ1SvbFkVdM)</span></b>
```go ```go
package main package main
@@ -232,7 +234,7 @@ func main() {
func ToChannel[T any](array []T) <-chan T func ToChannel[T any](array []T) <-chan T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/hOx_oYZbAnL)</span></b>
```go ```go
package main package main
@@ -269,7 +271,7 @@ func main() {
func ToFloat(value any) (float64, error) func ToFloat(value any) (float64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/4YTmPCibqHJ)</span></b>
```go ```go
package main package main
@@ -314,7 +316,7 @@ func main() {
func ToInt(value any) (int64, error) func ToInt(value any) (int64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9_h9vIt-QZ_b)</span></b>
```go ```go
package main package main
@@ -356,7 +358,7 @@ func main() {
func ToJson(value any) (string, error) func ToJson(value any) (string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/2rLIkMmXWvR)</span></b>
```go ```go
package main package main
@@ -391,7 +393,7 @@ func main() {
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tVFy7E-t24l)</span></b>
```go ```go
package main package main
@@ -432,7 +434,7 @@ func main() {
func ToPointer[T any](value T) *T func ToPointer[T any](value T) *T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ASf_etHNlw1)</span></b>
```go ```go
package main package main
@@ -461,7 +463,7 @@ func main() {
func ToString(value any) string func ToString(value any) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nF1zOOslpQq)</span></b>
```go ```go
package main package main
@@ -509,7 +511,7 @@ func main() {
func StructToMap(value any) (map[string]any, error) func StructToMap(value any) (map[string]any, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/KYGYJqNUBOI)</span></b>
```go ```go
package main package main
@@ -547,7 +549,7 @@ func main() {
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/dmX4Ix5V6Wl)</span></b>
```go ```go
package main package main
@@ -577,7 +579,7 @@ func main() {
func EncodeByte(data any) ([]byte, error) func EncodeByte(data any) ([]byte, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/DVmM1G5JfuP)</span></b>
```go ```go
package main package main
@@ -606,7 +608,7 @@ func main() {
func DecodeByte(data []byte, target any) error func DecodeByte(data []byte, target any) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -642,7 +644,7 @@ func main() {
func DeepClone[T any](src T) T func DeepClone[T any](src T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/j4DP5dquxnk)</span></b>
```go ```go
package main package main
@@ -706,7 +708,7 @@ func main() {
func CopyProperties[T, U any](dst T, src U) (err error) func CopyProperties[T, U any](dst T, src U) (err error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/oZujoB5Sgg5)</span></b>
```go ```go
package main package main
@@ -785,7 +787,7 @@ func main() {
func ToInterface(v reflect.Value) (value interface{}, ok bool) func ToInterface(v reflect.Value) (value interface{}, ok bool)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/syqw0-WG7Xd)</span></b>
```go ```go
package main package main
@@ -820,7 +822,7 @@ func main() {
func Utf8ToGbk(bs []byte) ([]byte, error) func Utf8ToGbk(bs []byte) ([]byte, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9FlIaFLArIL)</span></b>
```go ```go
package main package main
@@ -854,7 +856,7 @@ func main() {
func GbkToUtf8(bs []byte) ([]byte, error) func GbkToUtf8(bs []byte) ([]byte, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/OphmHCN_9u8)</span></b>
```go ```go
package main package main

View File

@@ -1,16 +1,18 @@
# Cryptor # Cryptor
cryptor加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。
cryptor包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [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/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.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>
## 用法: ## 用法:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/cryptor" "github.com/duke-git/lancet/v2/cryptor"
@@ -20,56 +22,56 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [AesEcbEncrypt](#AesEcbEncrypt)
- [AesEcbDecrypt](#AesEcbDecrypt)
- [AesCbcEncrypt](#AesCbcEncrypt)
- [AesCbcDecrypt](#AesCbcDecrypt)
- [AesCtrCrypt](#AesCtrCrypt)
- [AesCfbEncrypt](#AesCfbEncrypt)
- [AesCfbDecrypt](#AesCfbDecrypt)
- [AesOfbEncrypt](#AesOfbEncrypt)
- [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt)
- [DesCbcDecrypt](#DesCbcDecrypt)
- [DesCtrCrypt](#DesCtrCrypt)
- [DesCfbEncrypt](#DesCfbEncrypt)
- [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5)
- [HmacMd5WithBase64](#HmacMd5WithBase64)
- [HmacSha1](#HmacSha1)
- [HmacSha1WithBase64](#HmacSha1WithBase64)
- [HmacSha256](#HmacSha256)
- [HmacSha256WithBase64](#HmacSha256WithBase64)
- [HmacSha512](#HmacSha512)
- [HmacSha512WithBase64](#HmacSha512WithBase64)
- [Md5String](#Md5String)
- [Md5StringWithBase64](#Md5StringWithBase64)
- [Md5Byte](#Md5Byte)
- [Md5ByteWithBase64](#Md5ByteWithBase64)
- [Md5File](#Md5File)
- [Sha1](#Sha1)
- [Sha1WithBase64](#Sha1WithBase64)
- [Sha256](#Sha256)
- [Sha256WithBase64](#Sha256WithBase64)
- [Sha512](#Sha512)
- [Sha512WithBase64](#Sha512WithBase64)
- [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt)
- [AesEcbEncrypt](#AesEcbEncrypt)
- [AesEcbDecrypt](#AesEcbDecrypt)
- [AesCbcEncrypt](#AesCbcEncrypt)
- [AesCbcDecrypt](#AesCbcDecrypt)
- [AesCtrCrypt](#AesCtrCrypt)
- [AesCfbEncrypt](#AesCfbEncrypt)
- [AesCfbDecrypt](#AesCfbDecrypt)
- [AesOfbEncrypt](#AesOfbEncrypt)
- [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt)
- [DesCbcDecrypt](#DesCbcDecrypt)
- [DesCtrCrypt](#DesCtrCrypt)
- [DesCfbEncrypt](#DesCfbEncrypt)
- [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5)
- [HmacMd5WithBase64](#HmacMd5WithBase64)
- [HmacSha1](#HmacSha1)
- [HmacSha1WithBase64](#HmacSha1WithBase64)
- [HmacSha256](#HmacSha256)
- [HmacSha256WithBase64](#HmacSha256WithBase64)
- [HmacSha512](#HmacSha512)
- [HmacSha512WithBase64](#HmacSha512WithBase64)
- [Md5String](#Md5String)
- [Md5StringWithBase64](#Md5StringWithBase64)
- [Md5Byte](#Md5Byte)
- [Md5ByteWithBase64](#Md5ByteWithBase64)
- [Md5File](#Md5File)
- [Sha1](#Sha1)
- [Sha1WithBase64](#Sha1WithBase64)
- [Sha256](#Sha256)
- [Sha256WithBase64](#Sha256WithBase64)
- [Sha512](#Sha512)
- [Sha512WithBase64](#Sha512WithBase64)
- [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="AesEcbEncrypt">AesEcbEncrypt</span> ### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
<p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p> <p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
@@ -79,7 +81,8 @@ import (
```go ```go
func AesEcbEncrypt(data, key []byte) []byte func AesEcbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b>
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -112,7 +115,8 @@ func main() {
```go ```go
func AesEcbDecrypt(encrypted, key []byte) []byte func AesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b>
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -145,7 +149,8 @@ func main() {
```go ```go
func AesCbcEncrypt(data, key []byte) []byte func AesCbcEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b>
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
package main package main
@@ -179,7 +184,7 @@ func main() {
func AesCbcDecrypt(encrypted, key []byte) []byte func AesCbcDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
package main package main
@@ -213,7 +218,7 @@ func main() {
func AesCtrCrypt(data, key []byte) []byte func AesCtrCrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/SpaZO0-5Nsp)</span></b>
```go ```go
package main package main
@@ -247,7 +252,7 @@ func main() {
func AesCfbEncrypt(data, key []byte) []byte func AesCfbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tfkF10B13kH)</span></b>
```go ```go
package main package main
@@ -281,7 +286,7 @@ func main() {
func AesCfbDecrypt(encrypted, key []byte) []byte func AesCfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tfkF10B13kH)</span></b>
```go ```go
package main package main
@@ -315,7 +320,7 @@ func main() {
func AesOfbEncrypt(data, key []byte) []byte func AesOfbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/VtHxtkUj-3F)</span></b>
```go ```go
package main package main
@@ -338,6 +343,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="AesCfbDecrypt">AesOfbDecrypt</span> ### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
<p>使用AES OFB算法模式解密数据参数`key`的长度是16, 24 or 32。</p> <p>使用AES OFB算法模式解密数据参数`key`的长度是16, 24 or 32。</p>
@@ -348,7 +354,7 @@ func main() {
func AesOfbDecrypt(encrypted, key []byte) []byte func AesOfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/VtHxtkUj-3F)</span></b>
```go ```go
package main package main
@@ -381,7 +387,8 @@ func main() {
```go ```go
func Base64StdEncode(s string) string func Base64StdEncode(s string) string
``` ```
<b>示例:</b>
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/VOaUyQUreoK)</span></b>
```go ```go
package main package main
@@ -399,6 +406,7 @@ func main() {
// aGVsbG8= // aGVsbG8=
} }
``` ```
### <span id="Base64StdDecode">Base64StdDecode</span> ### <span id="Base64StdDecode">Base64StdDecode</span>
<p>解码base64字符串。</p> <p>解码base64字符串。</p>
@@ -409,7 +417,7 @@ func main() {
func Base64StdDecode(s string) string func Base64StdDecode(s string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/RWQylnJVgIe)</span></b>
```go ```go
package main package main
@@ -438,7 +446,7 @@ func main() {
func DesEcbEncrypt(data, key []byte) []byte func DesEcbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/8qivmPeZy4P)</span></b>
```go ```go
package main package main
@@ -462,6 +470,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesEcbDecrypt">DesEcbDecrypt</span> ### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
<p>使用DES ECB算法模式解决密数据参数`key`的长度是8。</p> <p>使用DES ECB算法模式解决密数据参数`key`的长度是8。</p>
@@ -472,7 +481,7 @@ func main() {
func DesEcbDecrypt(encrypted, key []byte) []byte func DesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/8qivmPeZy4P)</span></b>
```go ```go
package main package main
@@ -507,7 +516,7 @@ func main() {
func DesCbcEncrypt(data, key []byte) []byte func DesCbcEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/4cC4QvWfe3_1)</span></b>
```go ```go
package main package main
@@ -541,7 +550,7 @@ func main() {
func DesCbcDecrypt(encrypted, key []byte) []byte func DesCbcDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/4cC4QvWfe3_1)</span></b>
```go ```go
package main package main
@@ -564,6 +573,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCtrCrypt">DesCtrCrypt</span> ### <span id="DesCtrCrypt">DesCtrCrypt</span>
<p>使用DES CTR算法模式加密/解密数据,参数`key`的长度是8</p> <p>使用DES CTR算法模式加密/解密数据,参数`key`的长度是8</p>
@@ -574,7 +584,7 @@ func main() {
func DesCtrCrypt(data, key []byte) []byte func DesCtrCrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9-T6OjKpcdw)</span></b>
```go ```go
package main package main
@@ -608,7 +618,7 @@ func main() {
func DesCfbEncrypt(data, key []byte) []byte func DesCfbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/y-eNxcFBlxL)</span></b>
```go ```go
package main package main
@@ -631,6 +641,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCfbDecrypt">DesCfbDecrypt</span> ### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
<p>使用DES CFB算法模式解决密数据参数`key`的长度是8。</p> <p>使用DES CFB算法模式解决密数据参数`key`的长度是8。</p>
@@ -641,7 +652,7 @@ func main() {
func DesCfbDecrypt(encrypted, key []byte) []byte func DesCfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/y-eNxcFBlxL)</span></b>
```go ```go
package main package main
@@ -664,6 +675,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbEncrypt">DesOfbEncrypt</span> ### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
<p>使用DES OFB算法模式加密数据参数`key`的长度是8。</p> <p>使用DES OFB算法模式加密数据参数`key`的长度是8。</p>
@@ -674,7 +686,7 @@ func main() {
func DesOfbEncrypt(data, key []byte) []byte func DesOfbEncrypt(data, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/74KmNadjN1J)</span></b>
```go ```go
package main package main
@@ -697,6 +709,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbDecrypt">DesOfbDecrypt</span> ### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
<p>使用DES OFB算法模式解密数据参数`key`的长度是8。</p> <p>使用DES OFB算法模式解密数据参数`key`的长度是8。</p>
@@ -707,7 +720,7 @@ func main() {
func DesOfbDecrypt(encrypted, key []byte) []byte func DesOfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/74KmNadjN1J)</span></b>
```go ```go
package main package main
@@ -741,7 +754,7 @@ func main() {
func HmacMd5(str, key string) string func HmacMd5(str, key string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main
@@ -773,7 +786,7 @@ func main() {
func HmacMd5WithBase64(str, key string) string func HmacMd5WithBase64(str, key string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/UY0ng2AefFC)</span></b>
```go ```go
package main package main
@@ -794,6 +807,7 @@ func main() {
// 6DQwbquJLYclJdSRinpjmg== // 6DQwbquJLYclJdSRinpjmg==
} }
``` ```
### <span id="HmacSha1">HmacSha1</span> ### <span id="HmacSha1">HmacSha1</span>
<p>获取字符串的sha1 hmac值。</p> <p>获取字符串的sha1 hmac值。</p>
@@ -804,7 +818,7 @@ func main() {
func HmacSha1(str, key string) string func HmacSha1(str, key string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/1UI4oQ4WXKM)</span></b>
```go ```go
package main package main
@@ -836,7 +850,7 @@ func main() {
func HmacSha1WithBase64(str, key string) string func HmacSha1WithBase64(str, key string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/47JmmGrnF7B)</span></b>
```go ```go
package main package main
@@ -858,7 +872,6 @@ func main() {
} }
``` ```
### <span id="HmacSha256">HmacSha256</span> ### <span id="HmacSha256">HmacSha256</span>
<p>获取字符串sha256 hmac值。</p> <p>获取字符串sha256 hmac值。</p>
@@ -869,7 +882,7 @@ func main() {
func HmacSha256(str, key string) string func HmacSha256(str, key string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/HhpwXxFhhC0)</span></b>
```go ```go
package main package main
@@ -901,7 +914,7 @@ func main() {
func HmacSha256WithBase64(str, key string) string func HmacSha256WithBase64(str, key string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/EKbkUvPTLwO)</span></b>
```go ```go
package main package main
@@ -933,7 +946,7 @@ func main() {
func HmacSha512(str, key string) string func HmacSha512(str, key string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/59Od6m4A0Ud)</span></b>
```go ```go
package main package main
@@ -965,7 +978,7 @@ func main() {
func HmacSha512WithBase64(str, key string) string func HmacSha512WithBase64(str, key string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/c6dSe3E2ydU)</span></b>
```go ```go
package main package main
@@ -987,7 +1000,6 @@ func main() {
} }
``` ```
### <span id="Md5String">Md5String</span> ### <span id="Md5String">Md5String</span>
<p>获取字符串md5值。</p> <p>获取字符串md5值。</p>
@@ -998,7 +1010,7 @@ func main() {
func Md5String(str string) string func Md5String(str string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/1bLcVetbTOI)</span></b>
```go ```go
package main package main
@@ -1029,7 +1041,7 @@ func main() {
func Md5StringWithBase64(s string) string func Md5StringWithBase64(s string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Tcb-Z7LN2ax)</span></b>
```go ```go
package main package main
@@ -1058,7 +1070,7 @@ func main() {
func Md5Byte(data []byte) string func Md5Byte(data []byte) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/suraalH8lyC)</span></b>
```go ```go
package main package main
@@ -1087,7 +1099,7 @@ func main() {
func Md5ByteWithBase64(data []byte) string func Md5ByteWithBase64(data []byte) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/Lx4gH7Vdr5_y)</span></b>
```go ```go
package main package main
@@ -1142,7 +1154,7 @@ func main() {
func Sha1(str string) string func Sha1(str string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/_m_uoD1deMT)</span></b>
```go ```go
package main package main
@@ -1173,7 +1185,7 @@ func main() {
func Sha1WithBase64(str string) string func Sha1WithBase64(str string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/fSyx-Gl2l2-)</span></b>
```go ```go
package main package main
@@ -1202,7 +1214,7 @@ func main() {
func Sha256(str string) string func Sha256(str string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tU9tfBMIAr1)</span></b>
```go ```go
package main package main
@@ -1233,7 +1245,7 @@ func main() {
func Sha256WithBase64(str string) string func Sha256WithBase64(str string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/85IXJHIal1k)</span></b>
```go ```go
package main package main
@@ -1262,7 +1274,7 @@ func main() {
func Sha512(str string) string func Sha512(str string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/3WsvLYZxsHa)</span></b>
```go ```go
package main package main
@@ -1293,7 +1305,7 @@ func main() {
func Sha512WithBase64(str string) string func Sha512WithBase64(str string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/q_fY2rA-k5I)</span></b>
```go ```go
package main package main
@@ -1322,7 +1334,7 @@ func main() {
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/zutRHrDqs0X)</span></b>
```go ```go
package main package main
@@ -1350,7 +1362,7 @@ func main() {
func RsaEncrypt(data []byte, pubKeyFileName string) []byte func RsaEncrypt(data []byte, pubKeyFileName string) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main
@@ -1365,11 +1377,11 @@ func main() {
if err != nil { if err != nil {
return return
} }
data := []byte("hello") data := []byte("hello")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) fmt.Println(string(decrypted))
// Output: // Output:
@@ -1377,7 +1389,6 @@ func main() {
} }
``` ```
### <span id="RsaDecrypt">RsaDecrypt</span> ### <span id="RsaDecrypt">RsaDecrypt</span>
<p>用私钥文件rsa解密数据。</p> <p>用私钥文件rsa解密数据。</p>
@@ -1388,7 +1399,7 @@ func main() {
func RsaDecrypt(data []byte, privateKeyFileName string) []byte func RsaDecrypt(data []byte, privateKeyFileName string) []byte
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main
@@ -1403,14 +1414,14 @@ func main() {
if err != nil { if err != nil {
return return
} }
data := []byte("hello") data := []byte("hello")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) fmt.Println(string(decrypted))
// Output: // Output:
// hello // hello
} }
``` ```

View File

@@ -0,0 +1,471 @@
# CopyOnWriteList
CopyOnWriteList 是一个线程安全的 List 实现,底层使用 go 切片。写入时,会复制一份新的切片,写入完成后,再将新的切片赋值给原来的切片。读取时,直接读取原来的切片。
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go)
## 用法
```go
import (
"github.com/duke-git/lancet/datastructure/list"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
## 文档
### NewCopyOnWriteList
返回一个具有空切片的 CopyOnWriteList。
```go
type CopyOnWriteList[T any] struct {
data []T
lock sync.Locker
}
func NewCopyOnWriteList() *CopyOnWriteList
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l)
}
```
### Size
返回 CopyOnWriteList 的长度。
```go
func (l *CopyOnWriteList[T]) Size() int
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Size())
}
```
### Get
返回列表中指定位置的元素
```go
func (c *CopyOnWriteList[T]) Get(index int) *T
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Get(2))
}
```
### Set
将此列表中指定位置的元素替换为指定元素。
```go
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool)
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Set(2, 4))
}
```
### Remove
### IndexOf
返回列表中值的索引,如果没有找到返回-1。
```go
func (c *CopyOnWriteList[T]) IndexOf(e T) int
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.IndexOf(1))
}
```
### LastIndexOf
返回指定元素在此列表中最后出现的索引,如果此列表不包含该元素,则返回-1。
```go
func (c *CopyOnWriteList[T]) LastIndexOf(e T) int
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,1})
fmt.Println(l.LastIndexOf(1))
}
```
### IsEmpty
如果此列表不包含任何元素,则返回 true。
```go
func (c *CopyOnWriteList[T]) IsEmpty() bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{})
fmt.Println(l.IsEmpty())
}
```
### Contain
判断 CopyOnWriteList 是否包含某个元素
```go
func (c *CopyOnWriteList[T]) Contain(e T) bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Contain(1))
}
```
### ValueOf
返回列表中索引处的值指针
```go
func (c *CopyOnWriteList[T]) ValueOf(index int) []T
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.ValueOf(2))
}
```
### Add
将指定的元素追加到此列表的末尾。
```go
func (c *CopyOnWriteList[T]) Add(e T) bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
l.Add(4)
fmt.Println(l.getList())
}
```
### AddAll
将指定集合中的所有元素追加到此列表的末尾
```go
func (c *CopyOnWriteList[T]) AddAll(e []T) bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
l.AddAll([]int{4,5,6})
fmt.Println(l.getList())
}
```
### AddByIndex
将指定元素插入此列表中的指定位置。
```go
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.AddByIndex(2, 6)
fmt.Println(l.getList())
}
```
### DeleteAt
移除此列表中指定位置的元素。
```go
func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool)
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteAt(2)
fmt.Println(l.getList())
}
```
### DeleteIf
从此列表中删除第一个出现的指定元素(如果该元素存在)。
```go
func (c *CopyOnWriteList[T]) DeleteIf(f func(T) bool) (oldValue *T, ok bool)
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteIf(func(i int) bool {
return i == 2
})
fmt.Println(l.getList())
}
```
### DeleteBy
从此列表中删除第一个出现的指定元素(如果该元素存在)。
```go
func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool)
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteBy(2)
fmt.Println(l.getList())
}
```
### DeleteRange
从该列表中删除索引介于 fromIndex(包含)和 toIndex(不包含)之间的所有元素。
(左闭右开)。
```go
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
list.DeleteRange(2, 5)
fmt.Println(l.getList())
}
```
### Equal
如果指定的对象等于此列表,则返回 true。
```go
func (c *CopyOnWriteList[T]) Equal(e []T) bool
```
#### 示例
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
fmt.Println(l.Equal([]int{1,2,3,4,5,6,7,8,9}))
}
```

View File

@@ -67,6 +67,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
## 注: ## 注:
@@ -106,7 +108,7 @@ import (
func AddDay(t time.Time, day int64) time.Time func AddDay(t time.Time, day int64) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/dIGbs_uTdFa)</span></b>
```go ```go
package main package main
@@ -145,7 +147,7 @@ func main() {
func AddHour(t time.Time, hour int64) time.Time func AddHour(t time.Time, hour int64) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/rcMjd7OCsi5)</span></b>
```go ```go
package main package main
@@ -184,7 +186,7 @@ func main() {
func AddMinute(t time.Time, minute int64) time.Time func AddMinute(t time.Time, minute int64) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nT1heB1KUUK)</span></b>
```go ```go
package main package main
@@ -223,7 +225,7 @@ func main() {
func AddYear(t time.Time, year int64) time.Time func AddYear(t time.Time, year int64) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/MqW2ujnBx10)</span></b>
```go ```go
package main package main
@@ -262,7 +264,7 @@ func main() {
func BeginOfMinute(t time.Time) time.Time func BeginOfMinute(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ieOLVJ9CiFT)</span></b>
```go ```go
package main package main
@@ -294,7 +296,7 @@ func main() {
func BeginOfHour(t time.Time) time.Time func BeginOfHour(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/GhdGFnDWpYs)</span></b>
```go ```go
package main package main
@@ -326,7 +328,7 @@ func main() {
func BeginOfDay(t time.Time) time.Time func BeginOfDay(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/94m_UT6cWs9)</span></b>
```go ```go
package main package main
@@ -358,7 +360,7 @@ func main() {
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ynjoJPz7VNV)</span></b>
```go ```go
package main package main
@@ -390,7 +392,7 @@ func main() {
func BeginOfMonth(t time.Time) time.Time func BeginOfMonth(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/bWXVFsmmzwL)</span></b>
```go ```go
package main package main
@@ -422,7 +424,7 @@ func main() {
func BeginOfYear(t time.Time) time.Time func BeginOfYear(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/i326DSwLnV8)</span></b>
```go ```go
package main package main
@@ -454,7 +456,7 @@ func main() {
func EndOfMinute(t time.Time) time.Time func EndOfMinute(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/yrL5wGzPj4z)</span></b>
```go ```go
package main package main
@@ -486,7 +488,7 @@ func main() {
func EndOfHour(t time.Time) time.Time func EndOfHour(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/6ce3j_6cVqN)</span></b>
```go ```go
package main package main
@@ -518,7 +520,7 @@ func main() {
func EndOfDay(t time.Time) time.Time func EndOfDay(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/eMBOvmq5Ih1)</span></b>
```go ```go
package main package main
@@ -550,7 +552,7 @@ func main() {
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/i08qKXD9flf)</span></b>
```go ```go
package main package main
@@ -582,7 +584,7 @@ func main() {
func EndOfMonth(t time.Time) time.Time func EndOfMonth(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/_GWh10B3Nqi)</span></b>
```go ```go
package main package main
@@ -614,7 +616,7 @@ func main() {
func EndOfYear(t time.Time) time.Time func EndOfYear(t time.Time) time.Time
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/G01cKlMCvNm)</span></b>
```go ```go
package main package main
@@ -646,7 +648,7 @@ func main() {
func GetNowDate() string func GetNowDate() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/PvfkPpcpBBf)</span></b>
```go ```go
package main package main
@@ -675,7 +677,7 @@ func main() {
func GetNowTime() string func GetNowTime() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/l7BNxCkTmJS)</span></b>
```go ```go
package main package main
@@ -704,7 +706,7 @@ func main() {
func GetNowDateTime() string func GetNowDateTime() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/pI4AqngD0al)</span></b>
```go ```go
package main package main
@@ -733,7 +735,7 @@ func main() {
func GetTodayStartTime() string func GetTodayStartTime() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/84siyYF7t99)</span></b>
```go ```go
package main package main
@@ -762,7 +764,7 @@ func main() {
func GetTodayEndTime() string func GetTodayEndTime() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jjrLnfoqgn3)</span></b>
```go ```go
package main package main
@@ -791,7 +793,7 @@ func main() {
func GetZeroHourTimestamp() int64 func GetZeroHourTimestamp() int64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/QmL2oIaGE3q)</span></b>
```go ```go
package main package main
@@ -820,7 +822,7 @@ func main() {
func GetNightTimestamp() int64 func GetNightTimestamp() int64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/UolysR3MYP1)</span></b>
```go ```go
package main package main
@@ -849,7 +851,7 @@ func main() {
func FormatTimeToStr(t time.Time, format string, timezone ...string) string func FormatTimeToStr(t time.Time, format string, timezone ...string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/_Ia7M8H_OvE)</span></b>
```go ```go
package main package main
@@ -888,7 +890,7 @@ func main() {
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/1h9FwdU8ql4)</span></b>
```go ```go
package main package main
@@ -927,7 +929,7 @@ type theTime struct {
func NewUnixNow() *theTime func NewUnixNow() *theTime
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/U4PPx-9D0oz)</span></b>
```go ```go
package main package main
@@ -959,7 +961,7 @@ type theTime struct {
func NewUnix(unix int64) *theTime func NewUnix(unix int64) *theTime
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/psoSuh_kLRt)</span></b>
```go ```go
package main package main
@@ -991,7 +993,7 @@ type theTime struct {
func NewFormat(t string) (*theTime, error) func NewFormat(t string) (*theTime, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/VkW08ZOaXPZ)</span></b>
```go ```go
package main package main
@@ -1023,7 +1025,7 @@ type theTime struct {
func NewISO8601(iso8601 string) (*theTime, error) func NewISO8601(iso8601 string) (*theTime, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/mkhOHQkdeA2)</span></b>
```go ```go
package main package main
@@ -1052,7 +1054,7 @@ func main() {
func (t *theTime) ToUnix() int64 func (t *theTime) ToUnix() int64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/_LUiwAdocjy)</span></b>
```go ```go
package main package main
@@ -1081,7 +1083,7 @@ func main() {
func (t *theTime) ToFormat() string func (t *theTime) ToFormat() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/VkW08ZOaXPZ)</span></b>
```go ```go
package main package main
@@ -1110,7 +1112,7 @@ func main() {
func (t *theTime) ToFormatForTpl(tpl string) string func (t *theTime) ToFormatForTpl(tpl string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nyXxXcQJ8L5)</span></b>
```go ```go
package main package main
@@ -1140,7 +1142,7 @@ func main() {
func (t *theTime) ToIso8601() string func (t *theTime) ToIso8601() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/mkhOHQkdeA2)</span></b>
```go ```go
package main package main
@@ -1170,7 +1172,7 @@ func main() {
func IsLeapYear(year int) bool func IsLeapYear(year int) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/xS1eS2ejGew)</span></b>
```go ```go
package main package main
@@ -1203,7 +1205,7 @@ func main() {
func BetweenSeconds(t1 time.Time, t2 time.Time) int64 func BetweenSeconds(t1 time.Time, t2 time.Time) int64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/n3YDRyfyXJu)</span></b>
```go ```go
package main package main
@@ -1240,7 +1242,7 @@ func main() {
func DayOfYear(t time.Time) int func DayOfYear(t time.Time) int
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/0hjqhTwFNlH)</span></b>
```go ```go
package main package main
@@ -1281,7 +1283,7 @@ func main() {
func IsWeekend(t time.Time) bool func IsWeekend(t time.Time) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/cupRM5aZOIY)</span></b>
```go ```go
package main package main
@@ -1321,7 +1323,7 @@ func main() {
func NowDateOrTime(format string, timezone ...string) string func NowDateOrTime(format string, timezone ...string) string
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/EZ-begEjtT0)</span></b>
```go ```go
package main package main
@@ -1355,7 +1357,7 @@ func main() {
func Timestamp(timezone ...string) int64 func Timestamp(timezone ...string) int64
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/iU5b7Vvjx6x)</span></b>
```go ```go
package main package main
@@ -1386,7 +1388,7 @@ func main() {
func TimestampMilli(timezone ...string) int64 func TimestampMilli(timezone ...string) int64
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/4gvEusOTu1T)</span></b>
```go ```go
package main package main
@@ -1416,7 +1418,7 @@ func main() {
func TimestampMicro(timezone ...string) int64 func TimestampMicro(timezone ...string) int64
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/2maANglKHQE)</span></b>
```go ```go
package main package main
@@ -1446,7 +1448,7 @@ func main() {
func TimestampNano(timezone ...string) int64 func TimestampNano(timezone ...string) int64
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/A9Oq_COrcCF)</span></b>
```go ```go
package main package main

View File

@@ -50,6 +50,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="ClearFile">ClearFile</span> ### <span id="ClearFile">ClearFile</span>
@@ -62,7 +64,7 @@ import (
func ClearFile(path string) error func ClearFile(path string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/NRZ0ZT-G94H)</span></b>
```go ```go
package main package main
@@ -90,7 +92,7 @@ func main() {
func CreateFile(path string) bool func CreateFile(path string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/lDt8PEsTNKI)</span></b>
```go ```go
package main package main
@@ -116,7 +118,7 @@ func main() {
func CreateDir(absPath string) error func CreateDir(absPath string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/qUuCe1OGQnM)</span></b>
```go ```go
package main package main
@@ -142,7 +144,7 @@ func main() {
func CopyFile(srcPath string, dstPath string) error func CopyFile(srcPath string, dstPath string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Jg9AMJMLrJi)</span></b>
```go ```go
package main package main
@@ -170,7 +172,7 @@ func main() {
func CurrentPath() string func CurrentPath() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/s74a9iBGcSw)</span></b>
```go ```go
package main package main
@@ -196,7 +198,7 @@ func main() {
func FileMode(path string) (fs.FileMode, error) func FileMode(path string) (fs.FileMode, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/2l2hI42fA3p)</span></b>
```go ```go
package main package main
@@ -225,7 +227,7 @@ func main() {
func MiMeType(file any) string func MiMeType(file any) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/bd5sevSUZNu)</span></b>
```go ```go
package main package main
@@ -256,7 +258,7 @@ func main() {
func IsExist(path string) bool func IsExist(path string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nKKXt8ZQbmh)</span></b>
```go ```go
package main package main
@@ -283,7 +285,7 @@ func main() {
func IsLink(path string) bool func IsLink(path string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/TL-b-Kzvf44)</span></b>
```go ```go
package main package main
@@ -309,7 +311,7 @@ func main() {
func IsDir(path string) bool func IsDir(path string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/WkVwEKqtOWk)</span></b>
```go ```go
package main package main
@@ -338,7 +340,7 @@ func main() {
func ListFileNames(path string) ([]string, error) func ListFileNames(path string) ([]string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Tjd7Y07rejl)</span></b>
```go ```go
package main package main
@@ -364,7 +366,7 @@ func main() {
func RemoveFile(path string) error func RemoveFile(path string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
```go ```go
package main package main
@@ -392,7 +394,7 @@ func main() {
func ReadFileToString(path string) (string, error) func ReadFileToString(path string) (string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/cmfwp_5SQTp)</span></b>
```go ```go
package main package main
@@ -425,7 +427,7 @@ func main() {
func ReadFileByLine(path string)([]string, error) func ReadFileByLine(path string)([]string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/svJP_7ZrBrD)</span></b>
```go ```go
package main package main
@@ -459,7 +461,7 @@ func main() {
func Zip(fpath string, destPath string) error func Zip(fpath string, destPath string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/j-3sWBp8ik_P)</span></b>
```go ```go
package main package main
@@ -487,7 +489,7 @@ func main() {
func ZipAppendEntry(fpath string, destPath string) error func ZipAppendEntry(fpath string, destPath string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/cxvaT8TRNQp)</span></b>
```go ```go
package main package main
@@ -515,7 +517,7 @@ func main() {
func UnZip(zipFile string, destPath string) error func UnZip(zipFile string, destPath string) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/g0w34kS7B8m)</span></b>
```go ```go
package main package main
@@ -543,7 +545,7 @@ func main() {
func IsZipFile(filepath string) bool func IsZipFile(filepath string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9M0g2j_uF_e)</span></b>
```go ```go
package main package main
@@ -569,7 +571,7 @@ func main() {
func FileSize(path string) (int64, error) func FileSize(path string) (int64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/H9Z05uD-Jjc)</span></b>
```go ```go
package main package main
@@ -601,7 +603,7 @@ func main() {
func MTime(filepath string) (int64, error) func MTime(filepath string) (int64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/s_Tl7lZoAaY)</span></b>
```go ```go
package main package main
@@ -633,7 +635,7 @@ func main() {
func Sha(filepath string, shaType ...int) (string, error) func Sha(filepath string, shaType ...int) (string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/VfEEcO2MJYf)</span></b>
```go ```go
package main package main
@@ -671,7 +673,7 @@ func main() {
func ReadCsvFile(filepath string) ([][]string, error) func ReadCsvFile(filepath string) ([][]string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/OExTkhGEd3_u)</span></b>
```go ```go
package main package main
@@ -703,7 +705,7 @@ func main() {
func WriteCsvFile(filepath string, records [][]string, append bool) error func WriteCsvFile(filepath string, records [][]string, append bool) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/dAXm58Q5U1o)</span></b>
```go ```go
package main package main
@@ -752,7 +754,7 @@ func main() {
func WriteBytesToFile(filepath string, content []byte) error func WriteBytesToFile(filepath string, content []byte) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/s7QlDxMj3P8)</span></b>
```go ```go
package main package main
@@ -801,7 +803,7 @@ func main() {
func WriteStringToFile(filepath string, content string, append bool) error func WriteStringToFile(filepath string, content string, append bool) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/GhLS6d8lH_g)</span></b>
```go ```go
package main package main

View File

@@ -33,6 +33,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="Comma">Comma</span> ### <span id="Comma">Comma</span>
@@ -45,7 +47,7 @@ import (
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/eRD5k2vzUVX)</span></b>
```go ```go
package main package main
@@ -81,7 +83,7 @@ func main() {
func Pretty(v any) (string, error) func Pretty(v any) (string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/YsciGj3FH2x)</span></b>
```go ```go
package main package main
@@ -120,7 +122,7 @@ func main() {
func PrettyToWriter(v any, out io.Writer) error func PrettyToWriter(v any, out io.Writer) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/LPLZ3lDi5ma)</span></b>
```go ```go
package main package main
@@ -163,7 +165,7 @@ func main() {
func DecimalBytes(size float64, precision ...int) string func DecimalBytes(size float64, precision ...int) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/FPXs1suwRcs)</span></b>
```go ```go
package main package main
@@ -202,7 +204,7 @@ func main() {
func BinaryBytes(size float64, precision ...int) string func BinaryBytes(size float64, precision ...int) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/G9oHHMCAZxP)</span></b>
```go ```go
package main package main
@@ -241,7 +243,7 @@ func main() {
func ParseDecimalBytes(size string) (uint64, error) func ParseDecimalBytes(size string) (uint64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Am98ybWjvjj)</span></b>
```go ```go
package main package main
@@ -280,7 +282,7 @@ func main() {
func ParseBinaryBytes(size string) (uint64, error) func ParseBinaryBytes(size string) (uint64, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/69v1tTT62x8)</span></b>
```go ```go
package main package main

View File

@@ -37,6 +37,8 @@ import (
## 文档 ## 文档
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
### <span id="After">After</span> ### <span id="After">After</span>
<p>创建一个函数当他被调用n或更多次之后将马上触发fn</p> <p>创建一个函数当他被调用n或更多次之后将马上触发fn</p>
@@ -47,7 +49,7 @@ import (
func After(n int, fn any) func(args ...any) []reflect.Value func After(n int, fn any) func(args ...any) []reflect.Value
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/eRD5k2vzUVX)</span></b>
```go ```go
package main package main
@@ -80,7 +82,7 @@ func main() {
func Before(n int, fn any) func(args ...any) []reflect.Value func Before(n int, fn any) func(args ...any) []reflect.Value
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/0HqUDIFZ3IL)</span></b>
```go ```go
package main package main
@@ -88,7 +90,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/function" "github.com/duke-git/lancet/v2/function"
"github.com/duke-git/lancet/v2/internal"
) )
func main() { func main() {
@@ -118,7 +119,7 @@ type CurryFn[T any] func(...T) T
func (cf CurryFn[T]) New(val T) func(...T) T func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/5HopfDwANKX)</span></b>
```go ```go
package main package main
@@ -157,7 +158,7 @@ func main() {
func Compose[T any](fnList ...func(...T) T) func(...T) T func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/KKfugD4PKYF)</span></b>
```go ```go
package main package main
@@ -195,7 +196,7 @@ func main() {
func Debounced(fn func(), duration time.Duration) func() func Debounced(fn func(), duration time.Duration) func()
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/absuEGB_GN7)</span></b>
```go ```go
package main package main
@@ -245,7 +246,7 @@ func main() {
func Delay(delay time.Duration, fn any, args ...any) func Delay(delay time.Duration, fn any, args ...any)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Ivtc2ZE-Tye)</span></b>
```go ```go
package main package main
@@ -277,7 +278,7 @@ func main() {
func Schedule(d time.Duration, fn any, args ...any) chan bool func Schedule(d time.Duration, fn any, args ...any) chan bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/hbON-Xeyn5N)</span></b>
```go ```go
package main package main
@@ -316,7 +317,7 @@ func main() {
func Pipeline[T any](funcs ...func(T) T) func(T) T func Pipeline[T any](funcs ...func(T) T) func(T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/mPdUVvj6HD6)</span></b>
```go ```go
package main package main
@@ -361,13 +362,14 @@ type Watcher struct {
excuting bool excuting bool
} }
func NewWatcher() *Watcher func NewWatcher() *Watcher
func (w *Watcher) Start() //start the watcher func (w *Watcher) Start()
func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Stop()
func (w *Watcher) Reset() //reset the watcher func (w *Watcher) Reset()
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution func (w *Watcher) GetElapsedTime() time.Duration
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/l2yrOpCLd1I)</span></b>
```go ```go
package main package main

View File

@@ -53,9 +53,10 @@ import (
- [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range) - [ConcurrentMap_Range](#ConcurrentMap_Range)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## API 文档: ## API 文档:
### <span id="MapTo">MapTo</span> ### <span id="MapTo">MapTo</span>
@@ -68,7 +69,7 @@ import (
func MapTo(src any, dst any) error func MapTo(src any, dst any) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/4K7KBEPgS5M)</span></b>
```go ```go
package main package main
@@ -125,7 +126,7 @@ func main() {
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/OaThj6iNVXK)</span></b>
```go ```go
package main package main
@@ -166,7 +167,7 @@ func main() {
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/fSvF3wxuNG7)</span></b>
```go ```go
package main package main
@@ -211,7 +212,7 @@ func main() {
func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/7ov6BJHbVqh)</span></b>
```go ```go
package main package main
@@ -249,7 +250,7 @@ func main() {
func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/P3-9MdcXegR)</span></b>
```go ```go
package main package main
@@ -287,7 +288,7 @@ func main() {
func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/YJM4Hj5hNwm)</span></b>
```go ```go
package main package main
@@ -328,7 +329,7 @@ func main() {
func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jXGrWDBfSRp)</span></b>
```go ```go
package main package main
@@ -366,7 +367,7 @@ func main() {
func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/XB7Y10uw20_U)</span></b>
```go ```go
package main package main
@@ -404,7 +405,7 @@ func main() {
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Zld0oj3sjcC)</span></b>
```go ```go
package main package main
@@ -459,7 +460,7 @@ func main() {
func Keys[K comparable, V any](m map[K]V) []K func Keys[K comparable, V any](m map[K]V) []K
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/xNB5bTb97Wd)</span></b>
```go ```go
package main package main
@@ -498,7 +499,7 @@ func main() {
func Merge[K comparable, V any](maps ...map[K]V) map[K]V func Merge[K comparable, V any](maps ...map[K]V) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/H95LENF1uB-)</span></b>
```go ```go
package main package main
@@ -537,7 +538,7 @@ func main() {
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/3u5U9K7YZ9m)</span></b>
```go ```go
package main package main
@@ -579,7 +580,7 @@ func main() {
func Values[K comparable, V any](m map[K]V) []V func Values[K comparable, V any](m map[K]V) []V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/CBKdUc5FTW6)</span></b>
```go ```go
package main package main
@@ -616,7 +617,7 @@ func main() {
func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/hI371iB8Up8)</span></b>
```go ```go
package main package main
@@ -657,7 +658,7 @@ func main() {
func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/sg9-oRidh8f)</span></b>
```go ```go
package main package main
@@ -706,7 +707,7 @@ func main() {
func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/8scDxWeBDKd)</span></b>
```go ```go
package main package main
@@ -745,7 +746,7 @@ func main() {
func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/g92aY3fc7Iw)</span></b>
```go ```go
package main package main
@@ -788,7 +789,7 @@ type Entry[K comparable, V any] struct {
func Entries[K comparable, V any](m map[K]V) []Entry[K, V] func Entries[K comparable, V any](m map[K]V) []Entry[K, V]
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Ltb11LNcElY)</span></b>
```go ```go
package main package main
@@ -833,7 +834,7 @@ type Entry[K comparable, V any] struct {
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/C8L4ul9TVwf)</span></b>
```go ```go
package main package main
@@ -867,7 +868,7 @@ func main() {
func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2 func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/P6ovfToM3zj)</span></b>
```go ```go
package main package main
@@ -906,7 +907,7 @@ func main() {
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/N9qgYg_Ho6f)</span></b>
```go ```go
package main package main
@@ -961,7 +962,7 @@ if haskey {
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
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/isZZHOsDhFc)</span></b>
```go ```go
package main package main
@@ -977,8 +978,8 @@ func main() {
"b": 2, "b": 2,
} }
result1 := HasKey(m, "a") result1 := maputil.HasKey(m, "a")
result2 := HasKey(m, "c") result2 := maputil.HasKey(m, "c")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -1000,7 +1001,7 @@ func main() {
func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1026,7 +1027,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Set(key K, value V) func (cm *ConcurrentMap[K, V]) Set(key K, value V)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1050,13 +1051,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n)) val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) fmt.Println(val, ok)
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1077,7 +1081,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1101,13 +1105,16 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.Get(fmt.Sprintf("%d", n)) val, ok := cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) fmt.Println(val, ok)
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1128,7 +1135,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/aDcDApOK01a)</span></b>
```go ```go
package main package main
@@ -1172,7 +1179,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Delete(key K) func (cm *ConcurrentMap[K, V]) Delete(key K)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/uTIJZYhpVMS)</span></b>
```go ```go
package main package main
@@ -1196,17 +1203,18 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
cm.Delete(fmt.Sprintf("%d", n)) cm.Delete(fmt.Sprintf("%d", n))
wg2.Done() wg2.Done()
}(i) }(i)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span> ### <span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span>
<p>获取key然后删除。</p> <p>获取key然后删除。</p>
@@ -1217,7 +1225,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/ZyxeIXSZUiM)</span></b>
```go ```go
package main package main
@@ -1241,7 +1249,8 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
@@ -1249,12 +1258,14 @@ func main() {
_, ok = cm.Get(fmt.Sprintf("%d", n)) _, ok = cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) //false fmt.Println(val, ok) //false
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span> ### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span>
<p>验证是否包含key。</p> <p>验证是否包含key。</p>
@@ -1265,7 +1276,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Has(key K) bool func (cm *ConcurrentMap[K, V]) Has(key K) bool
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/C8L4ul9TVwf)</span></b>
```go ```go
package main package main
@@ -1280,7 +1291,6 @@ func main() {
var wg1 sync.WaitGroup var wg1 sync.WaitGroup
wg1.Add(5) wg1.Add(5)
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
go func(n int) { go func(n int) {
cm.Set(fmt.Sprintf("%d", n), n) cm.Set(fmt.Sprintf("%d", n), n)
@@ -1289,17 +1299,21 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
ok := cm.Has(fmt.Sprintf("%d", n)) ok := cm.Has(fmt.Sprintf("%d", n))
fmt.Println(ok) // true fmt.Println(ok) // true
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_Range">ConcurrentMap_Range</span> ### <span id="ConcurrentMap_Range">ConcurrentMap_Range</span>
<p>为map中每个键和值顺序调用迭代器。 如果iterator返回false则停止迭代。</p> <p>为map中每个键和值顺序调用迭代器。 如果iterator返回false则停止迭代。</p>
@@ -1310,7 +1324,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool) func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)
``` ```
<b>例:</b> <b>例:<span class="run-container">[运行](https://go.dev/play/p/iqcy7P8P0Pr)</span></b>
```go ```go
package main package main
@@ -1341,4 +1355,3 @@ func main() {
}) })
} }
``` ```

View File

@@ -50,7 +50,9 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation <link rel="stylesheet" type="text/css" href="../../styles/api_doc.css">
## 文档
### <span id="Average">Average</span> ### <span id="Average">Average</span>
@@ -62,7 +64,7 @@ import (
func Average[T constraints.Integer | constraints.Float](numbers ...T) T func Average[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/HFd70x4DrMj)</span></b>
```go ```go
package main package main
@@ -97,7 +99,7 @@ func main() {
func Exponent(x, n int64) int64 func Exponent(x, n int64) int64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Vv7LBwER-pz)</span></b>
```go ```go
package main package main
@@ -133,7 +135,7 @@ func main() {
func Fibonacci(first, second, n int) int func Fibonacci(first, second, n int) int
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/IscseUNMuUc)</span></b>
```go ```go
package main package main
@@ -169,7 +171,7 @@ func main() {
func Factorial(x uint) uint func Factorial(x uint) uint
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/tt6LdOK67Nx)</span></b>
```go ```go
package main package main
@@ -205,7 +207,7 @@ func main() {
func Max[T constraints.Integer | constraints.Float](numbers ...T) T func Max[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/cN8DHI0rTkH)</span></b>
```go ```go
package main package main
@@ -238,7 +240,7 @@ func main() {
func MaxBy[T any](slice []T, comparator func(T, T) bool) T func MaxBy[T any](slice []T, comparator func(T, T) bool) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/pbe2MT-7DV2)</span></b>
```go ```go
package main package main
@@ -282,7 +284,7 @@ func main() {
func Min[T constraints.Integer | constraints.Float](numbers ...T) T func Min[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/pbe2MT-7DV2)</span></b>
```go ```go
package main package main
@@ -315,7 +317,7 @@ func main() {
func MinBy[T any](slice []T, comparator func(T, T) bool) T func MinBy[T any](slice []T, comparator func(T, T) bool) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/N9qgYg_Ho6f)</span></b>
```go ```go
package main package main
@@ -359,7 +361,7 @@ func main() {
func Percent(val, total float64, n int) float64 func Percent(val, total float64, n int) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/s0NdFCtwuyd)</span></b>
```go ```go
package main package main
@@ -395,7 +397,7 @@ func main() {
func RoundToFloat(x float64, n int) float64 func RoundToFloat(x float64, n int) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ghyb528JRJL)</span></b>
```go ```go
package main package main
@@ -431,7 +433,7 @@ func main() {
func RoundToString(x float64, n int) string func RoundToString(x float64, n int) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/kZwpBRAcllO)</span></b>
```go ```go
package main package main
@@ -467,7 +469,7 @@ func main() {
func TruncRound(x float64, n int) float64 func TruncRound(x float64, n int) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/aumarSHIGzP)</span></b>
```go ```go
package main package main
@@ -503,7 +505,7 @@ func main() {
func Range[T constraints.Integer | constraints.Float](start T, count int) []T func Range[T constraints.Integer | constraints.Float](start T, count int) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/9ke2opxa8ZP)</span></b>
```go ```go
package main package main
@@ -542,7 +544,7 @@ func main() {
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/akLWz0EqOSM)</span></b>
```go ```go
package main package main
@@ -581,7 +583,7 @@ func main() {
func AngleToRadian(angle float64) float64 func AngleToRadian(angle float64) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/CIvlICqrHql)</span></b>
```go ```go
package main package main
@@ -617,7 +619,7 @@ func main() {
func RadianToAngle(radian float64) float64 func RadianToAngle(radian float64) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/dQtmOTUOMgi)</span></b>
```go ```go
package main package main
@@ -653,7 +655,7 @@ func main() {
func PointDistance(x1, y1, x2, y2 float64) float64 func PointDistance(x1, y1, x2, y2 float64) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/RrG4JIaziM8)</span></b>
```go ```go
package main package main
@@ -683,7 +685,7 @@ func main() {
func IsPrime(n int) bool func IsPrime(n int) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Rdd8UTHZJ7u)</span></b>
```go ```go
package main package main
@@ -722,7 +724,7 @@ func main() {
func GCD[T constraints.Integer](integers ...T) T func GCD[T constraints.Integer](integers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/CiEceLSoAKB)</span></b>
```go ```go
package main package main
@@ -764,7 +766,7 @@ func main() {
func LCM[T constraints.Integer](integers ...T) T func LCM[T constraints.Integer](integers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/EjcZxfY7G_g)</span></b>
```go ```go
package main package main
@@ -800,7 +802,7 @@ func main() {
func Cos(radian float64, precision ...int) float64 func Cos(radian float64, precision ...int) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Sm89LoIfvFq)</span></b>
```go ```go
package main package main
@@ -842,7 +844,7 @@ func main() {
func Sin(radian float64, precision ...int) float64 func Sin(radian float64, precision ...int) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/TWMQlMywDsP)</span></b>
```go ```go
package main package main
@@ -884,7 +886,7 @@ func main() {
func Log(n, base float64) float64 func Log(n, base float64) float64
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/_d4bi8oyhat)</span></b>
```go ```go
package main package main
@@ -920,7 +922,7 @@ func main() {
func Sum[T constraints.Integer | constraints.Float](numbers ...T) T func Sum[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/1To2ImAMJA7)</span></b>
```go ```go
package main package main
@@ -953,7 +955,7 @@ func main() {
func Abs[T constraints.Integer | constraints.Float](x T) T func Abs[T constraints.Integer | constraints.Float](x T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/fsyBh1Os-1d)</span></b>
```go ```go
package main package main

View File

@@ -51,6 +51,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span> ### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
@@ -63,7 +65,7 @@ import (
func ConvertMapToQueryString(param map[string]any) string func ConvertMapToQueryString(param map[string]any) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jnNt_qoSnRi)</span></b>
```go ```go
package main package main
@@ -98,7 +100,7 @@ func main() {
func EncodeUrl(urlStr string) (string, error) func EncodeUrl(urlStr string) (string, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/bsZ6BRC4uKI)</span></b>
```go ```go
package main package main
@@ -133,7 +135,7 @@ func main() {
func GetInternalIp() string func GetInternalIp() string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/fxnna_LLD9u)</span></b>
```go ```go
package main package main
@@ -165,7 +167,7 @@ func main() {
func GetIps() []string func GetIps() []string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/NUFfcEmukx1)</span></b>
```go ```go
package main package main
@@ -195,7 +197,7 @@ func main() {
func GetMacAddrs() []string { func GetMacAddrs() []string {
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/Rq9UUBS_Xp1)</span></b>
```go ```go
package main package main
@@ -239,7 +241,7 @@ type PublicIpInfo struct {
} }
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/YDxIfozsRHR)</span></b>
```go ```go
package main package main
@@ -269,7 +271,7 @@ func main() {
func GetRequestPublicIp(req *http.Request) string func GetRequestPublicIp(req *http.Request) string
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/kxU-YDc_eBo)</span></b>
```go ```go
package main package main
@@ -307,7 +309,7 @@ func main() {
func IsPublicIP(IP net.IP) bool func IsPublicIP(IP net.IP) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/nmktSQpJZnn)</span></b>
```go ```go
package main package main
@@ -344,7 +346,7 @@ func main() {
func IsInternalIP(IP net.IP) bool func IsInternalIP(IP net.IP) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/sYGhXbgO4Cb)</span></b>
```go ```go
package main package main
@@ -388,7 +390,7 @@ type HttpRequest struct {
} }
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -445,7 +447,7 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -476,7 +478,7 @@ func main() {
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -530,7 +532,7 @@ func main() {
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -584,7 +586,7 @@ func main() {
func StructToUrlValues(targetStruct any) url.Values func StructToUrlValues(targetStruct any) url.Values
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/pFqMkM40w9z)</span></b>
```go ```go
package main package main
@@ -966,7 +968,7 @@ func main() {
func IsPingConnected(host string) bool func IsPingConnected(host string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/q8OzTijsA87)</span></b>
```go ```go
package main package main
@@ -999,7 +1001,7 @@ func main() {
func IsTelnetConnected(host string, port string) bool func IsTelnetConnected(host string, port string) bool
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/yiLCGtQv_ZG)</span></b>
```go ```go
package main package main

View File

@@ -30,6 +30,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## 文档 ## 文档
### <span id="Of">Of</span> ### <span id="Of">Of</span>
@@ -42,7 +44,7 @@ import (
func Of[T any](v T) *T func Of[T any](v T) *T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/HFd70x4DrMj)</span></b>
```go ```go
package main package main
@@ -75,7 +77,7 @@ func main() {
func Unwrap[T any](p *T) T func Unwrap[T any](p *T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/cgeu3g7cjWb)</span></b>
```go ```go
package main package main
@@ -111,7 +113,7 @@ func main() {
func ExtractPointer(value any) any func ExtractPointer(value any) any
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/D7HFjeWU2ZP)</span></b>
```go ```go
package main package main
@@ -146,7 +148,7 @@ func main() {
UnwarpOr[T any](p *T, fallback T) T UnwarpOr[T any](p *T, fallback T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/mmNaLC38W8C)</span></b>
```go ```go
package main package main
@@ -191,7 +193,7 @@ func main() {
UnwarpOrDefault[T any](p *T) T UnwarpOrDefault[T any](p *T) T
``` ```
<b>示例:</b> <b>示例:<span class="run-container">[运行](https://go.dev/play/p/ZnGIHf8_o4E)</span></b>
```go ```go
package main package main

70
docs/en/api/overview.md Normal file
View File

@@ -0,0 +1,70 @@
---
outline: deep
---
# API Overview
<b>Lancet (Lancet) is a powerful, comprehensive, efficient and reusable go language tool function library. Contains 25 packages, more than 600 utility functions. Functions cover string processing, slice processing, network, concurrency, encryption and decryption, file processing, time/date, stream processing, iterators, and more.</b>
<style>
.package-title {
color: black;
font-size: 18px;
text-align: center;
font-weight: bold;
}
.package-container {
font-size: 16px;
border: 1px dashed;
padding: 10px;
text-align: center;
}
.package-cell {
height: 40px;
width: 140px;
display: inline-block;
vertical-align: middle;
line-height: 40px;
background: #ecefff;
border: 1px solid;
margin-right: 10px;
margin-bottom: 10px;
border-radius: 6px;
font-weight: bold;
}
</style>
<div>
<p class="package-title">lancet function module</p>
<div class="package-container">
<div class="package-cell">algorithm</div>
<div class="package-cell">compare</div>
<div class="package-cell">concurrency</div>
<div class="package-cell">condition</div>
<div class="package-cell">convertor</div>
<div class="package-cell">cryptor</div>
<div class="package-cell">datastructure</div>
<div class="package-cell">datetime</div>
<div class="package-cell">fileutil</div>
<div class="package-cell">formatter</div>
<div class="package-cell">function</div>
<div class="package-cell">iterator</div>
<div class="package-cell">maputil</div>
<div class="package-cell">mathutil</div>
<div class="package-cell">netutil</div>
<div class="package-cell">pointer</div>
<div class="package-cell">random</div>
<div class="package-cell">retry</div>
<div class="package-cell">slice</div>
<div class="package-cell">stream</div>
<div class="package-cell">structs</div>
<div class="package-cell">strutil</div>
<div class="package-cell">system</div>
<div class="package-cell">tuple</div>
<div class="package-cell">validator</div>
<div class="package-cell">xerror</div>
</div>
</div>

View File

@@ -12,6 +12,8 @@ Package algorithm implements some basic algorithm. eg. sort, search.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Usage ## Usage
```go ```go
@@ -51,7 +53,7 @@ import (
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
```go ```go
package main package main
@@ -99,7 +101,7 @@ func main() {
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
```go ```go
package main package main
@@ -162,7 +164,7 @@ func main() {
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/oXovbkekayS)</span></b>
```go ```go
package main package main
@@ -210,7 +212,7 @@ func main() {
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/3ibkszpJEu3)</span></b>
```go ```go
package main package main
@@ -258,7 +260,7 @@ func main() {
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
```go ```go
package main package main
@@ -306,7 +308,7 @@ func main() {
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
```go ```go
package main package main
@@ -354,7 +356,7 @@ func main() {
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/ydinn9YzUJn)</span></b>
```go ```go
package main package main
@@ -402,7 +404,7 @@ func main() {
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
```go ```go
package main package main
@@ -451,7 +453,7 @@ func main() {
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/t6MeGiUSN47)</span></b>
```go ```go
package main package main
@@ -502,7 +504,7 @@ func main() {
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
```go ```go
package main package main
@@ -553,7 +555,7 @@ func main() {
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
```go ```go
package main package main
@@ -596,7 +598,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int func (l *LRUCache[K, V]) Len() int
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
```go ```go
package main package main

View File

@@ -30,16 +30,19 @@ import (
- [GreaterThan](#GreaterThan) - [GreaterThan](#GreaterThan)
- [LessOrEqual](#LessOrEqual) - [LessOrEqual](#LessOrEqual)
- [GreaterOrEqual](#GreaterOrEqual) - [GreaterOrEqual](#GreaterOrEqual)
- [InDelta](#InDelta)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="Equal">Equal</span> ### <span id="Equal">Equal</span>
<p>Checks if two values are equal or not. (check both type and value)</p> <p>Checks if two values are equal or not. (check both type and value)</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/wmVxR-to4lz)</span></b>
```go ```go
func Equal(left, right any) bool func Equal(left, right any) bool
@@ -88,7 +91,7 @@ func main() {
<p>Checks if two values are equal or not. (check value only)</p> <p>Checks if two values are equal or not. (check value only)</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/fxnna_LLD9u)</span></b>
```go ```go
func EqualValue(left, right any) bool func EqualValue(left, right any) bool
@@ -127,7 +130,7 @@ func main() {
<p>Checks if value `left` less than value `right`.</p> <p>Checks if value `left` less than value `right`.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
```go ```go
func LessThan(left, right any) bool func LessThan(left, right any) bool
@@ -182,7 +185,7 @@ func main() {
func GreaterThan(left, right any) bool func GreaterThan(left, right any) bool
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
```go ```go
package main package main
@@ -234,7 +237,7 @@ func main() {
func LessOrEqual(left, right any) bool func LessOrEqual(left, right any) bool
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/e4T_scwoQzp)</span></b>
```go ```go
package main package main
@@ -277,7 +280,7 @@ func main() {
<p>Checks if value `left` less greater or equal than value `right`.</p> <p>Checks if value `left` less greater or equal than value `right`.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
```go ```go
func GreaterOrEqual(left, right any) bool func GreaterOrEqual(left, right any) bool
@@ -324,3 +327,50 @@ func main() {
// false // false
} }
``` ```
### <span id="InDelta">InDelta</span>
<p>Checks if two values are equal or not within a delta.</p>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
```go
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/compare"
)
func main() {
result1 := InDelta(1, 1, 0)
result2 := InDelta(1, 2, 0)
result3 := InDelta(2.0/3.0, 0.66667, 0.001)
result4 := InDelta(2.0/3.0, 0.0, 0.001)
result5 := InDelta(float64(74.96)-float64(20.48), 54.48, 0)
result6 := InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
// Output:
// true
// false
// true
// false
// false
// true
}
```

View File

@@ -33,8 +33,9 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation <link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation
## Channel ## Channel
### <span id="NewChannel">NewChannel</span> ### <span id="NewChannel">NewChannel</span>
@@ -46,7 +47,7 @@ import (
type Channel[T any] struct type Channel[T any] struct
func NewChannel[T any]() *Channel[T] func NewChannel[T any]() *Channel[T]
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/7aB4KyMMp9A)</span></b>
```go ```go
package main package main
@@ -70,7 +71,7 @@ func main() {
```go ```go
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/qmWSy1NVF-Y)</span></b>
```go ```go
package main package main
@@ -122,7 +123,7 @@ func main() {
```go ```go
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/2VYFMexEvTm)</span></b>
```go ```go
package main package main
@@ -156,7 +157,7 @@ func main() {
<p>Create channel, put values into the channel repeatly until cancel the context.</p> <p>Create channel, put values into the channel repeatly until cancel the context.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/k5N_ALVmYjE)</span></b>
```go ```go
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T
@@ -200,7 +201,7 @@ func main() {
```go ```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
``` ```
<b>Example:</b> <b>Example: <span class="run-container">[Run](https://go.dev/play/p/7aB4KyMMp9A)</span></b>
```go ```go
package main package main
@@ -233,7 +234,7 @@ func main() {
<p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p> <p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/4J1zAWttP85)</span></b>
```go ```go
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T
@@ -275,7 +276,7 @@ func main() {
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p> <p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/Wqz9rwioPww)</span></b>
```go ```go
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T func (c *Channel[T]) Or(channels ...<-chan T) <-chan T
@@ -318,7 +319,7 @@ func main() {
<p>Read a channel into another channel, will close until cancel context.</p> <p>Read a channel into another channel, will close until cancel context.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/lm_GoS6aDjo)</span></b>
```go ```go
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T
@@ -356,7 +357,7 @@ func main() {
<p>Create a channel whose values are taken from another channel with limit number.</p> <p>Create a channel whose values are taken from another channel with limit number.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/9Utt-1pDr2J)</span></b>
```go ```go
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T
@@ -402,7 +403,7 @@ func main() {
<p>Split one chanel into two channels, until cancel the context.</p> <p>Split one chanel into two channels, until cancel the context.</p>
<b>Signature:</b> <b>Signature: <span class="run-container">[Run](https://go.dev/play/p/3TQPKnCirrP)</span></b>
```go ```go
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)

View File

@@ -31,6 +31,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
@@ -46,7 +48,7 @@ All other types are truthy if they are not their zero value.</p>
```go ```go
func Bool[T any](value T) bool func Bool[T any](value T) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/ETzeDJRSvhm)</span></b>
```go ```go
package main package main
@@ -111,7 +113,7 @@ func main() {
```go ```go
func And[T, U any](a T, b U) bool func And[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/W1SSUmt6pvr)</span></b>
```go ```go
package main package main
@@ -139,7 +141,7 @@ func main() {
```go ```go
func Or[T, U any](a T, b U) bool func Or[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/UlQTxHaeEkq)</span></b>
```go ```go
package main package main
@@ -167,7 +169,7 @@ func main() {
```go ```go
func Xor[T, U any](a T, b U) bool func Xor[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/gObZrW7ZbG8)</span></b>
```go ```go
package main package main
@@ -195,7 +197,7 @@ func main() {
```go ```go
func Nor[T, U any](a T, b U) bool func Nor[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/g2j08F_zZky)</span></b>
```go ```go
package main package main
@@ -222,7 +224,7 @@ func main() {
```go ```go
func Xnor[T, U any](a T, b U) bool func Xnor[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/OuDB9g51643)</span></b>
```go ```go
package main package main
@@ -249,7 +251,7 @@ func main() {
```go ```go
func Nand[T, U any](a T, b U) bool func Nand[T, U any](a T, b U) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/vSRMLxLIbq8)</span></b>
```go ```go
package main package main
@@ -277,7 +279,7 @@ func main() {
```go ```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[运行](https://go.dev/play/p/ElllPZY0guT)</span></b>
```go ```go
package main package main

View File

@@ -46,6 +46,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="ColorHexToRGB">ColorHexToRGB</span> ### <span id="ColorHexToRGB">ColorHexToRGB</span>
@@ -58,7 +60,7 @@ import (
func ColorHexToRGB(colorHex string) (red, green, blue int) func ColorHexToRGB(colorHex string) (red, green, blue int)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/o7_ft-JCJBV)</span></b>
```go ```go
package main package main
@@ -89,7 +91,7 @@ func main() {
func ColorRGBToHex(red, green, blue int) string func ColorRGBToHex(red, green, blue int) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nzKS2Ro87J1)</span></b>
```go ```go
package main package main
@@ -122,7 +124,7 @@ func main() {
func ToBool(s string) (bool, error) func ToBool(s string) (bool, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ARht2WnGdIN)</span></b>
```go ```go
package main package main
@@ -163,7 +165,7 @@ func main() {
func ToBytes(data any) ([]byte, error) func ToBytes(data any) ([]byte, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/fAMXYFDvOvr)</span></b>
```go ```go
package main package main
@@ -196,7 +198,7 @@ func main() {
func ToChar(s string) []string func ToChar(s string) []string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/JJ1SvbFkVdM)</span></b>
```go ```go
package main package main
@@ -232,7 +234,7 @@ func main() {
func ToChannel[T any](array []T) <-chan T func ToChannel[T any](array []T) <-chan T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/hOx_oYZbAnL)</span></b>
```go ```go
package main package main
@@ -269,7 +271,7 @@ func main() {
func ToFloat(value any) (float64, error) func ToFloat(value any) (float64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/4YTmPCibqHJ)</span></b>
```go ```go
package main package main
@@ -314,7 +316,7 @@ func main() {
func ToInt(value any) (int64, error) func ToInt(value any) (int64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/9_h9vIt-QZ_b)</span></b>
```go ```go
package main package main
@@ -356,7 +358,7 @@ func main() {
func ToJson(value any) (string, error) func ToJson(value any) (string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/2rLIkMmXWvR)</span></b>
```go ```go
package main package main
@@ -391,7 +393,7 @@ func main() {
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/tVFy7E-t24l)</span></b>
```go ```go
package main package main
@@ -432,7 +434,7 @@ func main() {
func ToPointer[T any](value T) *T func ToPointer[T any](value T) *T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ASf_etHNlw1)</span></b>
```go ```go
package main package main
@@ -461,7 +463,7 @@ func main() {
func ToString(value any) string func ToString(value any) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nF1zOOslpQq)</span></b>
```go ```go
package main package main
@@ -509,7 +511,7 @@ func main() {
func StructToMap(value any) (map[string]any, error) func StructToMap(value any) (map[string]any, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/KYGYJqNUBOI)</span></b>
```go ```go
package main package main
@@ -547,7 +549,7 @@ func main() {
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/dmX4Ix5V6Wl)</span></b>
```go ```go
package main package main
@@ -577,7 +579,7 @@ func main() {
func EncodeByte(data any) ([]byte, error) func EncodeByte(data any) ([]byte, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/DVmM1G5JfuP)</span></b>
```go ```go
package main package main
@@ -606,7 +608,7 @@ func main() {
func DecodeByte(data []byte, target any) error func DecodeByte(data []byte, target any) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -642,7 +644,7 @@ func main() {
func DeepClone[T any](src T) T func DeepClone[T any](src T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/j4DP5dquxnk)</span></b>
```go ```go
package main package main
@@ -706,7 +708,7 @@ func main() {
func CopyProperties[T, U any](dst T, src U) (err error) func CopyProperties[T, U any](dst T, src U) (err error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/oZujoB5Sgg5)</span></b>
```go ```go
package main package main
@@ -785,7 +787,7 @@ func main() {
func ToInterface(v reflect.Value) (value interface{}, ok bool) func ToInterface(v reflect.Value) (value interface{}, ok bool)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/syqw0-WG7Xd)</span></b>
```go ```go
package main package main
@@ -820,7 +822,7 @@ func main() {
func Utf8ToGbk(bs []byte) ([]byte, error) func Utf8ToGbk(bs []byte) ([]byte, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/9FlIaFLArIL)</span></b>
```go ```go
package main package main
@@ -854,7 +856,7 @@ func main() {
func GbkToUtf8(bs []byte) ([]byte, error) func GbkToUtf8(bs []byte) ([]byte, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/OphmHCN_9u8)</span></b>
```go ```go
package main package main

View File

@@ -69,6 +69,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="AesEcbEncrypt">AesEcbEncrypt</span> ### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
@@ -80,7 +82,7 @@ import (
```go ```go
func AesEcbEncrypt(data, key []byte) []byte func AesEcbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -113,7 +115,7 @@ func main() {
```go ```go
func AesEcbDecrypt(encrypted, key []byte) []byte func AesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
package main package main
@@ -146,7 +148,7 @@ func main() {
```go ```go
func AesCbcEncrypt(data, key []byte) []byte func AesCbcEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
package main package main
@@ -180,7 +182,7 @@ func main() {
func AesCbcDecrypt(encrypted, key []byte) []byte func AesCbcDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
package main package main
@@ -214,7 +216,7 @@ func main() {
func AesCtrCrypt(data, key []byte) []byte func AesCtrCrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/SpaZO0-5Nsp)</span></b>
```go ```go
package main package main
@@ -248,7 +250,7 @@ func main() {
func AesCfbEncrypt(data, key []byte) []byte func AesCfbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/tfkF10B13kH)</span></b>
```go ```go
package main package main
@@ -282,7 +284,7 @@ func main() {
func AesCfbDecrypt(encrypted, key []byte) []byte func AesCfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/tfkF10B13kH)</span></b>
```go ```go
package main package main
@@ -316,7 +318,7 @@ func main() {
func AesOfbEncrypt(data, key []byte) []byte func AesOfbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VtHxtkUj-3F)</span></b>
```go ```go
package main package main
@@ -349,7 +351,7 @@ func main() {
func AesOfbDecrypt(encrypted, key []byte) []byte func AesOfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VtHxtkUj-3F)</span></b>
```go ```go
package main package main
@@ -382,7 +384,7 @@ func main() {
```go ```go
func Base64StdEncode(s string) string func Base64StdEncode(s string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VOaUyQUreoK)</span></b>
```go ```go
package main package main
@@ -410,7 +412,7 @@ func main() {
func Base64StdDecode(s string) string func Base64StdDecode(s string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/RWQylnJVgIe)</span></b>
```go ```go
package main package main
@@ -439,7 +441,7 @@ func main() {
func DesEcbEncrypt(data, key []byte) []byte func DesEcbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/8qivmPeZy4P)</span></b>
```go ```go
package main package main
@@ -473,7 +475,7 @@ func main() {
func DesEcbDecrypt(encrypted, key []byte) []byte func DesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/8qivmPeZy4P)</span></b>
```go ```go
package main package main
@@ -508,7 +510,7 @@ func main() {
func DesCbcEncrypt(data, key []byte) []byte func DesCbcEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/4cC4QvWfe3_1)</span></b>
```go ```go
package main package main
@@ -542,7 +544,7 @@ func main() {
func DesCbcDecrypt(encrypted, key []byte) []byte func DesCbcDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/4cC4QvWfe3_1)</span></b>
```go ```go
package main package main
@@ -575,7 +577,7 @@ func main() {
func DesCtrCrypt(data, key []byte) []byte func DesCtrCrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/9-T6OjKpcdw)</span></b>
```go ```go
package main package main
@@ -609,7 +611,7 @@ func main() {
func DesCfbEncrypt(data, key []byte) []byte func DesCfbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/y-eNxcFBlxL)</span></b>
```go ```go
package main package main
@@ -642,7 +644,7 @@ func main() {
func DesCfbDecrypt(encrypted, key []byte) []byte func DesCfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/y-eNxcFBlxL)</span></b>
```go ```go
package main package main
@@ -675,7 +677,7 @@ func main() {
func DesOfbEncrypt(data, key []byte) []byte func DesOfbEncrypt(data, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/74KmNadjN1J)</span></b>
```go ```go
package main package main
@@ -708,7 +710,7 @@ func main() {
func DesOfbDecrypt(encrypted, key []byte) []byte func DesOfbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/74KmNadjN1J)</span></b>
```go ```go
package main package main
@@ -742,7 +744,7 @@ func main() {
func HmacMd5(str, key string) string func HmacMd5(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main
@@ -774,7 +776,7 @@ func main() {
func HmacMd5WithBase64(str, key string) string func HmacMd5WithBase64(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/UY0ng2AefFC)</span></b>
```go ```go
package main package main
@@ -806,7 +808,7 @@ func main() {
func HmacSha1(str, key string) string func HmacSha1(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/1UI4oQ4WXKM)</span></b>
```go ```go
package main package main
@@ -838,7 +840,7 @@ func main() {
func HmacSha1WithBase64(str, key string) string func HmacSha1WithBase64(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/47JmmGrnF7B)</span></b>
```go ```go
package main package main
@@ -870,7 +872,7 @@ func main() {
func HmacSha256(str, key string) string func HmacSha256(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/HhpwXxFhhC0)</span></b>
```go ```go
package main package main
@@ -902,7 +904,7 @@ func main() {
func HmacSha256WithBase64(str, key string) string func HmacSha256WithBase64(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/EKbkUvPTLwO)</span></b>
```go ```go
package main package main
@@ -934,7 +936,7 @@ func main() {
func HmacSha512(str, key string) string func HmacSha512(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/59Od6m4A0Ud)</span></b>
```go ```go
package main package main
@@ -966,7 +968,7 @@ func main() {
func HmacSha512WithBase64(str, key string) string func HmacSha512WithBase64(str, key string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/c6dSe3E2ydU)</span></b>
```go ```go
package main package main
@@ -999,7 +1001,7 @@ func main() {
func Md5String(s string) string func Md5String(s string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/1bLcVetbTOI)</span></b>
```go ```go
package main package main
@@ -1030,7 +1032,7 @@ func main() {
func Md5StringWithBase64(s string) string func Md5StringWithBase64(s string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Tcb-Z7LN2ax)</span></b>
```go ```go
package main package main
@@ -1059,7 +1061,7 @@ func main() {
func Md5Byte(data []byte) string func Md5Byte(data []byte) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/suraalH8lyC)</span></b>
```go ```go
package main package main
@@ -1088,7 +1090,7 @@ func main() {
func Md5ByteWithBase64(data []byte) string func Md5ByteWithBase64(data []byte) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Lx4gH7Vdr5_y)</span></b>
```go ```go
package main package main
@@ -1143,7 +1145,7 @@ func main() {
func Sha1(str string) string func Sha1(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/_m_uoD1deMT)</span></b>
```go ```go
package main package main
@@ -1174,7 +1176,7 @@ func main() {
func Sha1WithBase64(str string) string func Sha1WithBase64(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/fSyx-Gl2l2-)</span></b>
```go ```go
package main package main
@@ -1203,7 +1205,7 @@ func main() {
func Sha256(str string) string func Sha256(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/tU9tfBMIAr1)</span></b>
```go ```go
package main package main
@@ -1234,7 +1236,7 @@ func main() {
func Sha256WithBase64(str string) string func Sha256WithBase64(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/85IXJHIal1k)</span></b>
```go ```go
package main package main
@@ -1263,7 +1265,7 @@ func main() {
func Sha512(str string) string func Sha512(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/3WsvLYZxsHa)</span></b>
```go ```go
package main package main
@@ -1294,7 +1296,7 @@ func main() {
func Sha512WithBase64(str string) string func Sha512WithBase64(str string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/q_fY2rA-k5I)</span></b>
```go ```go
package main package main
@@ -1323,7 +1325,7 @@ func main() {
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/zutRHrDqs0X)</span></b>
```go ```go
package main package main
@@ -1351,7 +1353,7 @@ func main() {
func RsaEncrypt(data []byte, pubKeyFileName string) []byte func RsaEncrypt(data []byte, pubKeyFileName string) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main
@@ -1389,7 +1391,7 @@ func main() {
func RsaDecrypt(data []byte, privateKeyFileName string) []byte func RsaDecrypt(data []byte, privateKeyFileName string) []byte
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/uef0q1fz53I)</span></b>
```go ```go
package main package main

View File

@@ -0,0 +1,470 @@
# CopyOnWriteList
CopyOnWriteList is a thread-safe list implementation that uses go slicing as its base. When writing, a new slice is copied and assigned to the original slice when writing is complete. When reading, the original slice is read directly.
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/copyonwritelist.go](https://github.com/duke-git/lancet/blob/main /datastructure/list/copyonwritelist.go)
## 用法
```go
import (
"github.com/duke-git/lancet/datastructure/list"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [NewCopyOnWriteList](#NewCopyOnWriteList)
- [Size](#Size)
- [Get](#Get)
- [Set](#Set)
- [Remove](#Remove)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IsEmpty](#IsEmpty)
- [Contain](#Contain)
- [ValueOf](#ValueOf)
- [Add](#Add)
- [AddAll](#AddAll)
- [AddByIndex](#AddByIndex)
- [DeleteAt](#DeleteAt)
- [DeleteIf](#DeleteIf)
- [DeleteBy](#DeleteBy)
- [DeleteRange](#DeleteRange)
- [Equal](#Equal)
## Documentation
### NewCopyOnWriteList
Returns a CopyOnWriteList with empty slices.
```go
type CopyOnWriteList[T any] struct {
data []T
lock sync.
}
func NewCopyOnWriteList() *CopyOnWriteList
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l)
}
```
### Size
Returns the length of the CopyOnWriteList.
```go
func (l *CopyOnWriteList[T]) Size() int
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Size())
}
```
### Get
Returns the element at the specified position in the list
```go
func (c *CopyOnWriteList[T]) Get(index int) *T
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Get(2))
}
```
### Set
Replaces the element at the specified position in this list with the specified element.
```go
func (c *CopyOnWriteList[T]) Set(index int, e T) (oldValue *T, ok bool)
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Set(2, 4))
}
```
### Remove
### IndexOf
Returns the index of the value in the list, or -1 if not found.
```go
func (c *CopyOnWriteList[T]) IndexOf(e T) int
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.IndexOf(1))
}
```
### LastIndexOf
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain that element.
```go
func (c *CopyOnWriteList[T]) LastIndexOf(e T) int
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,1})
fmt.Println(l.LastIndexOf(1))
}
```
### IsEmpty
Returns true if this list does not contain any elements.
```go
func (c *CopyOnWriteList[T]) IsEmpty() bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{})
fmt.Println(l.IsEmpty())
}
```
### Contain
Determines if a CopyOnWriteList contains an element.
```go
func (c *CopyOnWriteList[T]) Contain(e T) bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.Contain(1))
}
```
### ValueOf
Returns a pointer to the value at the index in the list
```go
func (c *CopyOnWriteList[T]) ValueOf(index int) []T
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
fmt.Println(l.ValueOf(2))
}
```
### Add
Appends the specified element to the end of the list.
```go
func (c *CopyOnWriteList[T]) Add(e T) bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
l.Add(4)
fmt.Println(l.getList())
}
```
### AddAll
Appends all the elements of the specified collection to the end of this list
```go
func (c *CopyOnWriteList[T]) AddAll(e []T) bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
l.AddAll([]int{4,5,6})
fmt.Println(l.getList())
}
```
### AddByIndex
Inserts the specified element into the list at the specified position.
```go
func (c *CopyOnWriteList[T]) AddByIndex(index int, e T) bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.AddByIndex(2, 6)
fmt.Println(l.getList())
}
```
### DeleteAt
Removes the element at the specified position in this list.
```go
func (c *CopyOnWriteList[T]) DeleteAt(index int) (oldValue *T, ok bool)
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteAt(2)
fmt.Println(l.getList())
}
```
### DeleteIf
Removes the first occurrence of the specified element from this list (if it exists).
```go
func (c *CopyOnWriteList[T]) DeleteIf(func(T) bool) (oldValue *T, ok bool)
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteIf(func(i int) bool {
return i == 2
})
fmt.Println(l.getList())
}
```
### DeleteBy
Deletes the first occurrence of the specified element from this list (if it exists).
```go
func (c *CopyOnWriteList[T]) DeleteBy(e T) (*T bool)
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3})
list.DeleteBy(2)
fmt.Println(l.getList())
}
```
### DeleteRange
Deletes all elements from this list with indexes between fromIndex (included) and toIndex (not included).
(leftCloseRightOpen)
```go
func (c *CopyOnWriteList[T]) DeleteRange(start int, end int)
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
list.DeleteRange(2, 5)
fmt.Println(l.getList())
}
```
### Equal
Returns true if the specified object is equal to this list
```go
func (c *CopyOnWriteList[T]) Equal(e []T) bool
```
#### Example
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/datastructure/list"
)
func main() {
l := list.NewCopyOnWriteList([]int{1,2,3,4,5,6,7,8,9})
fmt.Println(l.Equal([]int{1,2,3,4,5,6,7,8,9}))
}
```

View File

@@ -68,6 +68,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
## Note: ## Note:
@@ -107,7 +109,7 @@ import (
func AddDay(t time.Time, day int64) time.Time func AddDay(t time.Time, day int64) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/dIGbs_uTdFa)</span></b>
```go ```go
package main package main
@@ -146,7 +148,7 @@ func main() {
func AddHour(t time.Time, hour int64) time.Time func AddHour(t time.Time, hour int64) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/rcMjd7OCsi5)</span></b>
```go ```go
package main package main
@@ -185,7 +187,7 @@ func main() {
func AddMinute(t time.Time, minute int64) time.Time func AddMinute(t time.Time, minute int64) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nT1heB1KUUK)</span></b>
```go ```go
package main package main
@@ -224,7 +226,7 @@ func main() {
func AddYear(t time.Time, year int64) time.Time func AddYear(t time.Time, year int64) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/MqW2ujnBx10)</span></b>
```go ```go
package main package main
@@ -263,7 +265,7 @@ func main() {
func BeginOfMinute(t time.Time) time.Time func BeginOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ieOLVJ9CiFT)</span></b>
```go ```go
package main package main
@@ -295,7 +297,7 @@ func main() {
func BeginOfHour(t time.Time) time.Time func BeginOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/GhdGFnDWpYs)</span></b>
```go ```go
package main package main
@@ -327,7 +329,7 @@ func main() {
func BeginOfDay(t time.Time) time.Time func BeginOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/94m_UT6cWs9)</span></b>
```go ```go
package main package main
@@ -359,7 +361,7 @@ func main() {
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ynjoJPz7VNV)</span></b>
```go ```go
package main package main
@@ -391,7 +393,7 @@ func main() {
func BeginOfMonth(t time.Time) time.Time func BeginOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/bWXVFsmmzwL)</span></b>
```go ```go
package main package main
@@ -423,7 +425,7 @@ func main() {
func BeginOfYear(t time.Time) time.Time func BeginOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/i326DSwLnV8)</span></b>
```go ```go
package main package main
@@ -455,7 +457,7 @@ func main() {
func EndOfMinute(t time.Time) time.Time func EndOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/yrL5wGzPj4z)</span></b>
```go ```go
package main package main
@@ -487,7 +489,7 @@ func main() {
func EndOfHour(t time.Time) time.Time func EndOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/6ce3j_6cVqN)</span></b>
```go ```go
package main package main
@@ -519,7 +521,7 @@ func main() {
func EndOfDay(t time.Time) time.Time func EndOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/eMBOvmq5Ih1)</span></b>
```go ```go
package main package main
@@ -551,7 +553,7 @@ func main() {
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/i08qKXD9flf)</span></b>
```go ```go
package main package main
@@ -583,7 +585,7 @@ func main() {
func EndOfMonth(t time.Time) time.Time func EndOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/_GWh10B3Nqi)</span></b>
```go ```go
package main package main
@@ -615,7 +617,7 @@ func main() {
func EndOfYear(t time.Time) time.Time func EndOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/G01cKlMCvNm)</span></b>
```go ```go
package main package main
@@ -647,7 +649,7 @@ func main() {
func GetNowDate() string func GetNowDate() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/PvfkPpcpBBf)</span></b>
```go ```go
package main package main
@@ -676,7 +678,7 @@ func main() {
func GetNowTime() string func GetNowTime() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/l7BNxCkTmJS)</span></b>
```go ```go
package main package main
@@ -705,7 +707,7 @@ func main() {
func GetNowDateTime() string func GetNowDateTime() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/pI4AqngD0al)</span></b>
```go ```go
package main package main
@@ -734,7 +736,7 @@ func main() {
func GetTodayStartTime() string func GetTodayStartTime() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/84siyYF7t99)</span></b>
```go ```go
package main package main
@@ -763,7 +765,7 @@ func main() {
func GetTodayEndTime() string func GetTodayEndTime() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jjrLnfoqgn3)</span></b>
```go ```go
package main package main
@@ -792,7 +794,7 @@ func main() {
func GetZeroHourTimestamp() int64 func GetZeroHourTimestamp() int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/QmL2oIaGE3q)</span></b>
```go ```go
package main package main
@@ -821,7 +823,7 @@ func main() {
func GetNightTimestamp() int64 func GetNightTimestamp() int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/UolysR3MYP1)</span></b>
```go ```go
package main package main
@@ -850,7 +852,7 @@ func main() {
func FormatTimeToStr(t time.Time, format string, timezone ...string) string func FormatTimeToStr(t time.Time, format string, timezone ...string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/_Ia7M8H_OvE)</span></b>
```go ```go
package main package main
@@ -889,7 +891,7 @@ func main() {
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/1h9FwdU8ql4)</span></b>
```go ```go
package main package main
@@ -928,7 +930,7 @@ type theTime struct {
func NewUnixNow() *theTime func NewUnixNow() *theTime
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/U4PPx-9D0oz)</span></b>
```go ```go
package main package main
@@ -960,7 +962,7 @@ type theTime struct {
func NewUnix(unix int64) *theTime func NewUnix(unix int64) *theTime
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/psoSuh_kLRt)</span></b>
```go ```go
package main package main
@@ -992,7 +994,7 @@ type theTime struct {
func NewFormat(t string) (*theTime, error) func NewFormat(t string) (*theTime, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VkW08ZOaXPZ)</span></b>
```go ```go
package main package main
@@ -1024,7 +1026,7 @@ type theTime struct {
func NewISO8601(iso8601 string) (*theTime, error) func NewISO8601(iso8601 string) (*theTime, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/mkhOHQkdeA2)</span></b>
```go ```go
package main package main
@@ -1053,7 +1055,7 @@ func main() {
func (t *theTime) ToUnix() int64 func (t *theTime) ToUnix() int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/_LUiwAdocjy)</span></b>
```go ```go
package main package main
@@ -1082,7 +1084,7 @@ func main() {
func (t *theTime) ToFormat() string func (t *theTime) ToFormat() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VkW08ZOaXPZ)</span></b>
```go ```go
package main package main
@@ -1111,7 +1113,7 @@ func main() {
func (t *theTime) ToFormatForTpl(tpl string) string func (t *theTime) ToFormatForTpl(tpl string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nyXxXcQJ8L5)</span></b>
```go ```go
package main package main
@@ -1141,7 +1143,7 @@ func main() {
func (t *theTime) ToIso8601() string func (t *theTime) ToIso8601() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/mkhOHQkdeA2)</span></b>
```go ```go
package main package main
@@ -1171,7 +1173,7 @@ func main() {
func IsLeapYear(year int) bool func IsLeapYear(year int) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/xS1eS2ejGew)</span></b>
```go ```go
package main package main
@@ -1204,7 +1206,7 @@ func main() {
func BetweenSeconds(t1 time.Time, t2 time.Time) int64 func BetweenSeconds(t1 time.Time, t2 time.Time) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/n3YDRyfyXJu)</span></b>
```go ```go
package main package main
@@ -1241,7 +1243,7 @@ func main() {
func DayOfYear(t time.Time) int func DayOfYear(t time.Time) int
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/0hjqhTwFNlH)</span></b>
```go ```go
package main package main
@@ -1282,7 +1284,7 @@ func main() {
func IsWeekend(t time.Time) bool func IsWeekend(t time.Time) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/cupRM5aZOIY)</span></b>
```go ```go
package main package main
@@ -1322,7 +1324,7 @@ func main() {
func NowDateOrTime(format string, timezone ...string) string func NowDateOrTime(format string, timezone ...string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/EZ-begEjtT0)</span></b>
```go ```go
package main package main
@@ -1356,7 +1358,7 @@ func main() {
func Timestamp(timezone ...string) int64 func Timestamp(timezone ...string) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/iU5b7Vvjx6x)</span></b>
```go ```go
package main package main
@@ -1387,7 +1389,7 @@ func main() {
func TimestampMilli(timezone ...string) int64 func TimestampMilli(timezone ...string) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/4gvEusOTu1T)</span></b>
```go ```go
package main package main
@@ -1417,7 +1419,7 @@ func main() {
func TimestampMicro(timezone ...string) int64 func TimestampMicro(timezone ...string) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/2maANglKHQE)</span></b>
```go ```go
package main package main
@@ -1447,7 +1449,7 @@ func main() {
func TimestampNano(timezone ...string) int64 func TimestampNano(timezone ...string) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/A9Oq_COrcCF)</span></b>
```go ```go
package main package main

View File

@@ -50,6 +50,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="ClearFile">ClearFile</span> ### <span id="ClearFile">ClearFile</span>
@@ -62,7 +64,7 @@ import (
func ClearFile(path string) error func ClearFile(path string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/NRZ0ZT-G94H)</span></b>
```go ```go
package main package main
@@ -90,7 +92,7 @@ func main() {
func CreateFile(path string) bool func CreateFile(path string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/lDt8PEsTNKI)</span></b>
```go ```go
package main package main
@@ -116,7 +118,7 @@ func main() {
func CreateDir(absPath string) error func CreateDir(absPath string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/qUuCe1OGQnM)</span></b>
```go ```go
package main package main
@@ -142,7 +144,7 @@ func main() {
func CopyFile(srcPath string, dstPath string) error func CopyFile(srcPath string, dstPath string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Jg9AMJMLrJi)</span></b>
```go ```go
package main package main
@@ -170,7 +172,7 @@ func main() {
func CurrentPath() string func CurrentPath() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/s74a9iBGcSw)</span></b>
```go ```go
package main package main
@@ -196,7 +198,7 @@ func main() {
func FileMode(path string) (fs.FileMode, error) func FileMode(path string) (fs.FileMode, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/2l2hI42fA3p)</span></b>
```go ```go
package main package main
@@ -225,7 +227,7 @@ func main() {
func MiMeType(file any) string func MiMeType(file any) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/bd5sevSUZNu)</span></b>
```go ```go
package main package main
@@ -256,7 +258,7 @@ func main() {
func IsExist(path string) bool func IsExist(path string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nKKXt8ZQbmh)</span></b>
```go ```go
package main package main
@@ -283,7 +285,7 @@ func main() {
func IsLink(path string) bool func IsLink(path string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/TL-b-Kzvf44)</span></b>
```go ```go
package main package main
@@ -309,7 +311,7 @@ func main() {
func IsDir(path string) bool func IsDir(path string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/WkVwEKqtOWk)</span></b>
```go ```go
package main package main
@@ -338,7 +340,7 @@ func main() {
func ListFileNames(path string) ([]string, error) func ListFileNames(path string) ([]string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Tjd7Y07rejl)</span></b>
```go ```go
package main package main
@@ -364,7 +366,7 @@ func main() {
func RemoveFile(path string) error func RemoveFile(path string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
```go ```go
package main package main
@@ -392,7 +394,7 @@ func main() {
func ReadFileToString(path string) (string, error) func ReadFileToString(path string) (string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/cmfwp_5SQTp)</span></b>
```go ```go
package main package main
@@ -425,7 +427,7 @@ func main() {
func ReadFileByLine(path string)([]string, error) func ReadFileByLine(path string)([]string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/svJP_7ZrBrD)</span></b>
```go ```go
package main package main
@@ -459,7 +461,7 @@ func main() {
func Zip(fpath string, destPath string) error func Zip(fpath string, destPath string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/j-3sWBp8ik_P)</span></b>
```go ```go
package main package main
@@ -487,7 +489,7 @@ func main() {
func ZipAppendEntry(fpath string, destPath string) error func ZipAppendEntry(fpath string, destPath string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/cxvaT8TRNQp)</span></b>
```go ```go
package main package main
@@ -515,7 +517,7 @@ func main() {
func UnZip(zipFile string, destPath string) error func UnZip(zipFile string, destPath string) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/g0w34kS7B8m)</span></b>
```go ```go
package main package main
@@ -543,7 +545,7 @@ func main() {
func IsZipFile(filepath string) bool func IsZipFile(filepath string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/9M0g2j_uF_e)</span></b>
```go ```go
package main package main
@@ -569,7 +571,7 @@ func main() {
func FileSize(path string) (int64, error) func FileSize(path string) (int64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/H9Z05uD-Jjc)</span></b>
```go ```go
package main package main
@@ -601,7 +603,7 @@ func main() {
func MTime(filepath string) (int64, error) func MTime(filepath string) (int64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/s_Tl7lZoAaY)</span></b>
```go ```go
package main package main
@@ -633,7 +635,7 @@ func main() {
func Sha(filepath string, shaType ...int) (string, error) func Sha(filepath string, shaType ...int) (string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/VfEEcO2MJYf)</span></b>
```go ```go
package main package main
@@ -671,7 +673,7 @@ func main() {
func ReadCsvFile(filepath string) ([][]string, error) func ReadCsvFile(filepath string) ([][]string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/OExTkhGEd3_u)</span></b>
```go ```go
package main package main
@@ -703,7 +705,7 @@ func main() {
func WriteCsvFile(filepath string, records [][]string, append bool) error func WriteCsvFile(filepath string, records [][]string, append bool) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/dAXm58Q5U1o)</span></b>
```go ```go
package main package main
@@ -752,7 +754,7 @@ func main() {
func WriteBytesToFile(filepath string, content []byte) error func WriteBytesToFile(filepath string, content []byte) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/s7QlDxMj3P8)</span></b>
```go ```go
package main package main
@@ -801,7 +803,7 @@ func main() {
func WriteStringToFile(filepath string, content string, append bool) error func WriteStringToFile(filepath string, content string, append bool) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/GhLS6d8lH_g)</span></b>
```go ```go
package main package main

View File

@@ -33,6 +33,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="Comma">Comma</span> ### <span id="Comma">Comma</span>
@@ -45,7 +47,7 @@ import (
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/eRD5k2vzUVX)</span></b>
```go ```go
package main package main
@@ -81,7 +83,7 @@ func main() {
func Pretty(v any) (string, error) func Pretty(v any) (string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/YsciGj3FH2x)</span></b>
```go ```go
package main package main
@@ -120,7 +122,7 @@ func main() {
func PrettyToWriter(v any, out io.Writer) error func PrettyToWriter(v any, out io.Writer) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/LPLZ3lDi5ma)</span></b>
```go ```go
package main package main
@@ -163,7 +165,7 @@ func main() {
func DecimalBytes(size float64, precision ...int) string func DecimalBytes(size float64, precision ...int) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/FPXs1suwRcs)</span></b>
```go ```go
package main package main
@@ -202,7 +204,7 @@ func main() {
func BinaryBytes(size float64, precision ...int) string func BinaryBytes(size float64, precision ...int) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/G9oHHMCAZxP)</span></b>
```go ```go
package main package main
@@ -241,7 +243,7 @@ func main() {
func ParseDecimalBytes(size string) (uint64, error) func ParseDecimalBytes(size string) (uint64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Am98ybWjvjj)</span></b>
```go ```go
package main package main
@@ -280,7 +282,7 @@ func main() {
func ParseBinaryBytes(size string) (uint64, error) func ParseBinaryBytes(size string) (uint64, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/69v1tTT62x8)</span></b>
```go ```go
package main package main

View File

@@ -35,6 +35,7 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="After">After</span> ### <span id="After">After</span>
@@ -47,7 +48,7 @@ import (
func After(n int, fn any) func(args ...any) []reflect.Value func After(n int, fn any) func(args ...any) []reflect.Value
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/eRD5k2vzUVX)</span></b>
```go ```go
package main package main
@@ -80,7 +81,7 @@ func main() {
func Before(n int, fn any) func(args ...any) []reflect.Value func Before(n int, fn any) func(args ...any) []reflect.Value
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/0HqUDIFZ3IL)</span></b>
```go ```go
package main package main
@@ -88,7 +89,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/function" "github.com/duke-git/lancet/v2/function"
"github.com/duke-git/lancet/v2/internal"
) )
func main() { func main() {
@@ -118,7 +118,7 @@ type CurryFn[T any] func(...T) T
func (cf CurryFn[T]) New(val T) func(...T) T func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/5HopfDwANKX)</span></b>
```go ```go
package main package main
@@ -157,7 +157,7 @@ func main() {
func Compose[T any](fnList ...func(...T) T) func(...T) T func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/KKfugD4PKYF)</span></b>
```go ```go
package main package main
@@ -195,7 +195,7 @@ func main() {
func Debounced(fn func(), duration time.Duration) func() func Debounced(fn func(), duration time.Duration) func()
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/absuEGB_GN7)</span></b>
```go ```go
package main package main
@@ -245,7 +245,7 @@ func main() {
func Delay(delay time.Duration, fn any, args ...any) func Delay(delay time.Duration, fn any, args ...any)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Ivtc2ZE-Tye)</span></b>
```go ```go
package main package main
@@ -277,7 +277,7 @@ func main() {
func Schedule(d time.Duration, fn any, args ...any) chan bool func Schedule(d time.Duration, fn any, args ...any) chan bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/hbON-Xeyn5N)</span></b>
```go ```go
package main package main
@@ -317,7 +317,7 @@ the functions one by one.</p>
func Pipeline[T any](funcs ...func(T) T) func(T) T func Pipeline[T any](funcs ...func(T) T) func(T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/mPdUVvj6HD6)</span></b>
```go ```go
package main package main
@@ -368,7 +368,7 @@ func (w *Watcher) Reset() //reset the watcher
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/l2yrOpCLd1I)</span></b>
```go ```go
package main package main
@@ -379,7 +379,7 @@ import (
) )
func main() { func main() {
w := function.NewWatcher() w := function.NewWatcher()
w.Start() w.Start()

View File

@@ -55,6 +55,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="MapTo">MapTo</span> ### <span id="MapTo">MapTo</span>
@@ -67,7 +69,7 @@ import (
func MapTo(src any, dst any) error func MapTo(src any, dst any) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/4K7KBEPgS5M)</span></b>
```go ```go
package main package main
@@ -124,7 +126,7 @@ func main() {
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/OaThj6iNVXK)</span></b>
```go ```go
package main package main
@@ -165,7 +167,7 @@ func main() {
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/fSvF3wxuNG7)</span></b>
```go ```go
package main package main
@@ -210,7 +212,7 @@ func main() {
func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/7ov6BJHbVqh)</span></b>
```go ```go
package main package main
@@ -248,7 +250,7 @@ func main() {
func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/P3-9MdcXegR)</span></b>
```go ```go
package main package main
@@ -286,7 +288,7 @@ func main() {
func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/YJM4Hj5hNwm)</span></b>
```go ```go
package main package main
@@ -327,7 +329,7 @@ func main() {
func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jXGrWDBfSRp)</span></b>
```go ```go
package main package main
@@ -365,7 +367,7 @@ func main() {
func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/XB7Y10uw20_U)</span></b>
```go ```go
package main package main
@@ -403,7 +405,7 @@ func main() {
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Zld0oj3sjcC)</span></b>
```go ```go
package main package main
@@ -458,7 +460,7 @@ func main() {
func Keys[K comparable, V any](m map[K]V) []K func Keys[K comparable, V any](m map[K]V) []K
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/xNB5bTb97Wd)</span></b>
```go ```go
package main package main
@@ -498,7 +500,7 @@ func main() {
func Merge[K comparable, V any](maps ...map[K]V) map[K]V func Merge[K comparable, V any](maps ...map[K]V) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/H95LENF1uB-)</span></b>
```go ```go
package main package main
@@ -537,7 +539,7 @@ func main() {
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/3u5U9K7YZ9m)</span></b>
```go ```go
package main package main
@@ -579,7 +581,7 @@ func main() {
func Values[K comparable, V any](m map[K]V) []V func Values[K comparable, V any](m map[K]V) []V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/CBKdUc5FTW6)</span></b>
```go ```go
package main package main
@@ -619,7 +621,7 @@ func main() {
func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/hI371iB8Up8)</span></b>
```go ```go
package main package main
@@ -660,7 +662,7 @@ func main() {
func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/sg9-oRidh8f)</span></b>
```go ```go
package main package main
@@ -709,7 +711,7 @@ func main() {
func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/8scDxWeBDKd)</span></b>
```go ```go
package main package main
@@ -748,7 +750,7 @@ func main() {
func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/g92aY3fc7Iw)</span></b>
```go ```go
package main package main
@@ -791,7 +793,7 @@ type Entry[K comparable, V any] struct {
func Entries[K comparable, V any](m map[K]V) []Entry[K, V] func Entries[K comparable, V any](m map[K]V) []Entry[K, V]
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Ltb11LNcElY)</span></b>
```go ```go
package main package main
@@ -836,7 +838,7 @@ type Entry[K comparable, V any] struct {
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/fTdu4sCNjQO)</span></b>
```go ```go
package main package main
@@ -870,7 +872,7 @@ func main() {
func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2 func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/P6ovfToM3zj)</span></b>
```go ```go
package main package main
@@ -909,7 +911,7 @@ func main() {
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/N9qgYg_Ho6f)</span></b>
```go ```go
package main package main
@@ -964,7 +966,7 @@ if haskey {
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
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/isZZHOsDhFc)</span></b>
```go ```go
package main package main
@@ -980,8 +982,8 @@ func main() {
"b": 2, "b": 2,
} }
result1 := HasKey(m, "a") result1 := maputil.HasKey(m, "a")
result2 := HasKey(m, "c") result2 := maputil.HasKey(m, "c")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -1003,7 +1005,7 @@ func main() {
func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V] func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1029,7 +1031,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Set(key K, value V) func (cm *ConcurrentMap[K, V]) Set(key K, value V)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1054,12 +1056,16 @@ func main() {
wg1.Wait() wg1.Wait()
for j := 0; j < 5; j++ { var wg2 sync.WaitGroup
go func(n int) { wg2.Add(5)
val, ok := cm.Get(fmt.Sprintf("%d", n)) for j := 0; j < 5; j++ {
fmt.Println(val, ok) go func(n int) {
}(j) val, ok := cm.Get(fmt.Sprintf("%d", n))
} fmt.Println(val, ok)
wg2.Done()
}(j)
}
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1080,7 +1086,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool) func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/3PenTPETJT0)</span></b>
```go ```go
package main package main
@@ -1105,12 +1111,16 @@ func main() {
wg1.Wait() wg1.Wait()
for j := 0; j < 5; j++ { var wg2 sync.WaitGroup
go func(n int) { wg2.Add(5)
val, ok := cm.Get(fmt.Sprintf("%d", n)) for j := 0; j < 5; j++ {
fmt.Println(val, ok) go func(n int) {
}(j) val, ok := cm.Get(fmt.Sprintf("%d", n))
} fmt.Println(val, ok)
wg2.Done()
}(j)
}
wg2.Wait()
// output: (order may change) // output: (order may change)
// 1 true // 1 true
@@ -1131,7 +1141,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool) func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/aDcDApOK01a)</span></b>
```go ```go
package main package main
@@ -1175,7 +1185,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Delete(key K) func (cm *ConcurrentMap[K, V]) Delete(key K)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/uTIJZYhpVMS)</span></b>
```go ```go
package main package main
@@ -1199,17 +1209,19 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
cm.Delete(fmt.Sprintf("%d", n)) cm.Delete(fmt.Sprintf("%d", n))
wg2.Done() wg2.Done()
}(i) }(j)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span> ### <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> <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>
@@ -1220,7 +1232,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool) func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ZyxeIXSZUiM)</span></b>
```go ```go
package main package main
@@ -1244,7 +1256,8 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n)) val, ok := cm.GetAndDelete(fmt.Sprintf("%d", n))
@@ -1252,12 +1265,15 @@ func main() {
_, ok = cm.Get(fmt.Sprintf("%d", n)) _, ok = cm.Get(fmt.Sprintf("%d", n))
fmt.Println(val, ok) //false fmt.Println(val, ok) //false
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span> ### <span id="ConcurrentMap_Has">ConcurrentMap_Has</span>
<p>Checks if map has the value for a key.</p> <p>Checks if map has the value for a key.</p>
@@ -1268,7 +1284,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Has(key K) bool func (cm *ConcurrentMap[K, V]) Has(key K) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/C8L4ul9TVwf)</span></b>
```go ```go
package main package main
@@ -1292,17 +1308,19 @@ func main() {
} }
wg1.Wait() wg1.Wait()
var wg2 sync.WaitGroup
wg2.Add(5)
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
go func(n int) { go func(n int) {
ok := cm.Has(fmt.Sprintf("%d", n)) ok := cm.Has(fmt.Sprintf("%d", n))
fmt.Println(ok) // true fmt.Println(ok) // true
wg2.Done()
}(j) }(j)
} }
wg2.Wait()
} }
``` ```
### <span id="ConcurrentMap_Range">ConcurrentMap_Range</span> ### <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> <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>
@@ -1313,7 +1331,7 @@ func main() {
func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool) func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/iqcy7P8P0Pr)</span></b>
```go ```go
package main package main

View File

@@ -48,9 +48,10 @@ import (
- [Sum](#Sum) - [Sum](#Sum)
- [Abs](#Abs) - [Abs](#Abs)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="Average">Average</span> ### <span id="Average">Average</span>
@@ -63,7 +64,7 @@ import (
func Average[T constraints.Integer | constraints.Float](numbers ...T) T func Average[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Vv7LBwER-pz)</span></b>
```go ```go
package main package main
@@ -98,7 +99,7 @@ func main() {
func Exponent(x, n int64) int64 func Exponent(x, n int64) int64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/uF3HGNPk8wr)</span></b>
```go ```go
package main package main
@@ -134,7 +135,7 @@ func main() {
func Fibonacci(first, second, n int) int func Fibonacci(first, second, n int) int
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/IscseUNMuUc)</span></b>
```go ```go
package main package main
@@ -170,7 +171,7 @@ func main() {
func Factorial(x uint) uint func Factorial(x uint) uint
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/tt6LdOK67Nx)</span></b>
```go ```go
package main package main
@@ -206,7 +207,7 @@ func main() {
func Max[T constraints.Integer | constraints.Float](numbers ...T) T func Max[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/cN8DHI0rTkH)</span></b>
```go ```go
package main package main
@@ -239,7 +240,7 @@ func main() {
func MaxBy[T any](slice []T, comparator func(T, T) bool) T func MaxBy[T any](slice []T, comparator func(T, T) bool) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/pbe2MT-7DV2)</span></b>
```go ```go
package main package main
@@ -283,7 +284,7 @@ func main() {
func Min[T constraints.Integer | constraints.Float](numbers ...T) T func Min[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/21BER_mlGUj)</span></b>
```go ```go
package main package main
@@ -316,7 +317,7 @@ func main() {
func MinBy[T any](slice []T, comparator func(T, T) bool) T func MinBy[T any](slice []T, comparator func(T, T) bool) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/N9qgYg_Ho6f)</span></b>
```go ```go
package main package main
@@ -360,7 +361,7 @@ func main() {
func Percent(val, total float64, n int) float64 func Percent(val, total float64, n int) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/s0NdFCtwuyd)</span></b>
```go ```go
package main package main
@@ -396,7 +397,7 @@ func main() {
func RoundToFloat(x float64, n int) float64 func RoundToFloat(x float64, n int) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ghyb528JRJL)</span></b>
```go ```go
package main package main
@@ -432,7 +433,7 @@ func main() {
func RoundToString(x float64, n int) string func RoundToString(x float64, n int) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/kZwpBRAcllO)</span></b>
```go ```go
package main package main
@@ -468,7 +469,7 @@ func main() {
func TruncRound(x float64, n int) float64 func TruncRound(x float64, n int) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/aumarSHIGzP)</span></b>
```go ```go
package main package main
@@ -504,7 +505,7 @@ func main() {
func Range[T constraints.Integer | constraints.Float](start T, count int) []T func Range[T constraints.Integer | constraints.Float](start T, count int) []T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/9ke2opxa8ZP)</span></b>
```go ```go
package main package main
@@ -543,7 +544,7 @@ func main() {
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/akLWz0EqOSM)</span></b>
```go ```go
package main package main
@@ -582,7 +583,7 @@ func main() {
func AngleToRadian(angle float64) float64 func AngleToRadian(angle float64) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/CIvlICqrHql)</span></b>
```go ```go
package main package main
@@ -618,7 +619,7 @@ func main() {
func RadianToAngle(radian float64) float64 func RadianToAngle(radian float64) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/dQtmOTUOMgi)</span></b>
```go ```go
package main package main
@@ -654,7 +655,7 @@ func main() {
func PointDistance(x1, y1, x2, y2 float64) float64 func PointDistance(x1, y1, x2, y2 float64) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/RrG4JIaziM8)</span></b>
```go ```go
package main package main
@@ -684,7 +685,7 @@ func main() {
func IsPrime(n int) bool func IsPrime(n int) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Rdd8UTHZJ7u)</span></b>
```go ```go
package main package main
@@ -723,7 +724,7 @@ func main() {
func GCD[T constraints.Integer](integers ...T) T func GCD[T constraints.Integer](integers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/CiEceLSoAKB)</span></b>
```go ```go
package main package main
@@ -765,7 +766,7 @@ func main() {
func LCM[T constraints.Integer](integers ...T) T func LCM[T constraints.Integer](integers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/EjcZxfY7G_g)</span></b>
```go ```go
package main package main
@@ -791,7 +792,6 @@ func main() {
} }
``` ```
### <span id="Cos">Cos</span> ### <span id="Cos">Cos</span>
<p>Returns the cosine of the radian argument.</p> <p>Returns the cosine of the radian argument.</p>
@@ -802,7 +802,7 @@ func main() {
func Cos(radian float64, precision ...int) float64 func Cos(radian float64, precision ...int) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Sm89LoIfvFq)</span></b>
```go ```go
package main package main
@@ -834,7 +834,6 @@ func main() {
} }
``` ```
### <span id="Sin">Sin</span> ### <span id="Sin">Sin</span>
<p>Returns the sine of the radian argument.</p> <p>Returns the sine of the radian argument.</p>
@@ -845,7 +844,7 @@ func main() {
func Sin(radian float64, precision ...int) float64 func Sin(radian float64, precision ...int) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/TWMQlMywDsP)</span></b>
```go ```go
package main package main
@@ -887,7 +886,7 @@ func main() {
func Log(n, base float64) float64 func Log(n, base float64) float64
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/_d4bi8oyhat)</span></b>
```go ```go
package main package main
@@ -923,7 +922,7 @@ func main() {
func Sum[T constraints.Integer | constraints.Float](numbers ...T) T func Sum[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/1To2ImAMJA7)</span></b>
```go ```go
package main package main
@@ -956,7 +955,7 @@ func main() {
func Abs[T constraints.Integer | constraints.Float](x T) T func Abs[T constraints.Integer | constraints.Float](x T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/fsyBh1Os-1d)</span></b>
```go ```go
package main package main
@@ -980,4 +979,4 @@ func main() {
// 0.1 // 0.1
// 0.2 // 0.2
} }
``` ```

View File

@@ -51,6 +51,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span> ### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
@@ -63,7 +65,7 @@ import (
func ConvertMapToQueryString(param map[string]any) string func ConvertMapToQueryString(param map[string]any) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jnNt_qoSnRi)</span></b>
```go ```go
package main package main
@@ -96,7 +98,7 @@ func main() {
func EncodeUrl(urlStr string) (string, error) func EncodeUrl(urlStr string) (string, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/bsZ6BRC4uKI)</span></b>
```go ```go
package main package main
@@ -131,7 +133,7 @@ func main() {
func GetInternalIp() string func GetInternalIp() string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/5mbu-gFp7ei)</span></b>
```go ```go
package main package main
@@ -163,7 +165,7 @@ func main() {
func GetIps() []string func GetIps() []string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/NUFfcEmukx1)</span></b>
```go ```go
package main package main
@@ -193,7 +195,7 @@ func main() {
func GetMacAddrs() []string { func GetMacAddrs() []string {
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/Rq9UUBS_Xp1)</span></b>
```go ```go
package main package main
@@ -237,7 +239,7 @@ type PublicIpInfo struct {
} }
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/YDxIfozsRHR)</span></b>
```go ```go
package main package main
@@ -267,7 +269,7 @@ func main() {
func GetRequestPublicIp(req *http.Request) string func GetRequestPublicIp(req *http.Request) string
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/kxU-YDc_eBo)</span></b>
```go ```go
package main package main
@@ -305,7 +307,7 @@ func main() {
func IsPublicIP(IP net.IP) bool func IsPublicIP(IP net.IP) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/nmktSQpJZnn)</span></b>
```go ```go
package main package main
@@ -342,7 +344,7 @@ func main() {
func IsInternalIP(IP net.IP) bool func IsInternalIP(IP net.IP) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/sYGhXbgO4Cb)</span></b>
```go ```go
package main package main
@@ -386,7 +388,7 @@ type HttpRequest struct {
} }
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -443,7 +445,7 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -474,7 +476,7 @@ func main() {
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -528,7 +530,7 @@ func main() {
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/jUSgynekH7G)</span></b>
```go ```go
package main package main
@@ -582,7 +584,7 @@ func main() {
func StructToUrlValues(targetStruct any) url.Values func StructToUrlValues(targetStruct any) url.Values
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/pFqMkM40w9z)</span></b>
```go ```go
package main package main
@@ -964,7 +966,7 @@ func main() {
func IsPingConnected(host string) bool func IsPingConnected(host string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/q8OzTijsA87)</span></b>
```go ```go
package main package main
@@ -997,7 +999,7 @@ func main() {
func IsTelnetConnected(host string, port string) bool func IsTelnetConnected(host string, port string) bool
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/yiLCGtQv_ZG)</span></b>
```go ```go
package main package main

View File

@@ -30,6 +30,8 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="/styles/api_doc.css">
## Documentation ## Documentation
### <span id="Of">Of</span> ### <span id="Of">Of</span>
@@ -42,7 +44,7 @@ import (
func Of[T any](v T) *T func Of[T any](v T) *T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/HFd70x4DrMj)</span></b>
```go ```go
package main package main
@@ -76,7 +78,7 @@ func main() {
func Unwrap[T any](p *T) T func Unwrap[T any](p *T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/cgeu3g7cjWb)</span></b>
```go ```go
package main package main
@@ -112,7 +114,7 @@ func main() {
UnwarpOr[T any](p *T, fallback T) T UnwarpOr[T any](p *T, fallback T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/mmNaLC38W8C)</span></b>
```go ```go
package main package main
@@ -157,7 +159,7 @@ func main() {
UnwarpOrDefault[T any](p *T) T UnwarpOrDefault[T any](p *T) T
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/ZnGIHf8_o4E)</span></b>
```go ```go
package main package main
@@ -203,7 +205,7 @@ func main() {
func ExtractPointer(value any) any func ExtractPointer(value any) any
``` ```
<b>Example:</b> <b>Example:<span class="run-container">[Run](https://go.dev/play/p/D7HFjeWU2ZP)</span></b>
```go ```go
package main package main

View File

@@ -0,0 +1,50 @@
---
outline: deep
---
# Installation
1. <b>For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause in v2.x.x all functions was rewriten with generics of go1.18.</b>
```go
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.1. </b>
```go
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
```
## Usage
Lancet organizes the code into package structure, and you need to import the corresponding package name when use it. For example, if you use string-related functions, just import the strutil package like below:
```go
import "github.com/duke-git/lancet/v2/strutil"
```
## Example
Here takes the string function `Reverse` (reverse order string) as an example, and the strutil package needs to be imported.
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
```
## More
Check out the [APIs]([API](https://lancet.go.dev/api/overview.html)) for details.

View File

@@ -0,0 +1,18 @@
---
outline: deep
---
# What is lancet?
<b>Lancet is a powerful, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js. </b>
## Why lancet?
Lancet makes Go dev easier by taking the hassle out of working with concurrency, net, math, slice, string, etc.
Lancet's utility methods are great for:
- Iterating slice and array.
- Manipulating strings.
- Work with net and http.
- Other tools, eg. random, crypto, stream, retry, etc.

43
docs/en/index.md Normal file
View File

@@ -0,0 +1,43 @@
---
# https://vitepress.dev/reference/default-theme-home-page
layout: home
hero:
name: "Lancet"
text: "A powerful util function library of Go"
tagline: Simple, powerful, and efficient.
actions:
- theme: brand
text: Get Started
link: /en/guide/getting_started
- theme: alt
text: View on GitHub
link: https://github.com/duke-git/lancet
# - theme: alt
# text: API Examples
# link: /api-examples
image:
src: /lancet_logo.png
alt: lancet
features:
- title: Powerful
icon: 💪
details: support 600+ go util functions. inclueds string, slice, datetime, net, crypto, concurrency, etc.
- title: Modular by design
icon: 🏗
details: Each module is designed as a package with no coupling between modules.
- title: Pure
icon: 💅
details: Only depends on two kinds of libraries, go standard library and golang.org/x.
- title: Simple
icon: 👏
details: Well structure, test for every exported function.
---
<p style="position:relative; top: -316px;left: 560px;">
<img style="display: inline-block;margin-right:10px;" src="https://img.shields.io/github/stars/duke-git/lancet?style=social" alt="">
<img style="display: inline-block" src="https://img.shields.io/github/forks/duke-git/lancet?style=social" alt="">
</p>

View File

@@ -0,0 +1,48 @@
---
outline: deep
---
# 安装
1. <b>使用 go1.18 及以上版本的用户,建议安装 v2.x.x。 因为 v2.x.x 应用 go1.18 的泛型重写了大部分函数。</b>
```go
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
```
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.1。</b>
```go
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
```
## 用法
lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:
```go
import "github.com/duke-git/lancet/v2/strutil"
```
## 示例
此处以字符串工具函数 Reverse逆序字符串为例需要导入 strutil 包:
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
```
## More
其他特性请参考[API](https://lancet.go.dev/api/overview.html).

View File

@@ -0,0 +1,18 @@
---
outline: deep
---
# lancet是什么?
<b>lancet柳叶刀是一个强大、全面、高效、可复用的go语言工具函数库。lancet受到了java apache common包和lodash.js的启发。 </b>
## 为什么选择lancet?
Lancet 消除了处理并发、网络、数学、切片、字符串等的麻烦,使 Go 开发变得更容易。
Lancet 的实用方法非常适合:
- 迭代切片和数组。
- 操作字符串。
- 处理网络和http请求。
- 其他工具,例如。 随机、加密、流、重试等。

45
docs/index.md Normal file
View File

@@ -0,0 +1,45 @@
---
# https://vitepress.dev/reference/default-theme-home-page
layout: home
hero:
name: 'Lancet'
text: '一个强大的Go语言工具函数库'
tagline: '简洁, 强大, 高效'
actions:
- theme: brand
text: 开始使用
link: /guide/getting_started
- theme: alt
text: 在GitHub中查看
link: https://github.com/duke-git/lancet
image:
src: /lancet_logo.png
alt: lancet
features:
- title: 全面
icon: 💪
details: 特性丰富支持600+ go util函数。字符串、切片、日期时间、网络、加密、并发...
- title: 模块化设计
icon: 🏗
details: 每个模块设计成一个包,模块之间无耦合。
- title: 纯净
icon: 💅
details: 只依赖go标准库和golang.org/x。
- title: 简洁
icon: 👏
details: 结构良好,测试每个导出的函数。
---
<p style="position:relative; top: -316px;left: 540px;">
<img style="display: inline-block;margin-right:10px;" src="https://img.shields.io/github/stars/duke-git/lancet?style=social" alt="">
<img style="display: inline-block" src="https://img.shields.io/github/forks/duke-git/lancet?style=social" alt="">
<!-- [![](https://img.shields.io/github/stars/duke-git/lancet?style=social)](https://github.com/duke-git/lancet)
[![](https://img.shields.io/github/forks/duke-git/lancet?style=social)](https://github.com/duke-git/lancet) -->
</p>

BIN
docs/lancet_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
docs/lancet_logo_mini.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

1277
docs/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

13
docs/package.json Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "lancet-docs",
"private": true,
"type": "module",
"scripts": {
"docs:dev": "vitepress dev",
"docs:build": "vitepress build",
"docs:preview": "vitepress preview"
},
"devDependencies": {
"vitepress": "^1.0.0-rc.4"
}
}

4
docs/styles/api_doc.css Normal file
View File

@@ -0,0 +1,4 @@
.run-container {
float: right;
display: inline-block;
}

View File

@@ -199,7 +199,15 @@ func IsZipFile(filepath string) bool {
// Zip create zip file, fpath could be a single file or a directory. // Zip create zip file, fpath could be a single file or a directory.
// Play: https://go.dev/play/p/j-3sWBp8ik_P // Play: https://go.dev/play/p/j-3sWBp8ik_P
func Zip(fpath string, destPath string) error { func Zip(path string, destPath string) error {
if IsDir(path) {
return zipFolder(path, destPath)
}
return zipFile(path, destPath)
}
func zipFile(filePath string, destPath string) error {
zipFile, err := os.Create(destPath) zipFile, err := os.Create(destPath)
if err != nil { if err != nil {
return err return err
@@ -209,10 +217,32 @@ func Zip(fpath string, destPath string) error {
archive := zip.NewWriter(zipFile) archive := zip.NewWriter(zipFile)
defer archive.Close() defer archive.Close()
return addFileToArchive(fpath, archive) return addFileToArchive1(filePath, archive)
} }
func addFileToArchive(fpath string, archive *zip.Writer) error { func zipFolder(folderPath string, destPath string) error {
outFile, err := os.Create(destPath)
if err != nil {
return err
}
defer outFile.Close()
w := zip.NewWriter(outFile)
err = addFileToArchive2(w, folderPath, "")
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return nil
}
func addFileToArchive1(fpath string, archive *zip.Writer) error {
err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@@ -247,10 +277,42 @@ func addFileToArchive(fpath string, archive *zip.Writer) error {
return err return err
} }
func addFileToArchive2(w *zip.Writer, basePath, baseInZip string) error {
files, err := os.ReadDir(basePath)
if err != nil {
return err
}
if !strings.HasSuffix(basePath, "/") {
basePath = basePath + "/"
}
for _, file := range files {
if !file.IsDir() {
dat, err := os.ReadFile(basePath + file.Name())
if err != nil {
return err
}
f, err := w.Create(baseInZip + file.Name())
if err != nil {
return err
}
_, err = f.Write(dat)
if err != nil {
return err
}
} else if file.IsDir() {
newBase := basePath + file.Name() + "/"
addFileToArchive2(w, newBase, baseInZip+file.Name()+"/")
}
}
return nil
}
// UnZip unzip the file and save it to destPath. // UnZip unzip the file and save it to destPath.
// Play: https://go.dev/play/p/g0w34kS7B8m // Play: https://go.dev/play/p/g0w34kS7B8m
func UnZip(zipFile string, destPath string) error { func UnZip(zipFile string, destPath string) error {
zipReader, err := zip.OpenReader(zipFile) zipReader, err := zip.OpenReader(zipFile)
if err != nil { if err != nil {
return err return err
@@ -333,7 +395,7 @@ func ZipAppendEntry(fpath string, destPath string) error {
} }
} }
err = addFileToArchive(fpath, archive) err = addFileToArchive1(fpath, archive)
if err != nil { if err != nil {
return err return err
@@ -532,9 +594,11 @@ func WriteCsvFile(filepath string, records [][]string, append bool) error {
// WriteStringToFile write string to target file. // WriteStringToFile write string to target file.
// Play: https://go.dev/play/p/GhLS6d8lH_g // Play: https://go.dev/play/p/GhLS6d8lH_g
func WriteStringToFile(filepath string, content string, append bool) error { func WriteStringToFile(filepath string, content string, append bool) error {
flag := os.O_RDWR | os.O_CREATE var flag int
if append { if append {
flag = flag | os.O_APPEND flag = os.O_RDWR | os.O_CREATE | os.O_APPEND
} else {
flag = os.O_RDWR | os.O_CREATE | os.O_TRUNC
} }
f, err := os.OpenFile(filepath, flag, 0644) f, err := os.OpenFile(filepath, flag, 0644)

View File

@@ -241,6 +241,19 @@ func TestZipAppendEntry(t *testing.T) {
os.RemoveAll(unZipPath) os.RemoveAll(unZipPath)
} }
func TestZipFolder(t *testing.T) {
// assert := internal.NewAssert(t, "TestZipFolder")
// toZipFolder := "./tempdir/a/b"
// zipFolder := "./tempdir/a/b.zip"
// err := Zip(toZipFolder, zipFolder)
// assert.IsNil(err)
// assert.Equal(true, IsExist(zipFolder))
// os.Remove(zipFolder)
}
func TestFileMode(t *testing.T) { func TestFileMode(t *testing.T) {
assert := internal.NewAssert(t, "TestFileMode") assert := internal.NewAssert(t, "TestFileMode")
@@ -380,9 +393,7 @@ func TestWriteStringToFile(t *testing.T) {
t.Fail() t.Fail()
} }
defer file.Close() err = WriteStringToFile(filepath, "hello world", false)
err = WriteStringToFile(filepath, "hello", false)
if err != nil { if err != nil {
t.Fail() t.Fail()
} }
@@ -392,20 +403,32 @@ func TestWriteStringToFile(t *testing.T) {
t.Fail() t.Fail()
} }
err = WriteStringToFile(filepath, "hello", false)
if err != nil {
t.Fail()
}
content2, err := ReadFileToString(filepath)
if err != nil {
t.Fail()
}
err = WriteStringToFile(filepath, " world", true) err = WriteStringToFile(filepath, " world", true)
if err != nil { if err != nil {
t.Fail() t.Fail()
} }
content2, err := os.ReadFile(filepath) content3, err := os.ReadFile(filepath)
if err != nil { if err != nil {
t.Fail() t.Fail()
} }
assert.Equal("hello", content1) assert.Equal("hello world", content1)
assert.Equal("hello world", string(content2)) assert.Equal("hello", content2)
assert.Equal("hello world", string(content3))
os.Remove(filepath) _ = file.Close()
_ = os.Remove(filepath)
} }
func TestWriteBytesToFile(t *testing.T) { func TestWriteBytesToFile(t *testing.T) {

Some files were not shown because too many files have changed in this diff Show More