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

Compare commits

..

56 Commits

Author SHA1 Message Date
dudaodong
9cd9d1aeb5 release v2.1.14 2023-02-06 17:42:13 +08:00
dudaodong
71b27c0aa9 feat: add ForEach, Reduce for stream 2023-02-06 17:40:54 +08:00
dudaodong
09a379ec6d feat: add ForEach, Reduce for stream 2023-02-06 17:38:33 +08:00
dudaodong
b4b9b03835 feat: add NoneMatch, AllMatch, AnyMatch for stream 2023-02-06 17:24:17 +08:00
dudaodong
48c7794b01 feat: add Limit method of stream 2023-02-06 17:08:36 +08:00
dudaodong
8e3911833d doc: normalize documents 2023-02-06 11:42:03 +08:00
dudaodong
ebe494051b doc: add doc for some new methods of list 2023-02-06 11:35:32 +08:00
dudaodong
c35bda6a65 feat: add ListToMap for list 2023-02-06 11:06:46 +08:00
dudaodong
1fe4cdc429 feat: add Difference and SymmetricDifference for list 2023-02-06 10:55:42 +08:00
dudaodong
6a79e322e3 Merge branch 'main' into v2 2023-02-06 10:00:02 +08:00
Cai Zhijiang
17e8d2bb6d 部分城市有4位区号+8位号码 (#69) 2023-02-06 09:47:21 +08:00
燕归来
325be0d6a1 feat: add func Iterator, ForEach, RetainAll and DeleteAll for List structure (#71) 2023-02-06 09:46:25 +08:00
dudaodong
ea0f96a8c0 feat: add Skip for stream 2023-01-30 16:56:33 +08:00
dudaodong
82cbb54787 feat: add FromChannel for create stream 2023-01-17 16:47:20 +08:00
dudaodong
585d33cafa feat: add Generate for create stream 2023-01-17 16:31:44 +08:00
dudaodong
bc4cf35e15 feat: add Filter, Map, Count for stream 2023-01-17 14:59:47 +08:00
dudaodong
a3bc20af1d feat: add Distinct 2023-01-17 11:39:05 +08:00
dudaodong
61338b6b46 feat: add Stream package 2023-01-17 11:25:15 +08:00
dudaodong
bc3c080ac3 doc: normalize document 2023-01-15 12:43:00 +08:00
dudaodong
d3fab15af3 refactor: clean structure for netutil package 2023-01-14 14:39:06 +08:00
dudaodong
6e3e411d46 doc: normalize document 2023-01-14 12:48:39 +08:00
dudaodong
f976941e36 doc: normalize document 2023-01-14 12:32:27 +08:00
dudaodong
4c5524354c doc: normalize documents 2023-01-13 14:36:33 +08:00
dudaodong
2c6e9a3fb9 doc: format document 2023-01-13 11:01:22 +08:00
dudaodong
5f2c3edff4 doc: format document 2023-01-13 10:57:40 +08:00
dudaodong
b422d98702 doc: format document 2023-01-13 10:48:21 +08:00
dudaodong
6f27e0bfbf doc: format document 2023-01-13 10:47:47 +08:00
dudaodong
ce3b6b461e doc: format document 2023-01-13 10:43:16 +08:00
dudaodong
0d6ad4f0d2 doc: format document 2023-01-13 10:41:47 +08:00
dudaodong
c875a7f8b8 doc: format document 2023-01-13 10:39:39 +08:00
dudaodong
0c62d117a1 doc: format document 2023-01-13 10:12:37 +08:00
dudaodong
c260ce493d doc: normalize documents 2023-01-12 15:44:34 +08:00
dudaodong
9ffe96d3ef update readme file 2023-01-12 11:32:43 +08:00
dudaodong
d4bba76dc8 fix: fix ExampleSchedule failed 2023-01-09 11:25:14 +08:00
dudaodong
adaa3ebc43 Merge branch 'main' into v2 2023-01-09 11:16:13 +08:00
dudaodong
61d38ae3b8 doc: update doc for algorithm package 2023-01-09 11:15:25 +08:00
dudaodong
c85d910044 add playground demo 2023-01-09 11:02:41 +08:00
dudaodong
d95a7c6101 fix: fix example function bug in function package 2023-01-08 21:09:41 +08:00
dudaodong
bce3641ec6 fix: fix example function bug in datetime package 2023-01-08 21:04:39 +08:00
dudaodong
ceb134b2fd fix: fix example function bug in datetime package 2023-01-08 21:01:05 +08:00
dudaodong
4b9b1d32c5 release v2.1.13 2023-01-08 20:45:43 +08:00
dudaodong
395e0883c7 fix: fix example test failed of CreateFile 2023-01-08 20:44:23 +08:00
dudaodong
a930511054 test&doc: add example and update doc for xerror package 2023-01-08 20:36:32 +08:00
dudaodong
7380721ccc test&doc: add example and update doc for xerror package 2023-01-08 20:36:17 +08:00
dudaodong
936011dc3b fix: fix bug in file test 2023-01-08 20:25:11 +08:00
dudaodong
bc0ee08f38 test&doc: add example and update doc for datetime package 2023-01-08 20:16:04 +08:00
dudaodong
f1afd753d4 test&doc: add example and update doc for netutil package 2023-01-08 16:57:03 +08:00
dudaodong
b05a0a91c3 test&doc: add example and update doc for netutil package 2023-01-08 16:54:29 +08:00
dudaodong
da2eb66657 doc: update doc for datastructure package 2023-01-08 15:42:34 +08:00
dudaodong
ad20159de2 doc: update doc for datastructure package 2023-01-08 15:41:59 +08:00
dudaodong
278733d3d1 Todo: add todo for playground demo of function 2023-01-07 18:27:07 +08:00
dudaodong
2f184907ff doc: update document for cryptor package 2023-01-07 18:18:23 +08:00
dudaodong
50fcc718ee test: add example for cryptor package 2023-01-07 17:34:42 +08:00
dudaodong
cc68feb52d doc&test: add example and update doc for retry package 2023-01-07 14:22:07 +08:00
dudaodong
ca2a51b37e test&doc: add example and update doc for function package 2023-01-06 17:12:32 +08:00
dudaodong
be444f521d test: add examples for function package 2023-01-06 16:12:44 +08:00
96 changed files with 12528 additions and 7593 deletions

446
README.md
View File

@@ -1,10 +1,10 @@
<div align=center> <div align=center>
<a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a> <img src="./logo.png" width="200" height="200"/>
<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.1.12-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.14-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)
@@ -113,6 +113,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
[[play](https://go.dev/play/p/Anozfr8ZLH3)] [[play](https://go.dev/play/p/Anozfr8ZLH3)]
- **<big>LinearSearch</big>** : returns the index of target in slice base on equal function. - **<big>LinearSearch</big>** : returns the index of target in slice base on equal function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)]
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
- **<big>LRUCache</big>** : implements memory cache with lru algorithm. - **<big>LRUCache</big>** : implements memory cache with lru 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)]
@@ -127,24 +128,34 @@ import "github.com/duke-git/lancet/v2/concurrency"
- **<big>NewChannel</big>** : create a Channel pointer instance. - **<big>NewChannel</big>** : create a Channel pointer instance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **<big>Bridge</big>** : link multiply channels into one channel. - **<big>Bridge</big>** : link multiply channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
- **<big>FanIn</big>** : merge multiple channels into one channel. - **<big>FanIn</big>** : merge multiple channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)]
[[play](https://go.dev/play/p/2VYFMexEvTm)]
- **<big>Generate</big>** : creates a channel, then put values into the channel. - **<big>Generate</big>** : creates a channel, then put values into the channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **<big>Or</big>** : read one or more channels into one channel, will close when any readin channel is closed. - **<big>Or</big>** : read one or more channels into one channel, will close when any readin channel is closed.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)]
[[play](https://go.dev/play/p/Wqz9rwioPww)]
- **<big>OrDone</big>** : read a channel into another channel, will close until cancel context. - **<big>OrDone</big>** : read a channel into another channel, will close until cancel context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)]
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
- **<big>Repeat</big>** : create channel, put values into the channel repeatly until cancel the context. - **<big>Repeat</big>** : create channel, put values into the channel repeatly until cancel the context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)]
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
- **<big>RepeatFn</big>** : create a channel, excutes fn repeatly, and put the result into the channel, until close context. - **<big>RepeatFn</big>** : create a channel, excutes fn repeatly, and put the result into the channel, until close context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)]
[[play](https://go.dev/play/p/4J1zAWttP85)]
- **<big>Take</big>** : create a channel whose values are taken from another channel with limit number. - **<big>Take</big>** : create a channel whose values are taken from another channel with limit number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)]
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
- **<big>Tee</big>** : split one chanel into two channels, until cancel the context. - **<big>Tee</big>** : split one chanel into two channels, until cancel the context.
[[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)]
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator... ### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
@@ -162,7 +173,7 @@ import "github.com/duke-git/lancet/v2/condition"
[[play](https://go.dev/play/p/W1SSUmt6pvr)] [[play](https://go.dev/play/p/W1SSUmt6pvr)]
- **<big>Or</big>** : returns false if neither a nor b is truthy. - **<big>Or</big>** : returns false if neither a nor b is truthy.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Or)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Or)]
[[play](https://go.dev/play/p/UlQTxHaeEkq)] [[play](https://go.dev/play/p/UlQTxHaeEkq)]]
- **<big>Xor</big>** : returns true if a or b but not both is truthy. - **<big>Xor</big>** : returns true if a or b but not both is truthy.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Xor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Xor)]
[[play](https://go.dev/play/p/gObZrW7ZbG8)] [[play](https://go.dev/play/p/gObZrW7ZbG8)]
@@ -244,38 +255,101 @@ import "github.com/duke-git/lancet/v2/cryptor"
#### Function list: #### Function list:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbEncrypt) - **<big>AesEcbEncrypt</big>** : encrypt byte slice data with key use AES ECB algorithm.
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbDecrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbEncrypt)]
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcEncrypt) [[play](https://go.dev/play/p/zI6xsmuQRbn)]
- [AesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcDecrypt) - **<big>AesEcbDecrypt</big>** : decrypt byte slice data with key use AES ECB algorithm.
- [AesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCtrCrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbDecrypt)]
- [AesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbEncrypt) [[play](https://go.dev/play/p/zI6xsmuQRbn)]
- [AesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbDecrypt) - **<big>AesCbcEncrypt</big>** : encrypt byte slice data with key use AES CBC algorithm.
- [AesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcEncrypt)]
- [AesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbDecrypt) [[play](https://go.dev/play/p/IOq_g8_lKZD)]
- [Base64StdEncode](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdEncode) - **<big>AesCbcDecrypt</big>** : decrypt byte slice data with key use AES CBC algorithm.
- [Base64StdDecode](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdDecode) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcDecrypt)]
- [DesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbEncrypt) [[play](https://go.dev/play/p/IOq_g8_lKZD)]
- [DesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbDecrypt) - **<big>AesCtrCrypt</big>** : encrypt/ decrypt byte slice data with key use AES CRC algorithm.
- [DesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCtrCrypt)]
- [DesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcDecrypt) [[play](https://go.dev/play/p/SpaZO0-5Nsp)]
- [DesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCtrCrypt) - **<big>AesCfbEncrypt</big>** : encrypt byte slice data with key use AES CFB algorithm.
- [DesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbEncrypt)]
- [DesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbDecrypt) [[play](https://go.dev/play/p/tfkF10B13kH)]
- [DesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbEncrypt) - **<big>AesCfbDecrypt</big>** : decrypt byte slice data with key use AES CFB algorithm.
- [DesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbDecrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbDecrypt)]
- [HmacMd5](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5) [[play](https://go.dev/play/p/tfkF10B13kH)]
- [HmacSha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1) - **<big>AesOfbEncrypt</big>** : encrypt byte slice data with key use AES OFB algorithm.
- [HmacSha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbEncrypt)]
- [HmacSha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512) [[play](https://go.dev/play/p/VtHxtkUj-3F)]
- [Md5String](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String) - **<big>AesOfbDecrypt</big>** : decrypt byte slice data with key use AES OFB algorithm.
- [Md5File](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbDecrypt)]
- [Sha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1) [[play](https://go.dev/play/p/VtHxtkUj-3F)]
- [Sha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256) - **<big>Base64StdEncode</big>** : encode string with base64 encoding.
- [Sha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdEncode)]
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey) [[play](https://go.dev/play/p/VOaUyQUreoK)]
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt) - **<big>Base64StdDecode</big>** : decode string with base64 encoding.
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdDecode)]
[[play](https://go.dev/play/p/RWQylnJVgIe)]
- **<big>DesEcbEncrypt</big>** : encrypt byte slice data with key use DES ECB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbEncrypt)]
[[play](https://go.dev/play/p/8qivmPeZy4P)]
- **<big>DesEcbDecrypt</big>** : decrypt byte slice data with key use DES ECB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbDecrypt)]
[[play](https://go.dev/play/p/8qivmPeZy4P)]
- **<big>DesCbcEncrypt</big>** : encrypt byte slice data with key use DES CBC algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcEncrypt)]
[[play](https://go.dev/play/p/4cC4QvWfe3_1)]
- **<big>DesCbcDecrypt</big>** : decrypt byte slice data with key use DES CBC algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcDecrypt)]
[[play](https://go.dev/play/p/4cC4QvWfe3_1)]
- **<big>DesCtrCrypt</big>** : encrypt/decrypt byte slice data with key use DES CRY algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCtrCrypt)]
[[play](https://go.dev/play/p/9-T6OjKpcdw)]
- **<big>DesCfbEncrypt</big>** : encrypt byte slice data with key use DES CFB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbEncrypt)]
[[play](https://go.dev/play/p/y-eNxcFBlxL)]
- **<big>DesCfbDecrypt</big>** : decrypt byte slice data with key use DES CFB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbDecrypt)]
[[play](https://go.dev/play/p/y-eNxcFBlxL)]
- **<big>DesOfbEncrypt</big>** : encrypt byte slice data with key use DES OFB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbEncrypt)]
[[play](https://go.dev/play/p/74KmNadjN1J)]
- **<big>DesOfbDecrypt</big>** : decrypt byte slice data with key use DES OFB algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbDecrypt)]
[[play](https://go.dev/play/p/74KmNadjN1J)]
- **<big>HmacMd5</big>** : return the md5 hmac hash of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>HmacSha1</big>** : return the hmac hash of string use sha1.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1)]
[[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha256</big>** : return the hmac hash of string use sha256.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha512</big>** : return the hmac hash of string use sha512.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>Md5String</big>** : return the md5 value of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5File</big>** : return the md5 value of file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File)]
- **<big>Sha1</big>** : return the sha1 value (SHA-1 hash algorithm) of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1)]
[[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<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)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<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)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>GenerateRsaKey</big>** : create rsa private and public pemo file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)]
- **<big>RsaEncrypt</big>** : encrypt data with ras algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>RsaDecrypt</big>** : decrypt data with ras algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
### 6. Datetime package supports date and time format and compare. ### 6. Datetime package supports date and time format and compare.
@@ -285,36 +359,99 @@ import "github.com/duke-git/lancet/v2/datetime"
#### Function list: #### Function list:
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay) - **<big>AddDay</big>** : add or sub day to the time.
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)]
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute) [[play](https://go.dev/play/p/dIGbs_uTdFa)]
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute) - **<big>AddHour</big>** : add or sub day to the time.
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)]
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay) [[play](https://go.dev/play/p/rcMjd7OCsi5)]
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek) - **<big>AddMinute</big>** : add or sub day to the time.
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)]
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear) [[play](https://go.dev/play/p/nT1heB1KUUK)]
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute) - **<big>BeginOfMinute</big>** : return the date time at the begin of minute of specific date.
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)]
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay) [[play](https://go.dev/play/p/ieOLVJ9CiFT)]
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek) - **<big>BeginOfHour</big>** : return the date time at the begin of hour of specific date.
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)]
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear) [[play](https://go.dev/play/p/GhdGFnDWpYs)]
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate) - **<big>BeginOfDay</big>** : return the date time at the begin of day of specific date.
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)]
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime) [[play](https://go.dev/play/p/94m_UT6cWs9)]
- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp) - **<big>BeginOfWeek</big>** : return the date time at the begin of week of specific date.
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)]
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr) [[play](https://go.dev/play/p/ynjoJPz7VNV)]
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime) - **<big>BeginOfMonth</big>** : return the date time at the begin of month of specific date.
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)]
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow) [[play](https://go.dev/play/p/bWXVFsmmzwL)]
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat) - **<big>BeginOfYear</big>** : return the date time at the begin of year of specific date.
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)]
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix) [[play](https://go.dev/play/p/i326DSwLnV8)]
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat) - **<big>EndOfMinute</big>** : return the date time at the end of minute of specific date.
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)]
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601) [[play](https://go.dev/play/p/yrL5wGzPj4z)]
- **<big>EndOfHour</big>** : return the date time at the end of hour of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)]
[[play](https://go.dev/play/p/6ce3j_6cVqN)]
- **<big>EndOfDay</big>** : return the date time at the end of day of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)]
[[play](https://go.dev/play/p/eMBOvmq5Ih1)]
- **<big>EndOfWeek</big>** : return the date time at the end of week of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)]
[[play](https://go.dev/play/p/i08qKXD9flf)]
- **<big>EndOfMonth</big>** : return the date time at the end of month of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)]
[[play](https://go.dev/play/p/_GWh10B3Nqi)]
- **<big>EndOfYear</big>** : return the date time at the end of year of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)]
[[play](https://go.dev/play/p/G01cKlMCvNm)]
- **<big>GetNowDate</big>** : return format yyyy-mm-dd of current date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)]
[[play](https://go.dev/play/p/PvfkPpcpBBf)]
- **<big>GetNowTime</big>** : return format hh-mm-ss of current time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)]
[[play](https://go.dev/play/p/l7BNxCkTmJS)]
- **<big>GetNowDateTime</big>** : return format yyyy-mm-dd hh-mm-ss of current datetime.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)]
[[play](https://go.dev/play/p/pI4AqngD0al)]
- **<big>GetZeroHourTimestamp</big>** : return timestamp of zero hour (timestamp of 00:00).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp)]
[[play](https://go.dev/play/p/QmL2oIaGE3q)]
- **<big>GetNightTimestamp</big>** : return timestamp of zero hour (timestamp of 23:59).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)]
[[play](https://go.dev/play/p/UolysR3MYP1)]
- **<big>FormatTimeToStr</big>** : convert time to string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)]
[[play](https://go.dev/play/p/_Ia7M8H_OvE)]
- **<big>FormatStrToTime</big>** : convert string to time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)]
[[play](https://go.dev/play/p/1h9FwdU8ql4)]
- **<big>NewUnix</big>** : return unix timestamp of specific time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)]
[[play](https://go.dev/play/p/psoSuh_kLRt)]
- **<big>NewUnixNow</big>** : return unix timestamp of current time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)]
[[play](https://go.dev/play/p/U4PPx-9D0oz)]
- **<big>NewFormat</big>** : return unix timestamp of specific time string, t should be "yyyy-mm-dd hh:mm:ss".
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)]
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
- **<big>NewISO8601</big>** : return unix timestamp of specific iso8601 time string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)]
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
- **<big>ToUnix</big>** : return unix timestamp.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)]
[[play](https://go.dev/play/p/_LUiwAdocjy)]
- **<big>ToFormat</big>** : return the time string 'yyyy-mm-dd hh:mm:ss' of unix time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)]
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
- **<big>ToFormatForTpl</big>** : return the time string which format is specific tpl.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)]
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
- **<big>ToIso8601</big>** : return iso8601 time string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)]
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph. ### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
@@ -329,16 +466,24 @@ import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
``` ```
#### Function list: #### Structure list:
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md) - **<big>List</big>** : a linear table, implemented with slice.
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)]
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md) - **<big>Link</big>** : link list structure, contains singly link and doubly link.
- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link.md)]
- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set.md) - **<big>Stack</big>** : stack structure(fifo), contains array stack and link stack.
- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md)]
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md) - **<big>Queue</big>** : queue structure(filo), contains array queue, circular queue, link queue and priority queue.
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue.md)]
- **<big>Set</big>** : a data container, like slice, but element of set is not duplicate.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set.md)]
- **<big>Tree</big>** : binary search tree structure.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree.md)]
- **<big>Heap</big>** : a binary max heap.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)]
- **<big>Hashmap</big>** : hash map structure.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)]
### 8. Fileutil package implements some basic functions for file operations. ### 8. Fileutil package implements some basic functions for file operations.
@@ -414,14 +559,34 @@ import "github.com/duke-git/lancet/v2/function"
#### Function list: #### Function list:
- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After) - **<big>After</big>** : return a function that invokes passed funcation once the returned function is called more than n times.
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#After)]
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry) [[play](https://go.dev/play/p/eRD5k2vzUVX)]
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose) - **<big>Before</big>** : return a function that invokes passed funcation once the returned function is called less than n times
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before)]
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) [[play](https://go.dev/play/p/0HqUDIFZ3IL)]
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline) - **<big>CurryFn</big>** : make a curry function.
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)]
[[play](https://go.dev/play/p/5HopfDwANKX)]
- **<big>Compose</big>** : compose the functions from right to left.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)]
[[play](https://go.dev/play/p/KKfugD4PKYF)]
- **<big>Delay</big>** : call the function after delayed time.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)]
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
- **<big>Debounced</big>** : creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced)]
[[play](https://go.dev/play/p/absuEGB_GN7)]
- **<big>Schedule</big>** : invoke function every duration time, util close the returned bool channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Schedule)]
[[play](https://go.dev/play/p/hbON-Xeyn5N)]
- **<big>Pipeline</big>** : takes a list of functions and returns a function whose param will be passed into the functions one by one.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline)]
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
- **<big>Watcher</big>** : Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)]
### 11. Maputil package includes some functions to manipulate map. ### 11. Maputil package includes some functions to manipulate map.
@@ -490,6 +655,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[play](https://go.dev/play/p/N9qgYg_Ho6f)] [[play](https://go.dev/play/p/N9qgYg_Ho6f)]
- **<big>Percent</big>** : calculate the percentage of value to total. - **<big>Percent</big>** : calculate the percentage of value to total.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)]
[[play](https://go.dev/play/p/QQM9B13coSP)]
- **<big>RoundToFloat</big>** : round up to n decimal places for float64. - **<big>RoundToFloat</big>** : round up to n decimal places for float64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)]
[[play](https://go.dev/play/p/ghyb528JRJL)] [[play](https://go.dev/play/p/ghyb528JRJL)]
@@ -508,26 +674,62 @@ import "github.com/duke-git/lancet/v2/netutil"
#### Function list: #### Function list:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString) - **<big>ConvertMapToQueryString</big>** : convert map to sorted url query string.
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#EncodeUrl) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)]
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp) [[play](https://go.dev/play/p/jnNt_qoSnRi)]
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps) - **<big>EncodeUrl</big>** : encode url(?a=1&b=[2] -> ?a=1&b=%5B2%5D).
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#EncodeUrl)]
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo) [[play](https://go.dev/play/p/bsZ6BRC4uKI)]
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetRequestPublicIp) - **<big>GetInternalIp</big>** : return internal ipv4.
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)]
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsInternalIP) [[play](https://go.dev/play/p/5mbu-gFp7ei)]
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpRequest) - **<big>GetIps</big>** : return all ipv4 of current system.
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpClient) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)]
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#SendRequest) [[play](https://go.dev/play/p/NUFfcEmukx1)]
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse) - **<big>GetMacAddrs</big>** : return mac address of current system.
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)]
- [HttpGet<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet) [[play](https://go.dev/play/p/Rq9UUBS_Xp1)]
- [HttpDelete<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete) - **<big>GetPublicIpInfo</big>** : return [public ip information](http://ip-api.com/json/).
- [HttpPost<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo)]
- [HttpPut<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut) [[play](https://go.dev/play/p/YDxIfozsRHR)]
- [HttpPatch<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch) - **<big>GetRequestPublicIp</big>** : return the http request public ip.
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetRequestPublicIp)]
[[play](https://go.dev/play/p/kxU-YDc_eBo)]
- **<big>IsPublicIP</big>** : verify a ip is public or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)]
[[play](https://go.dev/play/p/nmktSQpJZnn)]
- **<big>IsInternalIP</big>** : verify an ip is intranet or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsInternalIP)]
[[play](https://go.dev/play/p/sYGhXbgO4Cb)]
- **<big>HttpRequest</big>** : a composed http request used for HttpClient send request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpRequest)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>HttpClient</big>** : a http client tool, used for sending http request
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpClient)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>SendRequest</big>** : send http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#SendRequest)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>DecodeResponse</big>** : decode http response into target object.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>StructToUrlValues</big>** : convert struct to url valuse.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues)]
[[play](https://go.dev/play/p/pFqMkM40w9z)]
- **<big>HttpGet<sup>deprecated</sup></big>** : send get http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)]
- **<big>HttpDelete<sup>deprecated</sup></big>** : send delete http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)]
- **<big>HttpPost<sup>deprecated</sup></big>** : send post http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)]
- **<big>HttpPut<sup>deprecated</sup></big>** : send put http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)]
- **<big>HttpPatch<sup>deprecated</sup></big>** : send patch http request.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)]
- **<big>ParseHttpResponse</big>** : decode http response into target object.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)]
### 14. Random package implements some basic functions to generate random int and string. ### 14. Random package implements some basic functions to generate random int and string.
@@ -543,7 +745,7 @@ import "github.com/duke-git/lancet/v2/random"
- **<big>RandInt</big>** : generate random int number between min and max. - **<big>RandInt</big>** : generate random int number between min and max.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)]
[[play](https://go.dev/play/p/pXyyAAI5YxD)] [[play](https://go.dev/play/p/pXyyAAI5YxD)]
- **<big>RandString</big>** : generate random string of specified length. - **<big>RandString</big>** : generate random string of specific length.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)]
[[play](https://go.dev/play/p/W2xvRUXA7Mi)] [[play](https://go.dev/play/p/W2xvRUXA7Mi)]
- **<big>RandUpper</big>** : generate a random upper case string. - **<big>RandUpper</big>** : generate a random upper case string.
@@ -552,7 +754,7 @@ import "github.com/duke-git/lancet/v2/random"
- **<big>RandLower</big>** : generate a random lower case string. - **<big>RandLower</big>** : generate a random lower case string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)]
[[play](https://go.dev/play/p/XJtZ471cmtI)] [[play](https://go.dev/play/p/XJtZ471cmtI)]
- **<big>RandNumeral</big>** : generate a random numeral string of specified length. - **<big>RandNumeral</big>** : generate a random numeral string of specific length.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)]
[[play](https://go.dev/play/p/g4JWVpHsJcf)] [[play](https://go.dev/play/p/g4JWVpHsJcf)]
- **<big>RandNumeralOrLetter</big>** : generate a random numeral or letter string. - **<big>RandNumeralOrLetter</big>** : generate a random numeral or letter string.
@@ -570,11 +772,21 @@ import "github.com/duke-git/lancet/v2/retry"
#### Function list: #### Function list:
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context) - **<big>Context</big>** : set retry context config option.
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry) [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context)]
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc) [[play](https://go.dev/play/p/xnAOOXv9GkS)]
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration) - **<big>Retry</big>** : executes the retryFunc repeatedly until it was successful or canceled by the context.
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes) [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryFunc</big>** : function that retry executes.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryDuration</big>** : set duration of retry
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryTimes</big>** : set times of retry.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)]
### 16. Slice contains some functions to manipulate slice. ### 16. Slice contains some functions to manipulate slice.
@@ -752,15 +964,15 @@ import "github.com/duke-git/lancet/v2/strutil"
#### Function list: #### Function list:
- **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string. - **<big>After</big>** : returns the substring after the first occurrence of a specific string in the source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
[[play](https://go.dev/play/p/RbCOQqCDA7m)] [[play](https://go.dev/play/p/RbCOQqCDA7m)]
- **<big>AfterLast</big>** : returns the substring after the last occurrence of a specified string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)] - **<big>AfterLast</big>** : returns the substring after the last occurrence of a specific string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
[[play](https://go.dev/play/p/1TegARrb8Yn)] [[play](https://go.dev/play/p/1TegARrb8Yn)]
- **<big>Before</big>** : returns the substring before the first occurrence of a specified string in the source string. - **<big>Before</big>** : returns the substring before the first occurrence of a specific string in the source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
[[play](https://go.dev/play/p/JAWTZDS4F5w)] [[play](https://go.dev/play/p/JAWTZDS4F5w)]
- **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specified string in the source string. - **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specific string in the source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
[[play](https://go.dev/play/p/pJfXXAoG_Te)] [[play](https://go.dev/play/p/pJfXXAoG_Te)]
- **<big>CamelCase</big>** : coverts source string to its camelCase string. - **<big>CamelCase</big>** : coverts source string to its camelCase string.
@@ -802,8 +1014,9 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not. - **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
[[play](https://go.dev/play/p/Us-ySSbWh-3)] [[play](https://go.dev/play/p/Us-ySSbWh-3)]
- **<big>Substring</big>** : returns a substring of the specified length starting at the specified offset position. - **<big>Substring</big>** : returns a substring of the specific length starting at the specific offset position.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
[[play](https://go.dev/play/p/q3sM6ehnPDp)]
- **<big>Wrap</big>** : wrap a string with given string. - **<big>Wrap</big>** : wrap a string with given string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
[[play](https://go.dev/play/p/KoZOlZDDt9y)] [[play](https://go.dev/play/p/KoZOlZDDt9y)]
@@ -948,7 +1161,10 @@ import "github.com/duke-git/lancet/v2/xerror"
#### Function list: #### Function list:
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap) - **<big>Unwrap</big>** : check if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap)]
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
## How to Contribute ## How to Contribute

View File

@@ -1,10 +1,10 @@
<div align=center> <div align=center>
<a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/> <img src="./logo.png" width="200" height="200"/>
<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.1.12-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.14-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)
@@ -51,7 +51,7 @@ lancet 是以包的结构组织代码的,使用时需要导入相应的包名
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
``` ```
## 例 ##
此处以字符串工具函数 Reverse逆序字符串为例需要导入 strutil 包: 此处以字符串工具函数 Reverse逆序字符串为例需要导入 strutil 包:
@@ -112,6 +112,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
[[play](https://go.dev/play/p/Anozfr8ZLH3)] [[play](https://go.dev/play/p/Anozfr8ZLH3)]
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找) - **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
- **<big>LRUCache</big>** : 应用 lru 算法实现内存缓存. - **<big>LRUCache</big>** : 应用 lru 算法实现内存缓存.
[[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)]
@@ -126,24 +127,34 @@ import "github.com/duke-git/lancet/v2/concurrency"
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。 - **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel直到取消上下文。 - **<big>Bridge</big>** : 将多个 channel 链接到一个 channel直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel直到取消上下文。 - **<big>FanIn</big>** : 将多个 channel 合并为一个 channel直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
[[play](https://go.dev/play/p/2VYFMexEvTm)]
- **<big>Generate</big>** : 根据传入的值,生成 channel。 - **<big>Generate</big>** : 根据传入的值,生成 channel。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **<big>Or</big>** : 将一个或多个 channel 读取到一个 channel 中,当任何读取 channel 关闭时将结束读取。 - **<big>Or</big>** : 将一个或多个 channel 读取到一个 channel 中,当任何读取 channel 关闭时将结束读取。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)]
[[play](https://go.dev/play/p/Wqz9rwioPww)]
- **<big>OrDone</big>** : 将一个 channel 读入另一个 channel直到取消上下文。 - **<big>OrDone</big>** : 将一个 channel 读入另一个 channel直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)]
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
- **<big>Repeat</big>** : 返回一个 channel将参数`values`重复放入 channel直到取消上下文。 - **<big>Repeat</big>** : 返回一个 channel将参数`values`重复放入 channel直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)]
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
- **<big>RepeatFn</big>** : 返回一个 channel重复执行函数 fn并将结果放入返回的 channel直到取消上下文。 - **<big>RepeatFn</big>** : 返回一个 channel重复执行函数 fn并将结果放入返回的 channel直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)]
[[play](https://go.dev/play/p/4J1zAWttP85)]
- **<big>Take</big>** : 返回一个 channel其值从另一个 channel 获取,直到取消上下文。 - **<big>Take</big>** : 返回一个 channel其值从另一个 channel 获取,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)]
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
- **<big>Tee</big>** : 将一个 channel 分成两个 channel直到取消上下文。 - **<big>Tee</big>** : 将一个 channel 分成两个 channel直到取消上下文。
[[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)]
### 3. condition 包含一些用于条件判断的函数。 ### 3. condition 包含一些用于条件判断的函数。
@@ -170,7 +181,7 @@ import "github.com/duke-git/lancet/v2/condition"
[[play](https://go.dev/play/p/g2j08F_zZky) [[play](https://go.dev/play/p/g2j08F_zZky)
- **<big>Xnor</big>** : 如果 a 和 b 都是真的或 a 和 b 均是假的,则返回 true。 - **<big>Xnor</big>** : 如果 a 和 b 都是真的或 a 和 b 均是假的,则返回 true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Xnor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Xnor)]
[[play](https://go.dev/play/p/OuDB9g51643)] [[play](https://go.dev/play/p/OuDB9g51643)]]
- **<big>Nand</big>** : 如果 a 和 b 都为真,返回 false否则返回 true - **<big>Nand</big>** : 如果 a 和 b 都为真,返回 false否则返回 true
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nand)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nand)]
[[play](https://go.dev/play/p/vSRMLxLIbq8)] [[play](https://go.dev/play/p/vSRMLxLIbq8)]
@@ -243,38 +254,103 @@ import "github.com/duke-git/lancet/v2/cryptor"
#### 函数列表: #### 函数列表:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbEncrypt) - **<big>AesEcbEncrypt</big>** : 使用AES ECB算法模式加密数据。
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbDecrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbEncrypt)]
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcEncrypt) [[play](https://go.dev/play/p/zI6xsmuQRbn)]
- [AesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcDecrypt) - **<big>AesEcbDecrypt</big>** : 使用AES ECB算法模解密数据。
- [AesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCtrCrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbDecrypt)]
- [AesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbEncrypt) [[play](https://go.dev/play/p/zI6xsmuQRbn)]
- [AesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbDecrypt) - **<big>AesCbcEncrypt</big>** : 使用AES CBC算法模式加密数据。
- [AesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesOfbEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcEncrypt)]
- [AesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesOfbDecrypt) [[play](https://go.dev/play/p/IOq_g8_lKZD)]
- [Base64StdEncode](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Base64StdEncode) - **<big>AesCbcDecrypt</big>** : 使用AES CBC算法模式解密数据。
- [Base64StdDecode](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Base64StdDecode) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcDecrypt)]
- [DesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbEncrypt) [[play](https://go.dev/play/p/IOq_g8_lKZD)]
- [DesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbDecrypt) - **<big>AesCtrCrypt</big>** : 使用AES CTR算法模式加密/解密数据。
- [DesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCtrCrypt)]
- [DesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcDecrypt) [[play](https://go.dev/play/p/SpaZO0-5Nsp)]
- [DesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCtrCrypt) - **<big>AesCfbEncrypt</big>** : 使用AES CFB算法模式加密数据。
- [DesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbEncrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbEncrypt)]
- [DesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbDecrypt) [[play](https://go.dev/play/p/tfkF10B13kH)]
- [DesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbEncrypt) - **<big>AesCfbDecrypt</big>** : 使用AES CFB算法模式解密数据。
- [DesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbDecrypt) [[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbDecrypt)]
- [HmacMd5](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5) [[play](https://go.dev/play/p/tfkF10B13kH)]
- [HmacSha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1) - **<big>AesOfbEncrypt</big>** : 使用AES OFB算法模式加密数据。
- [HmacSha256](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#AesOfbEncrypt)]
- [HmacSha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512) [[play](https://go.dev/play/p/VtHxtkUj-3F)]
- [Md5String](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String) - **<big>AesOfbDecrypt</big>** : 使用AES OFB算法模式解密数据。
- [Md5File](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#AesOfbDecrypt)]
- [Sha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1) [[play](https://go.dev/play/p/VtHxtkUj-3F)]
- [Sha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256) - **<big>Base64StdEncode</big>** : 将字符串base64编码。
- [Sha512](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#Base64StdEncode)]
- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey) [[play](https://go.dev/play/p/VOaUyQUreoK)]
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt) - **<big>Base64StdDecode</big>** : 解码base64字符串。
- [RsaDecrypt](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#Base64StdDecode)]
[[play](https://go.dev/play/p/RWQylnJVgIe)]
- **<big>DesEcbEncrypt</big>** : 使用DES ECB算法模式加密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbEncrypt)]
[[play](https://go.dev/play/p/8qivmPeZy4P)]
- **<big>DesEcbDecrypt</big>** : 使用DES ECB算法模解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbDecrypt)]
[[play](https://go.dev/play/p/8qivmPeZy4P)]
- **<big>DesCbcEncrypt</big>** : 使用DES CBC算法模式加密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcEncrypt)]
[[play](https://go.dev/play/p/4cC4QvWfe3_1)]
- **<big>DesCbcDecrypt</big>** : 使用DES CBC算法模式解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcDecrypt)]
[[play](https://go.dev/play/p/4cC4QvWfe3_1)]
- **<big>DesCtrCrypt</big>** : 使用DES CTR算法模式加密/解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCtrCrypt)]
[[play](https://go.dev/play/p/9-T6OjKpcdw)]
- **<big>DesCfbEncrypt</big>** : 使用DES CFB算法模式加密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbEncrypt)]
[[play](https://go.dev/play/p/y-eNxcFBlxL)]
- **<big>DesCfbDecrypt</big>** : 使用DES CFB算法模式解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbDecrypt)]
[[play](https://go.dev/play/p/y-eNxcFBlxL)]
- **<big>DesOfbEncrypt</big>** : 使用DES OFB算法模式加密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbEncrypt)]
[[play](https://go.dev/play/p/74KmNadjN1J)]
- **<big>DesOfbDecrypt</big>** : 使用DES OFB算法模式解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbDecrypt)]
[[play](https://go.dev/play/p/74KmNadjN1J)]
- **<big>HmacMd5</big>** : 返回字符串md5 hmac值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>HmacSha1</big>** : 返回字符串sha1 hmac值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1)]
[[play](https://go.dev/play/p/1UI4oQ4WXKM)]
- **<big>HmacSha256</big>** : 返回字符串sha256 hmac值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256)]
[[play](https://go.dev/play/p/HhpwXxFhhC0)]
- **<big>HmacSha512</big>** : 返回字符串sha256 hmac值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512)]
[[play](https://go.dev/play/p/59Od6m4A0Ud)]
- **<big>Md5String</big>** : 返回字符串md5值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String)]
[[play](https://go.dev/play/p/1bLcVetbTOI)]
- **<big>Md5File</big>** : 返回文件md5值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File)]
- **<big>Sha1</big>** : 返回字符串sha1哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1)]
[[play](https://go.dev/play/p/_m_uoD1deMT)]
- **<big>Sha256</big>** :返回字符串sha256哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256)]
[[play](https://go.dev/play/p/tU9tfBMIAr1)]
- **<big>Sha512</big>** : 返回字符串sha512哈希值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512)]
[[play](https://go.dev/play/p/3WsvLYZxsHa)]
- **<big>GenerateRsaKey</big>** : 在当前目录下创建rsa私钥文件和公钥文件。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey)]
[[play](https://go.dev/play/p/zutRHrDqs0X)]
- **<big>RsaEncrypt</big>** : 用公钥文件ras加密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
- **<big>RsaDecrypt</big>** : 用私钥文件rsa解密数据。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)]
[[play](https://go.dev/play/p/uef0q1fz53I)]
### 6. datetime 日期时间处理包,格式化日期,比较日期。 ### 6. datetime 日期时间处理包,格式化日期,比较日期。
@@ -284,36 +360,98 @@ import "github.com/duke-git/lancet/v2/datetime"
#### 函数列表: #### 函数列表:
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay) - **<big>AddDay</big>** : 将日期加/减天数。
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)]
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute) [[play](https://go.dev/play/p/dIGbs_uTdFa)]
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute) - **<big>AddHour</big>** : 将日期加/减小时数。
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)]
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay) [[play](https://go.dev/play/p/rcMjd7OCsi5)]
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek) - **<big>AddMinute</big>** : 将日期加/减分钟数。
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)]
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear) [[play](https://go.dev/play/p/nT1heB1KUUK)]
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute) - **<big>BeginOfMinute</big>** : 返回指定时间的分钟开始时间。
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMinute)]
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay) [[play](https://go.dev/play/p/ieOLVJ9CiFT)]
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek) - **<big>BeginOfHour</big>** : 返回指定时间的小时开始时间。
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfHour)]
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear) [[play](https://go.dev/play/p/GhdGFnDWpYs)]
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate) - **<big>BeginOfDay</big>** : 返回指定时间的当天开始时间。
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfDay)]
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime) [[play](https://go.dev/play/p/94m_UT6cWs9)]
- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp) - **<big>BeginOfWeek</big>** : 返回指定时间的每周开始时间,默认开始时间星期日。
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfWeek)]
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr) [[play](https://go.dev/play/p/ynjoJPz7VNV)]
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime) - **<big>BeginOfMonth</big>** : 返回指定时间的当月开始时间。
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMonth)]
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow) [[play](https://go.dev/play/p/bWXVFsmmzwL)]
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat) - **<big>BeginOfYear</big>** : 返回指定时间的当年开始时间。
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfYear)]
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix) [[play](https://go.dev/play/p/i326DSwLnV8)]
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat) - **<big>EndOfMinute</big>** : 返回指定时间的分钟结束时间。
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMinute)]
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601) [[play](https://go.dev/play/p/yrL5wGzPj4z)]
- **<big>EndOfHour</big>** : 返回指定时间的小时结束时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfHour)]
[[play](https://go.dev/play/p/6ce3j_6cVqN)]
- **<big>EndOfDay</big>** : 返回指定时间的当天结束时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfDay)]
[[play](https://go.dev/play/p/eMBOvmq5Ih1)]
- **<big>EndOfWeek</big>** : 返回指定时间的星期结束时间,默认结束时间星期六。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfWeek)]
[[play](https://go.dev/play/p/i08qKXD9flf)]
- **<big>EndOfMonth</big>** : 返回指定时间的月份结束时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMonth)]
[[play](https://go.dev/play/p/_GWh10B3Nqi)]
- **<big>EndOfYear</big>** : 返回指定时间的年份结束时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfYear)]
[[play](https://go.dev/play/p/G01cKlMCvNm)]
- **<big>GetNowDate</big>** : 获取当天日期返回格式yyyy-mm-dd。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)]
[[play](https://go.dev/play/p/PvfkPpcpBBf)]
- **<big>GetNowTime</big>** : 获取当时时间返回格式hh:mm:ss。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)]
[[play](https://go.dev/play/p/l7BNxCkTmJS)]
- **<big>GetNowDateTime</big>** : 获取当时日期和时间返回格式yyyy-mm-dd hh:mm:ss。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)]
[[play](https://go.dev/play/p/pI4AqngD0al)]
- **<big>GetZeroHourTimestamp</big>** : 获取零时时间戳(timestamp of 00:00)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp)]
[[play](https://go.dev/play/p/QmL2oIaGE3q)]
- **<big>GetNightTimestamp</big>** : 获取午夜时间戳(timestamp of 23:59)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)]
[[play](https://go.dev/play/p/UolysR3MYP1)]
- **<big>FormatTimeToStr</big>** : 将日期格式化成字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)]
[[play](https://go.dev/play/p/_Ia7M8H_OvE)]
- **<big>FormatStrToTime</big>** : 将字符串格式化成时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)]
[[play](https://go.dev/play/p/1h9FwdU8ql4)]
- **<big>NewUnix</big>** : 创建一个unix时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)]
[[play](https://go.dev/play/p/psoSuh_kLRt)]
- **<big>NewUnixNow</big>** : 创建一个当前时间的unix时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)]
[[play](https://go.dev/play/p/U4PPx-9D0oz)]
- **<big>NewFormat</big>** : 创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)]
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
- **<big>NewISO8601</big>** : 创建一个iso8601格式时间字符串的unix时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)]
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
- **<big>ToUnix</big>** : 返回unix时间戳。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)]
[[play](https://go.dev/play/p/_LUiwAdocjy)]
- **<big>ToFormat</big>** : 返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)]
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
- **<big>ToFormatForTpl</big>** : 返回tpl格式指定的日期字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)]
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
- **<big>ToIso8601</big>** : 返回iso8601日期字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)]
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
### 7. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph. ### 7. datastructure 包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph.
@@ -330,14 +468,25 @@ import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
#### Function list: #### Function list:
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md) - **<big>List</big>** : 线性表结构, 用切片实现。
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist_zh-CN.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)]
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md) - **<big>Link</big>** : 链表解构, 包括单链表和双向链表。
- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue_zh-CN.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/link_zh-CN.md)]
- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set_zh-CN.md) - **<big>Stack</big>** : 栈结构(fifo), 包括数组栈和链表栈。
- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree_zh-CN.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md)]
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md) - **<big>Queue</big>** : 队列结构(filo), 包括数组队列,链表队列,循环队列,优先级队列。
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md) [[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue_zh-CN.md)]
- **<big>Set</big>** : 集合set结构。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set_zh-CN.md)]
- **<big>Tree</big>** : 二叉搜索树。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree_zh-CN.md)]
- **<big>Heap</big>** : 二叉max堆。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap_zh-CN.md)]
- **<big>Hashmap</big>** : 哈希映射。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap_zh-CN.md)]
### 8. fileutil 包含文件基本操作。 ### 8. fileutil 包含文件基本操作。
@@ -413,14 +562,34 @@ import "github.com/duke-git/lancet/v2/function"
#### 函数列表: #### 函数列表:
- [After](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After) - **<big>After</big>** : 创建一个函数当该函数被调用n或更多次之后将执行传入的函数。
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)]
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry) [[play](https://go.dev/play/p/eRD5k2vzUVX)]
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose) - **<big>Before</big>** : 创建一个函数当该函数被调用不超过n次时将执行执行传入的函数。
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)]
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) [[play](https://go.dev/play/p/0HqUDIFZ3IL)]
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline) - **<big>CurryFn</big>** : 创建柯里化函数。
- [Watcher](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#CurryFn)]
[[play](https://go.dev/play/p/5HopfDwANKX)]
- **<big>Compose</big>** : 从右至左组合函数列表fnList返回组合后的函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)]
[[play](https://go.dev/play/p/KKfugD4PKYF)]
- **<big>Delay</big>** : 延迟delay时间后调用函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)]
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
- **<big>Debounced</big>** : 创建一个debounced函数该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced)]
[[play](https://go.dev/play/p/absuEGB_GN7)]
- **<big>Schedule</big>** : 每次持续时间调用函数直到关闭返回的channel。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Schedule)]
[[play](https://go.dev/play/p/hbON-Xeyn5N)]
- **<big>Pipeline</big>** : 从右至左执行函数列表。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)]
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
- **<big>Watcher</big>** : Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)]
### 11. maputil 包括一些操作 map 的函数. ### 11. maputil 包括一些操作 map 的函数.
@@ -489,6 +658,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[play](https://go.dev/play/p/N9qgYg_Ho6f)] [[play](https://go.dev/play/p/N9qgYg_Ho6f)]
- **<big>Percent</big>** : 计算百分比,可以指定保留 n 位小数。 - **<big>Percent</big>** : 计算百分比,可以指定保留 n 位小数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)]
[[play](https://go.dev/play/p/QQM9B13coSP)]
- **<big>RoundToFloat</big>** : 四舍五入,保留 n 位小数,返回 float64。 - **<big>RoundToFloat</big>** : 四舍五入,保留 n 位小数,返回 float64。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)]
[[play](https://go.dev/play/p/ghyb528JRJL)] [[play](https://go.dev/play/p/ghyb528JRJL)]
@@ -507,26 +677,63 @@ import "github.com/duke-git/lancet/v2/netutil"
#### 函数列表: #### 函数列表:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString) - **<big>ConvertMapToQueryString</big>** : 将map转换成http查询字符串。
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)]
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl) [[play](https://go.dev/play/p/jnNt_qoSnRi)]
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps) - **<big>EncodeUrl</big>** : 编码url query string的值(?a=1&b=[2] -> ?a=1&b=%5B2%5D)。
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)]
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo) [[play](https://go.dev/play/p/bsZ6BRC4uKI)]
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetRequestPublicIp) - **<big>GetInternalIp</big>** : 获取内部ipv4。
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)]
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsInternalIP) [[play](https://go.dev/play/p/5mbu-gFp7ei)]
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpRequest) - **<big>GetIps</big>** : 获取系统ipv4地址列表。
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpClient) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetIps)]
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#SendRequest) [[play](https://go.dev/play/p/NUFfcEmukx1)]
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse) - **<big>GetMacAddrs</big>** : 获取系统mac地址列。
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetMacAddrs)]
- [HttpGet<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet) [[play](https://go.dev/play/p/Rq9UUBS_Xp1)]
- [HttpDelete<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete) - **<big>GetPublicIpInfo</big>** : 获取[公网ip信息](http://ip-api.com/json/).
- [HttpPost<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)]
- [HttpPut<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut) [[play](https://go.dev/play/p/YDxIfozsRHR)]
- [HttpPatch<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch) - **<big>GetRequestPublicIp</big>** : 获取http请求ip。
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse) [[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetRequestPublicIp)]
[[play](https://go.dev/play/p/kxU-YDc_eBo)]
- **<big>IsPublicIP</big>** : 判断ip是否是公共ip。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)]
[[play](https://go.dev/play/p/nmktSQpJZnn)]
- **<big>IsInternalIP</big>** : 判断ip是否是局域网ip。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsInternalIP)]
[[play](https://go.dev/play/p/sYGhXbgO4Cb)]
- **<big>HttpRequest</big>** : 用于抽象HTTP请求实体的结构。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpRequest)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>HttpClient</big>** : 用于发送HTTP请求。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpClient)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>SendRequest</big>** : 发送http请求。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#SendRequest)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>DecodeResponse</big>** : 解析http响应体到目标结构体。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse)]
[[play](https://go.dev/play/p/jUSgynekH7G)]
- **<big>StructToUrlValues</big>** : 将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues)]
[[play](https://go.dev/play/p/pFqMkM40w9z)]
- **<big>HttpGet<sup>deprecated</sup></big>** : 发送http get请求已弃用SendRequest代替
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)]
- **<big>HttpDelete<sup>deprecated</sup></big>** : 发送http delete请求已弃用SendRequest代替
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)]
- **<big>HttpPost<sup>deprecated</sup></big>** : 发送http post请求已弃用SendRequest代替
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)]
- **<big>HttpPut<sup>deprecated</sup></big>** : 发送http put请求已弃用SendRequest代替
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)]
- **<big>HttpPatch<sup>deprecated</sup></big>** : 发送http patch请求已弃用SendRequest代替
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)]
- **<big>ParseHttpResponse</big>** : 解析http响应体到目标结构体。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)]
### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。 ### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
@@ -571,11 +778,23 @@ import "github.com/duke-git/lancet/v2/retry"
#### 函数列表: #### 函数列表:
- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context) - **<big>Context</big>** : 设置重试context参数。
- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry) [[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context)]
- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc) [[play](https://go.dev/play/p/xnAOOXv9GkS)]
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration) - **<big>Retry</big>** : 重试执行函数retryFunc直到函数运行成功或被context取消。
- [RetryTimes](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#Retry)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryFunc</big>** : 重试执行的函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryDuration</big>** : 设置重试间隔时间默认3秒。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration)]
[[play](https://go.dev/play/p/nk2XRmagfVF)]
- **<big>RetryTimes</big>** : 设置重试次数默认5。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)]
[[play](https://go.dev/play/p/ssfVeU2SwLO)]
### 16. slice 包含操作切片的方法集合。 ### 16. slice 包含操作切片的方法集合。
@@ -806,6 +1025,7 @@ import "github.com/duke-git/lancet/v2/strutil"
[[play](https://go.dev/play/p/Us-ySSbWh-3)] [[play](https://go.dev/play/p/Us-ySSbWh-3)]
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。 - **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
[[play](https://go.dev/play/p/q3sM6ehnPDp)]
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串 - **<big>Wrap</big>** : 用给定字符包裹传入的字符串
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
[[play](https://go.dev/play/p/KoZOlZDDt9y)] [[play](https://go.dev/play/p/KoZOlZDDt9y)]
@@ -951,7 +1171,11 @@ import "github.com/duke-git/lancet/v2/xerror"
#### 函数列表: #### 函数列表:
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)
- **<big>Unwrap</big>** : 检查error, 如果err为nil则展开则它返回一个有效值如果err不是nil则Unwrap使用err发生panic。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)]
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
## 如何贡献代码 ## 如何贡献代码

View File

@@ -10,6 +10,7 @@ import "github.com/duke-git/lancet/v2/lancetconstraints"
// LinearSearch return the index of target in slice base on equal function. // LinearSearch return the index of target in slice base on equal function.
// If not found return -1 // If not found return -1
// Play: https://go.dev/play/p/IsS7rgn5s3x
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 {
for i, v := range slice { for i, v := range slice {
if equal(v, target) { if equal(v, target) {

View File

@@ -20,7 +20,7 @@ func NewChannel[T any]() *Channel[T] {
} }
// Generate creates channel, then put values into the channel. // Generate creates channel, then put values into the channel.
// Play: // Play: https://go.dev/play/p/7aB4KyMMp9A
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T { func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
dataStream := make(chan T) dataStream := make(chan T)
@@ -40,7 +40,7 @@ func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
} }
// Repeat create channel, put values into the channel repeatly until cancel the context. // Repeat create channel, put values into the channel repeatly until cancel the context.
// Play: // Play: https://go.dev/play/p/k5N_ALVmYjE
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T { func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
dataStream := make(chan T) dataStream := make(chan T)
@@ -61,7 +61,7 @@ func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
// RepeatFn create a channel, excutes fn repeatly, and put the result into the channel // RepeatFn create a channel, excutes fn repeatly, and put the result into the channel
// until close context. // until close context.
// Play: // Play: https://go.dev/play/p/4J1zAWttP85
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 {
dataStream := make(chan T) dataStream := make(chan T)
@@ -79,7 +79,7 @@ func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
} }
// Take create a channel whose values are taken from another channel with limit number. // Take create a channel whose values are taken from another channel with limit number.
// Play: // Play: https://go.dev/play/p/9Utt-1pDr2J
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 {
takeStream := make(chan T) takeStream := make(chan T)
@@ -99,7 +99,7 @@ func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int)
} }
// FanIn merge multiple channels into one channel. // FanIn merge multiple channels into one channel.
// Play: // Play: https://go.dev/play/p/2VYFMexEvTm
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 {
out := make(chan T) out := make(chan T)
@@ -127,7 +127,7 @@ func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
} }
// Tee split one chanel into two channels, until cancel the context. // Tee split one chanel into two channels, until cancel the context.
// Play: // Play: https://go.dev/play/p/3TQPKnCirrP
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) {
out1 := make(chan T) out1 := make(chan T)
out2 := make(chan T) out2 := make(chan T)
@@ -154,7 +154,7 @@ func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
} }
// Bridge link multiply channels into one channel. // Bridge link multiply channels into one channel.
// Play: // Play: https://go.dev/play/p/qmWSy1NVF-Y
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 {
valStream := make(chan T) valStream := make(chan T)
@@ -186,7 +186,7 @@ func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-c
} }
// Or read one or more channels into one channel, will close when any readin channel is closed. // Or read one or more channels into one channel, will close when any readin channel is closed.
// Play: // Play: https://go.dev/play/p/Wqz9rwioPww
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T { func (c *Channel[T]) Or(channels ...<-chan T) <-chan T {
switch len(channels) { switch len(channels) {
case 0: case 0:
@@ -220,7 +220,7 @@ func (c *Channel[T]) Or(channels ...<-chan T) <-chan T {
} }
// OrDone read a channel into another channel, will close until cancel context. // OrDone read a channel into another channel, will close until cancel context.
// Play: // Play: https://go.dev/play/p/lm_GoS6aDjo
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 {
valStream := make(chan T) valStream := make(chan T)

View File

@@ -1,186 +0,0 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package cryptor implements some util functions to encrypt and decrypt.
// Note:
// 1. for aes crypt function, the `key` param length should be 16, 24 or 32. if not, will panic.
package cryptor
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
)
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
// len(key) should be 16, 24 or 32
func AesEcbEncrypt(data, key []byte) []byte {
length := (len(data) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, data)
pad := byte(len(plain) - len(data))
for i := len(data); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
cipher, _ := aes.NewCipher(generateAesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
// len(key) should be 16, 24 or 32
func AesEcbDecrypt(encrypted, key []byte) []byte {
cipher, _ := aes.NewCipher(generateAesKey(key))
decrypted := make([]byte, len(encrypted))
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
// AesCbcEncrypt encrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32
func AesCbcEncrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesCbcDecrypt decrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32
func AesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := aes.NewCipher(key)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}
// AesCtrCrypt encrypt data with key use AES CTR algorithm
// len(key) should be 16, 24 or 32
func AesCtrCrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
iv := bytes.Repeat([]byte("1"), block.BlockSize())
stream := cipher.NewCTR(block, iv)
dst := make([]byte, len(data))
stream.XORKeyStream(dst, data)
return dst
}
// AesCfbEncrypt encrypt data with key use AES CFB algorithm
// len(key) should be 16, 24 or 32
func AesCfbEncrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesCfbDecrypt decrypt data with key use AES CFB algorithm
// len(encrypted) should be great than 16, len(key) should be 16, 24 or 32
func AesCfbDecrypt(encrypted, key []byte) []byte {
if len(encrypted) < aes.BlockSize {
panic("encrypted data is too short")
}
block, _ := aes.NewCipher(key)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
// AesOfbEncrypt encrypt data with key use AES OFB algorithm
// len(key) should be 16, 24 or 32
func AesOfbEncrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
data = pkcs7Padding(data, aes.BlockSize)
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesOfbDecrypt decrypt data with key use AES OFB algorithm
// len(key) should be 16, 24 or 32
func AesOfbDecrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
iv := data[:aes.BlockSize]
data = data[aes.BlockSize:]
if len(data)%aes.BlockSize != 0 {
return nil
}
decrypted := make([]byte, len(data))
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(decrypted, data)
decrypted = pkcs7UnPadding(decrypted)
return decrypted
}

View File

@@ -1,62 +0,0 @@
package cryptor
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestAesEcbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesEcbEncrypt := AesEcbEncrypt([]byte(data), []byte(key))
aesEcbDecrypt := AesEcbDecrypt(aesEcbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesEcbEncrypt")
assert.Equal(data, string(aesEcbDecrypt))
}
func TestAesCbcEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCbcEncrypt := AesCbcEncrypt([]byte(data), []byte(key))
aesCbcDecrypt := AesCbcDecrypt(aesCbcEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCbcEncrypt")
assert.Equal(data, string(aesCbcDecrypt))
}
func TestAesCtrCrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCtrCrypt := AesCtrCrypt([]byte(data), []byte(key))
aesCtrDeCrypt := AesCtrCrypt(aesCtrCrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCtrCrypt")
assert.Equal(data, string(aesCtrDeCrypt))
}
func TestAesCfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCfbEncrypt := AesCfbEncrypt([]byte(data), []byte(key))
aesCfbDecrypt := AesCfbDecrypt(aesCfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCfbEncrypt")
assert.Equal(data, string(aesCfbDecrypt))
}
func TestAesOfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesOfbEncrypt := AesOfbEncrypt([]byte(data), []byte(key))
aesOfbDecrypt := AesOfbDecrypt(aesOfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesOfbEncrypt")
assert.Equal(data, string(aesOfbDecrypt))
}

View File

@@ -19,25 +19,28 @@ import (
"os" "os"
) )
// Base64StdEncode encode string with base64 encoding // Base64StdEncode encode string with base64 encoding.
// Play: https://go.dev/play/p/VOaUyQUreoK
func Base64StdEncode(s string) string { func Base64StdEncode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s)) return base64.StdEncoding.EncodeToString([]byte(s))
} }
// Base64StdDecode decode a base64 encoded string // Base64StdDecode decode a base64 encoded string.
// Play: https://go.dev/play/p/RWQylnJVgIe
func Base64StdDecode(s string) string { func Base64StdDecode(s string) string {
b, _ := base64.StdEncoding.DecodeString(s) b, _ := base64.StdEncoding.DecodeString(s)
return string(b) return string(b)
} }
// Md5String return the md5 value of string // Md5String return the md5 value of string.
// Play: https://go.dev/play/p/1bLcVetbTOI
func Md5String(s string) string { func Md5String(s string) string {
h := md5.New() h := md5.New()
h.Write([]byte(s)) h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
// Md5File return the md5 value of file // Md5File return the md5 value of file.
func Md5File(filename string) (string, error) { func Md5File(filename string) (string, error) {
if fileInfo, err := os.Stat(filename); err != nil { if fileInfo, err := os.Stat(filename); err != nil {
return "", err return "", err
@@ -69,49 +72,56 @@ func Md5File(filename string) (string, error) {
return checksum, nil return checksum, nil
} }
// HmacMd5 return the hmac hash of string use md5 // HmacMd5 return the hmac hash of string use md5.
// Play: https://go.dev/play/p/uef0q1fz53I
func HmacMd5(data, key string) string { func HmacMd5(data, key string) string {
h := hmac.New(md5.New, []byte(key)) h := hmac.New(md5.New, []byte(key))
h.Write([]byte(data)) h.Write([]byte(data))
return hex.EncodeToString(h.Sum([]byte(""))) return hex.EncodeToString(h.Sum([]byte("")))
} }
// HmacSha1 return the hmac hash of string use sha1 // HmacSha1 return the hmac hash of string use sha1.
// Play: https://go.dev/play/p/1UI4oQ4WXKM
func HmacSha1(data, key string) string { func HmacSha1(data, key string) string {
h := hmac.New(sha1.New, []byte(key)) h := hmac.New(sha1.New, []byte(key))
h.Write([]byte(data)) h.Write([]byte(data))
return hex.EncodeToString(h.Sum([]byte(""))) return hex.EncodeToString(h.Sum([]byte("")))
} }
// HmacSha256 return the hmac hash of string use sha256 // HmacSha256 return the hmac hash of string use sha256.
// Play: https://go.dev/play/p/HhpwXxFhhC0
func HmacSha256(data, key string) string { func HmacSha256(data, key string) string {
h := hmac.New(sha256.New, []byte(key)) h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(data)) h.Write([]byte(data))
return hex.EncodeToString(h.Sum([]byte(""))) return hex.EncodeToString(h.Sum([]byte("")))
} }
// HmacSha512 return the hmac hash of string use sha512 // HmacSha512 return the hmac hash of string use sha512.
// Play: https://go.dev/play/p/59Od6m4A0Ud
func HmacSha512(data, key string) string { func HmacSha512(data, key string) string {
h := hmac.New(sha512.New, []byte(key)) h := hmac.New(sha512.New, []byte(key))
h.Write([]byte(data)) h.Write([]byte(data))
return hex.EncodeToString(h.Sum([]byte(""))) return hex.EncodeToString(h.Sum([]byte("")))
} }
// Sha1 return the sha1 value (SHA-1 hash algorithm) of string // Sha1 return the sha1 value (SHA-1 hash algorithm) of string.
// Play: https://go.dev/play/p/_m_uoD1deMT
func Sha1(data string) string { func Sha1(data string) string {
sha1 := sha1.New() sha1 := sha1.New()
sha1.Write([]byte(data)) sha1.Write([]byte(data))
return hex.EncodeToString(sha1.Sum([]byte(""))) return hex.EncodeToString(sha1.Sum([]byte("")))
} }
// Sha256 return the sha256 value (SHA256 hash algorithm) of string // Sha256 return the sha256 value (SHA256 hash algorithm) of string.
// Play: https://go.dev/play/p/tU9tfBMIAr1
func Sha256(data string) string { func Sha256(data string) string {
sha256 := sha256.New() sha256 := sha256.New()
sha256.Write([]byte(data)) sha256.Write([]byte(data))
return hex.EncodeToString(sha256.Sum([]byte(""))) return hex.EncodeToString(sha256.Sum([]byte("")))
} }
// Sha512 return the sha512 value (SHA512 hash algorithm) of string // Sha512 return the sha512 value (SHA512 hash algorithm) of string.
// Play: https://go.dev/play/p/3WsvLYZxsHa
func Sha512(data string) string { func Sha512(data string) string {
sha512 := sha512.New() sha512 := sha512.New()
sha512.Write([]byte(data)) sha512.Write([]byte(data))

View File

@@ -0,0 +1,410 @@
package cryptor
import "fmt"
func ExampleAesEcbEncrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesEcbEncrypt([]byte(data), []byte(key))
decrypted := AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesEcbDecrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesEcbEncrypt([]byte(data), []byte(key))
decrypted := AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesCbcEncrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesCbcEncrypt([]byte(data), []byte(key))
decrypted := AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesCbcDecrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesCbcEncrypt([]byte(data), []byte(key))
decrypted := AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesCtrCrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesCtrCrypt([]byte(data), []byte(key))
decrypted := AesCtrCrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesCfbEncrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesCfbEncrypt([]byte(data), []byte(key))
decrypted := AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesCfbDecrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesCfbEncrypt([]byte(data), []byte(key))
decrypted := AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesOfbEncrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesOfbEncrypt([]byte(data), []byte(key))
decrypted := AesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleAesOfbDecrypt() {
data := "hello"
key := "abcdefghijklmnop"
encrypted := AesOfbEncrypt([]byte(data), []byte(key))
decrypted := AesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesEcbEncrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesEcbEncrypt([]byte(data), []byte(key))
decrypted := DesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesEcbDecrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesEcbEncrypt([]byte(data), []byte(key))
decrypted := DesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesCbcEncrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesCbcEncrypt([]byte(data), []byte(key))
decrypted := DesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesCbcDecrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesCbcEncrypt([]byte(data), []byte(key))
decrypted := DesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesCtrCrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesCtrCrypt([]byte(data), []byte(key))
decrypted := DesCtrCrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesCfbEncrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesCfbEncrypt([]byte(data), []byte(key))
decrypted := DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesCfbDecrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesCfbEncrypt([]byte(data), []byte(key))
decrypted := DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesOfbEncrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesOfbEncrypt([]byte(data), []byte(key))
decrypted := DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleDesOfbDecrypt() {
data := "hello"
key := "abcdefgh"
encrypted := DesOfbEncrypt([]byte(data), []byte(key))
decrypted := DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleGenerateRsaKey() {
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
return
}
fmt.Println("foo")
// Output:
// foo
}
func ExampleRsaEncrypt() {
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
return
}
data := []byte("hello")
encrypted := RsaEncrypt(data, "rsa_public.pem")
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleRsaDecrypt() {
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
return
}
data := []byte("hello")
encrypted := RsaEncrypt(data, "rsa_public.pem")
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted))
// Output:
// hello
}
func ExampleBase64StdEncode() {
base64Str := Base64StdEncode("hello")
fmt.Println(base64Str)
// Output:
// aGVsbG8=
}
func ExampleBase64StdDecode() {
str := Base64StdDecode("aGVsbG8=")
fmt.Println(str)
// Output:
// hello
}
func ExampleHmacMd5() {
str := "hello"
key := "12345"
hms := HmacMd5(str, key)
fmt.Println(hms)
// Output:
// e834306eab892d872525d4918a7a639a
}
func ExampleHmacSha1() {
str := "hello"
key := "12345"
hms := HmacSha1(str, key)
fmt.Println(hms)
// Output:
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
}
func ExampleHmacSha256() {
str := "hello"
key := "12345"
hms := HmacSha256(str, key)
fmt.Println(hms)
// Output:
// 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
}
func ExampleHmacSha512() {
str := "hello"
key := "12345"
hms := HmacSha512(str, key)
fmt.Println(hms)
// Output:
// dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
}
func ExampleMd5String() {
str := "hello"
md5Str := Md5String(str)
fmt.Println(md5Str)
// Output:
// 5d41402abc4b2a76b9719d911017c592
}
func ExampleSha1() {
str := "hello"
result := Sha1(str)
fmt.Println(result)
// Output:
// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
}
func ExampleSha256() {
str := "hello"
result := Sha256(str)
fmt.Println(result)
// Output:
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}
func ExampleSha512() {
str := "hello"
result := Sha512(str)
fmt.Println(result)
// Output:
// 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
}

View File

@@ -1,180 +0,0 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package cryptor implements some util functions to encrypt and decrypt.
package cryptor
import (
"bytes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
"io"
)
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
// len(key) should be 8
func DesEcbEncrypt(data, key []byte) []byte {
length := (len(data) + des.BlockSize) / des.BlockSize
plain := make([]byte, length*des.BlockSize)
copy(plain, data)
pad := byte(len(plain) - len(data))
for i := len(data); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
cipher, _ := des.NewCipher(generateDesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
// DesEcbDecrypt decrypt data with key use DES ECB algorithm
// len(key) should be 8
func DesEcbDecrypt(encrypted, key []byte) []byte {
cipher, _ := des.NewCipher(generateDesKey(key))
decrypted := make([]byte, len(encrypted))
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
// DesCbcEncrypt encrypt data with key use DES CBC algorithm
// len(key) should be 8
func DesCbcEncrypt(data, key []byte) []byte {
block, _ := des.NewCipher(key)
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[des.BlockSize:], data)
return encrypted
}
// DesCbcDecrypt decrypt data with key use DES CBC algorithm
// len(key) should be 8
func DesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := des.NewCipher(key)
iv := encrypted[:des.BlockSize]
encrypted = encrypted[des.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}
// DesCtrCrypt encrypt data with key use DES CTR algorithm
// len(key) should be 8
func DesCtrCrypt(data, key []byte) []byte {
block, _ := des.NewCipher(key)
iv := bytes.Repeat([]byte("1"), block.BlockSize())
stream := cipher.NewCTR(block, iv)
dst := make([]byte, len(data))
stream.XORKeyStream(dst, data)
return dst
}
// DesCfbEncrypt encrypt data with key use DES CFB algorithm
// len(key) should be 8
func DesCfbEncrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[des.BlockSize:], data)
return encrypted
}
// DesCfbDecrypt decrypt data with key use DES CFB algorithm
// len(encrypted) should be great than 16, len(key) should be 8
func DesCfbDecrypt(encrypted, key []byte) []byte {
block, _ := des.NewCipher(key)
if len(encrypted) < des.BlockSize {
panic("encrypted data is too short")
}
iv := encrypted[:des.BlockSize]
encrypted = encrypted[des.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
// DesOfbEncrypt encrypt data with key use DES OFB algorithm
// len(key) should be 16, 24 or 32
func DesOfbEncrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
data = pkcs7Padding(data, des.BlockSize)
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(encrypted[des.BlockSize:], data)
return encrypted
}
// DesOfbDecrypt decrypt data with key use DES OFB algorithm
// len(key) should be 8
func DesOfbDecrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
iv := data[:des.BlockSize]
data = data[des.BlockSize:]
if len(data)%des.BlockSize != 0 {
return nil
}
decrypted := make([]byte, len(data))
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(decrypted, data)
decrypted = pkcs7UnPadding(decrypted)
return decrypted
}

View File

@@ -1,62 +0,0 @@
package cryptor
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestDesEcbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desEcbEncrypt := DesEcbEncrypt([]byte(data), []byte(key))
desEcbDecrypt := DesEcbDecrypt(desEcbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesEcbEncrypt")
assert.Equal(data, string(desEcbDecrypt))
}
func TestDesCbcEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCbcEncrypt := DesCbcEncrypt([]byte(data), []byte(key))
desCbcDecrypt := DesCbcDecrypt(desCbcEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCbcEncrypt")
assert.Equal(data, string(desCbcDecrypt))
}
func TestDesCtrCrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCtrCrypt := DesCtrCrypt([]byte(data), []byte(key))
desCtrDeCrypt := DesCtrCrypt(desCtrCrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCtrCrypt")
assert.Equal(data, string(desCtrDeCrypt))
}
func TestDesCfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCfbEncrypt := DesCfbEncrypt([]byte(data), []byte(key))
desCfbDecrypt := DesCfbDecrypt(desCfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCfbEncrypt")
assert.Equal(data, string(desCfbDecrypt))
}
func TestDesOfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desOfbEncrypt := DesOfbEncrypt([]byte(data), []byte(key))
desOfbDecrypt := DesOfbDecrypt(desOfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesOfbEncrypt")
assert.Equal(data, string(desOfbDecrypt))
}

498
cryptor/encrypt.go Normal file
View File

@@ -0,0 +1,498 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package cryptor implements some util functions to encrypt and decrypt.
// Note:
// 1. for aes crypt function, the `key` param length should be 16, 24 or 32. if not, will panic.
package cryptor
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io"
"os"
)
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/jT5irszHx-j
func AesEcbEncrypt(data, key []byte) []byte {
length := (len(data) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, data)
pad := byte(len(plain) - len(data))
for i := len(data); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
cipher, _ := aes.NewCipher(generateAesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/jT5irszHx-j
func AesEcbDecrypt(encrypted, key []byte) []byte {
cipher, _ := aes.NewCipher(generateAesKey(key))
decrypted := make([]byte, len(encrypted))
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
// AesCbcEncrypt encrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/IOq_g8_lKZD
func AesCbcEncrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesCbcDecrypt decrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/IOq_g8_lKZD
func AesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := aes.NewCipher(key)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}
// AesCtrCrypt encrypt data with key use AES CTR algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/SpaZO0-5Nsp
func AesCtrCrypt(data, key []byte) []byte {
block, _ := aes.NewCipher(key)
iv := bytes.Repeat([]byte("1"), block.BlockSize())
stream := cipher.NewCTR(block, iv)
dst := make([]byte, len(data))
stream.XORKeyStream(dst, data)
return dst
}
// AesCfbEncrypt encrypt data with key use AES CFB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/tfkF10B13kH
func AesCfbEncrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesCfbDecrypt decrypt data with key use AES CFB algorithm
// len(encrypted) should be great than 16, len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/tfkF10B13kH
func AesCfbDecrypt(encrypted, key []byte) []byte {
if len(encrypted) < aes.BlockSize {
panic("encrypted data is too short")
}
block, _ := aes.NewCipher(key)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
// AesOfbEncrypt encrypt data with key use AES OFB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/VtHxtkUj-3F
func AesOfbEncrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
data = pkcs7Padding(data, aes.BlockSize)
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], data)
return encrypted
}
// AesOfbDecrypt decrypt data with key use AES OFB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/VtHxtkUj-3F
func AesOfbDecrypt(data, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
iv := data[:aes.BlockSize]
data = data[aes.BlockSize:]
if len(data)%aes.BlockSize != 0 {
return nil
}
decrypted := make([]byte, len(data))
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(decrypted, data)
decrypted = pkcs7UnPadding(decrypted)
return decrypted
}
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/8qivmPeZy4P
func DesEcbEncrypt(data, key []byte) []byte {
length := (len(data) + des.BlockSize) / des.BlockSize
plain := make([]byte, length*des.BlockSize)
copy(plain, data)
pad := byte(len(plain) - len(data))
for i := len(data); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
cipher, _ := des.NewCipher(generateDesKey(key))
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
// DesEcbDecrypt decrypt data with key use DES ECB algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/8qivmPeZy4P
func DesEcbDecrypt(encrypted, key []byte) []byte {
cipher, _ := des.NewCipher(generateDesKey(key))
decrypted := make([]byte, len(encrypted))
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
// DesCbcEncrypt encrypt data with key use DES CBC algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/4cC4QvWfe3_1
func DesCbcEncrypt(data, key []byte) []byte {
block, _ := des.NewCipher(key)
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[des.BlockSize:], data)
return encrypted
}
// DesCbcDecrypt decrypt data with key use DES CBC algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/4cC4QvWfe3_1
func DesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := des.NewCipher(key)
iv := encrypted[:des.BlockSize]
encrypted = encrypted[des.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}
// DesCtrCrypt encrypt data with key use DES CTR algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/9-T6OjKpcdw
func DesCtrCrypt(data, key []byte) []byte {
block, _ := des.NewCipher(key)
iv := bytes.Repeat([]byte("1"), block.BlockSize())
stream := cipher.NewCTR(block, iv)
dst := make([]byte, len(data))
stream.XORKeyStream(dst, data)
return dst
}
// DesCfbEncrypt encrypt data with key use DES CFB algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/y-eNxcFBlxL
func DesCfbEncrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[des.BlockSize:], data)
return encrypted
}
// DesCfbDecrypt decrypt data with key use DES CFB algorithm
// len(encrypted) should be great than 16, len(key) should be 8.
// Play: https://go.dev/play/p/y-eNxcFBlxL
func DesCfbDecrypt(encrypted, key []byte) []byte {
block, _ := des.NewCipher(key)
if len(encrypted) < des.BlockSize {
panic("encrypted data is too short")
}
iv := encrypted[:des.BlockSize]
encrypted = encrypted[des.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
// DesOfbEncrypt encrypt data with key use DES OFB algorithm
// len(key) should be 16, 24 or 32.
// Play: https://go.dev/play/p/74KmNadjN1J
func DesOfbEncrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
data = pkcs7Padding(data, des.BlockSize)
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(encrypted[des.BlockSize:], data)
return encrypted
}
// DesOfbDecrypt decrypt data with key use DES OFB algorithm
// len(key) should be 8.
// Play: https://go.dev/play/p/74KmNadjN1J
func DesOfbDecrypt(data, key []byte) []byte {
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
iv := data[:des.BlockSize]
data = data[des.BlockSize:]
if len(data)%des.BlockSize != 0 {
return nil
}
decrypted := make([]byte, len(data))
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(decrypted, data)
decrypted = pkcs7UnPadding(decrypted)
return decrypted
}
// GenerateRsaKey create rsa private and public pemo file.
// Play: https://go.dev/play/p/zutRHrDqs0X
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
// private key
privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return err
}
derText := x509.MarshalPKCS1PrivateKey(privateKey)
block := pem.Block{
Type: "rsa private key",
Bytes: derText,
}
file, err := os.Create(priKeyFile)
if err != nil {
panic(err)
}
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
// public key
publicKey := privateKey.PublicKey
derpText, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return err
}
block = pem.Block{
Type: "rsa public key",
Bytes: derpText,
}
file, err = os.Create(pubKeyFile)
if err != nil {
return err
}
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
return nil
}
// RsaEncrypt encrypt data with ras algorithm.
// Play: https://go.dev/play/p/rDqTT01SPkZ
func RsaEncrypt(data []byte, pubKeyFileName string) []byte {
file, err := os.Open(pubKeyFileName)
if err != nil {
panic(err)
}
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
defer file.Close()
buf := make([]byte, fileInfo.Size())
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
panic(err)
}
pubKey := pubInterface.(*rsa.PublicKey)
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
if err != nil {
panic(err)
}
return cipherText
}
// RsaDecrypt decrypt data with ras algorithm.
// Play: https://go.dev/play/p/rDqTT01SPkZ
func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
file, err := os.Open(privateKeyFileName)
if err != nil {
panic(err)
}
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
buf := make([]byte, fileInfo.Size())
defer file.Close()
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)
priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
panic(err)
}
plainText, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, data)
if err != nil {
panic(err)
}
return plainText
}

130
cryptor/encrypt_test.go Normal file
View File

@@ -0,0 +1,130 @@
package cryptor
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestAesEcbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesEcbEncrypt := AesEcbEncrypt([]byte(data), []byte(key))
aesEcbDecrypt := AesEcbDecrypt(aesEcbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesEcbEncrypt")
assert.Equal(data, string(aesEcbDecrypt))
}
func TestAesCbcEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCbcEncrypt := AesCbcEncrypt([]byte(data), []byte(key))
aesCbcDecrypt := AesCbcDecrypt(aesCbcEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCbcEncrypt")
assert.Equal(data, string(aesCbcDecrypt))
}
func TestAesCtrCrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCtrCrypt := AesCtrCrypt([]byte(data), []byte(key))
aesCtrDeCrypt := AesCtrCrypt(aesCtrCrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCtrCrypt")
assert.Equal(data, string(aesCtrDeCrypt))
}
func TestAesCfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesCfbEncrypt := AesCfbEncrypt([]byte(data), []byte(key))
aesCfbDecrypt := AesCfbDecrypt(aesCfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesCfbEncrypt")
assert.Equal(data, string(aesCfbDecrypt))
}
func TestAesOfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefghijklmnop"
aesOfbEncrypt := AesOfbEncrypt([]byte(data), []byte(key))
aesOfbDecrypt := AesOfbDecrypt(aesOfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestAesOfbEncrypt")
assert.Equal(data, string(aesOfbDecrypt))
}
func TestDesEcbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desEcbEncrypt := DesEcbEncrypt([]byte(data), []byte(key))
desEcbDecrypt := DesEcbDecrypt(desEcbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesEcbEncrypt")
assert.Equal(data, string(desEcbDecrypt))
}
func TestDesCbcEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCbcEncrypt := DesCbcEncrypt([]byte(data), []byte(key))
desCbcDecrypt := DesCbcDecrypt(desCbcEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCbcEncrypt")
assert.Equal(data, string(desCbcDecrypt))
}
func TestDesCtrCrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCtrCrypt := DesCtrCrypt([]byte(data), []byte(key))
desCtrDeCrypt := DesCtrCrypt(desCtrCrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCtrCrypt")
assert.Equal(data, string(desCtrDeCrypt))
}
func TestDesCfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desCfbEncrypt := DesCfbEncrypt([]byte(data), []byte(key))
desCfbDecrypt := DesCfbDecrypt(desCfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesCfbEncrypt")
assert.Equal(data, string(desCfbDecrypt))
}
func TestDesOfbEncrypt(t *testing.T) {
data := "hello world"
key := "abcdefgh"
desOfbEncrypt := DesOfbEncrypt([]byte(data), []byte(key))
desOfbDecrypt := DesOfbDecrypt(desOfbEncrypt, []byte(key))
assert := internal.NewAssert(t, "TestDesOfbEncrypt")
assert.Equal(data, string(desOfbDecrypt))
}
func TestRsaEncrypt(t *testing.T) {
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
t.FailNow()
}
data := []byte("hello world")
encrypted := RsaEncrypt(data, "rsa_public.pem")
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
assert := internal.NewAssert(t, "TestRsaEncrypt")
assert.Equal(string(data), string(decrypted))
}

View File

@@ -1,134 +0,0 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package cryptor implements some util functions to encrypt and decrypt.
package cryptor
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
)
// GenerateRsaKey make a rsa private key, and return key file name
// Generated key file is `rsa_private.pem` and `rsa_public.pem` in current path
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
// private key
privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return err
}
derText := x509.MarshalPKCS1PrivateKey(privateKey)
block := pem.Block{
Type: "rsa private key",
Bytes: derText,
}
//file,err := os.Create("rsa_private.pem")
file, err := os.Create(priKeyFile)
if err != nil {
panic(err)
}
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
// public key
publicKey := privateKey.PublicKey
derpText, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return err
}
block = pem.Block{
Type: "rsa public key",
Bytes: derpText,
}
file, err = os.Create(pubKeyFile)
if err != nil {
return err
}
err = pem.Encode(file, &block)
if err != nil {
return err
}
file.Close()
return nil
}
// RsaEncrypt encrypt data with ras algorithm
func RsaEncrypt(data []byte, pubKeyFileName string) []byte {
file, err := os.Open(pubKeyFileName)
if err != nil {
panic(err)
}
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
defer file.Close()
buf := make([]byte, fileInfo.Size())
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
panic(err)
}
pubKey := pubInterface.(*rsa.PublicKey)
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
if err != nil {
panic(err)
}
return cipherText
}
// RsaDecrypt decrypt data with ras algorithm
func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
file, err := os.Open(privateKeyFileName)
if err != nil {
panic(err)
}
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
buf := make([]byte, fileInfo.Size())
defer file.Close()
_, err = file.Read(buf)
if err != nil {
panic(err)
}
block, _ := pem.Decode(buf)
priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
panic(err)
}
plainText, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, data)
if err != nil {
panic(err)
}
return plainText
}

View File

@@ -1,20 +0,0 @@
package cryptor
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestRsaEncrypt(t *testing.T) {
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
t.FailNow()
}
data := []byte("hello world")
encrypted := RsaEncrypt(data, "rsa_public.pem")
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
assert := internal.NewAssert(t, "TestRsaEncrypt")
assert.Equal(string(data), string(decrypted))
}

View File

@@ -6,19 +6,21 @@ package datastructure
import ( import (
"reflect" "reflect"
"github.com/duke-git/lancet/v2/iterator"
) )
// List is a linear table, implemented with slice // List is a linear table, implemented with slice.
type List[T any] struct { type List[T any] struct {
data []T data []T
} }
// NewList return a pointer of List // NewList return a pointer of List.
func NewList[T any](data []T) *List[T] { func NewList[T any](data []T) *List[T] {
return &List[T]{data: data} return &List[T]{data: data}
} }
// Data return list data // Data return list data.
func (l *List[T]) Data() []T { func (l *List[T]) Data() []T {
return l.data return l.data
} }
@@ -31,7 +33,7 @@ func (l *List[T]) ValueOf(index int) (*T, bool) {
return &l.data[index], true return &l.data[index], true
} }
// IndexOf returns the index of value. if not found return -1 // IndexOf returns the index of value. if not found return -1.
func (l *List[T]) IndexOf(value T) int { func (l *List[T]) IndexOf(value T) int {
index := -1 index := -1
data := l.data data := l.data
@@ -45,7 +47,7 @@ func (l *List[T]) IndexOf(value T) int {
} }
// LastIndexOf returns the index of the last occurrence of the value in this list. // LastIndexOf returns the index of the last occurrence of the value in this list.
// if not found return -1 // if not found return -1.
func (l *List[T]) LastIndexOf(value T) int { func (l *List[T]) LastIndexOf(value T) int {
index := -1 index := -1
data := l.data data := l.data
@@ -59,7 +61,7 @@ func (l *List[T]) LastIndexOf(value T) int {
} }
// IndexOfFunc returns the first index satisfying f(v) // IndexOfFunc returns the first index satisfying f(v)
// if not found return -1 // if not found return -1.
func (l *List[T]) IndexOfFunc(f func(T) bool) int { func (l *List[T]) IndexOfFunc(f func(T) bool) int {
index := -1 index := -1
data := l.data data := l.data
@@ -73,7 +75,7 @@ func (l *List[T]) IndexOfFunc(f func(T) bool) int {
} }
// LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i]) // LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i])
// if not found return -1 // if not found return -1.
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int { func (l *List[T]) LastIndexOfFunc(f func(T) bool) int {
index := -1 index := -1
data := l.data data := l.data
@@ -86,7 +88,7 @@ func (l *List[T]) LastIndexOfFunc(f func(T) bool) int {
return index return index
} }
// Contain checks if the value in the list or not // Contain checks if the value in the list or not.
func (l *List[T]) Contain(value T) bool { func (l *List[T]) Contain(value T) bool {
data := l.data data := l.data
for _, v := range data { for _, v := range data {
@@ -97,22 +99,22 @@ func (l *List[T]) Contain(value T) bool {
return false return false
} }
// Push append value to the list data // Push append value to the list data.
func (l *List[T]) Push(value T) { func (l *List[T]) Push(value T) {
l.data = append(l.data, value) l.data = append(l.data, value)
} }
// InsertAtFirst insert value into list at first index // InsertAtFirst insert value into list at first index.
func (l *List[T]) InsertAtFirst(value T) { func (l *List[T]) InsertAtFirst(value T) {
l.InsertAt(0, value) l.InsertAt(0, value)
} }
// InsertAtLast insert value into list at last index // InsertAtLast insert value into list at last index.
func (l *List[T]) InsertAtLast(value T) { func (l *List[T]) InsertAtLast(value T) {
l.InsertAt(len(l.data), value) l.InsertAt(len(l.data), value)
} }
// InsertAt insert value into list at index // InsertAt insert value into list at index.
func (l *List[T]) InsertAt(index int, value T) { func (l *List[T]) InsertAt(index int, value T) {
data := l.data data := l.data
size := len(data) size := len(data)
@@ -123,7 +125,7 @@ func (l *List[T]) InsertAt(index int, value T) {
l.data = append(data[:index], append([]T{value}, data[index:]...)...) l.data = append(data[:index], append([]T{value}, data[index:]...)...)
} }
// PopFirst delete the first value of list and return it // PopFirst delete the first value of list and return it.
func (l *List[T]) PopFirst() (*T, bool) { func (l *List[T]) PopFirst() (*T, bool) {
if len(l.data) == 0 { if len(l.data) == 0 {
return nil, false return nil, false
@@ -135,7 +137,7 @@ func (l *List[T]) PopFirst() (*T, bool) {
return &v, true return &v, true
} }
// PopLast delete the last value of list and return it // PopLast delete the last value of list and return it.
func (l *List[T]) PopLast() (*T, bool) { func (l *List[T]) PopLast() (*T, bool) {
size := len(l.data) size := len(l.data)
if size == 0 { if size == 0 {
@@ -148,7 +150,7 @@ func (l *List[T]) PopLast() (*T, bool) {
return &v, true return &v, true
} }
// DeleteAt delete the value of list at index // DeleteAt delete the value of list at index.
func (l *List[T]) DeleteAt(index int) { func (l *List[T]) DeleteAt(index int) {
data := l.data data := l.data
size := len(data) size := len(data)
@@ -199,7 +201,7 @@ func (l *List[T]) UpdateAt(index int, value T) {
l.data = append(data[:index], append([]T{value}, data[index+1:]...)...) l.data = append(data[:index], append([]T{value}, data[index+1:]...)...)
} }
// Equal compare list to other list, use reflect.DeepEqual // Equal compare list to other list, use reflect.DeepEqual.
func (l *List[T]) Equal(other *List[T]) bool { func (l *List[T]) Equal(other *List[T]) bool {
if len(l.data) != len(other.data) { if len(l.data) != len(other.data) {
return false return false
@@ -214,17 +216,17 @@ func (l *List[T]) Equal(other *List[T]) bool {
return true return true
} }
// IsEmpty check if the list is empty or not // IsEmpty check if the list is empty or not.
func (l *List[T]) IsEmpty() bool { func (l *List[T]) IsEmpty() bool {
return len(l.data) == 0 return len(l.data) == 0
} }
// Clear the data of list // Clear the data of list.
func (l *List[T]) Clear() { func (l *List[T]) Clear() {
l.data = make([]T, 0) l.data = make([]T, 0)
} }
// Clone return a copy of list // Clone return a copy of list.
func (l *List[T]) Clone() *List[T] { func (l *List[T]) Clone() *List[T] {
cl := NewList(make([]T, len(l.data))) cl := NewList(make([]T, len(l.data)))
copy(cl.data, l.data) copy(cl.data, l.data)
@@ -232,7 +234,7 @@ func (l *List[T]) Clone() *List[T] {
return cl return cl
} }
// Merge two list, return new list, don't change original list // Merge two list, return new list, don't change original list.
func (l *List[T]) Merge(other *List[T]) *List[T] { func (l *List[T]) Merge(other *List[T]) *List[T] {
l1, l2 := len(l.data), len(other.data) l1, l2 := len(l.data), len(other.data)
ml := NewList(make([]T, l1+l2)) ml := NewList(make([]T, l1+l2))
@@ -243,17 +245,17 @@ func (l *List[T]) Merge(other *List[T]) *List[T] {
return ml return ml
} }
// Size return number of list data items // Size return number of list data items.
func (l *List[T]) Size() int { func (l *List[T]) Size() int {
return len(l.data) return len(l.data)
} }
// Cap return cap of the inner data // Cap return cap of the inner data.
func (l *List[T]) Cap() int { func (l *List[T]) Cap() int {
return cap(l.data) return cap(l.data)
} }
// Swap the value of index i and j in list // Swap the value of index i and j in list.
func (l *List[T]) Swap(i, j int) { func (l *List[T]) Swap(i, j int) {
size := len(l.data) size := len(l.data)
if i < 0 || i >= size || j < 0 || j >= size { if i < 0 || i >= size || j < 0 || j >= size {
@@ -262,14 +264,14 @@ func (l *List[T]) Swap(i, j int) {
l.data[i], l.data[j] = l.data[j], l.data[i] l.data[i], l.data[j] = l.data[j], l.data[i]
} }
// Reverse the item order of list // Reverse the item order of list.
func (l *List[T]) Reverse() { func (l *List[T]) Reverse() {
for i, j := 0, len(l.data)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(l.data)-1; i < j; i, j = i+1, j-1 {
l.data[i], l.data[j] = l.data[j], l.data[i] l.data[i], l.data[j] = l.data[j], l.data[i]
} }
} }
// Unique remove duplicate items in list // Unique remove 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)
@@ -303,7 +305,7 @@ func (l *List[T]) Union(other *List[T]) *List[T] {
return result return result
} }
// Intersection creates a new list whose element both be contained in list l and other // Intersection creates a new list whose element both be contained in list l and other.
func (l *List[T]) Intersection(other *List[T]) *List[T] { func (l *List[T]) Intersection(other *List[T]) *List[T] {
result := NewList(make([]T, 0)) result := NewList(make([]T, 0))
@@ -316,6 +318,43 @@ func (l *List[T]) Intersection(other *List[T]) *List[T] {
return result return result
} }
// Difference returns the difference between two collections.
// return a list whose element in the original list, not in the given list.
func (l *List[T]) Difference(other *List[T]) *List[T] {
result := NewList(make([]T, 0))
intersectList := l.Intersection(other)
for _, v := range l.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
return result
}
// SymmetricDifference oppoiste operation of intersection function.
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T] {
result := NewList(make([]T, 0))
intersectList := l.Intersection(other)
for _, v := range l.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
for _, v := range other.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
return result
}
// SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive. // SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] { func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
data := l.data[fromIndex:toIndex] data := l.data[fromIndex:toIndex]
@@ -323,3 +362,57 @@ func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
copy(subList, data) copy(subList, data)
return NewList(subList) return NewList(subList)
} }
// ForEach performs the given action for each element of the list.
func (l *List[T]) ForEach(consumer func(T)) {
for _, it := range l.data {
consumer(it)
}
}
// RetainAll retains only the elements in this list that are contained in the given list.
func (l *List[T]) RetainAll(list *List[T]) bool {
return l.batchRemove(list, true)
}
// DeleteAll removes from this list all of its elements that are contained in the given list.
func (l *List[T]) DeleteAll(list *List[T]) bool {
return l.batchRemove(list, false)
}
func (l *List[T]) batchRemove(list *List[T], complement bool) bool {
var (
w = 0
data = l.data
size = len(data)
)
for i := 0; i < size; i++ {
if list.Contain(data[i]) == complement {
data[w] = data[i]
w++
}
}
if w != size {
l.data = data[:w]
return true
}
return false
}
// Iterator returns an iterator over the elements in this list in proper sequence.
func (l *List[T]) Iterator() iterator.Iterator[T] {
return iterator.FromSlice(l.data)
}
// ListToMap convert a list to a map based on iteratee function.
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V {
result := make(map[K]V, list.Size())
for _, item := range list.data {
k, v := iteratee(item)
result[k] = v
}
return result
}

View File

@@ -328,6 +328,28 @@ func TestIntersection(t *testing.T) {
assert.Equal(true, expected.Equal(list3)) assert.Equal(true, expected.Equal(list3))
} }
func TestDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestDifference")
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
expected := NewList([]int{3})
list3 := list1.Difference(list2)
assert.Equal(true, expected.Equal(list3))
}
func TestSymmetricDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestSymmetricDifference")
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
expected := NewList([]int{3, 4})
list3 := list1.SymmetricDifference(list2)
assert.Equal(true, expected.Equal(list3))
}
func TestSubSlice(t *testing.T) { func TestSubSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestSubSlice") assert := internal.NewAssert(t, "TestSubSlice")
@@ -357,3 +379,84 @@ func TestDeleteIf(t *testing.T) {
assert.Equal([]int{2, 3, 4}, list.Data()) assert.Equal([]int{2, 3, 4}, list.Data())
assert.Equal(0, count) assert.Equal(0, count)
} }
func TestForEach(t *testing.T) {
assert := internal.NewAssert(t, "TestForEach")
list := NewList([]int{1, 2, 3, 4})
rs := make([]int, 0)
list.ForEach(func(i int) {
rs = append(rs, i)
})
assert.Equal([]int{1, 2, 3, 4}, rs)
}
func TestRetainAll(t *testing.T) {
assert := internal.NewAssert(t, "TestRetainAll")
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
retain := NewList([]int{1, 2})
retain1 := NewList([]int{2, 3})
retain2 := NewList([]int{1, 2, 5})
list.RetainAll(retain)
list1.RetainAll(retain1)
list2.RetainAll(retain2)
assert.Equal([]int{1, 2}, list.Data())
assert.Equal([]int{2, 3}, list1.Data())
assert.Equal([]int{1, 2}, list2.Data())
}
func TestDeleteAll(t *testing.T) {
assert := internal.NewAssert(t, "TestDeleteAll")
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
del := NewList([]int{1})
del1 := NewList([]int{2, 3})
del2 := NewList([]int{1, 2, 5})
list.DeleteAll(del)
list1.DeleteAll(del1)
list2.DeleteAll(del2)
assert.Equal([]int{2, 3, 4}, list.Data())
assert.Equal([]int{1, 4}, list1.Data())
assert.Equal([]int{3, 4}, list2.Data())
}
func TestIterator(t *testing.T) {
assert := internal.NewAssert(t, "TestIterator")
list := NewList([]int{1, 2, 3, 4})
iterator := list.Iterator()
rs := make([]int, 0)
for iterator.HasNext() {
item, _ := iterator.Next()
rs = append(rs, item)
}
assert.Equal([]int{1, 2, 3, 4}, rs)
}
func TestListToMap(t *testing.T) {
assert := internal.NewAssert(t, "ListToMap")
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
expected := map[int]bool{1: false, 2: true, 3: true, 4: true}
assert.Equal(expected, result)
}

View File

@@ -9,17 +9,20 @@ type theTime struct {
unix int64 unix int64
} }
// NewUnixNow return unix timestamp of current time // NewUnixNow return unix timestamp of current time.
// Play: https://go.dev/play/p/U4PPx-9D0oz
func NewUnixNow() *theTime { func NewUnixNow() *theTime {
return &theTime{unix: time.Now().Unix()} return &theTime{unix: time.Now().Unix()}
} }
// NewUnix return unix timestamp of specified time // NewUnix return unix timestamp of specified time.
// Play: https://go.dev/play/p/psoSuh_kLRt
func NewUnix(unix int64) *theTime { func NewUnix(unix int64) *theTime {
return &theTime{unix: unix} return &theTime{unix: unix}
} }
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss" // NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".
// Play: https://go.dev/play/p/VkW08ZOaXPZ
func NewFormat(t string) (*theTime, error) { func NewFormat(t string) (*theTime, error) {
timeLayout := "2006-01-02 15:04:05" timeLayout := "2006-01-02 15:04:05"
loc := time.FixedZone("CST", 8*3600) loc := time.FixedZone("CST", 8*3600)
@@ -30,7 +33,8 @@ func NewFormat(t string) (*theTime, error) {
return &theTime{unix: tt.Unix()}, nil return &theTime{unix: tt.Unix()}, nil
} }
// NewISO8601 return unix timestamp of specified iso8601 time string // NewISO8601 return unix timestamp of specified iso8601 time string.
// Play: https://go.dev/play/p/mkhOHQkdeA2
func NewISO8601(iso8601 string) (*theTime, error) { func NewISO8601(iso8601 string) (*theTime, error) {
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC) t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
if err != nil { if err != nil {
@@ -39,22 +43,26 @@ func NewISO8601(iso8601 string) (*theTime, error) {
return &theTime{unix: t.Unix()}, nil return &theTime{unix: t.Unix()}, nil
} }
// ToUnix return unix timestamp // ToUnix return unix timestamp.
// Play: https://go.dev/play/p/_LUiwAdocjy
func (t *theTime) ToUnix() int64 { func (t *theTime) ToUnix() int64 {
return t.unix return t.unix
} }
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time // ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time.
// Play: https://go.dev/play/p/VkW08ZOaXPZ
func (t *theTime) ToFormat() string { func (t *theTime) ToFormat() string {
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05") return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
} }
// ToFormatForTpl return the time string which format is specified tpl // ToFormatForTpl return the time string which format is specified tpl.
// Play: https://go.dev/play/p/nyXxXcQJ8L5
func (t *theTime) ToFormatForTpl(tpl string) string { func (t *theTime) ToFormatForTpl(tpl string) string {
return time.Unix(t.unix, 0).Format(tpl) return time.Unix(t.unix, 0).Format(tpl)
} }
// ToFormatForTpl return iso8601 time string // ToFormatForTpl return iso8601 time string.
// Play: https://go.dev/play/p/mkhOHQkdeA2
func (t *theTime) ToIso8601() string { func (t *theTime) ToIso8601() string {
return time.Unix(t.unix, 0).Format(time.RFC3339) return time.Unix(t.unix, 0).Format(time.RFC3339)
} }

View File

@@ -19,9 +19,6 @@ func TestToUnix(t *testing.T) {
func TestToFormat(t *testing.T) { func TestToFormat(t *testing.T) {
assert := internal.NewAssert(t, "TestToFormat") assert := internal.NewAssert(t, "TestToFormat")
_, err := NewFormat("2022/03/18 17:04:05")
assert.IsNotNil(err)
tm, err := NewFormat("2022-03-18 17:04:05") tm, err := NewFormat("2022-03-18 17:04:05")
assert.IsNil(err) assert.IsNil(err)

View File

@@ -54,54 +54,64 @@ func init() {
} }
} }
// AddMinute add or sub minute to the time // AddMinute add or sub minute to the time.
// Play: https://go.dev/play/p/nT1heB1KUUK
func AddMinute(t time.Time, minute int64) time.Time { func AddMinute(t time.Time, minute int64) time.Time {
return t.Add(time.Minute * time.Duration(minute)) return t.Add(time.Minute * time.Duration(minute))
} }
// AddHour add or sub hour to the time // AddHour add or sub hour to the time.
// Play: https://go.dev/play/p/rcMjd7OCsi5
func AddHour(t time.Time, hour int64) time.Time { func AddHour(t time.Time, hour int64) time.Time {
return t.Add(time.Hour * time.Duration(hour)) return t.Add(time.Hour * time.Duration(hour))
} }
// AddDay add or sub day to the time // AddDay add or sub day to the time.
// Play: https://go.dev/play/p/dIGbs_uTdFa
func AddDay(t time.Time, day int64) time.Time { func AddDay(t time.Time, day int64) time.Time {
return t.Add(24 * time.Hour * time.Duration(day)) return t.Add(24 * time.Hour * time.Duration(day))
} }
// GetNowDate return format yyyy-mm-dd of current date // GetNowDate return format yyyy-mm-dd of current date.
// Play: https://go.dev/play/p/PvfkPpcpBBf
func GetNowDate() string { func GetNowDate() string {
return time.Now().Format("2006-01-02") return time.Now().Format("2006-01-02")
} }
// GetNowTime return format hh-mm-ss of current time // GetNowTime return format hh-mm-ss of current time.
// Play: https://go.dev/play/p/l7BNxCkTmJS
func GetNowTime() string { func GetNowTime() string {
return time.Now().Format("15:04:05") return time.Now().Format("15:04:05")
} }
// GetNowDateTime return format yyyy-mm-dd hh-mm-ss of current datetime // GetNowDateTime return format yyyy-mm-dd hh-mm-ss of current datetime.
// Play: https://go.dev/play/p/pI4AqngD0al
func GetNowDateTime() string { func GetNowDateTime() string {
return time.Now().Format("2006-01-02 15:04:05") return time.Now().Format("2006-01-02 15:04:05")
} }
// GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00) // GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00).
// Play: https://go.dev/play/p/QmL2oIaGE3q
func GetZeroHourTimestamp() int64 { func GetZeroHourTimestamp() int64 {
ts := time.Now().Format("2006-01-02") ts := time.Now().Format("2006-01-02")
t, _ := time.Parse("2006-01-02", ts) t, _ := time.Parse("2006-01-02", ts)
return t.UTC().Unix() - 8*3600 return t.UTC().Unix() - 8*3600
} }
// GetNightTimestamp return timestamp of zero hour (timestamp of 23:59) // GetNightTimestamp return timestamp of zero hour (timestamp of 23:59).
// Play: https://go.dev/play/p/UolysR3MYP1
func GetNightTimestamp() int64 { func GetNightTimestamp() int64 {
return GetZeroHourTimestamp() + 86400 - 1 return GetZeroHourTimestamp() + 86400 - 1
} }
// FormatTimeToStr convert time to string // FormatTimeToStr convert time to string.
// Play: https://go.dev/play/p/_Ia7M8H_OvE
func FormatTimeToStr(t time.Time, format string) string { func FormatTimeToStr(t time.Time, format string) string {
return t.Format(timeFormat[format]) return t.Format(timeFormat[format])
} }
// FormatStrToTime convert string to time // FormatStrToTime convert string to time.
// Play: https://go.dev/play/p/1h9FwdU8ql4
func FormatStrToTime(str, format string) (time.Time, error) { func FormatStrToTime(str, format string) (time.Time, error) {
v, ok := timeFormat[format] v, ok := timeFormat[format]
if !ok { if !ok {
@@ -111,43 +121,50 @@ func FormatStrToTime(str, format string) (time.Time, error) {
return time.Parse(v, str) return time.Parse(v, str)
} }
// BeginOfMinute return beginning minute time of day // BeginOfMinute return beginning minute time of day.
// Play: https://go.dev/play/p/ieOLVJ9CiFT
func BeginOfMinute(t time.Time) time.Time { func BeginOfMinute(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location()) return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location())
} }
// EndOfMinute return end minute time of day // EndOfMinute return end minute time of day.
// Play: https://go.dev/play/p/yrL5wGzPj4z
func EndOfMinute(t time.Time) time.Time { func EndOfMinute(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), t.Minute(), 59, int(time.Second-time.Nanosecond), t.Location()) return time.Date(y, m, d, t.Hour(), t.Minute(), 59, int(time.Second-time.Nanosecond), t.Location())
} }
// BeginOfHour return beginning hour time of day // BeginOfHour return beginning hour time of day.
// Play: https://go.dev/play/p/GhdGFnDWpYs
func BeginOfHour(t time.Time) time.Time { func BeginOfHour(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location()) return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
} }
// EndOfHour return end hour time of day // EndOfHour return end hour time of day.
// Play: https://go.dev/play/p/6ce3j_6cVqN
func EndOfHour(t time.Time) time.Time { func EndOfHour(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location()) return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location())
} }
// BeginOfDay return beginning hour time of day // BeginOfDay return beginning hour time of day.
// Play: https://go.dev/play/p/94m_UT6cWs9
func BeginOfDay(t time.Time) time.Time { func BeginOfDay(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location()) return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
} }
// EndOfDay return end time of day // EndOfDay return end time of day.
// Play: https://go.dev/play/p/eMBOvmq5Ih1
func EndOfDay(t time.Time) time.Time { func EndOfDay(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
} }
// BeginOfWeek return beginning week, default week begin from Sunday // BeginOfWeek return beginning week, default week begin from Sunday.
// Play: https://go.dev/play/p/ynjoJPz7VNV
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time { func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
var beginFromWeekday = time.Sunday var beginFromWeekday = time.Sunday
if len(beginFrom) > 0 { if len(beginFrom) > 0 {
@@ -161,7 +178,8 @@ func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
return beginOfWeek return beginOfWeek
} }
// EndOfWeek return end week time, default week end with Saturday // EndOfWeek return end week time, default week end with Saturday.
// Play: https://go.dev/play/p/i08qKXD9flf
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time { func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
var endWithWeekday = time.Saturday var endWithWeekday = time.Saturday
if len(endWith) > 0 { if len(endWith) > 0 {
@@ -175,24 +193,28 @@ func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
return endWithWeek return endWithWeek
} }
// BeginOfMonth return beginning of month // BeginOfMonth return beginning of month.
// Play: https://go.dev/play/p/bWXVFsmmzwL
func BeginOfMonth(t time.Time) time.Time { func BeginOfMonth(t time.Time) time.Time {
y, m, _ := t.Date() y, m, _ := t.Date()
return time.Date(y, m, 1, 0, 0, 0, 0, t.Location()) return time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
} }
// EndOfMonth return end of month // EndOfMonth return end of month.
// Play: https://go.dev/play/p/_GWh10B3Nqi
func EndOfMonth(t time.Time) time.Time { func EndOfMonth(t time.Time) time.Time {
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond) return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
} }
// BeginOfYear return beginning of year // BeginOfYear return the date time at the begin of year.
// Play: https://go.dev/play/p/i326DSwLnV8
func BeginOfYear(t time.Time) time.Time { func BeginOfYear(t time.Time) time.Time {
y, _, _ := t.Date() y, _, _ := t.Date()
return time.Date(y, time.January, 1, 0, 0, 0, 0, t.Location()) return time.Date(y, time.January, 1, 0, 0, 0, 0, t.Location())
} }
// EndOfYear return end of year // EndOfYear return the date time at the end of year.
// Play: https://go.dev/play/p/G01cKlMCvNm
func EndOfYear(t time.Time) time.Time { func EndOfYear(t time.Time) time.Time {
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond) return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
} }

View File

@@ -0,0 +1,323 @@
package datetime
import (
"fmt"
"reflect"
"time"
)
func ExampleAddDay() {
now := time.Now()
tomorrow := AddDay(now, 1)
diff1 := tomorrow.Sub(now)
yesterday := AddDay(now, -1)
diff2 := yesterday.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 24h0m0s
// -24h0m0s
}
func ExampleAddHour() {
now := time.Now()
after2Hours := AddHour(now, 2)
diff1 := after2Hours.Sub(now)
before2Hours := AddHour(now, -2)
diff2 := before2Hours.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 2h0m0s
// -2h0m0s
}
func ExampleAddMinute() {
now := time.Now()
after2Minutes := AddMinute(now, 2)
diff1 := after2Minutes.Sub(now)
before2Minutes := AddMinute(now, -2)
diff2 := before2Minutes.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 2m0s
// -2m0s
}
func ExampleGetNowDate() {
result := GetNowDate()
expected := time.Now().Format("2006-01-02")
fmt.Println(result == expected)
// Output:
// true
}
func ExampleGetNowTime() {
result := GetNowTime()
expected := time.Now().Format("15:04:05")
fmt.Println(result == expected)
// Output:
// true
}
func ExampleGetNowDateTime() {
result := GetNowDateTime()
expected := time.Now().Format("2006-01-02 15:04:05")
fmt.Println(result == expected)
// Output:
// true
}
// func ExampleGetZeroHourTimestamp() {
// ts := GetZeroHourTimestamp()
// fmt.Println(ts)
// // Output:
// // 1673107200
// }
// func ExampleGetNightTimestamp() {
// ts := GetNightTimestamp()
// fmt.Println(ts)
// // Output:
// // 1673193599
// }
func ExampleFormatTimeToStr() {
datetime, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08")
result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss")
result2 := FormatTimeToStr(datetime, "yyyy-mm-dd")
result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 2021-01-02 16:04:08
// 2021-01-02
// 02-01-21 16:04:08
}
func ExampleFormatStrToTime() {
result1, _ := FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss")
result2, _ := FormatStrToTime("2021-01-02", "yyyy-mm-dd")
result3, _ := FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 2021-01-02 16:04:08 +0000 UTC
// 2021-01-02 00:00:00 +0000 UTC
// 2021-01-02 16:04:08 +0000 UTC
}
func ExampleBeginOfMinute() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfMinute(input)
fmt.Println(result)
// Output:
// 2023-01-08 18:50:00 +0000 UTC
}
func ExampleEndOfMinute() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfMinute(input)
fmt.Println(result)
// Output:
// 2023-01-08 18:50:59.999999999 +0000 UTC
}
func ExampleBeginOfHour() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfHour(input)
fmt.Println(result)
// Output:
// 2023-01-08 18:00:00 +0000 UTC
}
func ExampleEndOfHour() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfHour(input)
fmt.Println(result)
// Output:
// 2023-01-08 18:59:59.999999999 +0000 UTC
}
func ExampleBeginOfDay() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfDay(input)
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
}
func ExampleEndOfDay() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfDay(input)
fmt.Println(result)
// Output:
// 2023-01-08 23:59:59.999999999 +0000 UTC
}
func ExampleBeginOfWeek() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfWeek(input)
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
}
func ExampleEndOfWeek() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfWeek(input)
fmt.Println(result)
// Output:
// 2023-01-14 23:59:59.999999999 +0000 UTC
}
func ExampleBeginOfMonth() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfMonth(input)
fmt.Println(result)
// Output:
// 2023-01-01 00:00:00 +0000 UTC
}
func ExampleEndOfMonth() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfMonth(input)
fmt.Println(result)
// Output:
// 2023-01-31 23:59:59.999999999 +0000 UTC
}
func ExampleBeginOfYear() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfYear(input)
fmt.Println(result)
// Output:
// 2023-01-01 00:00:00 +0000 UTC
}
func ExampleEndOfYear() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfYear(input)
fmt.Println(result)
// Output:
// 2023-12-31 23:59:59.999999999 +0000 UTC
}
func ExampleNewUnix() {
result := NewUnix(1647597438)
fmt.Println(result)
// Output:
// &{1647597438}
}
func ExampleNewUnixNow() {
tm1 := NewUnixNow()
unixTimestamp := tm1.ToUnix()
tm2 := NewUnix(unixTimestamp)
fmt.Println(reflect.DeepEqual(tm1, tm2))
// Output:
// true
}
// func ExampleNewFormat() {
// tm, err := NewFormat("2022-03-18 17:04:05")
// if err != nil {
// return
// }
// result := tm.ToFormat()
// fmt.Println(result)
// // Output:
// // 2022-03-18 17:04:05
// }
// func ExampleNewISO8601() {
// tm, err := NewISO8601("2006-01-02T15:04:05.999Z")
// if err != nil {
// return
// }
// result := tm.ToIso8601()
// fmt.Println(result)
// // Output:
// // 2006-01-02T23:04:05+08:00
// }

View File

@@ -52,8 +52,8 @@ func TestGetNowDate(t *testing.T) {
assert.Equal(expected, GetNowDate()) assert.Equal(expected, GetNowDate())
} }
func TestGetNotTime(t *testing.T) { func TestGetNowTime(t *testing.T) {
assert := internal.NewAssert(t, "TestGetNotTime") assert := internal.NewAssert(t, "TestGetNowTime")
expected := time.Now().Format("15:04:05") expected := time.Now().Format("15:04:05")
assert.Equal(expected, GetNowTime()) assert.Equal(expected, GetNowTime())
} }
@@ -81,7 +81,6 @@ func TestFormatTimeToStr(t *testing.T) {
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
actual := FormatTimeToStr(datetime, cases[i]) actual := FormatTimeToStr(datetime, cases[i])
assert.Equal(expected[i], actual) assert.Equal(expected[i], actual)
} }
} }

View File

@@ -1,17 +1,19 @@
# Algorithm # Algorithm
Package algorithm implements some basic algorithm. eg. sort, search. Package algorithm implements some basic algorithm. eg. sort, search.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source ## Source
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage ## Usage
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
@@ -22,27 +24,25 @@ import (
## Index ## Index
- [BubbleSort](#BubbleSort) - [BubbleSort](#BubbleSort)
- [InsertionSort](#InsertionSort) - [InsertionSort](#InsertionSort)
- [SelectionSort](#SelectionSort) - [SelectionSort](#SelectionSort)
- [ShellSort](#ShellSort) - [ShellSort](#ShellSort)
- [QuickSort](#QuickSort) - [QuickSort](#QuickSort)
- [HeapSort](#HeapSort) - [HeapSort](#HeapSort)
- [MergeSort](#MergeSort) - [MergeSort](#MergeSort)
- [CountSort](#CountSort) - [CountSort](#CountSort)
- [BinarySearch](#BinarySearch) - [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch) - [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch) - [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache) - [LRUCache](#LRUCache)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="BubbleSort">BubbleSort</span> ### <span id="BubbleSort">BubbleSort</span>
<p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -50,6 +50,7 @@ import (
```go ```go
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -60,34 +61,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.BubbleSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.BubbleSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="InsertionSort">InsertionSort</span> ### <span id="InsertionSort">InsertionSort</span>
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -95,6 +98,7 @@ func main() {
```go ```go
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -105,54 +109,51 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
type people struct {
Name string
Age int
}
// PeopleAageComparator sort people slice by age field
type peopleAgeComparator struct{}
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
p1, _ := v1.(people)
p2, _ := v2.(people)
//ascending order
if p1.Age < p2.Age {
return -1
} else if p1.Age > p2.Age {
return 1
}
return 0
}
func main() { func main() {
type people struct { peoples := []people{
Name string
Age int
}
// PeopleAageComparator sort people slice by age field
type peopleAgeComparator struct{}
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
p1, _ := v1.(people)
p2, _ := v2.(people)
//ascending order
if p1.Age < p2.Age {
return -1
} else if p1.Age > p2.Age {
return 1
}
return 0
//decending order
// if p1.Age > p2.Age {
// return -1
// } else if p1.Age < p2.Age {
// return 1
// }
}
var peoples = []people{
{Name: "a", Age: 20}, {Name: "a", Age: 20},
{Name: "b", Age: 10}, {Name: "b", Age: 10},
{Name: "c", Age: 17}, {Name: "c", Age: 17},
{Name: "d", Age: 8}, {Name: "d", Age: 8},
{Name: "e", Age: 28}, {Name: "e", Age: 28},
} }
comparator := &peopleAgeComparator{} comparator := &peopleAgeComparator{}
algorithm.InsertionSort(peoples, comparator) algorithm.InsertionSort(peoples, comparator)
fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}] fmt.Println(peoples)
// Output:
// [{d 8} {b 10} {c 17} {a 20} {e 28}]
} }
``` ```
### <span id="SelectionSort">SelectionSort</span> ### <span id="SelectionSort">SelectionSort</span>
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -160,6 +161,7 @@ func main() {
```go ```go
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -170,34 +172,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.SelectionSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.SelectionSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="ShellSort">ShellSort</span> ### <span id="ShellSort">ShellSort</span>
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -205,6 +209,7 @@ func main() {
```go ```go
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -215,34 +220,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.ShellSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.ShellSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="QuickSort">QuickSort</span> ### <span id="QuickSort">QuickSort</span>
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -250,6 +257,7 @@ func main() {
```go ```go
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -260,34 +268,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.QuickSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.QuickSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="HeapSort">HeapSort</span> ### <span id="HeapSort">HeapSort</span>
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -295,6 +305,7 @@ func main() {
```go ```go
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -305,34 +316,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.HeapSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.HeapSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="MergeSort">MergeSort</span> ### <span id="MergeSort">MergeSort</span>
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -340,6 +353,7 @@ func main() {
```go ```go
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -350,33 +364,36 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.MergeSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.MergeSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="CountSort">CountSort</span> ### <span id="CountSort">CountSort</span>
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p> <p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -384,6 +401,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -394,34 +412,37 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() {
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { type intComparator struct{}
val1, _ := v1.(int)
val2, _ := v2.(int)
//ascending order func (c *intComparator) Compare(v1 any, v2 any) int {
if val1 < val2 { val1, _ := v1.(int)
return -1 val2, _ := v2.(int)
} else if val1 > val2 {
return 1 //ascending order
} if val1 < val2 {
return 0 return -1
} else if val1 > val2 {
return 1
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
sortedSlice := algorithm.CountSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6} sortedNums := algorithm.CountSort(numbers, comparator)
fmt.Println(sortedNums)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="BinarySearch">BinarySearch</span> ### <span id="BinarySearch">BinarySearch</span>
<p>BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p> <p>BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -429,6 +450,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -439,35 +461,39 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
comparator := &intComparator{} comparator := &intComparator{}
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
fmt.Println(foundIndex) //4
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
fmt.Println(notFoundIndex) //-1 result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 4
// -1
} }
``` ```
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span> ### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
<p>BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p> <p>BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -475,6 +501,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -485,43 +512,47 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
comparator := &intComparator{} comparator := &intComparator{}
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
fmt.Println(foundIndex) //4
notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
fmt.Println(notFoundIndex) //-1 result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 4
// -1
} }
``` ```
### <span id="LinearSearch">LinearSearch</span> ### <span id="LinearSearch">LinearSearch</span>
<p>LinearSearch Simple linear search algorithm that iterates over all elements of an slice. If a target is found, the index of the target is returned. Else the function return -1.</p>
<p>return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -533,35 +564,26 @@ import (
) )
func main() { func main() {
type intComparator struct{} numbers := []int{3, 4, 5, 3, 2, 1}
func (c *intComparator) Compare(v1 any, v2 any) int { equalFunc := func(a, b int) bool {
val1, _ := v1.(int) return a == b
val2, _ := v2.(int)
//ascending order
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
} }
intSlice := []int{2, 1, 5, 3, 6, 4} result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
comparator := &intComparator{} result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
fmt.Println(foundIndex) //2
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator) fmt.Println(result1)
fmt.Println(notFoundIndex) //-1 fmt.Println(result2)
// Output:
// 0
// -1
} }
``` ```
### <span id="LRUCache">LRUCache</span> ### <span id="LRUCache">LRUCache</span>
<p>LRUCache implements mem cache with lru.</p> <p>LRUCache implements mem cache with lru.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -573,6 +595,7 @@ func (l *LRUCache[K, V]) Put(key K, value V)
func (l *LRUCache[K, V]) Delete(key K) bool 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:</b>
```go ```go
@@ -588,14 +611,26 @@ func main() {
cache.Put(1, 1) cache.Put(1, 1)
cache.Put(2, 2) cache.Put(2, 2)
cache.Put(3, 3)
fmt.Println(cache.Len()) // 3 result1, ok1 := cache.Get(1)
result2, ok2 := cache.Get(2)
result3, ok3 := cache.Get(3)
v, ok := cache.Get(1) fmt.Println(result1, ok1)
fmt.Println(v, ok) // 1 true fmt.Println(result2, ok2)
fmt.Println(result3, ok3)
ok = cache.Delete(1) fmt.Println(cache.Len())
fmt.Println(ok) // true
ok := cache.Delete(2)
fmt.Println(ok)
// Output:
// 1 true
// 2 true
// 0 false
// 2
// true
} }
``` ```

View File

@@ -1,17 +1,19 @@
# Algorithm # Algorithm
algorithm算法包实现一些基本算法sortsearchlrucache。
algorithm 算法包实现一些基本算法sortsearchlrucache。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码 ## 源码
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.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/algorithm" "github.com/duke-git/lancet/v2/algorithm"
@@ -22,35 +24,34 @@ import (
## 目录 ## 目录
- [BubbleSort](#BubbleSort) - [BubbleSort](#BubbleSort)
- [InsertionSort](#InsertionSort) - [InsertionSort](#InsertionSort)
- [SelectionSort](#SelectionSort) - [SelectionSort](#SelectionSort)
- [ShellSort](#ShellSort) - [ShellSort](#ShellSort)
- [QuickSort](#QuickSort) - [QuickSort](#QuickSort)
- [HeapSort](#HeapSort) - [HeapSort](#HeapSort)
- [MergeSort](#MergeSort) - [MergeSort](#MergeSort)
- [CountSort](#CountSort) - [CountSort](#CountSort)
- [BinarySearch](#BinarySearch) - [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch) - [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch) - [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache) - [LRUCache](#LRUCache)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="BubbleSort">BubbleSort</span> ### <span id="BubbleSort">BubbleSort</span>
<p>冒泡排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>冒泡排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -60,42 +61,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.BubbleSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.BubbleSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="InsertionSort">InsertionSort</span> ### <span id="InsertionSort">InsertionSort</span>
<p>插入排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>插入排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -105,62 +109,60 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
type people struct {
Name string
Age int
}
// PeopleAageComparator sort people slice by age field
type peopleAgeComparator struct{}
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
p1, _ := v1.(people)
p2, _ := v2.(people)
//ascending order
if p1.Age < p2.Age {
return -1
} else if p1.Age > p2.Age {
return 1
}
return 0
}
func main() { func main() {
type people struct { peoples := []people{
Name string
Age int
}
// PeopleAageComparator sort people slice by age field
type peopleAgeComparator struct{}
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
p1, _ := v1.(people)
p2, _ := v2.(people)
//ascending order
if p1.Age < p2.Age {
return -1
} else if p1.Age > p2.Age {
return 1
}
return 0
//decending order
// if p1.Age > p2.Age {
// return -1
// } else if p1.Age < p2.Age {
// return 1
// }
}
var peoples = []people{
{Name: "a", Age: 20}, {Name: "a", Age: 20},
{Name: "b", Age: 10}, {Name: "b", Age: 10},
{Name: "c", Age: 17}, {Name: "c", Age: 17},
{Name: "d", Age: 8}, {Name: "d", Age: 8},
{Name: "e", Age: 28}, {Name: "e", Age: 28},
} }
comparator := &peopleAgeComparator{} comparator := &peopleAgeComparator{}
algorithm.InsertionSort(peoples, comparator) algorithm.InsertionSort(peoples, comparator)
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}] fmt.Println(peoples)
// Output:
// [{d 8} {b 10} {c 17} {a 20} {e 28}]
} }
``` ```
### <span id="SelectionSort">SelectionSort</span> ### <span id="SelectionSort">SelectionSort</span>
<p>选择排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>选择排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -170,42 +172,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.SelectionSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.SelectionSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="ShellSort">ShellSort</span> ### <span id="ShellSort">ShellSort</span>
<p>希尔排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>希尔排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -215,42 +220,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.ShellSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.ShellSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="QuickSort">QuickSort</span> ### <span id="QuickSort">QuickSort</span>
<p>快速排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>快速排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -260,42 +268,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.QuickSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.QuickSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="HeapSort">HeapSort</span> ### <span id="HeapSort">HeapSort</span>
<p>堆排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>堆排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -305,42 +316,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.HeapSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.HeapSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="MergeSort">MergeSort</span> ### <span id="MergeSort">MergeSort</span>
<p>归并排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>归并排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -350,41 +364,45 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
algorithm.MergeSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} algorithm.MergeSort(numbers, comparator)
fmt.Println(numbers)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="CountSort">CountSort</span> ### <span id="CountSort">CountSort</span>
<p>计数排序参数comparator需要实现包lancetconstraints.Comparator</p>
<p>计数排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -394,42 +412,46 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() {
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { type intComparator struct{}
val1, _ := v1.(int)
val2, _ := v2.(int)
//ascending order func (c *intComparator) Compare(v1 any, v2 any) int {
if val1 < val2 { val1, _ := v1.(int)
return -1 val2, _ := v2.(int)
} else if val1 > val2 {
return 1 //ascending order
} if val1 < val2 {
return 0 return -1
} else if val1 > val2 {
return 1
} }
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4} func main() {
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{} comparator := &intComparator{}
sortedSlice := algorithm.CountSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6} sortedNums := algorithm.CountSort(numbers, comparator)
fmt.Println(sortedNums)
// Output:
// [1 2 3 4 5 6]
} }
``` ```
### <span id="BinarySearch">BinarySearch</span> ### <span id="BinarySearch">BinarySearch</span>
<p>二分递归查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator</p>
<p>二分递归查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -439,43 +461,48 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
comparator := &intComparator{} comparator := &intComparator{}
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
fmt.Println(foundIndex) //4
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
fmt.Println(notFoundIndex) //-1 result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 4
// -1
} }
``` ```
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span> ### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
<p>二分迭代查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator</p>
<p>二分迭代查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -485,44 +512,48 @@ import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
) )
func main() { type intComparator struct{}
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int { func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int) val1, _ := v1.(int)
val2, _ := v2.(int) val2, _ := v2.(int)
//ascending order //ascending order
if val1 < val2 { if val1 < val2 {
return -1 return -1
} else if val1 > val2 { } else if val1 > val2 {
return 1 return 1
}
return 0
} }
return 0
}
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
comparator := &intComparator{} comparator := &intComparator{}
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
fmt.Println(foundIndex) //4
notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
fmt.Println(notFoundIndex) //-1 result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 4
// -1
} }
``` ```
### <span id="LinearSearch">LinearSearch</span> ### <span id="LinearSearch">LinearSearch</span>
<p>线性查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator</p>
<p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
``` ```
<b>Example:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -533,36 +564,27 @@ import (
) )
func main() { func main() {
type intComparator struct{} numbers := []int{3, 4, 5, 3, 2, 1}
func (c *intComparator) Compare(v1 any, v2 any) int { equalFunc := func(a, b int) bool {
val1, _ := v1.(int) return a == b
val2, _ := v2.(int)
//ascending order
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
} }
intSlice := []int{2, 1, 5, 3, 6, 4} result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
comparator := &intComparator{} result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
fmt.Println(foundIndex) //2
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator) fmt.Println(result1)
fmt.Println(notFoundIndex) //-1 fmt.Println(result2)
// Output:
// 0
// -1
} }
``` ```
### <span id="LRUCache">LRUCache</span> ### <span id="LRUCache">LRUCache</span>
<p>lru实现缓存</p>
<p>lru算法实现缓存。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -573,7 +595,8 @@ func (l *LRUCache[K, V]) Put(key K, value V)
func (l *LRUCache[K, V]) Delete(key K) bool 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>示例:</b>
```go ```go
package main package main
@@ -588,14 +611,26 @@ func main() {
cache.Put(1, 1) cache.Put(1, 1)
cache.Put(2, 2) cache.Put(2, 2)
cache.Put(3, 3)
fmt.Println(cache.Len()) // 3 result1, ok1 := cache.Get(1)
result2, ok2 := cache.Get(2)
result3, ok3 := cache.Get(3)
v, ok := cache.Get(1) fmt.Println(result1, ok1)
fmt.Println(v, ok) // 1 true fmt.Println(result2, ok2)
fmt.Println(result3, ok3)
ok = cache.Delete(1) fmt.Println(cache.Len())
fmt.Println(ok) // true
ok := cache.Delete(2)
fmt.Println(ok)
// Output:
// 1 true
// 2 true
// 0 false
// 2
// true
} }
``` ```

View File

@@ -61,8 +61,6 @@ func main() {
} }
``` ```
### <span id="Bridge">Bridge</span> ### <span id="Bridge">Bridge</span>
<p>Link multiple channels into one channel until cancel the context.</p> <p>Link multiple channels into one channel until cancel the context.</p>
@@ -84,39 +82,37 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
genVals := func() <-chan <-chan int { genVals := func() <-chan <-chan int {
out := make(chan (<-chan int)) out := make(chan (<-chan int))
go func() { go func() {
defer close(out) defer close(out)
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
stream := make(chan int, 1) stream := make(chan int, 1)
stream <- i stream <- i
close(stream) close(stream)
out <- stream out <- stream
} }
}() }()
return out return out
} }
for v := range c.Bridge(ctx, genVals()) { for v := range c.Bridge(ctx, genVals()) {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 3 // 2
// 4 // 3
// 5 // 4
// 5
} }
``` ```
### <span id="FanIn">FanIn</span> ### <span id="FanIn">FanIn</span>
<p>Merge multiple channels into one channel until cancel the context.</p> <p>Merge multiple channels into one channel until cancel the context.</p>
@@ -138,25 +134,24 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
channels := make([]<-chan int, 2) channels := make([]<-chan int, 2)
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2) channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2)
} }
chs := c.FanIn(ctx, channels...) chs := c.FanIn(ctx, channels...)
for v := range chs { for v := range chs {
fmt.Println(v) //1 1 0 0 or 0 0 1 1 fmt.Println(v) //1 1 0 0 or 0 0 1 1
} }
} }
``` ```
### <span id="Repeat">Repeat</span> ### <span id="Repeat">Repeat</span>
<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>
@@ -178,25 +173,24 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4) intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 1 // 2
// 2 // 1
// 2
} }
``` ```
### <span id="Generate">Generate</span> ### <span id="Generate">Generate</span>
<p>Creates a channel, then put values into the channel.</p> <p>Creates a channel, then put values into the channel.</p>
@@ -218,20 +212,20 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Generate(ctx, 1, 2, 3) intStream := c.Generate(ctx, 1, 2, 3)
fmt.Println(<-intStream) fmt.Println(<-intStream)
fmt.Println(<-intStream) fmt.Println(<-intStream)
fmt.Println(<-intStream) fmt.Println(<-intStream)
// Output: // Output:
// 1 // 1
// 2 // 2
// 3 // 3
} }
``` ```
@@ -256,28 +250,27 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
fn := func() string { fn := func() string {
return "hello" return "hello"
} }
c := concurrency.NewChannel[string]() c := concurrency.NewChannel[string]()
intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3) intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// hello // Output:
// hello // hello
// hello // hello
// hello
} }
``` ```
### <span id="Or">Or</span> ### <span id="Or">Or</span>
<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>
@@ -299,31 +292,28 @@ import (
) )
func main() { func main() {
sig := func(after time.Duration) <-chan any { sig := func(after time.Duration) <-chan any {
c := make(chan any) c := make(chan any)
go func() { go func() {
defer close(c) defer close(c)
time.Sleep(after) time.Sleep(after)
}() }()
return c return c
} }
start := time.Now() start := time.Now()
c := concurrency.NewChannel[any]() c := concurrency.NewChannel[any]()
<-c.Or( <-c.Or(
sig(1*time.Second), sig(1*time.Second),
sig(2*time.Second), sig(2*time.Second),
sig(3*time.Second), sig(3*time.Second),
) )
fmt.Println("done after %v", time.Since(start)) //1.003s fmt.Println("done after %v", time.Since(start)) //1.003s
} }
``` ```
### <span id="OrDone">OrDone</span> ### <span id="OrDone">OrDone</span>
<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>
@@ -345,25 +335,23 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3) intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
for v := range c.OrDone(ctx, intStream) { for v := range c.OrDone(ctx, intStream) {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 1 // 1
// 1 // 1
// 1
} }
``` ```
### <span id="Take">Take</span> ### <span id="Take">Take</span>
<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>
@@ -385,32 +373,31 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
numbers := make(chan int, 5) numbers := make(chan int, 5)
numbers <- 1 numbers <- 1
numbers <- 2 numbers <- 2
numbers <- 3 numbers <- 3
numbers <- 4 numbers <- 4
numbers <- 5 numbers <- 5
defer close(numbers) defer close(numbers)
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, numbers, 3) intStream := c.Take(ctx, numbers, 3)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 3 // 2
// 3
} }
``` ```
### <span id="Tee">Tee</span> ### <span id="Tee">Tee</span>
<p>Split one chanel into two channels, until cancel the context.</p> <p>Split one chanel into two channels, until cancel the context.</p>
@@ -432,22 +419,23 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 2) intStream := c.Take(ctx, c.Repeat(ctx, 1), 2)
ch1, ch2 := c.Tee(ctx, intStream) ch1, ch2 := c.Tee(ctx, intStream)
for v := range ch1 { for v := range ch1 {
fmt.Println(v) fmt.Println(v)
fmt.Println(<-ch2) fmt.Println(<-ch2)
} }
// Output:
// 1 // Output:
// 1 // 1
// 1 // 1
// 1 // 1
// 1
} }
``` ```

View File

@@ -46,7 +46,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>例:</b>
```go ```go
package main package main
@@ -61,8 +61,6 @@ func main() {
} }
``` ```
### <span id="Bridge">Bridge</span> ### <span id="Bridge">Bridge</span>
<p>将多个channel链接到一个channel直到取消上下文。</p> <p>将多个channel链接到一个channel直到取消上下文。</p>
@@ -72,7 +70,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>例:</b>
```go ```go
package main package main
@@ -84,39 +82,37 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
genVals := func() <-chan <-chan int { genVals := func() <-chan <-chan int {
out := make(chan (<-chan int)) out := make(chan (<-chan int))
go func() { go func() {
defer close(out) defer close(out)
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
stream := make(chan int, 1) stream := make(chan int, 1)
stream <- i stream <- i
close(stream) close(stream)
out <- stream out <- stream
} }
}() }()
return out return out
} }
for v := range c.Bridge(ctx, genVals()) { for v := range c.Bridge(ctx, genVals()) {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 3 // 2
// 4 // 3
// 5 // 4
// 5
} }
``` ```
### <span id="FanIn">FanIn</span> ### <span id="FanIn">FanIn</span>
<p>将多个channel合并为一个channel直到取消上下文。</p> <p>将多个channel合并为一个channel直到取消上下文。</p>
@@ -126,7 +122,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>例:</b>
```go ```go
package main package main
@@ -138,25 +134,24 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
channels := make([]<-chan int, 2) channels := make([]<-chan int, 2)
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2) channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2)
} }
chs := c.FanIn(ctx, channels...) chs := c.FanIn(ctx, channels...)
for v := range chs { for v := range chs {
fmt.Println(v) //1 1 0 0 or 0 0 1 1 fmt.Println(v) //1 1 0 0 or 0 0 1 1
} }
} }
``` ```
### <span id="Generate">Generate</span> ### <span id="Generate">Generate</span>
<p>根据传入的值生成channel.</p> <p>根据传入的值生成channel.</p>
@@ -166,7 +161,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>例:</b>
```go ```go
package main package main
@@ -178,20 +173,20 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Generate(ctx, 1, 2, 3) intStream := c.Generate(ctx, 1, 2, 3)
fmt.Println(<-intStream) fmt.Println(<-intStream)
fmt.Println(<-intStream) fmt.Println(<-intStream)
fmt.Println(<-intStream) fmt.Println(<-intStream)
// Output: // Output:
// 1 // 1
// 2 // 2
// 3 // 3
} }
``` ```
@@ -204,7 +199,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>例:</b>
```go ```go
package main package main
@@ -216,26 +211,24 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4) intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 1 // 2
// 2 // 1
// 2
} }
``` ```
### <span id="RepeatFn">RepeatFn</span> ### <span id="RepeatFn">RepeatFn</span>
<p>返回一个channel重复执行函数fn并将结果放入返回的channel直到取消上下文。</p> <p>返回一个channel重复执行函数fn并将结果放入返回的channel直到取消上下文。</p>
@@ -245,7 +238,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>例:</b>
```go ```go
package main package main
@@ -257,28 +250,26 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
fn := func() string { fn := func() string {
return "hello" return "hello"
} }
c := concurrency.NewChannel[string]() c := concurrency.NewChannel[string]()
intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3) intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output: // Output:
// hello // hello
// hello // hello
// hello // hello
} }
``` ```
### <span id="Or">Or</span> ### <span id="Or">Or</span>
<p>将一个或多个channel读取到一个channel中当任何读取channel关闭时将结束读取。</p> <p>将一个或多个channel读取到一个channel中当任何读取channel关闭时将结束读取。</p>
@@ -288,7 +279,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>例:</b>
```go ```go
package main package main
@@ -300,31 +291,28 @@ import (
) )
func main() { func main() {
sig := func(after time.Duration) <-chan any { sig := func(after time.Duration) <-chan any {
c := make(chan any) c := make(chan any)
go func() { go func() {
defer close(c) defer close(c)
time.Sleep(after) time.Sleep(after)
}() }()
return c return c
} }
start := time.Now() start := time.Now()
c := concurrency.NewChannel[any]() c := concurrency.NewChannel[any]()
<-c.Or( <-c.Or(
sig(1*time.Second), sig(1*time.Second),
sig(2*time.Second), sig(2*time.Second),
sig(3*time.Second), sig(3*time.Second),
) )
fmt.Println("done after %v", time.Since(start)) //1.003s fmt.Println("done after %v", time.Since(start)) //1.003s
} }
``` ```
### <span id="OrDone">OrDone</span> ### <span id="OrDone">OrDone</span>
<p>将一个channel读入另一个channel直到取消上下文。</p> <p>将一个channel读入另一个channel直到取消上下文。</p>
@@ -334,7 +322,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>例:</b>
```go ```go
package main package main
@@ -346,25 +334,23 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3) intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
for v := range c.OrDone(ctx, intStream) { for v := range c.OrDone(ctx, intStream) {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 1 // 1
// 1 // 1
// 1
} }
``` ```
### <span id="Take">Take</span> ### <span id="Take">Take</span>
<p>返回一个channel其值从另一个channel获取直到取消上下文。</p> <p>返回一个channel其值从另一个channel获取直到取消上下文。</p>
@@ -374,7 +360,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>例:</b>
```go ```go
package main package main
@@ -386,32 +372,31 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
numbers := make(chan int, 5) numbers := make(chan int, 5)
numbers <- 1 numbers <- 1
numbers <- 2 numbers <- 2
numbers <- 3 numbers <- 3
numbers <- 4 numbers <- 4
numbers <- 5 numbers <- 5
defer close(numbers) defer close(numbers)
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, numbers, 3) intStream := c.Take(ctx, numbers, 3)
for v := range intStream { for v := range intStream {
fmt.Println(v) fmt.Println(v)
} }
// Output:
// 1 // Output:
// 2 // 1
// 3 // 2
// 3
} }
``` ```
### <span id="Tee">Tee</span> ### <span id="Tee">Tee</span>
<p>将一个channel分成两个channel直到取消上下文。</p> <p>将一个channel分成两个channel直到取消上下文。</p>
@@ -421,7 +406,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>例:</b>
```go ```go
package main package main
@@ -433,22 +418,22 @@ import (
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 2) intStream := c.Take(ctx, c.Repeat(ctx, 1), 2)
ch1, ch2 := c.Tee(ctx, intStream) ch1, ch2 := c.Tee(ctx, intStream)
for v := range ch1 { for v := range ch1 {
fmt.Println(v) fmt.Println(v)
fmt.Println(<-ch2) fmt.Println(<-ch2)
} }
// Output: // Output:
// 1 // 1
// 1 // 1
// 1 // 1
// 1 // 1
} }
``` ```

View File

@@ -57,41 +57,52 @@ import (
) )
func main() { func main() {
// bool // bool
fmt.Println(condition.Bool(false)) // false result1 := condition.Bool(false)
fmt.Println(condition.Bool(true)) // true result2 := condition.Bool(true)
fmt.Println(result1) // false
fmt.Println(result2) // true
// integer // integer
fmt.Println(condition.Bool(0)) // false result3 := condition.Bool(0)
fmt.Println(condition.Bool(1)) // true result4 := condition.Bool(1)
fmt.Println(result3) // false
fmt.Println(result4) // true
// float // string
fmt.Println(condition.Bool(0.0)) // false result5 := condition.Bool("")
fmt.Println(condition.Bool(0.1)) // true result6 := condition.Bool(" ")
fmt.Println(result5) // false
fmt.Println(result6) // true
// string // slice
fmt.Println(condition.Bool("")) // false nums := []int{}
fmt.Println(condition.Bool(" ")) // true result7 := condition.Bool(nums)
fmt.Println(condition.Bool("0")) // true
// slice nums = append(nums, 1, 2)
var nums [2]int result8 := condition.Bool(nums)
fmt.Println(condition.Bool(nums)) // false fmt.Println(result7) // false
nums = [2]int{0, 1} fmt.Println(result8) // true
fmt.Println(condition.Bool(nums)) // true
// map // struct
fmt.Println(condition.Bool(map[string]string{})) // false result9 = condition.Bool(struct{}{})
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true fmt.Println(result8) // false
// struct
fmt.Println(condition.Bool(struct{}{})) // false // Output:
fmt.Println(condition.Bool(time.Now())) // true // false
// true
// false
// true
// false
// true
// false
// true
// false
} }
``` ```
### <span id="And">And</span> ### <span id="And">And</span>
<p>Returns true if both a and b are truthy.</p> <p>Returns true if both a and b are truthy.</p>
@@ -111,10 +122,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.And(0, 1)) // false fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true fmt.Println(condition.And(1, "0")) // true
} }
``` ```
@@ -139,10 +150,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Or(0, "")) // false fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true fmt.Println(condition.Or(1, "0")) // true
} }
``` ```
@@ -167,10 +178,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Xor(0, 0)) // false fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false fmt.Println(condition.Xor(1, 1)) // false
} }
``` ```
@@ -195,10 +206,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Nor(0, 0)) // true fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // false fmt.Println(condition.Nor(1, 1)) // false
} }
``` ```
@@ -222,10 +233,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Xnor(0, 0)) // true fmt.Println(condition.Xnor(0, 0)) // true
fmt.Println(condition.Xnor(0, 1)) // false fmt.Println(condition.Xnor(0, 1)) // false
fmt.Println(condition.Xnor(1, 0)) // false fmt.Println(condition.Xnor(1, 0)) // false
fmt.Println(condition.Xnor(1, 1)) // true fmt.Println(condition.Xnor(1, 1)) // true
} }
``` ```
@@ -249,10 +260,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Nand(0, 0)) // true fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.Nand(1, 0)) // true fmt.Println(condition.Nand(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false fmt.Println(condition.Nand(1, 1)) // false
} }
``` ```
@@ -277,10 +288,18 @@ import (
) )
func main() { func main() {
trueValue := "1" conditionTrue := 2 > 1
falseValue := "0" result1 := condition.TernaryOperator(conditionTrue, 0, 1)
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1" conditionFalse := 2 > 3
result2 := condition.TernaryOperator(conditionFalse, 0, 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0
// 1
} }
``` ```

View File

@@ -45,7 +45,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>例:</b>
```go ```go
package main package main
@@ -56,41 +56,51 @@ import (
) )
func main() { func main() {
// bool // bool
fmt.Println(condition.Bool(false)) // false result1 := condition.Bool(false)
fmt.Println(condition.Bool(true)) // true result2 := condition.Bool(true)
fmt.Println(result1) // false
fmt.Println(result2) // true
// integer // integer
fmt.Println(condition.Bool(0)) // false result3 := condition.Bool(0)
fmt.Println(condition.Bool(1)) // true result4 := condition.Bool(1)
fmt.Println(result3) // false
fmt.Println(result4) // true
// float // string
fmt.Println(condition.Bool(0.0)) // false result5 := condition.Bool("")
fmt.Println(condition.Bool(0.1)) // true result6 := condition.Bool(" ")
fmt.Println(result5) // false
fmt.Println(result6) // true
// string // slice
fmt.Println(condition.Bool("")) // false nums := []int{}
fmt.Println(condition.Bool(" ")) // true result7 := condition.Bool(nums)
fmt.Println(condition.Bool("0")) // true
// slice nums = append(nums, 1, 2)
var nums [2]int result8 := condition.Bool(nums)
fmt.Println(condition.Bool(nums)) // false fmt.Println(result7) // false
nums = [2]int{0, 1} fmt.Println(result8) // true
fmt.Println(condition.Bool(nums)) // true
// map // struct
fmt.Println(condition.Bool(map[string]string{})) // false result9 = condition.Bool(struct{}{})
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true fmt.Println(result8) // false
// struct
fmt.Println(condition.Bool(struct{}{})) // false // Output:
fmt.Println(condition.Bool(time.Now())) // true // false
// true
// false
// true
// false
// true
// false
// true
// false
} }
``` ```
### <span id="And">And</span> ### <span id="And">And</span>
<p>逻辑且操作当切仅当a和b都为true时返回true</p> <p>逻辑且操作当切仅当a和b都为true时返回true</p>
@@ -99,7 +109,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>例:</b>
```go ```go
package main package main
@@ -110,15 +120,13 @@ import (
) )
func main() { func main() {
fmt.Println(condition.And(0, 1)) // false fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true fmt.Println(condition.And(1, "0")) // true
} }
``` ```
### <span id="Or">Or</span> ### <span id="Or">Or</span>
<p>逻辑或操作当切仅当a和b都为false时返回false</p> <p>逻辑或操作当切仅当a和b都为false时返回false</p>
@@ -127,7 +135,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>例:</b>
```go ```go
package main package main
@@ -138,15 +146,13 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Or(0, "")) // false fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true fmt.Println(condition.Or(1, "0")) // true
} }
``` ```
### <span id="Xor">Xor</span> ### <span id="Xor">Xor</span>
<p>逻辑异或操作a和b相同返回falsea和b不相同返回true</p> <p>逻辑异或操作a和b相同返回falsea和b不相同返回true</p>
@@ -155,7 +161,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>例:</b>
```go ```go
package main package main
@@ -166,15 +172,13 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Xor(0, 0)) // false fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false fmt.Println(condition.Xor(1, 1)) // false
} }
``` ```
### <span id="Nor">Nor</span> ### <span id="Nor">Nor</span>
<p>异或的取反操作</p> <p>异或的取反操作</p>
@@ -183,7 +187,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>例:</b>
```go ```go
package main package main
@@ -194,15 +198,13 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Nor(0, 0)) // true fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // false fmt.Println(condition.Nor(1, 1)) // false
} }
``` ```
### <span id="Xnor">Xnor</span> ### <span id="Xnor">Xnor</span>
<p>如果a和b都是真的或a和b均是假的则返回true。</p> <p>如果a和b都是真的或a和b均是假的则返回true。</p>
@@ -211,7 +213,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>例:</b>
```go ```go
package main package main
@@ -222,10 +224,10 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Xnor(0, 0)) // true fmt.Println(condition.Xnor(0, 0)) // true
fmt.Println(condition.Xnor(0, 1)) // false fmt.Println(condition.Xnor(0, 1)) // false
fmt.Println(condition.Xnor(1, 0)) // false fmt.Println(condition.Xnor(1, 0)) // false
fmt.Println(condition.Xnor(1, 1)) // true fmt.Println(condition.Xnor(1, 1)) // true
} }
``` ```
@@ -237,7 +239,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>例:</b>
```go ```go
package main package main
@@ -248,15 +250,13 @@ import (
) )
func main() { func main() {
fmt.Println(condition.Nand(0, 0)) // true fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.Nand(1, 0)) // true fmt.Println(condition.Nand(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false fmt.Println(condition.Nand(1, 1)) // false
} }
``` ```
### <span id="TernaryOperator">TernaryOperator</span> ### <span id="TernaryOperator">TernaryOperator</span>
<p>三元运算符</p> <p>三元运算符</p>
@@ -265,7 +265,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>例:</b>
```go ```go
package main package main
@@ -276,10 +276,18 @@ import (
) )
func main() { func main() {
trueValue := "1" conditionTrue := 2 > 1
falseValue := "0" result1 := condition.TernaryOperator(conditionTrue, 0, 1)
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1" conditionFalse := 2 > 3
result2 := condition.TernaryOperator(conditionFalse, 0, 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0
// 1
} }
``` ```

View File

@@ -40,8 +40,6 @@ import (
## Documentation ## Documentation
### <span id="ColorHexToRGB">ColorHexToRGB</span> ### <span id="ColorHexToRGB">ColorHexToRGB</span>
<p>Convert color hex to color rgb.</p> <p>Convert color hex to color rgb.</p>
@@ -63,12 +61,14 @@ import (
func main() { func main() {
colorHex := "#003366" colorHex := "#003366"
r, g, b := convertor.ColorHexToRGB(colorHex) r, g, b := convertor.ColorHexToRGB(colorHex)
fmt.Println(r, g, b) //0,51,102
fmt.Println(r, g, b)
// Output:
// 0 51 102
} }
``` ```
### <span id="ColorRGBToHex">ColorRGBToHex</span> ### <span id="ColorRGBToHex">ColorRGBToHex</span>
<p>Convert color rgb to color hex.</p> <p>Convert color rgb to color hex.</p>
@@ -92,14 +92,15 @@ func main() {
r := 0 r := 0
g := 51 g := 51
b := 102 b := 102
colorHex := convertor.ColorRGBToHex(r, g, b) colorHex := ColorRGBToHex(r, g, b)
fmt.Println(colorHex) //#003366 fmt.Println(colorHex)
// Output:
// #003366
} }
``` ```
### <span id="ToBool">ToBool</span> ### <span id="ToBool">ToBool</span>
<p>Convert string to bool. Use strconv.ParseBool.</p> <p>Convert string to bool. Use strconv.ParseBool.</p>
@@ -120,22 +121,26 @@ import (
) )
func main() { func main() {
v1, _ := convertor.ToBool("1") cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
fmt.Println(v1) //true
v2, _ := convertor.ToBool("true") for i := 0; i < len(cases); i++ {
fmt.Println(v2) //true result, _ := convertor.ToBool(cases[i])
fmt.Println(result)
}
v3, _ := convertor.ToBool("True") // Output:
fmt.Println(v3) //true // true
// true
v4, _ := convertor.ToBool("123") // true
fmt.Println(v4) //false // false
// false
// false
// false
// false
// false
} }
``` ```
### <span id="ToBytes">ToBytes</span> ### <span id="ToBytes">ToBytes</span>
<p>Convert value to byte slice.</p> <p>Convert value to byte slice.</p>
@@ -156,16 +161,18 @@ import (
) )
func main() { func main() {
bytesData, err := convertor.ToBytes("0") bytesData, err := convertor.ToBytes("abc")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(bytesData) //[]bytes{3, 4, 0, 0}
fmt.Println(bytesData)
// Output:
// [97 98 99]
} }
``` ```
### <span id="ToChar">ToChar</span> ### <span id="ToChar">ToChar</span>
<p>Convert string to char slice.</p> <p>Convert string to char slice.</p>
@@ -186,18 +193,21 @@ import (
) )
func main() { func main() {
chars := convertor.ToChar("") result1 := convertor.ToChar("")
fmt.Println(chars) //[]string{""} result2 := convertor.ToChar("abc")
result3 := convertor.ToChar("1 2#3")
chars = convertor.ToChar("abc") fmt.Println(result1)
fmt.Println(chars) //[]string{"a", "b", "c"} fmt.Println(result2)
fmt.Println(result3)
chars = convertor.ToChar("1 2#3") // Output:
fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} // []
// [a b c]
// [1 2 # 3]
} }
``` ```
### <span id="ToChannel">ToChannel</span> ### <span id="ToChannel">ToChannel</span>
<p>Convert a collection of elements to a read-only channel.</p> <p>Convert a collection of elements to a read-only channel.</p>
@@ -219,23 +229,21 @@ import (
func main() { func main() {
ch := convertor.ToChannel([]int{1, 2, 3}) ch := convertor.ToChannel([]int{1, 2, 3})
result1 := <-ch
result2 := <-ch
result3 := <-ch
val1, _ := <-ch fmt.Println(result1)
fmt.Println(val1) //1 fmt.Println(result2)
fmt.Println(result3)
val2, _ := <-ch // Output:
fmt.Println(val2) //2 // 1
// 2
val3, _ := <-ch // 3
fmt.Println(val3) //3
_, ok := <-ch
fmt.Println(ok) //false
} }
``` ```
### <span id="ToFloat">ToFloat</span> ### <span id="ToFloat">ToFloat</span>
<p>Convert value to a float64 value. If param is a invalid floatable, will return 0.0 and error. </p> <p>Convert value to a float64 value. If param is a invalid floatable, will return 0.0 and error. </p>
@@ -256,19 +264,30 @@ import (
) )
func main() { func main() {
v, err := convertor.ToFloat("") result1, _ := convertor.ToFloat("")
if err != nil { result2, err := convertor.ToFloat("abc")
fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax result3, _ := convertor.ToFloat("-1")
} result4, _ := convertor.ToFloat("-.11")
fmt.Println(v) //0 result5, _ := convertor.ToFloat("1.23e3")
result6, _ := convertor.ToFloat(true)
v, _ = convertor.ToFloat("-.11") fmt.Println(result1)
fmt.Println(v) //-0.11 fmt.Println(result2, err)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
// Output:
// 0
// 0 strconv.ParseFloat: parsing "": invalid syntax
// -1
// -0.11
// 1230
// 0
} }
``` ```
### <span id="ToInt">ToInt</span> ### <span id="ToInt">ToInt</span>
<p>Convert value to a int64 value. If param is a invalid intable, will return 0 and error. </p> <p>Convert value to a int64 value. If param is a invalid intable, will return 0 and error. </p>
@@ -289,19 +308,27 @@ import (
) )
func main() { func main() {
v, err := convertor.ToInt("") result1, _ := convertor.ToInt("123")
if err != nil { result2, _ := convertor.ToInt("-123")
fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax result3, _ := convertor.ToInt(float64(12.3))
} result4, err := convertor.ToInt("abc")
fmt.Println(v) //0 result5, _ := convertor.ToInt(true)
v, _ = convertor.ToFloat(1.12) fmt.Println(result1)
fmt.Println(v) //1 fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4, err)
fmt.Println(result5)
// Output:
// 123
// -123
// 12
// 0 strconv.ParseInt: parsing "": invalid syntax
// 0
} }
``` ```
### <span id="ToJson">ToJson</span> ### <span id="ToJson">ToJson</span>
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p> <p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
@@ -322,14 +349,20 @@ import (
) )
func main() { func main() {
var aMap = map[string]int{"a": 1, "b": 2, "c": 3} aMap := map[string]int{"a": 1, "b": 2, "c": 3}
jsonStr, _ := convertor.ToJson(aMap) result, err := ToJson(aMap)
fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}"
if err != nil {
fmt.Printf("%v", err)
}
fmt.Println(result)
// Output:
// {"a":1,"b":2,"c":3}
} }
``` ```
### <span id="ToMap">ToMap</span> ### <span id="ToMap">ToMap</span>
<p>Convert a slice of structs to a map based on iteratee function. </p> <p>Convert a slice of structs to a map based on iteratee function. </p>
@@ -358,16 +391,18 @@ func main() {
{name: "Hello", code: 100}, {name: "Hello", code: 100},
{name: "Hi", code: 101}, {name: "Hi", code: 101},
} }
result := convertor.ToMap(messages, func(msg Message) (int, string) { result := convertor.ToMap(messages, func(msg Message) (int, string) {
return msg.code, msg.name return msg.code, msg.name
}) })
fmt.Println(result) //{100: "Hello", 101: "Hi"} fmt.Println(result)
// Output:
// map[100:Hello 101:Hi]
} }
``` ```
### <span id="ToPointer">ToPointer</span> ### <span id="ToPointer">ToPointer</span>
<p>Returns a pointer to passed value. </p> <p>Returns a pointer to passed value. </p>
@@ -389,11 +424,13 @@ import (
func main() { func main() {
result := convertor.ToPointer(123) result := convertor.ToPointer(123)
fmt.Println(*result) //123 fmt.Println(*result)
// Output:
// 123
} }
``` ```
### <span id="ToString">ToString</span> ### <span id="ToString">ToString</span>
<p>ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal</p> <p>ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal</p>
@@ -414,13 +451,33 @@ import (
) )
func main() { func main() {
fmt.Printf("%q", convertor.ToString(1)) //"1" result1 := convertor.ToString("")
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" result2 := convertor.ToString(nil)
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" result3 := convertor.ToString(0)
result4 := convertor.ToString(1.23)
result5 := convertor.ToString(true)
result6 := convertor.ToString(false)
result7 := convertor.ToString([]int{1, 2, 3})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
fmt.Println(result7)
// Output:
//
//
// 0
// 1.23
// true
// false
// [1,2,3]
} }
``` ```
### <span id="StructToMap">StructToMap</span> ### <span id="StructToMap">StructToMap</span>
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p> <p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
@@ -451,12 +508,13 @@ func main() {
} }
pm, _ := convertor.StructToMap(p) pm, _ := convertor.StructToMap(p)
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] fmt.Println(pm)
// Output:
// map[name:test]
} }
``` ```
### <span id="MapToSlice">MapToSlice</span> ### <span id="MapToSlice">MapToSlice</span>
<p>Convert a map to a slice based on iteratee function.</p> <p>Convert a map to a slice based on iteratee function.</p>
@@ -508,12 +566,13 @@ import (
func main() { func main() {
byteData, _ := convertor.EncodeByte("abc") byteData, _ := convertor.EncodeByte("abc")
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} fmt.Println(byteData)
// Output:
// [6 12 0 3 97 98 99]
} }
``` ```
### <span id="DecodeByte">DecodeByte</span> ### <span id="DecodeByte">DecodeByte</span>
<p>Decode byte data to target object. target should be a pointer instance.</p> <p>Decode byte data to target object. target should be a pointer instance.</p>
@@ -535,8 +594,16 @@ import (
func main() { func main() {
var result string var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99} byteData := []byte{6, 12, 0, 3, 97, 98, 99}
convertor.DecodeByte(byteData, &result)
fmt.Println(result) //"abc" err := convertor.DecodeByte(byteData, &result)
if err != nil {
return
}
fmt.Println(result)
// Output:
// abc
} }
``` ```

View File

@@ -43,16 +43,15 @@ import (
## 文档 ## 文档
### <span id="ColorHexToRGB">ColorHexToRGB</span> ### <span id="ColorHexToRGB">ColorHexToRGB</span>
<p>颜色值十六进制转rgb</p> <p>颜色值十六进制转rgb</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ColorHexToRGB(colorHex string) (red, green, blue int) func ColorHexToRGB(colorHex string) (red, green, blue int)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -65,22 +64,24 @@ import (
func main() { func main() {
colorHex := "#003366" colorHex := "#003366"
r, g, b := convertor.ColorHexToRGB(colorHex) r, g, b := convertor.ColorHexToRGB(colorHex)
fmt.Println(r, g, b) //0,51,102
fmt.Println(r, g, b)
// Output:
// 0 51 102
} }
``` ```
### <span id="ColorRGBToHex">ColorRGBToHex</span> ### <span id="ColorRGBToHex">ColorRGBToHex</span>
<p>颜色值rgb转十六进制</p> <p>颜色值rgb转十六进制</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ColorRGBToHex(red, green, blue int) string func ColorRGBToHex(red, green, blue int) string
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -94,24 +95,25 @@ func main() {
r := 0 r := 0
g := 51 g := 51
b := 102 b := 102
colorHex := convertor.ColorRGBToHex(r, g, b) colorHex := ColorRGBToHex(r, g, b)
fmt.Println(colorHex) //#003366 fmt.Println(colorHex)
// Output:
// #003366
} }
``` ```
### <span id="ToBool">ToBool</span> ### <span id="ToBool">ToBool</span>
<p>字符串转布尔类型使用strconv.ParseBool</p> <p>字符串转布尔类型使用strconv.ParseBool</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToBool(s string) (bool, error) func ToBool(s string) (bool, error)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -122,32 +124,36 @@ import (
) )
func main() { func main() {
v1, _ := convertor.ToBool("1") cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
fmt.Println(v1) //true
v2, _ := convertor.ToBool("true") for i := 0; i < len(cases); i++ {
fmt.Println(v2) //true result, _ := convertor.ToBool(cases[i])
fmt.Println(result)
}
v3, _ := convertor.ToBool("True") // Output:
fmt.Println(v3) //true // true
// true
v4, _ := convertor.ToBool("123") // true
fmt.Println(v4) //false // false
// false
// false
// false
// false
// false
} }
``` ```
### <span id="ToBytes">ToBytes</span> ### <span id="ToBytes">ToBytes</span>
<p>interface转字节切片.</p> <p>Interface转字节切片</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToBytes(data any) ([]byte, error) func ToBytes(data any) ([]byte, error)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -158,26 +164,28 @@ import (
) )
func main() { func main() {
bytesData, err := convertor.ToBytes("0") bytesData, err := convertor.ToBytes("abc")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(bytesData) //[]bytes{3, 4, 0, 0}
fmt.Println(bytesData)
// Output:
// [97 98 99]
} }
``` ```
### <span id="ToChar">ToChar</span> ### <span id="ToChar">ToChar</span>
<p>字符串转字符切片</p> <p>字符串转字符切片</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToChar(s string) []string func ToChar(s string) []string
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -188,29 +196,31 @@ import (
) )
func main() { func main() {
chars := convertor.ToChar("") result1 := convertor.ToChar("")
fmt.Println(chars) //[]string{""} result2 := convertor.ToChar("abc")
result3 := convertor.ToChar("1 2#3")
chars = convertor.ToChar("abc") fmt.Println(result1)
fmt.Println(chars) //[]string{"a", "b", "c"} fmt.Println(result2)
fmt.Println(result3)
chars = convertor.ToChar("1 2#3") // Output:
fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} // []
// [a b c]
// [1 2 # 3]
} }
``` ```
### <span id="ToChannel">ToChannel</span> ### <span id="ToChannel">ToChannel</span>
<p>将切片转为只读channel</p> <p>将切片转为只读channel</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToChannel[T any](array []T) <-chan T func ToChannel[T any](array []T) <-chan T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -222,33 +232,31 @@ import (
func main() { func main() {
ch := convertor.ToChannel([]int{1, 2, 3}) ch := convertor.ToChannel([]int{1, 2, 3})
result1 := <-ch
result2 := <-ch
result3 := <-ch
val1, _ := <-ch fmt.Println(result1)
fmt.Println(val1) //1 fmt.Println(result2)
fmt.Println(result3)
val2, _ := <-ch // Output:
fmt.Println(val2) //2 // 1
// 2
val3, _ := <-ch // 3
fmt.Println(val3) //3
_, ok := <-ch
fmt.Println(ok) //false
} }
``` ```
### <span id="ToFloat">ToFloat</span> ### <span id="ToFloat">ToFloat</span>
<p>将interface转成float64类型如果参数无法转换会返回0和error</p> <p>将interface转成float64类型如果参数无法转换会返回0和error</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToFloat(value any) (float64, error) func ToFloat(value any) (float64, error)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -259,29 +267,40 @@ import (
) )
func main() { func main() {
v, err := convertor.ToFloat("") result1, _ := convertor.ToFloat("")
if err != nil { result2, err := convertor.ToFloat("abc")
fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax result3, _ := convertor.ToFloat("-1")
} result4, _ := convertor.ToFloat("-.11")
fmt.Println(v) //0 result5, _ := convertor.ToFloat("1.23e3")
result6, _ := convertor.ToFloat(true)
v, _ = convertor.ToFloat("-.11") fmt.Println(result1)
fmt.Println(v) //-0.11 fmt.Println(result2, err)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
// Output:
// 0
// 0 strconv.ParseFloat: parsing "": invalid syntax
// -1
// -0.11
// 1230
// 0
} }
``` ```
### <span id="ToInt">ToInt</span> ### <span id="ToInt">ToInt</span>
<p>将interface转成int64类型如果参数无法转换会返回0和error</p> <p>将interface转成int64类型如果参数无法转换会返回0和error</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToInt(value any) (int64, error) func ToInt(value any) (int64, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -292,29 +311,37 @@ import (
) )
func main() { func main() {
v, err := convertor.ToInt("") result1, _ := convertor.ToInt("123")
if err != nil { result2, _ := convertor.ToInt("-123")
fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax result3, _ := convertor.ToInt(float64(12.3))
} result4, err := convertor.ToInt("abc")
fmt.Println(v) //0 result5, _ := convertor.ToInt(true)
v, _ = convertor.ToFloat(1.12) fmt.Println(result1)
fmt.Println(v) //1 fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4, err)
fmt.Println(result5)
// Output:
// 123
// -123
// 12
// 0 strconv.ParseInt: parsing "": invalid syntax
// 0
} }
``` ```
### <span id="ToJson">ToJson</span> ### <span id="ToJson">ToJson</span>
<p>将interface转成json字符串如果参数无法转换会返回""和error</p> <p>将interface转成json字符串如果参数无法转换会返回""和error</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToJson(value any) (string, error) func ToJson(value any) (string, error)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -325,24 +352,30 @@ import (
) )
func main() { func main() {
var aMap = map[string]int{"a": 1, "b": 2, "c": 3} aMap := map[string]int{"a": 1, "b": 2, "c": 3}
jsonStr, _ := convertor.ToJson(aMap) result, err := ToJson(aMap)
fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}"
if err != nil {
fmt.Printf("%v", err)
}
fmt.Println(result)
// Output:
// {"a":1,"b":2,"c":3}
} }
``` ```
### <span id="ToMap">ToMap</span> ### <span id="ToMap">ToMap</span>
<p>将切片转为map</p> <p>将切片转为map</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
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>例:</b>
```go ```go
package main package main
@@ -361,26 +394,28 @@ func main() {
{name: "Hello", code: 100}, {name: "Hello", code: 100},
{name: "Hi", code: 101}, {name: "Hi", code: 101},
} }
result := convertor.ToMap(messages, func(msg Message) (int, string) { result := convertor.ToMap(messages, func(msg Message) (int, string) {
return msg.code, msg.name return msg.code, msg.name
}) })
fmt.Println(result) //{100: "Hello", 101: "Hi"} fmt.Println(result)
// Output:
// map[100:Hello 101:Hi]
} }
``` ```
### <span id="ToPointer">ToPointer</span> ### <span id="ToPointer">ToPointer</span>
<p>返回传入值的指针</p> <p>返回传入值的指针</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToPointer[T any](value T) *T func ToPointer[T any](value T) *T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -392,22 +427,23 @@ import (
func main() { func main() {
result := convertor.ToPointer(123) result := convertor.ToPointer(123)
fmt.Println(*result) //123 fmt.Println(*result)
// Output:
// 123
} }
``` ```
### <span id="ToString">ToString</span> ### <span id="ToString">ToString</span>
<p>将值转换为字符串,对于数字、字符串、[]byte将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal</p> <p>将值转换为字符串,对于数字、字符串、[]byte将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func ToString(value any) string func ToString(value any) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -418,24 +454,43 @@ import (
) )
func main() { func main() {
fmt.Printf("%q", convertor.ToString(1)) //"1" result1 := convertor.ToString("")
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" result2 := convertor.ToString(nil)
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" result3 := convertor.ToString(0)
result4 := convertor.ToString(1.23)
result5 := convertor.ToString(true)
result6 := convertor.ToString(false)
result7 := convertor.ToString([]int{1, 2, 3})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
fmt.Println(result7)
// Output:
//
//
// 0
// 1.23
// true
// false
// [1,2,3]
} }
``` ```
### <span id="StructToMap">StructToMap</span> ### <span id="StructToMap">StructToMap</span>
<p>将struct转成map只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p> <p>将struct转成map只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func StructToMap(value any) (map[string]any, error) func StructToMap(value any) (map[string]any, error)
``` ```
<b>列子:</b> <b>示例:</b>
```go ```go
package main package main
@@ -456,22 +511,23 @@ func main() {
} }
pm, _ := convertor.StructToMap(p) pm, _ := convertor.StructToMap(p)
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] fmt.Println(pm)
// Output:
// map[name:test]
} }
``` ```
### <span id="MapToSlice">MapToSlice</span> ### <span id="MapToSlice">MapToSlice</span>
<p>map中key和value执行函数iteratee后转为切片</p> <p>map中key和value执行函数iteratee后转为切片</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
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>例:</b>
```go ```go
package main package main
@@ -491,18 +547,16 @@ func main() {
} }
``` ```
### <span id="EncodeByte">EncodeByte</span> ### <span id="EncodeByte">EncodeByte</span>
<p>将data编码成字节切片</p> <p>将data编码成字节切片</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func EncodeByte(data any) ([]byte, error) func EncodeByte(data any) ([]byte, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -514,22 +568,23 @@ import (
func main() { func main() {
byteData, _ := convertor.EncodeByte("abc") byteData, _ := convertor.EncodeByte("abc")
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} fmt.Println(byteData)
// Output:
// [6 12 0 3 97 98 99]
} }
``` ```
### <span id="DecodeByte">DecodeByte</span> ### <span id="DecodeByte">DecodeByte</span>
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例</p> <p>解码字节切片到目标对象,目标对象需要传入一个指针实例</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func DecodeByte(data []byte, target any) error func DecodeByte(data []byte, target any) error
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -541,8 +596,16 @@ import (
func main() { func main() {
var result string var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99} byteData := []byte{6, 12, 0, 3, 97, 98, 99}
convertor.DecodeByte(byteData, &result)
fmt.Println(result) //"abc" err := convertor.DecodeByte(byteData, &result)
if err != nil {
return
}
fmt.Println(result)
// Output:
// abc
} }
``` ```

View File

@@ -5,10 +5,10 @@ Package cryptor contains some functions for data encryption and decryption. Supp
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/cryptor/aes.go](https://github.com/duke-git/lancet/blob/main/cryptor/aes.go)
- [https://github.com/duke-git/lancet/blob/main/cryptor/des.go](https://github.com/duke-git/lancet/blob/main/cryptor/des.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/basic.go)
- [https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go](https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go](https://github.com/duke-git/lancet/blob/main/cryptor/encrypt.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -61,8 +61,6 @@ import (
## Documentation ## Documentation
### <span id="AesEcbEncrypt">AesEcbEncrypt</span> ### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -83,16 +81,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesEcbDecrypt">AesEcbDecrypt</span> ### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -113,16 +114,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCbcEncrypt">AesCbcEncrypt</span> ### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -143,16 +147,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCbcDecrypt">AesCbcDecrypt</span> ### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -174,16 +181,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCtrCrypt">AesCtrCrypt</span> ### <span id="AesCtrCrypt">AesCtrCrypt</span>
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -205,17 +215,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCfbEncrypt">AesCfbEncrypt</span> ### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -237,15 +249,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCfbDecrypt">AesCfbDecrypt</span> ### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -267,16 +283,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesOfbEncrypt">AesOfbEncrypt</span> ### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -298,15 +317,18 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="AesCfbDecrypt">AesOfbDecrypt</span> ### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -328,17 +350,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefghijklmnop" key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="Base64StdEncode">Base64StdEncode</span> ### <span id="Base64StdEncode">Base64StdEncode</span>
<p>Encode string with base64 encoding.</p> <p>Encode string with base64 encoding.</p>
@@ -359,13 +383,13 @@ import (
) )
func main() { func main() {
base64Str := cryptor.Base64StdEncode("hello world") base64Str := cryptor.Base64StdEncode("hello")
fmt.Println(base64Str) //aGVsbG8gd29ybGQ= fmt.Println(base64Str)
// Output:
// aGVsbG8=
} }
``` ```
### <span id="Base64StdDecode">Base64StdDecode</span> ### <span id="Base64StdDecode">Base64StdDecode</span>
<p>Decode a base64 encoded string.</p> <p>Decode a base64 encoded string.</p>
@@ -387,13 +411,14 @@ import (
) )
func main() { func main() {
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=") str := cryptor.Base64StdDecode("aGVsbG8=")
fmt.Println(str) //hello world fmt.Println(str)
// Output:
// hello
} }
``` ```
### <span id="DesEcbEncrypt">DesEcbEncrypt</span> ### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p> <p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
@@ -415,16 +440,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesEcbDecrypt">DesEcbDecrypt</span> ### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
@@ -446,17 +474,20 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesCbcEncrypt">DesCbcEncrypt</span> ### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p> <p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
@@ -478,16 +509,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
fmt.Println(string(encrypted)) encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesCbcDecrypt">DesCbcDecrypt</span> ### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
@@ -509,17 +543,18 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesCtrCrypt">DesCtrCrypt</span> ### <span id="DesCtrCrypt">DesCtrCrypt</span>
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p> <p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
@@ -541,17 +576,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesCfbEncrypt">DesCfbEncrypt</span> ### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p> <p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
@@ -573,15 +610,18 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
fmt.Println(string(encrypted)) encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesCfbDecrypt">DesCfbDecrypt</span> ### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
@@ -603,16 +643,18 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesOfbEncrypt">DesOfbEncrypt</span> ### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -634,15 +676,18 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted)) decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="DesOfbDecrypt">DesOfbDecrypt</span> ### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -664,17 +709,19 @@ import (
) )
func main() { func main() {
data := "hello world" data := "hello"
key := "abcdefgh" key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="HmacMd5">HmacMd5</span> ### <span id="HmacMd5">HmacMd5</span>
<p>Get the md5 hmac hash of string.</p> <p>Get the md5 hmac hash of string.</p>
@@ -696,13 +743,16 @@ import (
) )
func main() { func main() {
s := cryptor.HmacMd5("hello world", "12345")) str := "hello"
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d key := "12345"
hms := cryptor.HmacMd5(str, key)
fmt.Println(hms)
// Output:
// e834306eab892d872525d4918a7a639a
} }
``` ```
### <span id="HmacSha1">HmacSha1</span> ### <span id="HmacSha1">HmacSha1</span>
<p>Get the sha1 hmac hash of string.</p> <p>Get the sha1 hmac hash of string.</p>
@@ -724,13 +774,16 @@ import (
) )
func main() { func main() {
s := cryptor.HmacSha1("hello world", "12345")) str := "hello"
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd key := "12345"
hms := cryptor.HmacSha1(str, key)
fmt.Println(hms)
// Output:
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
} }
``` ```
### <span id="HmacSha256">HmacSha256</span> ### <span id="HmacSha256">HmacSha256</span>
<p>Get the sha256 hmac hash of string</p> <p>Get the sha256 hmac hash of string</p>
@@ -752,13 +805,17 @@ import (
) )
func main() { func main() {
s := cryptor.HmacSha256("hello world", "12345")) str := "hello"
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8 key := "12345"
hms := cryptor.HmacSha256(str, key)
fmt.Println(hms)
// Output:
// 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
} }
``` ```
### <span id="HmacSha512">HmacSha512</span> ### <span id="HmacSha512">HmacSha512</span>
<p>Get the sha512 hmac hash of string.</p> <p>Get the sha512 hmac hash of string.</p>
@@ -780,14 +837,18 @@ import (
) )
func main() { func main() {
s := cryptor.HmacSha512("hello world", "12345")) str := "hello"
fmt.Println(s) key := "12345"
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
hms := cryptor.HmacSha512(str, key)
fmt.Println(hms)
// Output:
// dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
} }
``` ```
### <span id="Md5String">Md5String</span> ### <span id="Md5String">Md5String</span>
<p>Get the md5 value of string.</p> <p>Get the md5 value of string.</p>
@@ -809,13 +870,16 @@ import (
) )
func main() { func main() {
s := cryptor.Md5String("hello")) str := "hello"
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
md5Str := cryptor.Md5String(str)
fmt.Println(md5Str)
// Output:
// 5d41402abc4b2a76b9719d911017c592
} }
``` ```
### <span id="Md5File">Md5File</span> ### <span id="Md5File">Md5File</span>
<p>Get the md5 value of file.</p> <p>Get the md5 value of file.</p>
@@ -842,8 +906,6 @@ func main() {
} }
``` ```
### <span id="Sha1">Sha1</span> ### <span id="Sha1">Sha1</span>
<p>Get the sha1 value of string.</p> <p>Get the sha1 value of string.</p>
@@ -865,13 +927,16 @@ import (
) )
func main() { func main() {
s := cryptor.Sha1("hello world")) str := "hello"
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
result := cryptor.Sha1(str)
fmt.Println(result)
// Output:
// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
} }
``` ```
### <span id="Sha256">Sha256</span> ### <span id="Sha256">Sha256</span>
<p>Get the sha256 value of string.</p> <p>Get the sha256 value of string.</p>
@@ -893,13 +958,16 @@ import (
) )
func main() { func main() {
s := cryptor.Sha256("hello world")) str := "hello"
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
result := cryptor.Sha256(str)
fmt.Println(result)
// Output:
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
} }
``` ```
### <span id="Sha512">Sha512</span> ### <span id="Sha512">Sha512</span>
<p>Get the sha512 value of string.</p> <p>Get the sha512 value of string.</p>
@@ -921,13 +989,16 @@ import (
) )
func main() { func main() {
s := cryptor.Sha512("hello world")) str := "hello"
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
result := cryptor.Sha512(str)
fmt.Println(result)
// Output:
// 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
} }
``` ```
### <span id="GenerateRsaKey">GenerateRsaKey</span> ### <span id="GenerateRsaKey">GenerateRsaKey</span>
<p>Create the rsa public and private key file in current directory.</p> <p>Create the rsa public and private key file in current directory.</p>
@@ -956,8 +1027,6 @@ func main() {
} }
``` ```
### <span id="RsaEncrypt">RsaEncrypt</span> ### <span id="RsaEncrypt">RsaEncrypt</span>
<p>Encrypt data with public key file useing ras algorithm.</p> <p>Encrypt data with public key file useing ras algorithm.</p>
@@ -981,19 +1050,21 @@ import (
func main() { func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil { if err != nil {
fmt.Println(err) return
} }
data := []byte("hello world") 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)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```
### <span id="RsaDecrypt">RsaDecrypt</span> ### <span id="RsaDecrypt">RsaDecrypt</span>
<p>Decrypt data with private key file useing ras algorithm.</p> <p>Decrypt data with private key file useing ras algorithm.</p>
@@ -1017,14 +1088,17 @@ import (
func main() { func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil { if err != nil {
fmt.Println(err) return
} }
data := []byte("hello world") 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)) //hello world fmt.Println(string(decrypted))
// Output:
// hello
} }
``` ```

File diff suppressed because it is too large Load Diff

View File

@@ -46,7 +46,7 @@ import (
func NewHashMap() *HashMap func NewHashMap() *HashMap
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -72,7 +72,7 @@ func main() {
func NewHashMapWithCapacity(size, capacity uint64) *HashMap func NewHashMapWithCapacity(size, capacity uint64) *HashMap
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -98,7 +98,7 @@ func main() {
func (hm *HashMap) Get(key any) any func (hm *HashMap) Get(key any) any
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -126,7 +126,7 @@ func main() {
func (hm *HashMap) Put(key any, value any) any func (hm *HashMap) Put(key any, value any) any
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -155,7 +155,7 @@ func main() {
func (hm *HashMap) Delete(key any) func (hm *HashMap) Delete(key any)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -187,7 +187,7 @@ func main() {
func (hm *HashMap) Contains(key any) bool func (hm *HashMap) Contains(key any) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -217,7 +217,7 @@ func main() {
func (hm *HashMap) Iterate(iteratee func(key, value any)) func (hm *HashMap) Iterate(iteratee func(key, value any))
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -252,7 +252,7 @@ func main() {
func (hm *HashMap) Keys() []any func (hm *HashMap) Keys() []any
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -284,7 +284,7 @@ func main() {
func (hm *HashMap) Values() []any func (hm *HashMap) Values() []any
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -48,7 +48,7 @@ type MaxHeap[T any] struct {
} }
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T] func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -89,7 +89,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) Push(value T) func (h *MaxHeap[T]) Push(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -136,7 +136,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) Pop() (T, bool) func (h *MaxHeap[T]) Pop() (T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -184,7 +184,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) Peek() (T, bool) func (h *MaxHeap[T]) Peek() (T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -232,7 +232,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) Data() []T func (h *MaxHeap[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -277,7 +277,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) Size() int func (h *MaxHeap[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -323,7 +323,7 @@ func main() {
```go ```go
func (h *MaxHeap[T]) PrintStructure() func (h *MaxHeap[T]) PrintStructure()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -81,7 +81,7 @@ type SinglyLink[T any] struct {
} }
func NewSinglyLink[T any]() *SinglyLink[T] func NewSinglyLink[T any]() *SinglyLink[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -107,7 +107,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) Values() []T func (link *SinglyLink[T]) Values() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -139,7 +139,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) InsertAt(index int, value T) func (link *SinglyLink[T]) InsertAt(index int, value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -174,7 +174,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) InsertAtHead(value T) func (link *SinglyLink[T]) InsertAtHead(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -206,7 +206,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) InsertAtTail(value T) func (link *SinglyLink[T]) InsertAtTail(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -237,7 +237,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) DeleteAt(index int) func (link *SinglyLink[T]) DeleteAt(index int)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -271,7 +271,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) DeleteAtHead() func (link *SinglyLink[T]) DeleteAtHead()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -306,7 +306,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) DeleteAtTail() func (link *SinglyLink[T]) DeleteAtTail()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -339,7 +339,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) DeleteValue(value T) func (link *SinglyLink[T]) DeleteValue(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -373,7 +373,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) Reverse() func (link *SinglyLink[T]) Reverse()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -405,7 +405,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -437,7 +437,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) Size() int func (link *SinglyLink[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -468,7 +468,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) IsEmpty() bool func (link *SinglyLink[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -500,7 +500,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) Clear() func (link *SinglyLink[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -533,7 +533,7 @@ func main() {
```go ```go
func (link *SinglyLink[T]) Clear() func (link *SinglyLink[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -576,7 +576,7 @@ type DoublyLink[T any] struct {
} }
func NewDoublyLink[T any]() *DoublyLink[T] func NewDoublyLink[T any]() *DoublyLink[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -602,7 +602,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) Values() []T func (link *DoublyLink[T]) Values() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -634,7 +634,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) InsertAt(index int, value T) func (link *DoublyLink[T]) InsertAt(index int, value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -669,7 +669,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) InsertAtHead(value T) func (link *DoublyLink[T]) InsertAtHead(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -701,7 +701,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) InsertAtTail(value T) func (link *DoublyLink[T]) InsertAtTail(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -732,7 +732,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) DeleteAt(index int) func (link *DoublyLink[T]) DeleteAt(index int)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -766,7 +766,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) DeleteAtHead() func (link *DoublyLink[T]) DeleteAtHead()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -801,7 +801,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) DeleteAtTail() func (link *DoublyLink[T]) DeleteAtTail()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -835,7 +835,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) Reverse() func (link *DoublyLink[T]) Reverse()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -867,7 +867,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T] func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -899,7 +899,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) Size() int func (link *DoublyLink[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -930,7 +930,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) IsEmpty() bool func (link *DoublyLink[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -962,7 +962,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) Clear() func (link *DoublyLink[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -995,7 +995,7 @@ func main() {
```go ```go
func (link *DoublyLink[T]) Clear() func (link *DoublyLink[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -47,6 +47,13 @@ import (
- [Unique](#Unique) - [Unique](#Unique)
- [Union](#Union) - [Union](#Union)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [Difference](#Difference)
- [SymmetricDifference](#SymmetricDifference)
- [RetainAll](#RetainAll)
- [DeleteAll](#DeleteAll)
- [ForEach](#ForEach)
- [Iterator](#Iterator)
- [ListToMap](#ListToMap)
- [SubList](#SubList) - [SubList](#SubList)
- [DeleteIf](#DeleteIf) - [DeleteIf](#DeleteIf)
@@ -62,7 +69,7 @@ NewList function return a list pointer</p>
```go ```go
type List[T any] struct { type List[T any] struct {
data []T data []T
} }
func NewList[T any](data []T) *List[T] func NewList[T any](data []T) *List[T]
``` ```
@@ -671,7 +678,7 @@ import (
) )
func main() { func main() {
data := make([]int, 0, 100) data := make([]int, 0, 100)
li := list.NewList(data) li := list.NewList(data)
@@ -828,6 +835,233 @@ func main() {
### <span id="Difference">Difference</span>
<p>Return a list whose element in the original list, not in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) Difference(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>Oppoiste operation of intersection function.</p>
<b>Signature:</b>
```go
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3, 4
}
```
### <span id="RetainAll">RetainAll</span>
<p>Retains only the elements in this list that are contained in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) RetainAll(list *List[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
retain := NewList([]int{1, 2})
retain1 := NewList([]int{2, 3})
retain2 := NewList([]int{1, 2, 5})
list.RetainAll(retain)
list1.RetainAll(retain1)
list2.RetainAll(retain2)
fmt.Println(list.Data()) //1, 2
fmt.Println(list1.Data()) //2, 3
fmt.Println(list2.Data()) //1, 2
}
```
### <span id="DeleteAll">DeleteAll</span>
<p>Removes from this list all of its elements that are contained in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) DeleteAll(list *List[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
del := NewList([]int{1})
del1 := NewList([]int{2, 3})
del2 := NewList([]int{1, 2, 5})
list.DeleteAll(del)
list1.DeleteAll(del1)
list2.DeleteAll(del2)
fmt.Println(list.Data()) //2,3,4
fmt.Println(list1.Data()) //1,4
fmt.Println(list2.Data()) //3,4
}
```
### <span id="ForEach">ForEach</span>
<p>Performs the given action for each element of the list.</p>
<b>Signature:</b>
```go
func (l *List[T]) ForEach(consumer func(T))
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := make([]int, 0)
list.ForEach(func(i int) {
result = append(result, i)
})
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="Iterator">Iterator</span>
<p>Returns an iterator over the elements in this list in proper sequence.</p>
<b>Signature:</b>
```go
func (l *List[T]) Iterator() iterator.Iterator[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
iterator := list.Iterator()
result := make([]int, 0)
for iterator.HasNext() {
item, _ := iterator.Next()
result = append(result, item)
}
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="ListToMap">ListToMap</span>
<p>Converts a list to a map based on iteratee function.</p>
<b>Signature:</b>
```go
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
fmt.Println(result) //map[int]bool{1: false, 2: true, 3: true, 4: true}
}
```
### <span id="SubList">SubList</span> ### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p> <p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p>
@@ -876,9 +1110,9 @@ import (
) )
func main() { func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1}) l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12 fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4} fmt.Println(l.Data()) // []int{2, 3, 4}
} }
``` ```

View File

@@ -34,7 +34,6 @@ import (
- [PopLast](#PopLast) - [PopLast](#PopLast)
- [DeleteAt](#DeleteAt) - [DeleteAt](#DeleteAt)
- [InsertAt](#InsertAt) - [InsertAt](#InsertAt)
- [UpdateAt](#UpdateAt) - [UpdateAt](#UpdateAt)
- [Equal](#Equal) - [Equal](#Equal)
- [IsEmpty](#IsEmpty) - [IsEmpty](#IsEmpty)
@@ -48,6 +47,13 @@ import (
- [Unique](#Unique) - [Unique](#Unique)
- [Union](#Union) - [Union](#Union)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [Difference](#Difference)
- [SymmetricDifference](#SymmetricDifference)
- [RetainAll](#RetainAll)
- [DeleteAll](#DeleteAll)
- [ForEach](#ForEach)
- [Iterator](#Iterator)
- [ListToMap](#ListToMap)
- [SubList](#SubList) - [SubList](#SubList)
- [DeleteIf](#DeleteIf) - [DeleteIf](#DeleteIf)
@@ -62,11 +68,11 @@ import (
```go ```go
type List[T any] struct { type List[T any] struct {
data []T data []T
} }
func NewList[T any](data []T) *List[T] func NewList[T any](data []T) *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -92,7 +98,7 @@ func main() {
```go ```go
func (l *List[T]) Contain(value T) bool func (l *List[T]) Contain(value T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -121,7 +127,7 @@ func main() {
```go ```go
func (l *List[T]) Data() []T func (l *List[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -150,7 +156,7 @@ func main() {
```go ```go
func (l *List[T]) ValueOf(index int) (*T, bool) func (l *List[T]) ValueOf(index int) (*T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -180,7 +186,7 @@ func main() {
```go ```go
func (l *List[T]) IndexOf(value T) int func (l *List[T]) IndexOf(value T) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -207,7 +213,7 @@ func main() {
```go ```go
func (l *List[T]) LastIndexOf(value T) int func (l *List[T]) LastIndexOf(value T) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -233,7 +239,7 @@ func main() {
```go ```go
func (l *List[T]) IndexOfFunc(f func(T) bool) int func (l *List[T]) IndexOfFunc(f func(T) bool) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -259,7 +265,7 @@ func main() {
```go ```go
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int func (l *List[T]) LastIndexOfFunc(f func(T) bool) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -287,7 +293,7 @@ func main() {
```go ```go
func (l *List[T]) Push(value T) func (l *List[T]) Push(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -316,7 +322,7 @@ func main() {
```go ```go
func (l *List[T]) PopFirst() (*T, bool) func (l *List[T]) PopFirst() (*T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -348,7 +354,7 @@ func main() {
```go ```go
func (l *List[T]) PopLast() (*T, bool) func (l *List[T]) PopLast() (*T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -379,7 +385,7 @@ func main() {
```go ```go
func (l *List[T]) DeleteAt(index int) func (l *List[T]) DeleteAt(index int)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -417,7 +423,7 @@ func main() {
```go ```go
func (l *List[T]) InsertAt(index int, value T) func (l *List[T]) InsertAt(index int, value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -454,7 +460,7 @@ func main() {
```go ```go
func (l *List[T]) UpdateAt(index int, value T) func (l *List[T]) UpdateAt(index int, value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -487,7 +493,7 @@ func main() {
```go ```go
func (l *List[T]) Equal(other *List[T]) bool func (l *List[T]) Equal(other *List[T]) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -517,7 +523,7 @@ func main() {
```go ```go
func (l *List[T]) IsEmpty() bool func (l *List[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -547,7 +553,7 @@ func main() {
```go ```go
func (l *List[T]) Clear() func (l *List[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -575,7 +581,7 @@ func main() {
```go ```go
func (l *List[T]) Clone() *List[T] func (l *List[T]) Clone() *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -604,7 +610,7 @@ func main() {
```go ```go
func (l *List[T]) Merge(other *List[T]) *List[T] func (l *List[T]) Merge(other *List[T]) *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -633,7 +639,7 @@ func main() {
```go ```go
func (l *List[T]) Size() int func (l *List[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -660,7 +666,7 @@ func main() {
```go ```go
func (l *List[T]) Cap() int func (l *List[T]) Cap() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -671,7 +677,7 @@ import (
) )
func main() { func main() {
data := make([]int, 0, 100) data := make([]int, 0, 100)
li := list.NewList(data) li := list.NewList(data)
@@ -689,7 +695,7 @@ func main() {
```go ```go
func (l *List[T]) Swap(i, j int) func (l *List[T]) Swap(i, j int)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -718,7 +724,7 @@ func main() {
```go ```go
func (l *List[T]) Reverse() func (l *List[T]) Reverse()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -747,7 +753,7 @@ func main() {
```go ```go
func (l *List[T]) Unique() func (l *List[T]) Unique()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -776,7 +782,7 @@ func main() {
```go ```go
func (l *List[T]) Union(other *List[T]) *List[T] func (l *List[T]) Union(other *List[T]) *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -806,7 +812,7 @@ func main() {
```go ```go
func (l *List[T]) Intersection(other *List[T]) *List[T] func (l *List[T]) Intersection(other *List[T]) *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -826,16 +832,244 @@ func main() {
``` ```
### <span id="Difference">Difference</span>
<p>差集运算。</p>
<b>函数签名:</b>
```go
func (l *List[T]) Difference(other *List[T]) *List[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>对称差集运算。</p>
<b>函数签名:</b>
```go
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3, 4
}
```
### <span id="RetainAll">RetainAll</span>
<p>仅保留列表中包含在给定列表中的元素。</p>
<b>函数签名:</b>
```go
func (l *List[T]) RetainAll(list *List[T]) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
retain := NewList([]int{1, 2})
retain1 := NewList([]int{2, 3})
retain2 := NewList([]int{1, 2, 5})
list.RetainAll(retain)
list1.RetainAll(retain1)
list2.RetainAll(retain2)
fmt.Println(list.Data()) //1, 2
fmt.Println(list1.Data()) //2, 3
fmt.Println(list2.Data()) //1, 2
}
```
### <span id="DeleteAll">DeleteAll</span>
<p>从列表中删除给定列表中包含的所有元素。</p>
<b>函数签名:</b>
```go
func (l *List[T]) DeleteAll(list *List[T]) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
del := NewList([]int{1})
del1 := NewList([]int{2, 3})
del2 := NewList([]int{1, 2, 5})
list.DeleteAll(del)
list1.DeleteAll(del1)
list2.DeleteAll(del2)
fmt.Println(list.Data()) //2,3,4
fmt.Println(list1.Data()) //1,4
fmt.Println(list2.Data()) //3,4
}
```
### <span id="ForEach">ForEach</span>
<p>对列表的每个元素执行给定的操作。</p>
<b>函数签名:</b>
```go
func (l *List[T]) ForEach(consumer func(T))
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := make([]int, 0)
list.ForEach(func(i int) {
result = append(result, i)
})
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="Iterator">Iterator</span>
<p>按顺序返回列表中元素的迭代器。</p>
<b>函数签名:</b>
```go
func (l *List[T]) Iterator() iterator.Iterator[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
iterator := list.Iterator()
result := make([]int, 0)
for iterator.HasNext() {
item, _ := iterator.Next()
result = append(result, item)
}
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="ListToMap">ListToMap</span>
<p>基于iteratee函数将列表转换为映射map。</p>
<b>函数签名:</b>
```go
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
fmt.Println(result) //map[int]bool{1: false, 2: true, 3: true, 4: true}
}
```
### <span id="SubList">SubList</span> ### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p> <p>返回指定的fromIndex包含和toIndex不包含之间的原始列表的子列表。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] func (l *List[T]) SubList(fromIndex, toIndex int) *List[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -863,7 +1097,7 @@ func main() {
```go ```go
func (l *List[T]) DeleteIf(f func(T) bool) int func (l *List[T]) DeleteIf(f func(T) bool) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -874,9 +1108,9 @@ import (
) )
func main() { func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1}) l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12 fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4} fmt.Println(l.Data()) // []int{2, 3, 4}
} }
``` ```

View File

@@ -99,7 +99,7 @@ type ArrayQueue[T any] struct {
size int size int
} }
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -125,7 +125,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Data() []T func (q *ArrayQueue[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -152,7 +152,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Enqueue(item T) bool func (q *ArrayQueue[T]) Enqueue(item T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -183,7 +183,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Dequeue() (T, bool) func (q *ArrayQueue[T]) Dequeue() (T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -215,7 +215,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Front() T func (q *ArrayQueue[T]) Front() T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -247,7 +247,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Back() T func (q *ArrayQueue[T]) Back() T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -278,7 +278,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Size() int func (q *ArrayQueue[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -308,7 +308,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) IsEmpty() bool func (q *ArrayQueue[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -341,7 +341,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) IsFull() bool func (q *ArrayQueue[T]) IsFull() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -373,7 +373,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Clear() func (q *ArrayQueue[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -404,7 +404,7 @@ func main() {
```go ```go
func (q *ArrayQueue[T]) Contain(value T) bool func (q *ArrayQueue[T]) Contain(value T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -448,7 +448,7 @@ type QueueNode[T any] struct {
Next *QueueNode[T] Next *QueueNode[T]
} }
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -474,7 +474,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Data() []T func (q *LinkedQueue[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -501,7 +501,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Enqueue(value T) func (q *LinkedQueue[T]) Enqueue(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -532,7 +532,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Dequeue() (T, error) func (q *LinkedQueue[T]) Dequeue() (T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -564,7 +564,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Front() (*T, error) func (q *LinkedQueue[T]) Front() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -596,7 +596,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Back() (*T, error) func (q *LinkedQueue[T]) Back() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -627,7 +627,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Size() int func (q *LinkedQueue[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -657,7 +657,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) IsEmpty() bool func (q *LinkedQueue[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -690,7 +690,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Clear() func (q *LinkedQueue[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -721,7 +721,7 @@ func main() {
```go ```go
func (q *LinkedQueue[T]) Contain(value T) bool func (q *LinkedQueue[T]) Contain(value T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -763,7 +763,7 @@ type CircularQueue[T any] struct {
capacity int capacity int
} }
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -789,7 +789,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Data() []T func (q *CircularQueue[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -816,7 +816,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Enqueue(value T) error func (q *CircularQueue[T]) Enqueue(value T) error
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -847,7 +847,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Dequeue() (*T, bool) func (q *CircularQueue[T]) Dequeue() (*T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -880,7 +880,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Front() T func (q *CircularQueue[T]) Front() T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -912,7 +912,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Back() T func (q *CircularQueue[T]) Back() T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -943,7 +943,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Size() int func (q *CircularQueue[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -973,7 +973,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) IsEmpty() bool func (q *CircularQueue[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1006,7 +1006,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) IsFull() bool func (q *CircularQueue[T]) IsFull() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1038,7 +1038,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Clear() func (q *CircularQueue[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1069,7 +1069,7 @@ func main() {
```go ```go
func (q *CircularQueue[T]) Contain(value T) bool func (q *CircularQueue[T]) Contain(value T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1108,7 +1108,7 @@ type PriorityQueue[T any] struct {
comparator lancetconstraints.Comparator comparator lancetconstraints.Comparator
} }
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1134,7 +1134,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) Data() []T func (q *PriorityQueue[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1161,7 +1161,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) Enqueue(item T) bool func (q *PriorityQueue[T]) Enqueue(item T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1207,7 +1207,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) Dequeue() (T, bool) func (q *PriorityQueue[T]) Dequeue() (T, bool)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1254,7 +1254,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) IsEmpty() bool func (q *PriorityQueue[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1301,7 +1301,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) IsFull() bool func (q *PriorityQueue[T]) IsFull() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -1348,7 +1348,7 @@ func main() {
```go ```go
func (q *PriorityQueue[T]) Size() int func (q *PriorityQueue[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -55,7 +55,7 @@ import (
type Set[T comparable] map[T]bool type Set[T comparable] map[T]bool
func NewSet[T comparable](items ...T) Set[T] func NewSet[T comparable](items ...T) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -81,7 +81,7 @@ func main() {
```go ```go
func NewSetFromSlice[T comparable](items []T) Set[T] func NewSetFromSlice[T comparable](items []T) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -107,7 +107,7 @@ func main() {
```go ```go
func (s Set[T]) Values() []T func (s Set[T]) Values() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -134,7 +134,7 @@ func main() {
```go ```go
func (s Set[T]) Add(items ...T) func (s Set[T]) Add(items ...T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -161,7 +161,7 @@ func main() {
```go ```go
func (s Set[T]) AddIfNotExist(item T) bool func (s Set[T]) AddIfNotExist(item T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -193,7 +193,7 @@ func main() {
```go ```go
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -232,7 +232,7 @@ func main() {
```go ```go
func (s Set[T]) Delete(items ...T) func (s Set[T]) Delete(items ...T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -261,7 +261,7 @@ func main() {
```go ```go
func (s Set[T]) Contain(item T) bool func (s Set[T]) Contain(item T) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -291,7 +291,7 @@ func main() {
```go ```go
func (s Set[T]) ContainAll(other Set[T]) bool func (s Set[T]) ContainAll(other Set[T]) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -321,7 +321,7 @@ func main() {
```go ```go
func (s Set[T]) Size() int func (s Set[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -348,7 +348,7 @@ func main() {
```go ```go
func (s Set[T]) Clone() Set[T] func (s Set[T]) Clone() Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -378,7 +378,7 @@ func main() {
```go ```go
func (s Set[T]) Equal(other Set[T]) bool func (s Set[T]) Equal(other Set[T]) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -408,7 +408,7 @@ func main() {
```go ```go
func (s Set[T]) Iterate(fn func(item T)) func (s Set[T]) Iterate(fn func(item T))
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -439,7 +439,7 @@ func main() {
```go ```go
func (s Set[T]) IsEmpty() bool func (s Set[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -468,7 +468,7 @@ func main() {
```go ```go
func (s Set[T]) Union(other Set[T]) Set[T] func (s Set[T]) Union(other Set[T]) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -497,7 +497,7 @@ func main() {
```go ```go
func (s Set[T]) Intersection(other Set[T]) Set[T] func (s Set[T]) Intersection(other Set[T]) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -525,7 +525,7 @@ func main() {
```go ```go
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -556,7 +556,7 @@ func main() {
```go ```go
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] func (s Set[T]) Minus(comparedSet Set[T]) Set[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -64,7 +64,7 @@ type ArrayStack[T any] struct {
} }
func NewArrayStack[T any]() *ArrayStack[T] func NewArrayStack[T any]() *ArrayStack[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -91,7 +91,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Push(value T) func (s *ArrayStack[T]) Push(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -122,7 +122,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Pop() (*T, error) func (s *ArrayStack[T]) Pop() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -157,7 +157,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Peak() (*T, error) func (s *ArrayStack[T]) Peak() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -192,7 +192,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Data() []T func (s *ArrayStack[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -223,7 +223,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Size() int func (s *ArrayStack[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -254,7 +254,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) IsEmpty() bool func (s *ArrayStack[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -287,7 +287,7 @@ func main() {
```go ```go
func (s *ArrayStack[T]) Clear() func (s *ArrayStack[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -331,7 +331,7 @@ type LinkedStack[T any] struct {
} }
func NewLinkedStack[T any]() *LinkedStack[T] func NewLinkedStack[T any]() *LinkedStack[T]
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -358,7 +358,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Push(value T) func (s *LinkedStack[T]) Push(value T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -389,7 +389,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Pop() (*T, error) func (s *LinkedStack[T]) Pop() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -424,7 +424,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Peak() (*T, error) func (s *LinkedStack[T]) Peak() (*T, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -459,7 +459,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Data() []T func (s *LinkedStack[T]) Data() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -490,7 +490,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Size() int func (s *LinkedStack[T]) Size() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -521,7 +521,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) IsEmpty() bool func (s *LinkedStack[T]) IsEmpty() bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -554,7 +554,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Clear() func (s *LinkedStack[T]) Clear()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -588,7 +588,7 @@ func main() {
```go ```go
func (s *LinkedStack[T]) Print() func (s *LinkedStack[T]) Print()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -62,7 +62,7 @@ type TreeNode[T any] struct {
Right *TreeNode[T] Right *TreeNode[T]
} }
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -103,7 +103,7 @@ func main() {
```go ```go
func (t *BSTree[T]) Insert(data T) func (t *BSTree[T]) Insert(data T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -149,7 +149,7 @@ func main() {
```go ```go
func (t *BSTree[T]) Delete(data T) func (t *BSTree[T]) Delete(data T)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -197,7 +197,7 @@ func main() {
```go ```go
func (t *BSTree[T]) PreOrderTraverse() []T func (t *BSTree[T]) PreOrderTraverse() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -243,7 +243,7 @@ func main() {
```go ```go
func (t *BSTree[T]) InOrderTraverse() []T func (t *BSTree[T]) InOrderTraverse() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -289,7 +289,7 @@ func main() {
```go ```go
func (t *BSTree[T]) PostOrderTraverse() []T func (t *BSTree[T]) PostOrderTraverse() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -335,7 +335,7 @@ func main() {
```go ```go
func (t *BSTree[T]) LevelOrderTraverse() []T func (t *BSTree[T]) LevelOrderTraverse() []T
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -381,7 +381,7 @@ func main() {
```go ```go
func (t *BSTree[T]) Depth() int func (t *BSTree[T]) Depth() int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -427,7 +427,7 @@ func main() {
```go ```go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -479,7 +479,7 @@ func main() {
```go ```go
func (t *BSTree[T]) Print() func (t *BSTree[T]) Print()
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main

View File

@@ -1,16 +1,18 @@
# Datetime # Datetime
Package datetime supports date and time format and compare. Package datetime supports date and time format and compare.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go) - [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
- [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go) - [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/datetime" "github.com/duke-git/lancet/v2/datetime"
@@ -20,66 +22,67 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [AddDay](#AddDay)
- [AddHour](#AddHour)
- [AddMinute](#AddMinute)
- [BeginOfMinute](#BeginOfMinute)
- [BeginOfHour](#BeginOfHour)
- [BeginOfDay](#BeginOfDay)
- [BeginOfWeek](#BeginOfWeek)
- [BeginOfMonth](#BeginOfMonth)
- [BeginOfYear](#BeginOfYear)
- [EndOfMinute](#EndOfMinute)
- [EndOfHour](#EndOfHour)
- [EndOfDay](#EndOfDay)
- [EndOfWeek](#EndOfWeek)
- [EndOfMonth](#EndOfMonth)
- [EndOfYear](#EndOfYear)
- [GetNowDate](#GetNowDate)
- [GetNowTime](#GetNowTime)
- [GetNowDateTime](#GetNowDateTime)
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
- [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix)
- [NewFormat](#NewFormat)
- [NewISO8601](#NewISO8601)
- [ToUnix](#ToUnix)
- [ToFormat](#ToFormat)
- [ToFormatForTpl](#ToFormatForTpl)
- [ToIso8601](#ToIso8601)
- [AddDay](#AddDay)
- [AddHour](#AddHour)
- [AddMinute](#AddMinute)
- [BeginOfMinute](#BeginOfMinute)
- [BeginOfHour](#BeginOfHour)
- [BeginOfDay](#BeginOfDay)
- [BeginOfWeek](#BeginOfWeek)
- [BeginOfMonth](#BeginOfMonth)
- [BeginOfYear](#BeginOfYear)
- [EndOfMinute](#EndOfMinute)
- [EndOfHour](#EndOfHour)
- [EndOfDay](#EndOfDay)
- [EndOfWeek](#EndOfWeek)
- [EndOfMonth](#EndOfMonth)
- [EndOfYear](#EndOfYear)
- [GetNowDate](#GetNowDate)
- [GetNowTime](#GetNowTime)
- [GetNowDateTime](#GetNowDateTime)
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
- [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix)
- [NewFormat](#NewFormat)
- [NewISO8601](#NewISO8601)
- [ToUnix](#ToUnix)
- [ToFormat](#ToFormat)
- [ToFormatForTpl](#ToFormatForTpl)
- [ToIso8601](#ToIso8601)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
## Note: ## Note:
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
- yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm
- yyyy-mm-dd hh
- yyyy-mm-dd
- yyyy-mm
- mm-dd
- dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm
- yyyy-mm-dd hh
- yyyy/mm/dd
- yyyy/mm
- mm/dd
- dd/mm/yy hh:mm:ss
- yyyy
- mm
- hh:mm:ss
- mm:ss
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
- yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm
- yyyy-mm-dd hh
- yyyy-mm-dd
- yyyy-mm
- mm-dd
- dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm
- yyyy-mm-dd hh
- yyyy/mm/dd
- yyyy/mm
- mm/dd
- dd/mm/yy hh:mm:ss
- yyyy
- mm
- hh:mm:ss
- mm:ss
### <span id="AddDay">AddDay</span> ### <span id="AddDay">AddDay</span>
<p>Add or sub days to time.</p> <p>Add or sub days to time.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -87,6 +90,7 @@ import (
```go ```go
func AddDay(t time.Time, day int64) time.Time func AddDay(t time.Time, day int64) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -100,15 +104,24 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
after2Days := datetime.AddDay(now, 2)
before2Days := datetime.AddDay(now, -2)
fmt.Println(after2Days, before2Days) tomorrow := datetime.AddDay(now, 1)
diff1 := tomorrow.Sub(now)
yesterday := datetime.AddDay(now, -1)
diff2 := yesterday.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 24h0m0s
// -24h0m0s
} }
``` ```
### <span id="AddHour">AddHour</span> ### <span id="AddHour">AddHour</span>
<p>Add or sub hours to time.</p> <p>Add or sub hours to time.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -116,6 +129,7 @@ func main() {
```go ```go
func AddHour(t time.Time, hour int64) time.Time func AddHour(t time.Time, hour int64) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -129,14 +143,24 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
after2Hours := datetime.AddHour(now, 2)
before2Hours := datetime.AddHour(now, -2)
fmt.Println(after2Hours, after2Hours) after2Hours := datetime.AddHour(now, 2)
diff1 := after2Hours.Sub(now)
before2Hours := datetime.AddHour(now, -2)
diff2 := before2Hours.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 2h0m0s
// -2h0m0s
} }
``` ```
### <span id="AddMinute">AddMinute</span> ### <span id="AddMinute">AddMinute</span>
<p>Add or sub minutes to time.</p> <p>Add or sub minutes to time.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -144,6 +168,7 @@ func main() {
```go ```go
func AddMinute(t time.Time, minute int64) time.Time func AddMinute(t time.Time, minute int64) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -157,14 +182,24 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
after2Minute := datetime.AddMinute(now, 2)
before2Minute := datetime.AddMinute(now, -2)
fmt.Println(after2Minute, before2Minute) after2Minutes := datetime.AddMinute(now, 2)
diff1 := after2Minutes.Sub(now)
before2Minutes := datetime.AddMinute(now, -2)
diff2 := before2Minutes.Sub(now)
fmt.Println(diff1)
fmt.Println(diff2)
// Output:
// 2m0s
// -2m0s
} }
``` ```
### <span id="BeginOfMinute">BeginOfMinute</span> ### <span id="BeginOfMinute">BeginOfMinute</span>
<p>Return beginning minute time of day.</p> <p>Return beginning minute time of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -172,6 +207,7 @@ func main() {
```go ```go
func BeginOfMinute(t time.Time) time.Time func BeginOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -184,13 +220,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfMinute(td) result := datetime.BeginOfMinute(input)
fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 18:50:00 +0000 UTC
} }
``` ```
### <span id="BeginOfHour">BeginOfHour</span> ### <span id="BeginOfHour">BeginOfHour</span>
<p>Return zero time of day.</p> <p>Return zero time of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -198,6 +239,7 @@ func main() {
```go ```go
func BeginOfHour(t time.Time) time.Time func BeginOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -210,13 +252,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfHour(td) result := datetime.BeginOfHour(input)
fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 18:00:00 +0000 UTC
} }
``` ```
### <span id="BeginOfDay">BeginOfDay</span> ### <span id="BeginOfDay">BeginOfDay</span>
<p>Return begin time of day.</p> <p>Return begin time of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -224,6 +271,7 @@ func main() {
```go ```go
func BeginOfDay(t time.Time) time.Time func BeginOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -236,15 +284,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfDay(td) result := datetime.BeginOfDay(input)
fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
} }
``` ```
### <span id="BeginOfWeek">BeginOfWeek</span> ### <span id="BeginOfWeek">BeginOfWeek</span>
<p>Return beginning time of week, week begin from Sunday.</p> <p>Return beginning time of week, week begin from Sunday.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -252,6 +303,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -264,15 +316,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfWeek(td) result := datetime.BeginOfWeek(input)
fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
} }
``` ```
### <span id="BeginOfMonth">BeginOfMonth</span> ### <span id="BeginOfMonth">BeginOfMonth</span>
<p>Return beginning time of month</p> <p>Return beginning time of month</p>
<b>Signature:</b> <b>Signature:</b>
@@ -280,6 +335,7 @@ func main() {
```go ```go
func BeginOfMonth(t time.Time) time.Time func BeginOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -292,14 +348,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfMonth(td) result := datetime.BeginOfMonth(input)
fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-01 00:00:00 +0000 UTC
} }
``` ```
### <span id="BeginOfYear">BeginOfYear</span> ### <span id="BeginOfYear">BeginOfYear</span>
<p>Return beginning time of year.</p> <p>Return beginning time of year.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -307,6 +367,7 @@ func main() {
```go ```go
func BeginOfYear(t time.Time) time.Time func BeginOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -319,15 +380,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.BeginOfYear(td) result := datetime.BeginOfYear(input)
fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-01 00:00:00 +0000 UTC
} }
``` ```
### <span id="EndOfMinute">EndOfMinute</span> ### <span id="EndOfMinute">EndOfMinute</span>
<p>Return end time minute of day.</p> <p>Return end time minute of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -335,6 +399,7 @@ func main() {
```go ```go
func EndOfMinute(t time.Time) time.Time func EndOfMinute(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -347,13 +412,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfMinute(td) result := datetime.EndOfMinute(input)
fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 18:50:59.999999999 +0000 UTC
} }
``` ```
### <span id="EndOfHour">EndOfHour</span> ### <span id="EndOfHour">EndOfHour</span>
<p>Return end time hour of day.</p> <p>Return end time hour of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -361,6 +431,7 @@ func main() {
```go ```go
func EndOfHour(t time.Time) time.Time func EndOfHour(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -373,13 +444,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfHour(td) result := datetime.EndOfHour(input)
fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 18:59:59.999999999 +0000 UTC
} }
``` ```
### <span id="EndOfDay">EndOfDay</span> ### <span id="EndOfDay">EndOfDay</span>
<p>Return end time hour of day.</p> <p>Return end time hour of day.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -387,6 +463,7 @@ func main() {
```go ```go
func EndOfDay(t time.Time) time.Time func EndOfDay(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -399,15 +476,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfDay(td) result := datetime.EndOfDay(input)
fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-08 23:59:59.999999999 +0000 UTC
} }
``` ```
### <span id="EndOfWeek">EndOfWeek</span> ### <span id="EndOfWeek">EndOfWeek</span>
<p>Return end time of week, week end with Saturday.</p> <p>Return end time of week, week end with Saturday.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -415,6 +495,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -427,15 +508,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfWeek(td) result := datetime.EndOfWeek(input)
fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-14 23:59:59.999999999 +0000 UTC
} }
``` ```
### <span id="EndOfMonth">EndOfMonth</span> ### <span id="EndOfMonth">EndOfMonth</span>
<p>Return end time of month</p> <p>Return end time of month</p>
<b>Signature:</b> <b>Signature:</b>
@@ -443,6 +527,7 @@ func main() {
```go ```go
func EndOfMonth(t time.Time) time.Time func EndOfMonth(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -455,14 +540,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfMonth(td) result := datetime.EndOfMonth(input)
fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-01-31 23:59:59.999999999 +0000 UTC
} }
``` ```
### <span id="EndOfYear">EndOfYear</span> ### <span id="EndOfYear">EndOfYear</span>
<p>Return beginning time of year.</p> <p>Return beginning time of year.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -470,6 +559,7 @@ func main() {
```go ```go
func EndOfYear(t time.Time) time.Time func EndOfYear(t time.Time) time.Time
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -482,14 +572,18 @@ import (
) )
func main() { func main() {
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
bm := datetime.EndOfYear(td) result := datetime.EndOfYear(input)
fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST
fmt.Println(result)
// Output:
// 2023-12-31 23:59:59.999999999 +0000 UTC
} }
``` ```
### <span id="GetNowDate">GetNowDate</span> ### <span id="GetNowDate">GetNowDate</span>
<p>Get current date string, format is yyyy-mm-dd.</p> <p>Get current date string, format is yyyy-mm-dd.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -497,6 +591,7 @@ func main() {
```go ```go
func GetNowDate() string func GetNowDate() string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -511,12 +606,16 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
currentDate := datetime.GetNowDate() currentDate := datetime.GetNowDate()
fmt.Println(currentDate) // 2022-01-28
fmt.Println(currentDate)
// Output:
// 2022-01-28
} }
``` ```
### <span id="GetNowTime">GetNowTime</span> ### <span id="GetNowTime">GetNowTime</span>
<p>Get current time string, format is hh:mm:ss.</p> <p>Get current time string, format is hh:mm:ss.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -524,6 +623,7 @@ func main() {
```go ```go
func GetNowTime() string func GetNowTime() string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -538,12 +638,16 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
currentTime := datetime.GetNowTime() currentTime := datetime.GetNowTime()
fmt.Println(currentDate) // 15:57:33
fmt.Println(currentTime) // 15:57:33
// Output:
// 15:57:33
} }
``` ```
### <span id="GetNowDateTime">GetNowDateTime</span> ### <span id="GetNowDateTime">GetNowDateTime</span>
<p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p> <p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -551,6 +655,7 @@ func main() {
```go ```go
func GetNowDateTime() string func GetNowDateTime() string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -565,12 +670,16 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
current := datetime.GetNowDateTime() current := datetime.GetNowDateTime()
fmt.Println(current) // 2022-01-28 15:59:33
fmt.Println(current)
// Output:
// 2022-01-28 15:59:33
} }
``` ```
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span> ### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
<p>Return timestamp of zero hour (timestamp of 00:00).</p> <p>Return timestamp of zero hour (timestamp of 00:00).</p>
<b>Signature:</b> <b>Signature:</b>
@@ -578,6 +687,7 @@ func main() {
```go ```go
func GetZeroHourTimestamp() int64 func GetZeroHourTimestamp() int64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -592,12 +702,16 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
zeroTime := datetime.GetZeroHourTimestamp() zeroTime := datetime.GetZeroHourTimestamp()
fmt.Println(zeroTime) // 1643299200
fmt.Println(zeroTime)
// Output:
// 1643299200
} }
``` ```
### <span id="GetNightTimestamp">GetNightTimestamp</span> ### <span id="GetNightTimestamp">GetNightTimestamp</span>
<p>Return timestamp of zero hour (timestamp of 23:59).</p> <p>Return timestamp of zero hour (timestamp of 23:59).</p>
<b>Signature:</b> <b>Signature:</b>
@@ -605,6 +719,7 @@ func main() {
```go ```go
func GetNightTimestamp() int64 func GetNightTimestamp() int64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -619,11 +734,16 @@ import (
func main() { func main() {
now := time.Now() now := time.Now()
nightTime := datetime.GetNightTimestamp() nightTime := datetime.GetNightTimestamp()
fmt.Println(nightTime) // 1643385599
fmt.Println(nightTime)
// Output:
// 1643385599
} }
``` ```
### <span id="FormatTimeToStr">FormatTimeToStr</span> ### <span id="FormatTimeToStr">FormatTimeToStr</span>
<p>Format time to string, `format` param specification see note 1.</p> <p>Format time to string, `format` param specification see note 1.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -631,6 +751,7 @@ func main() {
```go ```go
func FormatTimeToStr(t time.Time, format string) string func FormatTimeToStr(t time.Time, format string) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -643,14 +764,25 @@ import (
) )
func main() { func main() {
now := time.Now() t, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08")
timeStr := datetime.FormatTimeToStr(now, "yyyy/mm/dd hh:mm:ss")
fmt.Println(timeStr) //2022/01/28 16:07:44 result1 := datetime.FormatTimeToStr(t, "yyyy-mm-dd hh:mm:ss")
result2 := datetime.FormatTimeToStr(t, "yyyy-mm-dd")
result3 := datetime.FormatTimeToStr(t, "dd-mm-yy hh:mm:ss")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 2021-01-02 16:04:08
// 2021-01-02
// 02-01-21 16:04:08
} }
``` ```
### <span id="FormatStrToTime">FormatStrToTime</span> ### <span id="FormatStrToTime">FormatStrToTime</span>
<p>Format string to time, `format` param specification see note 1.</p> <p>Format string to time, `format` param specification see note 1.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -658,6 +790,7 @@ func main() {
```go ```go
func FormatStrToTime(str, format string) (time.Time, error) func FormatStrToTime(str, format string) (time.Time, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -669,24 +802,34 @@ import (
) )
func main() { func main() {
time := datetime.FormatStrToTime("2006-01-02 15:04:05", "yyyy/mm/dd hh:mm:ss") result1, _ := datetime.FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss")
fmt.Println(time) result2, _ := datetime.FormatStrToTime("2021-01-02", "yyyy-mm-dd")
result3, _ := datetime.FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 2021-01-02 16:04:08 +0000 UTC
// 2021-01-02 00:00:00 +0000 UTC
// 2021-01-02 16:04:08 +0000 UTC
} }
``` ```
### <span id="NewUnixNow">NewUnixNow</span> ### <span id="NewUnixNow">NewUnixNow</span>
<p>Return unix timestamp of current time</p> <p>Return unix timestamp of current time</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
type theTime struct { type theTime struct {
unix int64 unix int64
} }
func NewUnixNow() *theTime func NewUnixNow() *theTime
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -699,12 +842,15 @@ import (
func main() { func main() {
tm := datetime.NewUnixNow() tm := datetime.NewUnixNow()
fmt.Println(tm) //&{1647597438} fmt.Println(tm)
// Output:
// &{1647597438}
} }
``` ```
### <span id="NewUnix">NewUnix</span> ### <span id="NewUnix">NewUnix</span>
<p>Return unix timestamp of specified int64 value.</p> <p>Return unix timestamp of specified int64 value.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -715,6 +861,7 @@ type theTime struct {
} }
func NewUnix(unix int64) *theTime func NewUnix(unix int64) *theTime
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -727,13 +874,15 @@ import (
func main() { func main() {
tm := datetime.NewUnix(1647597438) tm := datetime.NewUnix(1647597438)
fmt.Println(tm) //&{1647597438} fmt.Println(tm)
// Output:
// &{1647597438}
} }
``` ```
### <span id="NewFormat">NewFormat</span> ### <span id="NewFormat">NewFormat</span>
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p> <p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
<b>Signature:</b> <b>Signature:</b>
@@ -744,6 +893,7 @@ type theTime struct {
} }
func NewFormat(t string) (*theTime, error) func NewFormat(t string) (*theTime, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -756,14 +906,15 @@ import (
func main() { func main() {
tm, err := datetime.NewFormat("2022-03-18 17:04:05") tm, err := datetime.NewFormat("2022-03-18 17:04:05")
fmt.Println(tm) //&{1647594245} fmt.Println(tm)
// Output:
// &{1647594245}
} }
``` ```
### <span id="NewISO8601">NewISO8601</span> ### <span id="NewISO8601">NewISO8601</span>
<p>Return unix timestamp of specified iso8601 time string.</p> <p>Return unix timestamp of specified iso8601 time string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -774,6 +925,7 @@ type theTime struct {
} }
func NewISO8601(iso8601 string) (*theTime, error) func NewISO8601(iso8601 string) (*theTime, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -786,13 +938,15 @@ import (
func main() { func main() {
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z") tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
fmt.Println(tm) //&{1136214245} fmt.Println(tm)
// Output:
// &{1136214245}
} }
``` ```
### <span id="ToUnix">ToUnix</span> ### <span id="ToUnix">ToUnix</span>
<p>Return unix timestamp.</p> <p>Return unix timestamp.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -800,6 +954,7 @@ func main() {
```go ```go
func (t *theTime) ToUnix() int64 func (t *theTime) ToUnix() int64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -812,13 +967,15 @@ import (
func main() { func main() {
tm := datetime.NewUnixNow() tm := datetime.NewUnixNow()
fmt.Println(tm.ToUnix()) //1647597438 fmt.Println(tm.ToUnix())
// Output:
// 1647597438
} }
``` ```
### <span id="ToFormat">ToFormat</span> ### <span id="ToFormat">ToFormat</span>
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p> <p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -826,6 +983,7 @@ func main() {
```go ```go
func (t *theTime) ToFormat() string func (t *theTime) ToFormat() string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -838,13 +996,15 @@ import (
func main() { func main() {
tm, _ := datetime.NewFormat("2022-03-18 17:04:05") tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05" fmt.Println(tm.ToFormat())
// Output:
// 2022-03-18 17:04:05
} }
``` ```
### <span id="ToFormatForTpl">ToFormatForTpl</span> ### <span id="ToFormatForTpl">ToFormatForTpl</span>
<p>Return the time string which format is specified tpl.</p> <p>Return the time string which format is specified tpl.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -852,6 +1012,7 @@ func main() {
```go ```go
func (t *theTime) ToFormatForTpl(tpl string) string func (t *theTime) ToFormatForTpl(tpl string) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -865,12 +1026,15 @@ import (
func main() { func main() {
tm, _ := datetime.NewFormat("2022-03-18 17:04:05") tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
ts := tm.ToFormatForTpl("2006/01/02 15:04:05") ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
fmt.Println(ts) //"2022/03/18 17:04:05" fmt.Println(ts)
// Output:
// 2022/03/18 17:04:05
} }
``` ```
### <span id="ToIso8601">ToIso8601</span> ### <span id="ToIso8601">ToIso8601</span>
<p>Return iso8601 time string.</p> <p>Return iso8601 time string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -878,6 +1042,7 @@ func main() {
```go ```go
func (t *theTime) ToIso8601() string func (t *theTime) ToIso8601() string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -891,6 +1056,9 @@ import (
func main() { func main() {
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z") tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
ts := tm.ToIso8601() ts := tm.ToIso8601()
fmt.Println(ts) //"2006-01-02T23:04:05+08:00" fmt.Println(ts)
// Output:
// 2006-01-02T23:04:05+08:00
} }
``` ```

File diff suppressed because it is too large Load Diff

View File

@@ -1,15 +1,17 @@
# Fileutil # Fileutil
Package fileutil implements some basic functions for file operations. Package fileutil implements some basic functions for file operations.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go) - [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/fileutil" "github.com/duke-git/lancet/v2/fileutil"
@@ -19,30 +21,30 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [ClearFile](#ClearFile)
- [CreateFile](#CreateFile) - [ClearFile](#ClearFile)
- [CreateDir](#CreateDir) - [CreateFile](#CreateFile)
- [CopyFile](#CopyFile) - [CreateDir](#CreateDir)
- [FileMode](#FileMode) - [CopyFile](#CopyFile)
- [MiMeType](#MiMeType) - [FileMode](#FileMode)
- [IsExist](#IsExist) - [MiMeType](#MiMeType)
- [IsLink](#IsLink) - [IsExist](#IsExist)
- [IsDir](#IsDir) - [IsLink](#IsLink)
- [ListFileNames](#ListFileNames) - [IsDir](#IsDir)
- [RemoveFile](#RemoveFile) - [ListFileNames](#ListFileNames)
- [ReadFileToString](#ReadFileToString) - [RemoveFile](#RemoveFile)
- [ReadFileByLine](#ReadFileByLine) - [ReadFileToString](#ReadFileToString)
- [Zip](#Zip) - [ReadFileByLine](#ReadFileByLine)
- [UnZip](#UnZip) - [Zip](#Zip)
- [UnZip](#UnZip) - [UnZip](#UnZip)
- [UnZip](#UnZip)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="ClearFile">ClearFile</span> ### <span id="ClearFile">ClearFile</span>
<p>Clear the file content, write empty string to the file.</p> <p>Clear the file content, write empty string to the file.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -50,6 +52,7 @@ import (
```go ```go
func ClearFile(path string) error func ClearFile(path string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -69,6 +72,7 @@ func main() {
``` ```
### <span id="CreateFile">CreateFile</span> ### <span id="CreateFile">CreateFile</span>
<p>Create file in path. return true if create succeed.</p> <p>Create file in path. return true if create succeed.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -76,6 +80,7 @@ func main() {
```go ```go
func CreateFile(path string) bool func CreateFile(path string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -92,9 +97,8 @@ func main() {
} }
``` ```
### <span id="CreateDir">CreateDir</span> ### <span id="CreateDir">CreateDir</span>
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p> <p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -102,6 +106,7 @@ func main() {
```go ```go
func CreateDir(absPath string) error func CreateDir(absPath string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -118,8 +123,8 @@ func main() {
} }
``` ```
### <span id="CopyFile">CopyFile</span> ### <span id="CopyFile">CopyFile</span>
<p>Copy src file to dest file. If dest file exist will overwrite it.</p> <p>Copy src file to dest file. If dest file exist will overwrite it.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -127,6 +132,7 @@ func main() {
```go ```go
func CopyFile(srcFilePath string, dstFilePath string) error func CopyFile(srcFilePath string, dstFilePath string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -145,9 +151,8 @@ func main() {
} }
``` ```
### <span id="FileMode">FileMode</span> ### <span id="FileMode">FileMode</span>
<p>Return file mode infomation.</p> <p>Return file mode infomation.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -155,6 +160,7 @@ func main() {
```go ```go
func FileMode(path string) (fs.FileMode, error) func FileMode(path string) (fs.FileMode, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -174,9 +180,8 @@ func main() {
} }
``` ```
### <span id="MiMeType">MiMeType</span> ### <span id="MiMeType">MiMeType</span>
<p>Get file mime type, 'file' param's type should be string or *os.File.</p> <p>Get file mime type, 'file' param's type should be string or *os.File.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -184,6 +189,7 @@ func main() {
```go ```go
func MiMeType(file any) string func MiMeType(file any) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -205,10 +211,8 @@ func main() {
} }
``` ```
### <span id="IsExist">IsExist</span> ### <span id="IsExist">IsExist</span>
<p>Checks if a file or directory exists.</p> <p>Checks if a file or directory exists.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -216,6 +220,7 @@ func main() {
```go ```go
func IsExist(path string) bool func IsExist(path string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -233,9 +238,8 @@ func main() {
} }
``` ```
### <span id="IsLink">IsLink</span> ### <span id="IsLink">IsLink</span>
<p>Checks if a file is symbol link or not.</p> <p>Checks if a file is symbol link or not.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -243,6 +247,7 @@ func main() {
```go ```go
func IsLink(path string) bool func IsLink(path string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -259,9 +264,8 @@ func main() {
} }
``` ```
### <span id="IsDir">IsDir</span> ### <span id="IsDir">IsDir</span>
<p>Checks if the path is directy or not.</p> <p>Checks if the path is directy or not.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -269,6 +273,7 @@ func main() {
```go ```go
func IsDir(path string) bool func IsDir(path string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -288,9 +293,8 @@ func main() {
} }
``` ```
### <span id="ListFileNames">ListFileNames</span> ### <span id="ListFileNames">ListFileNames</span>
<p>List all file names in given path.</p> <p>List all file names in given path.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -298,6 +302,7 @@ func main() {
```go ```go
func ListFileNames(path string) ([]string, error) func ListFileNames(path string) ([]string, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -314,9 +319,8 @@ func main() {
} }
``` ```
### <span id="RemoveFile">RemoveFile</span> ### <span id="RemoveFile">RemoveFile</span>
<p>Remove the file of path.</p> <p>Remove the file of path.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -324,6 +328,7 @@ func main() {
```go ```go
func RemoveFile(path string) error func RemoveFile(path string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -342,8 +347,8 @@ func main() {
} }
``` ```
### <span id="ReadFileToString">ReadFileToString</span> ### <span id="ReadFileToString">ReadFileToString</span>
<p>Return string of file content.</p> <p>Return string of file content.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -351,6 +356,7 @@ func main() {
```go ```go
func ReadFileToString(path string) (string, error) func ReadFileToString(path string) (string, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -374,9 +380,8 @@ func main() {
} }
``` ```
### <span id="ReadFileByLine">ReadFileByLine</span> ### <span id="ReadFileByLine">ReadFileByLine</span>
<p>Read file line by line, and return slice of lines</p> <p>Read file line by line, and return slice of lines</p>
<b>Signature:</b> <b>Signature:</b>
@@ -384,6 +389,7 @@ func main() {
```go ```go
func ReadFileByLine(path string)([]string, error) func ReadFileByLine(path string)([]string, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -408,9 +414,8 @@ func main() {
} }
``` ```
### <span id="Zip">Zip</span> ### <span id="Zip">Zip</span>
<p>Create a zip file of fpath, fpath could be a file or a directory.</p> <p>Create a zip file of fpath, fpath could be a file or a directory.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -418,6 +423,7 @@ func main() {
```go ```go
func Zip(fpath string, destPath string) error func Zip(fpath string, destPath string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -436,10 +442,8 @@ func main() {
} }
``` ```
### <span id="UnZip">UnZip</span> ### <span id="UnZip">UnZip</span>
<p>Unzip the file and save it to dest path.</p> <p>Unzip the file and save it to dest path.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -447,6 +451,7 @@ func main() {
```go ```go
func UnZip(zipFile string, destPath string) error func UnZip(zipFile string, destPath string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -464,8 +469,3 @@ func main() {
} }
} }
``` ```

View File

@@ -1,15 +1,17 @@
# Fileutil # Fileutil
fileutil包支持文件基本操作。
fileutil 包支持文件基本操作。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go) - [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.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/fileutil" "github.com/duke-git/lancet/v2/fileutil"
@@ -19,29 +21,29 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [ClearFile](#ClearFile)
- [CreateFile](#CreateFile) - [ClearFile](#ClearFile)
- [CreateDir](#CreateDir) - [CreateFile](#CreateFile)
- [CopyFile](#CopyFile) - [CreateDir](#CreateDir)
- [FileMode](#FileMode) - [CopyFile](#CopyFile)
- [MiMeType](#MiMeType) - [FileMode](#FileMode)
- [IsExist](#IsExist) - [MiMeType](#MiMeType)
- [IsLink](#IsLink) - [IsExist](#IsExist)
- [IsDir](#IsDir) - [IsLink](#IsLink)
- [ListFileNames](#ListFileNames) - [IsDir](#IsDir)
- [RemoveFile](#RemoveFile) - [ListFileNames](#ListFileNames)
- [ReadFileToString](#ReadFileToString) - [RemoveFile](#RemoveFile)
- [ReadFileByLine](#ReadFileByLine) - [ReadFileToString](#ReadFileToString)
- [Zip](#Zip) - [ReadFileByLine](#ReadFileByLine)
- [UnZip](#UnZip) - [Zip](#Zip)
- [UnZip](#UnZip)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="ClearFile">ClearFile</span> ### <span id="ClearFile">ClearFile</span>
<p>清空文件内容</p> <p>清空文件内容</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -49,7 +51,8 @@ import (
```go ```go
func ClearFile(path string) error func ClearFile(path string) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -68,6 +71,7 @@ func main() {
``` ```
### <span id="CreateFile">CreateFile</span> ### <span id="CreateFile">CreateFile</span>
<p>创建文件创建成功返回true, 否则返回false</p> <p>创建文件创建成功返回true, 否则返回false</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -75,7 +79,8 @@ func main() {
```go ```go
func CreateFile(path string) bool func CreateFile(path string) bool
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -91,8 +96,8 @@ func main() {
} }
``` ```
### <span id="CreateDir">CreateDir</span> ### <span id="CreateDir">CreateDir</span>
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p> <p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -100,6 +105,7 @@ func main() {
```go ```go
func CreateDir(absPath string) error func CreateDir(absPath string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -116,9 +122,8 @@ func main() {
} }
``` ```
### <span id="CopyFile">CopyFile</span> ### <span id="CopyFile">CopyFile</span>
<p>拷贝文件,会覆盖原有的文件</p> <p>拷贝文件,会覆盖原有的文件</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -126,7 +131,8 @@ func main() {
```go ```go
func CopyFile(srcFilePath string, dstFilePath string) error func CopyFile(srcFilePath string, dstFilePath string) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -144,9 +150,8 @@ func main() {
} }
``` ```
### <span id="FileMode">FileMode</span> ### <span id="FileMode">FileMode</span>
<p>获取文件mode信息</p> <p>获取文件mode信息</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -154,7 +159,8 @@ func main() {
```go ```go
func FileMode(path string) (fs.FileMode, error) func FileMode(path string) (fs.FileMode, error)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -173,9 +179,8 @@ func main() {
} }
``` ```
### <span id="MiMeType">MiMeType</span> ### <span id="MiMeType">MiMeType</span>
<p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p> <p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -183,7 +188,8 @@ func main() {
```go ```go
func MiMeType(file any) string func MiMeType(file any) string
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -204,10 +210,8 @@ func main() {
} }
``` ```
### <span id="IsExist">IsExist</span> ### <span id="IsExist">IsExist</span>
<p>判断文件或目录是否存在</p> <p>判断文件或目录是否存在</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -215,7 +219,8 @@ func main() {
```go ```go
func IsExist(path string) bool func IsExist(path string) bool
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -232,9 +237,8 @@ func main() {
} }
``` ```
### <span id="IsLink">IsLink</span> ### <span id="IsLink">IsLink</span>
<p>判断文件是否是符号链接</p> <p>判断文件是否是符号链接</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -242,7 +246,8 @@ func main() {
```go ```go
func IsLink(path string) bool func IsLink(path string) bool
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -258,9 +263,8 @@ func main() {
} }
``` ```
### <span id="IsDir">IsDir</span> ### <span id="IsDir">IsDir</span>
<p>判断参数是否是目录</p> <p>判断参数是否是目录</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -268,7 +272,8 @@ func main() {
```go ```go
func IsDir(path string) bool func IsDir(path string) bool
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -287,9 +292,8 @@ func main() {
} }
``` ```
### <span id="ListFileNames">ListFileNames</span> ### <span id="ListFileNames">ListFileNames</span>
<p>返回目录下所有文件名</p> <p>返回目录下所有文件名</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -297,7 +301,8 @@ func main() {
```go ```go
func ListFileNames(path string) ([]string, error) func ListFileNames(path string) ([]string, error)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -313,9 +318,8 @@ func main() {
} }
``` ```
### <span id="RemoveFile">RemoveFile</span> ### <span id="RemoveFile">RemoveFile</span>
<p>删除文件</p> <p>删除文件</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -323,7 +327,8 @@ func main() {
```go ```go
func RemoveFile(path string) error func RemoveFile(path string) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -341,8 +346,8 @@ func main() {
} }
``` ```
### <span id="ReadFileToString">ReadFileToString</span> ### <span id="ReadFileToString">ReadFileToString</span>
<p>读取文件内容并返回字符串</p> <p>读取文件内容并返回字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -350,7 +355,8 @@ func main() {
```go ```go
func ReadFileToString(path string) (string, error) func ReadFileToString(path string) (string, error)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -373,9 +379,8 @@ func main() {
} }
``` ```
### <span id="ReadFileByLine">ReadFileByLine</span> ### <span id="ReadFileByLine">ReadFileByLine</span>
<p>按行读取文件内容,返回字符串切片包含每一行</p> <p>按行读取文件内容,返回字符串切片包含每一行</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -383,7 +388,8 @@ func main() {
```go ```go
func ReadFileByLine(path string)([]string, error) func ReadFileByLine(path string)([]string, error)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -407,9 +413,8 @@ func main() {
} }
``` ```
### <span id="Zip">Zip</span> ### <span id="Zip">Zip</span>
<p>zip压缩文件, fpath参数可以是文件或目录</p> <p>zip压缩文件, fpath参数可以是文件或目录</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -417,7 +422,8 @@ func main() {
```go ```go
func Zip(fpath string, destPath string) error func Zip(fpath string, destPath string) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -435,10 +441,8 @@ func main() {
} }
``` ```
### <span id="UnZip">UnZip</span> ### <span id="UnZip">UnZip</span>
<p>zip解压缩文件并保存在目录中</p> <p>zip解压缩文件并保存在目录中</p>
<b>Signature:</b> <b>Signature:</b>
@@ -446,7 +450,8 @@ func main() {
```go ```go
func UnZip(zipFile string, destPath string) error func UnZip(zipFile string, destPath string) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -463,8 +468,3 @@ func main() {
} }
} }
``` ```

View File

@@ -1,15 +1,17 @@
# Formatter # Formatter
formatter contains some functions for data formatting. formatter contains some functions for data formatting.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go) - [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/formatter" "github.com/duke-git/lancet/v2/formatter"
@@ -19,15 +21,15 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [Comma](#Comma)
- [Comma](#Comma)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="Comma">Comma</span> ### <span id="Comma">Comma</span>
<p>Add comma to a number value by every 3 numbers from right to left. ahead by symbol char. if value is a invalid number string like "aa", return empty string.</p> <p>Add comma to a number value by every 3 numbers from right to left. ahead by symbol char. if value is a invalid number string like "aa", return empty string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -35,6 +37,7 @@ import (
```go ```go
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:</b>
```go ```go
@@ -46,7 +49,17 @@ import (
) )
func main() { func main() {
fmt.Println(formatter.Comma("12345", "")) // "12,345" result1 := formatter.Comma("123", "")
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" result2 := formatter.Comma("12345", "$")
result3 := formatter.Comma(1234567, "¥")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 123
// $12,345
// ¥1,234,567
} }
``` ```

View File

@@ -1,15 +1,17 @@
# Formatter # Formatter
formatter格式化器包含一些数据格式化处理方法。
formatter 格式化器包含一些数据格式化处理方法。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go) - [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.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/formatter" "github.com/duke-git/lancet/v2/formatter"
@@ -19,15 +21,15 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [Comma](#Comma)
- [Comma](#Comma)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="Comma">Comma</span> ### <span id="Comma">Comma</span>
<p>用逗号每隔3位分割数字/字符串支持前缀添加符号。参数value必须是数字或者可以转为数字的字符串, 否则返回空字符串</p> <p>用逗号每隔3位分割数字/字符串支持前缀添加符号。参数value必须是数字或者可以转为数字的字符串, 否则返回空字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -35,7 +37,8 @@ import (
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -46,7 +49,17 @@ import (
) )
func main() { func main() {
fmt.Println(formatter.Comma("12345", "")) // "12,345" result1 := formatter.Comma("123", "")
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" result2 := formatter.Comma("12345", "$")
result3 := formatter.Comma(1234567, "¥")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 123
// $12,345
// ¥1,234,567
} }
``` ```

View File

@@ -1,16 +1,18 @@
# Function # Function
Package function can control the flow of function execution and support part of functional programming. Package function can control the flow of function execution and support part of functional programming.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) - [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go) - [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/function" "github.com/duke-git/lancet/v2/function"
@@ -20,22 +22,22 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [After](#After)
- [Before](#Before) - [After](#After)
- [Curry](#Curry) - [Before](#Before)
- [Compose](#Compose) - [CurryFn](#CurryFn)
- [Debounced](#Debounced) - [Compose](#Compose)
- [Delay](#Delay) - [Debounced](#Debounced)
- [Pipeline](#Pipeline) - [Delay](#Delay)
- [Watcher](#Watcher) - [Pipeline](#Pipeline)
- [Watcher](#Watcher)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="After">After</span> ### <span id="After">After</span>
<p>Creates a function that invokes given func once it's called n or more times.</p> <p>Creates a function that invokes given func once it's called n or more times.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -43,6 +45,7 @@ import (
```go ```go
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:</b>
```go ```go
@@ -54,33 +57,18 @@ import (
) )
func main() { func main() {
arr := []string{"a", "b"} fn := function.After(2, func() {
f := function.After(len(arr), func(i int) int { fmt.Println("hello")
fmt.Println("last print") })
return i
})
type cb func(args ...any) []reflect.Value fn()
print := func(i int, s string, fn cb) { fn()
fmt.Printf("arr[%d] is %s \n", i, s)
fn(i)
}
fmt.Println("arr is", arr) // Output:
for i := 0; i < len(arr); i++ { // hello
print(i, arr[i], f)
}
//output:
// arr is [a b]
// arr[0] is a
// arr[1] is b
// last print
} }
``` ```
### <span id="Before">Before</span> ### <span id="Before">Before</span>
<p>creates a function that invokes func once it's called less than n times.</p> <p>creates a function that invokes func once it's called less than n times.</p>
@@ -90,6 +78,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -102,39 +91,32 @@ import (
) )
func main() { func main() {
arr := []string{"a", "b", "c", "d", "e"} fn := function.Before(2, func() {
f := function.Before(3, func(i int) int { fmt.Println("hello")
return i })
})
var res []int64 fn()
type cb func(args ...any) []reflect.Value fn()
appendStr := func(i int, s string, fn cb) { fn()
v := fn(i) fn()
res = append(res, v[0].Int())
}
for i := 0; i < len(arr); i++ { // Output:
appendStr(i, arr[i], f) // hello
} // hello
expected := []int64{0, 1, 2, 2, 2}
fmt.Println(res) // 0, 1, 2, 2, 2
} }
``` ```
### <span id="CurryFn">CurryFn</span>
<p>Make curry function.</p>
### <span id="Curry">Curry</span>
<p>Make a curry function.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
type Fn func(...any) any type CurryFn[T any] func(...T) T
func (f Fn) Curry(i any) func(...any) any func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -147,19 +129,23 @@ import (
func main() { func main() {
add := func(a, b int) int { add := func(a, b int) int {
return a + b return a + b
} }
var addCurry function.Fn = func(values ...any) any {
return add(values[0].(int), values[1].(int)) var addCurry function.CurryFn[int] = func(values ...int) int {
} return add(values[0], values[1])
add1 := addCurry.Curry(1) }
result := add1(2) add1 := addCurry.New(1)
fmt.Println(result) //3
result := add1(2)
fmt.Println(result)
// Output:
// 3
} }
``` ```
### <span id="Compose">Compose</span> ### <span id="Compose">Compose</span>
<p>Compose the function list from right to left, then return the composed function.</p> <p>Compose the function list from right to left, then return the composed function.</p>
@@ -167,8 +153,9 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func Compose(fnList ...func(...any) any) func(...any) any func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -180,22 +167,23 @@ import (
) )
func main() { func main() {
add1 := func(v ...any) any { toUpper := func(strs ...string) string {
return v[0].(int) + 1 return strings.ToUpper(strs[0])
} }
add2 := func(v ...any) any { toLower := func(strs ...string) string {
return v[0].(int) + 2 return strings.ToLower(strs[0])
} }
transform := function.Compose(toUpper, toLower)
add3 := function.Compose(add1, add2) result := transform("aBCde")
result := add3(1)
fmt.Println(result) //4 fmt.Println(result)
// Output:
// ABCDE
} }
``` ```
### <span id="Debounced">Debounced</span> ### <span id="Debounced">Debounced</span>
<p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p> <p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p>
@@ -205,6 +193,7 @@ func main() {
```go ```go
func Debounced(fn func(), duration time.Duration) func() func Debounced(fn func(), duration time.Duration) func()
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -217,27 +206,34 @@ import (
func main() { func main() {
count := 0 count := 0
add := func() {
count++
}
debouncedAdd := function.Debounced(add, 50*time.Microsecond) add := func() {
function.debouncedAdd() count++
function.debouncedAdd() }
function.debouncedAdd()
function.debouncedAdd()
time.Sleep(100 * time.Millisecond) debouncedAdd := function.Debounced(add, 50*time.Microsecond)
fmt.Println(count) //1
function.debouncedAdd() debouncedAdd()
time.Sleep(100 * time.Millisecond) debouncedAdd()
fmt.Println(count) //2 debouncedAdd()
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
// Output:
// 1
// 2
} }
``` ```
### <span id="Delay">Delay</span> ### <span id="Delay">Delay</span>
<p>Invoke function after delayed time.</p> <p>Invoke function after delayed time.</p>
@@ -247,6 +243,7 @@ func main() {
```go ```go
func Delay(delay time.Duration, fn any, args ...any) func Delay(delay time.Duration, fn any, args ...any)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -258,15 +255,17 @@ import (
) )
func main() { func main() {
var print = func(s string) { var print = func(s string) {
fmt.Println(count) //test delay fmt.Println(s)
} }
function.Delay(2*time.Second, print, "test delay")
function.Delay(2*time.Second, print, "hello")
// Output:
// hello
} }
``` ```
### <span id="Schedule">Schedule</span> ### <span id="Schedule">Schedule</span>
<p>Invoke function every duration time, until close the returned bool chan.</p> <p>Invoke function every duration time, until close the returned bool chan.</p>
@@ -276,6 +275,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -287,20 +287,24 @@ import (
) )
func main() { func main() {
var res []string count := 0
appendStr := func(s string) {
res = append(res, s)
}
stop := function.Schedule(1*time.Second, appendStr, "*") increase := func() {
time.Sleep(5 * time.Second) count++
close(stop) }
fmt.Println(res) //[* * * * *] stop := function.Schedule(2*time.Second, increase)
time.Sleep(2 * time.Second)
close(stop)
fmt.Println(count)
// Output:
// 2
} }
``` ```
### <span id="Pipeline">Pipeline</span> ### <span id="Pipeline">Pipeline</span>
<p>Pipeline takes a list of functions and returns a function whose param will be passed into <p>Pipeline takes a list of functions and returns a function whose param will be passed into
@@ -311,6 +315,7 @@ the functions one by one.</p>
```go ```go
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:</b>
```go ```go
@@ -323,22 +328,26 @@ import (
func main() { func main() {
addOne := func(x int) int { addOne := func(x int) int {
return x + 1 return x + 1
} }
double := func(x int) int { double := func(x int) int {
return 2 * x return 2 * x
} }
square := func(x int) int { square := func(x int) int {
return x * x return x * x
} }
f := Pipeline(addOne, double, square) fn := function.Pipeline(addOne, double, square)
fmt.Println(f(2)) //36 result := fn(2)
fmt.Println(result)
// Output:
// 36
} }
``` ```
### <span id="Watcher">Watcher</span> ### <span id="Watcher">Watcher</span>
<p>Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.</p> <p>Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.</p>
@@ -347,15 +356,17 @@ func main() {
```go ```go
type Watcher struct { type Watcher struct {
startTime int64 startTime int64
stopTime int64 stopTime int64
excuting bool excuting bool
} }
func NewWatcher() *Watcher
func (w *Watcher) Start() //start the watcher func (w *Watcher) Start() //start the watcher
func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Stop() //stop the watcher
func (w *Watcher) Reset() //reset the watcher 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:</b>
```go ```go
@@ -367,34 +378,28 @@ import (
) )
func main() { func main() {
w := &function.Watcher{} w := function.NewWatcher()
w.Start()
longRunningTask() w.Start()
fmt.Println(w.excuting) //true longRunningTask()
w.Stop() fmt.Println(w.excuting) //true
eapsedTime := w.GetElapsedTime().Milliseconds() w.Stop()
fmt.Println(eapsedTime)
w.Reset() eapsedTime := w.GetElapsedTime().Milliseconds()
fmt.Println(w.excuting) //false fmt.Println(eapsedTime)
fmt.Println(w.startTime) //0 w.Reset()
fmt.Println(w.stopTime) //0
} }
func longRunningTask() { func longRunningTask() {
var slice []int64 var slice []int64
for i := 0; i < 10000000; i++ { for i := 0; i < 10000000; i++ {
slice = append(slice, int64(i)) slice = append(slice, int64(i))
} }
} }
``` ```

View File

@@ -1,16 +1,18 @@
# Function # Function
function函数包控制函数执行流程包含部分函数式编程。
function 函数包控制函数执行流程,包含部分函数式编程。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) - [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go) - [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.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/function" "github.com/duke-git/lancet/v2/function"
@@ -20,22 +22,22 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [After](#After)
- [Before](#Before) - [After](#After)
- [Curry](#Curry) - [Before](#Before)
- [Compose](#Compose) - [CurryFn](#CurryFn)
- [Debounced](#Debounced) - [Compose](#Compose)
- [Delay](#Delay) - [Debounced](#Debounced)
- [Pipeline](#Pipeline) - [Delay](#Delay)
- [Watcher](#Watcher) - [Pipeline](#Pipeline)
- [Watcher](#Watcher)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="After">After</span> ### <span id="After">After</span>
<p>创建一个函数当他被调用n或更多次之后将马上触发fn</p> <p>创建一个函数当他被调用n或更多次之后将马上触发fn</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -43,7 +45,8 @@ import (
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -54,33 +57,18 @@ import (
) )
func main() { func main() {
arr := []string{"a", "b"} fn := function.After(2, func() {
f := function.After(len(arr), func(i int) int { fmt.Println("hello")
fmt.Println("last print") })
return i
})
type cb func(args ...any) []reflect.Value fn()
print := func(i int, s string, fn cb) { fn()
fmt.Printf("arr[%d] is %s \n", i, s)
fn(i)
}
fmt.Println("arr is", arr) // Output:
for i := 0; i < len(arr); i++ { // hello
print(i, arr[i], f)
}
//output:
// arr is [a b]
// arr[0] is a
// arr[1] is b
// last print
} }
``` ```
### <span id="Before">Before</span> ### <span id="Before">Before</span>
<p>创建一个函数调用次数不超过n次之后再调用这个函数将返回一次最后调用fn的结果</p> <p>创建一个函数调用次数不超过n次之后再调用这个函数将返回一次最后调用fn的结果</p>
@@ -90,7 +78,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -102,39 +91,33 @@ import (
) )
func main() { func main() {
arr := []string{"a", "b", "c", "d", "e"} fn := function.Before(2, func() {
f := function.Before(3, func(i int) int { fmt.Println("hello")
return i })
})
var res []int64 fn()
type cb func(args ...any) []reflect.Value fn()
appendStr := func(i int, s string, fn cb) { fn()
v := fn(i) fn()
res = append(res, v[0].Int())
}
for i := 0; i < len(arr); i++ { // Output:
appendStr(i, arr[i], f) // hello
} // hello
fmt.Println(res) // 0, 1, 2, 2, 2
} }
``` ```
### <span id="CurryFn">CurryFn</span>
<p>创建柯里化函数</p>
### <span id="Curry">Curry</span>
<p>创建一个柯里化的函数</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
type Fn func(...any) any type CurryFn[T any] func(...T) T
func (f Fn) Curry(i any) func(...any) any func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -145,30 +128,35 @@ import (
) )
func main() { func main() {
add := func(a, b int) int { add := func(a, b int) int {
return a + b return a + b
} }
var addCurry function.Fn = func(values ...any) any {
return add(values[0].(int), values[1].(int)) var addCurry function.CurryFn[int] = func(values ...int) int {
} return add(values[0], values[1])
add1 := addCurry.Curry(1) }
result := add1(2) add1 := addCurry.New(1)
fmt.Println(result) //3
result := add1(2)
fmt.Println(result)
// Output:
// 3
} }
``` ```
### <span id="Compose">Compose</span> ### <span id="Compose">Compose</span>
<p>从右至左组合函数列表fnList 返回组合后的函数</p> <p>从右至左组合函数列表fnList返回组合后的函数</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Compose(fnList ...func(...any) any) func(...any) any func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -179,32 +167,34 @@ import (
) )
func main() { func main() {
add1 := func(v ...any) any { toUpper := func(strs ...string) string {
return v[0].(int) + 1 return strings.ToUpper(strs[0])
} }
add2 := func(v ...any) any { toLower := func(strs ...string) string {
return v[0].(int) + 2 return strings.ToLower(strs[0])
} }
transform := function.Compose(toUpper, toLower)
add3 := function.Compose(add1, add2) result := transform("aBCde")
result := add3(1)
fmt.Println(result) //4 fmt.Println(result)
// Output:
// ABCDE
} }
``` ```
### <span id="Debounced">Debounced</span> ### <span id="Debounced">Debounced</span>
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p> <p>创建一个debounced函数该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Debounced(fn func(), duration time.Duration) func() func Debounced(fn func(), duration time.Duration) func()
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -215,28 +205,35 @@ import (
) )
func main() { func main() {
count := 0 count := 0
add := func() {
count++
}
debouncedAdd := function.Debounced(add, 50*time.Microsecond) add := func() {
function.debouncedAdd() count++
function.debouncedAdd() }
function.debouncedAdd()
function.debouncedAdd()
time.Sleep(100 * time.Millisecond) debouncedAdd := function.Debounced(add, 50*time.Microsecond)
fmt.Println(count) //1
function.debouncedAdd() debouncedAdd()
time.Sleep(100 * time.Millisecond) debouncedAdd()
fmt.Println(count) //2 debouncedAdd()
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
// Output:
// 1
// 2
} }
``` ```
### <span id="Delay">Delay</span> ### <span id="Delay">Delay</span>
<p>延迟delay时间后调用函数</p> <p>延迟delay时间后调用函数</p>
@@ -246,7 +243,8 @@ func main() {
```go ```go
func Delay(delay time.Duration, fn any, args ...any) func Delay(delay time.Duration, fn any, args ...any)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -257,15 +255,17 @@ import (
) )
func main() { func main() {
var print = func(s string) { var print = func(s string) {
fmt.Println(count) //test delay fmt.Println(s)
} }
function.Delay(2*time.Second, print, "test delay")
function.Delay(2*time.Second, print, "hello")
// Output:
// hello
} }
``` ```
### <span id="Schedule">Schedule</span> ### <span id="Schedule">Schedule</span>
<p>每次持续时间调用函数,直到关闭返回的 bool chan</p> <p>每次持续时间调用函数,直到关闭返回的 bool chan</p>
@@ -275,7 +275,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -286,21 +287,24 @@ import (
) )
func main() { func main() {
var res []string count := 0
appendStr := func(s string) {
res = append(res, s)
}
stop := function.Schedule(1*time.Second, appendStr, "*") increase := func() {
time.Sleep(5 * time.Second) count++
close(stop) }
fmt.Println(res) //[* * * * *] stop := function.Schedule(2*time.Second, increase)
time.Sleep(2 * time.Second)
close(stop)
fmt.Println(count)
// Output:
// 2
} }
``` ```
### <span id="Pipeline">Pipeline</span> ### <span id="Pipeline">Pipeline</span>
<p>执行函数pipeline.</p> <p>执行函数pipeline.</p>
@@ -310,7 +314,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -322,41 +327,46 @@ import (
func main() { func main() {
addOne := func(x int) int { addOne := func(x int) int {
return x + 1 return x + 1
} }
double := func(x int) int { double := func(x int) int {
return 2 * x return 2 * x
} }
square := func(x int) int { square := func(x int) int {
return x * x return x * x
} }
f := Pipeline(addOne, double, square) fn := function.Pipeline(addOne, double, square)
fmt.Println(f(2)) //36 result := fn(2)
fmt.Println(result)
// Output:
// 36
} }
``` ```
### <span id="Watcher">Watcher</span> ### <span id="Watcher">Watcher</span>
<p>Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 </p> <p>Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
type Watcher struct { type Watcher struct {
startTime int64 startTime int64
stopTime int64 stopTime int64
excuting bool excuting bool
} }
func NewWatcher() *Watcher
func (w *Watcher) Start() //start the watcher func (w *Watcher) Start() //start the watcher
func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Stop() //stop the watcher
func (w *Watcher) Reset() //reset the watcher 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>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -367,34 +377,29 @@ import (
) )
func main() { func main() {
w := &function.Watcher{} w := function.NewWatcher()
w.Start()
longRunningTask() w.Start()
fmt.Println(w.excuting) //true longRunningTask()
w.Stop() fmt.Println(w.excuting) //true
eapsedTime := w.GetElapsedTime().Milliseconds() w.Stop()
fmt.Println(eapsedTime)
w.Reset() eapsedTime := w.GetElapsedTime().Milliseconds()
fmt.Println(w.excuting) //false fmt.Println(eapsedTime)
w.Reset()
fmt.Println(w.startTime) //0
fmt.Println(w.stopTime) //0
} }
func longRunningTask() { func longRunningTask() {
var slice []int64 var slice []int64
for i := 0; i < 10000000; i++ { for i := 0; i < 10000000; i++ {
slice = append(slice, int64(i)) slice = append(slice, int64(i))
} }
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Maputil # Maputil
Package maputil includes some functions to manipulate map. Package maputil includes some functions to manipulate map.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go) - [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Example: ## Example:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/maputil" "github.com/duke-git/lancet/v2/maputil"
@@ -20,22 +21,22 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [ForEach](#ForEach)
- [Filter](#Filter) - [ForEach](#ForEach)
- [Intersect](#Intersect) - [Filter](#Filter)
- [Keys](#Keys) - [Intersect](#Intersect)
- [Merge](#Merge) - [Keys](#Keys)
- [Minus](#Minus) - [Merge](#Merge)
- [Values](#Values) - [Minus](#Minus)
- [IsDisjoint](#IsDisjoint) - [Values](#Values)
- [IsDisjoint](#IsDisjoint)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="ForEach">ForEach</span> ### <span id="ForEach">ForEach</span>
<p>Executes iteratee funcation for every key and value pair in map.</p> <p>Executes iteratee funcation for every key and value pair in map.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -43,6 +44,7 @@ import (
```go ```go
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:</b>
```go ```go
@@ -54,26 +56,28 @@ import (
) )
func main() { func main() {
m := map[string]int{ m := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
"d": 4, "d": 4,
} }
var sum int var sum int
maputil.ForEach(m, func(_ string, value int) { maputil.ForEach(m, func(_ string, value int) {
sum += value sum += value
}) })
fmt.Println(sum) // 10
fmt.Println(sum)
// Output:
// 10
} }
``` ```
### <span id="Filter">Filter</span> ### <span id="Filter">Filter</span>
<p>Iterates over map, return a new map contains all key and value pairs pass the predicate function.</p> <p>Iterates over map, return a new map contains all key and value pairs pass the predicate function.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -81,6 +85,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -92,29 +97,32 @@ import (
) )
func main() { func main() {
m := map[string]int{ m := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
"d": 4, "d": 4,
"e": 5, "e": 5,
} }
isEven := func(_ string, value int) bool { isEven := func(_ string, value int) bool {
return value%2 == 0 return value%2 == 0
} }
maputil.Filter(m, func(_ string, value int) { maputil.Filter(m, func(_ string, value int) {
sum += value sum += value
}) })
res := maputil.Filter(m, isEven)
fmt.Println(res) // map[string]int{"b": 2, "d": 4,} result := maputil.Filter(m, isEven)
fmt.Println(result)
// Output:
// map[b:2 d:4]
} }
``` ```
### <span id="Intersect">Intersect</span> ### <span id="Intersect">Intersect</span>
<p>Iterates over maps, return a new map of key and value pairs in all given maps.</p> <p>Iterates over maps, return a new map of key and value pairs in all given maps.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -122,6 +130,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -133,37 +142,42 @@ import (
) )
func main() { func main() {
m1 := map[string]int{ m1 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
} }
m2 := map[string]int{ m2 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 6, "c": 6,
"d": 7, "d": 7,
} }
m3 := map[string]int{ m3 := map[string]int{
"a": 1, "a": 1,
"b": 9, "b": 9,
"e": 9, "e": 9,
} }
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3} result1 := maputil.Intersect(m1)
result2 := maputil.Intersect(m1, m2)
result3 := maputil.Intersect(m1, m2, m3)
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2} fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1} // Output:
// map[a:1 b:2 c:3]
// map[a:1 b:2]
// map[a:1]
} }
``` ```
### <span id="Keys">Keys</span> ### <span id="Keys">Keys</span>
<p>Returns a slice of the map's keys.</p> <p>Returns a slice of the map's keys.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -171,6 +185,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -182,24 +197,26 @@ import (
) )
func main() { func main() {
m := map[int]string{ m := map[int]string{
1: "a", 1: "a",
2: "a", 2: "a",
3: "b", 3: "b",
4: "c", 4: "c",
5: "d", 5: "d",
} }
keys := maputil.Keys(m) keys := maputil.Keys(m)
sort.Ints(keys) sort.Ints(keys)
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
fmt.Println(keys)
// Output:
// [1 2 3 4 5]
} }
``` ```
### <span id="Merge">Merge</span> ### <span id="Merge">Merge</span>
<p>Merge maps, next key will overwrite previous key.</p> <p>Merge maps, next key will overwrite previous key.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -207,6 +224,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -218,22 +236,26 @@ import (
) )
func main() { func main() {
m1 := map[int]string{ m1 := map[int]string{
1: "a", 1: "a",
2: "b", 2: "b",
} }
m2 := map[int]string{ m2 := map[int]string{
1: "1", 1: "1",
3: "2", 3: "2",
} }
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
result := maputil.Merge(m1, m2)
fmt.Println(result)
// Output:
// map[1:c 2:b 3:d]
} }
``` ```
### <span id="Minus">Minus</span> ### <span id="Minus">Minus</span>
<p>Creates an map of whose key in mapA but not in mapB.</p> <p>Creates an map of whose key in mapA but not in mapB.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -241,6 +263,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -252,25 +275,29 @@ import (
) )
func main() { func main() {
m1 := map[string]int{ m1 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
} }
m2 := map[string]int{ m2 := map[string]int{
"a": 11, "a": 11,
"b": 22, "b": 22,
"d": 33, "d": 33,
} }
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3} result := maputil.Minus(m1, m2)
fmt.Println(result)
// Output:
// map[c:3]
} }
``` ```
### <span id="Values">Values</span> ### <span id="Values">Values</span>
<p>Returns a slice of the map's values.</p> <p>Returns a slice of the map's values.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -278,6 +305,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -289,22 +317,26 @@ import (
) )
func main() { func main() {
m := map[int]string{ m := map[int]string{
1: "a", 1: "a",
2: "a", 2: "a",
3: "b", 3: "b",
4: "c", 4: "c",
5: "d", 5: "d",
} }
values := maputil.Values(m) values := maputil.Values(m)
sort.Strings(values) sort.Strings(values)
fmt.Println(values) // []string{"a", "a", "b", "c", "d"} fmt.Println(values)
// Output:
// [a a b c d]
} }
``` ```
### <span id="IsDisjoint">IsDisjoint</span> ### <span id="IsDisjoint">IsDisjoint</span>
<p>Checks two maps are disjoint if they have no keys in common</p> <p>Checks two maps are disjoint if they have no keys in common</p>
<b>Signature:</b> <b>Signature:</b>
@@ -312,6 +344,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -323,30 +356,28 @@ import (
) )
func main() { func main() {
m1 := map[int]string{ m1 := map[string]int{
1: "a", "a": 1,
2: "a", "b": 2,
3: "b", "c": 3,
4: "c", }
5: "d",
}
m2 := map[int]string{ m2 := map[string]int{
1: "a", "d": 22,
2: "a", }
3: "b",
4: "c",
5: "d",
}
m3 := map[int]string{ m3 := map[string]int{
6: "a", "a": 22,
} }
ok := maputil.IsDisjoint(m2, m1) result1 := maputil.IsDisjoint(m1, m2)
fmt.Println(ok) // false result2 := maputil.IsDisjoint(m1, m3)
ok = maputil.IsDisjoint(m2, m3) fmt.Println(result1)
fmt.Println(ok) // true fmt.Println(result2)
// Output:
// true
// false
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Maputil # Maputil
maputil包包括一些操作map的函数。
maputil 包包括一些操作 map 的函数。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go) - [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.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/maputil" "github.com/duke-git/lancet/v2/maputil"
@@ -20,22 +21,22 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录: ## 目录:
- [ForEach](#ForEach)
- [Filter](#Filter) - [ForEach](#ForEach)
- [Intersect](#Intersect) - [Filter](#Filter)
- [Keys](#Keys) - [Intersect](#Intersect)
- [Merge](#Merge) - [Keys](#Keys)
- [Minus](#Minus) - [Merge](#Merge)
- [Values](#Values) - [Minus](#Minus)
- [IsDisjoint](#IsDisjoint) - [Values](#Values)
- [IsDisjoint](#IsDisjoint)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## API文档: ## API 文档:
### <span id="ForEach">ForEach</span> ### <span id="ForEach">ForEach</span>
<p>对map中的每对key和value执行iteratee函数</p> <p>对map中的每对key和value执行iteratee函数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -43,7 +44,8 @@ import (
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -54,26 +56,28 @@ import (
) )
func main() { func main() {
m := map[string]int{ m := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
"d": 4, "d": 4,
} }
var sum int var sum int
maputil.ForEach(m, func(_ string, value int) { maputil.ForEach(m, func(_ string, value int) {
sum += value sum += value
}) })
fmt.Println(sum) // 10
fmt.Println(sum)
// Output:
// 10
} }
``` ```
### <span id="Filter">Filter</span> ### <span id="Filter">Filter</span>
<p>迭代map中的每对key和value, 返回符合predicate函数的key, value</p> <p>迭代map中的每对key和value, 返回符合predicate函数的key, value</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -81,7 +85,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -92,29 +97,32 @@ import (
) )
func main() { func main() {
m := map[string]int{ m := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
"d": 4, "d": 4,
"e": 5, "e": 5,
} }
isEven := func(_ string, value int) bool { isEven := func(_ string, value int) bool {
return value%2 == 0 return value%2 == 0
} }
maputil.Filter(m, func(_ string, value int) { maputil.Filter(m, func(_ string, value int) {
sum += value sum += value
}) })
res := maputil.Filter(m, isEven)
fmt.Println(res) // map[string]int{"b": 2, "d": 4,} result := Filter(m, isEven)
fmt.Println(result)
// Output:
// map[b:2 d:4]
} }
``` ```
### <span id="Intersect">Intersect</span> ### <span id="Intersect">Intersect</span>
<p>多个map的交集操作</p> <p>多个map的交集操作</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -122,7 +130,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -133,37 +142,42 @@ import (
) )
func main() { func main() {
m1 := map[string]int{ m1 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
} }
m2 := map[string]int{ m2 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 6, "c": 6,
"d": 7, "d": 7,
} }
m3 := map[string]int{ m3 := map[string]int{
"a": 1, "a": 1,
"b": 9, "b": 9,
"e": 9, "e": 9,
} }
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3} result1 := maputil.Intersect(m1)
result2 := maputil.Intersect(m1, m2)
result3 := maputil.Intersect(m1, m2, m3)
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2} fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1} // Output:
// map[a:1 b:2 c:3]
// map[a:1 b:2]
// map[a:1]
} }
``` ```
### <span id="Keys">Keys</span> ### <span id="Keys">Keys</span>
<p>返回map中所有key的切片</p> <p>返回map中所有key的切片</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -171,7 +185,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -182,24 +197,26 @@ import (
) )
func main() { func main() {
m := map[int]string{ m := map[int]string{
1: "a", 1: "a",
2: "a", 2: "a",
3: "b", 3: "b",
4: "c", 4: "c",
5: "d", 5: "d",
} }
keys := maputil.Keys(m) keys := maputil.Keys(m)
sort.Ints(keys) sort.Ints(keys)
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
fmt.Println(keys)
// Output:
// [1 2 3 4 5]
} }
``` ```
### <span id="Merge">Merge</span> ### <span id="Merge">Merge</span>
<p>合并多个maps, 相同的key会被后来的key覆盖</p> <p>合并多个maps, 相同的key会被后来的key覆盖</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -207,7 +224,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -218,21 +236,26 @@ import (
) )
func main() { func main() {
m1 := map[int]string{ m1 := map[int]string{
1: "a", 1: "a",
2: "b", 2: "b",
} }
m2 := map[int]string{ m2 := map[int]string{
1: "1", 1: "1",
3: "2", 3: "2",
} }
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
result := maputil.Merge(m1, m2)
fmt.Println(result)
// Output:
// map[1:c 2:b 3:d]
} }
``` ```
### <span id="Minus">Minus</span> ### <span id="Minus">Minus</span>
<p>返回一个map其中的key存在于mapA不存在于mapB.</p> <p>返回一个map其中的key存在于mapA不存在于mapB.</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -240,7 +263,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -251,25 +275,29 @@ import (
) )
func main() { func main() {
m1 := map[string]int{ m1 := map[string]int{
"a": 1, "a": 1,
"b": 2, "b": 2,
"c": 3, "c": 3,
} }
m2 := map[string]int{ m2 := map[string]int{
"a": 11, "a": 11,
"b": 22, "b": 22,
"d": 33, "d": 33,
} }
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3} result := maputil.Minus(m1, m2)
fmt.Println(result)
// Output:
// map[c:3]
} }
``` ```
### <span id="Values">Values</span> ### <span id="Values">Values</span>
<p>返回map中所有value的切片</p> <p>返回map中所有value的切片</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -277,7 +305,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -288,23 +317,24 @@ import (
) )
func main() { func main() {
m := map[int]string{ m := map[int]string{
1: "a", 1: "a",
2: "a", 2: "a",
3: "b", 3: "b",
4: "c", 4: "c",
5: "d", 5: "d",
} }
values := maputil.Values(m) values := maputil.Values(m)
sort.Strings(values) sort.Strings(values)
fmt.Println(values) // []string{"a", "a", "b", "c", "d"} // Output:
// [a a b c d]
} }
``` ```
### <span id="IsDisjoint">IsDisjoint</span> ### <span id="IsDisjoint">IsDisjoint</span>
<p>验证两个map是否具有不同的key</p> <p>验证两个map是否具有不同的key</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -312,7 +342,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -323,30 +354,28 @@ import (
) )
func main() { func main() {
m1 := map[int]string{ m1 := map[string]int{
1: "a", "a": 1,
2: "a", "b": 2,
3: "b", "c": 3,
4: "c", }
5: "d",
}
m2 := map[int]string{ m2 := map[string]int{
1: "a", "d": 22,
2: "a", }
3: "b",
4: "c",
5: "d",
}
m3 := map[int]string{ m3 := map[string]int{
6: "a", "a": 22,
} }
ok := maputil.IsDisjoint(m2, m1) result1 := maputil.IsDisjoint(m1, m2)
fmt.Println(ok) // false result2 := maputil.IsDisjoint(m1, m3)
ok = maputil.IsDisjoint(m2, m3) fmt.Println(result1)
fmt.Println(ok) // true fmt.Println(result2)
// Output:
// true
// false
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Mathutil # Mathutil
Package mathutil implements some functions for math calculation. Package mathutil implements some functions for math calculation.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) - [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Example: ## Example:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/mathutil" "github.com/duke-git/lancet/v2/mathutil"
@@ -20,26 +21,26 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [Average](#Average)
- [Exponent](#Exponent) - [Average](#Average)
- [Fibonacci](#Fibonacci) - [Exponent](#Exponent)
- [Factorial](#Factorial) - [Fibonacci](#Fibonacci)
- [Max](#Max) - [Factorial](#Factorial)
- [MaxBy](#MaxBy) - [Max](#Max)
- [Min](#Min) - [MaxBy](#MaxBy)
- [MinBy](#MaxBy) - [Min](#Min)
- [Percent](#Percent) - [MinBy](#MaxBy)
- [RoundToFloat](#RoundToFloat) - [Percent](#Percent)
- [RoundToString](#RoundToString) - [RoundToFloat](#RoundToFloat)
- [TruncRound](#TruncRound) - [RoundToString](#RoundToString)
- [TruncRound](#TruncRound)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="Average">Average</span> ### <span id="Average">Average</span>
<p>Return average value of numbers. Maybe call RoundToFloat to round result.</p> <p>Return average value of numbers. Maybe call RoundToFloat to round result.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -47,6 +48,7 @@ import (
```go ```go
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:</b>
```go ```go
@@ -58,16 +60,22 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Average(0, 0)) //0 result1 := mathutil.Average(1, 2)
fmt.Println(mathutil.Average(1, 1)) //1
avg := mathutil.Average(1.2, 1.4) //1.2999999998 avg := mathutil.Average(1.2, 1.4)
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3 result2 := mathutil.RoundToFloat(avg, 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.3
} }
``` ```
### <span id="Exponent">Exponent</span> ### <span id="Exponent">Exponent</span>
<p>Calculate x to the nth power.</p> <p>Calculate x to the nth power.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -75,6 +83,7 @@ func main() {
```go ```go
func Exponent(x, n int64) int64 func Exponent(x, n int64) int64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -86,15 +95,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Exponent(10, 0)) //1 result1 := mathutil.Exponent(10, 0)
fmt.Println(mathutil.Exponent(10, 1)) //10 result2 := mathutil.Exponent(10, 1)
fmt.Println(mathutil.Exponent(10, 2)) //100 result3 := mathutil.Exponent(10, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 10
// 100
} }
``` ```
### <span id="Fibonacci">Fibonacci</span> ### <span id="Fibonacci">Fibonacci</span>
<p>Calculate the nth number of fibonacci sequence.</p> <p>Calculate the nth number of fibonacci sequence.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -102,6 +119,7 @@ func main() {
```go ```go
func Fibonacci(first, second, n int) int func Fibonacci(first, second, n int) int
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -113,17 +131,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1 result1 := mathutil.Fibonacci(1, 1, 1)
fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1 result2 := mathutil.Fibonacci(1, 1, 2)
fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2 result3 := mathutil.Fibonacci(1, 1, 5)
fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3
fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 1
// 5
} }
``` ```
### <span id="Factorial">Factorial</span> ### <span id="Factorial">Factorial</span>
<p>Calculate the factorial of x.</p> <p>Calculate the factorial of x.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -131,6 +155,7 @@ func main() {
```go ```go
func Factorial(x uint) uint func Factorial(x uint) uint
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -142,16 +167,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Factorial(0)) //1 result1 := mathutil.Factorial(1)
fmt.Println(mathutil.Factorial(1)) //1 result2 := mathutil.Factorial(2)
fmt.Println(mathutil.Factorial(2)) //2 result3 := mathutil.Factorial(3)
fmt.Println(mathutil.Factorial(3)) //6
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 2
// 6
} }
``` ```
### <span id="Max">Max</span> ### <span id="Max">Max</span>
<p>Return max value of numbers.</p> <p>Return max value of numbers.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -159,6 +191,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -170,16 +203,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Max(0, 0)) //0 result1 := mathutil.Max(1, 2, 3)
fmt.Println(mathutil.Max(1, 2, 3)) //3 result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4)
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 3
// 1.4
} }
``` ```
### <span id="MaxBy">MaxBy</span> ### <span id="MaxBy">MaxBy</span>
<p>Return the maximum value of a slice using the given comparator function.</p> <p>Return the maximum value of a slice using the given comparator function.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -187,6 +224,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -198,26 +236,31 @@ import (
) )
func main() { func main() {
res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { result1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res1) //abc
res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { result2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res2) //abd
res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res3) //“”
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// abc
// abd
//
} }
``` ```
### <span id="Min">Min</span> ### <span id="Min">Min</span>
<p>Return the minimum value of numbers.</p> <p>Return the minimum value of numbers.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -225,6 +268,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -236,15 +280,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Min(0, 0)) //0 result1 := mathutil.Min(1, 2, 3)
fmt.Println(mathutil.Min(1, 2, 3)) //1 result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4)
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.1
} }
``` ```
### <span id="MinBy">MinBy</span> ### <span id="MinBy">MinBy</span>
<p>Return the minimum value of a slice using the given comparator function.</p> <p>Return the minimum value of a slice using the given comparator function.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -252,6 +301,7 @@ func main() {
```go ```go
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:</b>
```go ```go
@@ -263,27 +313,31 @@ import (
) )
func main() { func main() {
res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { result1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res1) //a
res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { result2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res2) //ab
res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res3) //“”
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// a
// ab
//
} }
``` ```
### <span id="Percent">Percent</span> ### <span id="Percent">Percent</span>
<p>calculate the percentage of val to total, retain n decimal places.</p> <p>calculate the percentage of val to total, retain n decimal places.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -291,6 +345,7 @@ func main() {
```go ```go
func Percent(val, total float64, n int) float64 func Percent(val, total float64, n int) float64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -302,14 +357,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 result1 := mathutil.Percent(1, 2, 2)
fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 result2 := mathutil.Percent(0.1, 0.3, 2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0.5
// 0.33
} }
``` ```
### <span id="RoundToFloat">RoundToFloat</span> ### <span id="RoundToFloat">RoundToFloat</span>
<p>Round float up to n decimal places.</p> <p>Round float up to n decimal places.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -317,6 +378,7 @@ func main() {
```go ```go
func RoundToFloat(x float64, n int) float64 func RoundToFloat(x float64, n int) float64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -328,18 +390,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.RoundToFloat(0, 0)) //0 result1 := mathutil.RoundToFloat(0.124, 2)
fmt.Println(mathutil.RoundToFloat(0, 1)) //0 result2 := mathutil.RoundToFloat(0.125, 2)
fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12 result3 := mathutil.RoundToFloat(0.125, 3)
fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13
fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
} }
``` ```
### <span id="RoundToString">RoundToString</span> ### <span id="RoundToString">RoundToString</span>
<p>Round float up to n decimal places. will return string.</p> <p>Round float up to n decimal places. will return string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -347,6 +414,7 @@ func main() {
```go ```go
func RoundToString(x float64, n int) string func RoundToString(x float64, n int) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -358,17 +426,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.RoundToString(0, 0)) //"0" result1 := mathutil.RoundToString(0.124, 2)
fmt.Println(mathutil.RoundToString(0, 1)) //"0.0: result2 := mathutil.RoundToString(0.125, 2)
fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12" result3 := mathutil.RoundToString(0.125, 3)
fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13"
fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125" fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
} }
``` ```
### <span id="TruncRound">TruncRound</span> ### <span id="TruncRound">TruncRound</span>
<p>Round float off n decimal places.</p> <p>Round float off n decimal places.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -376,6 +450,7 @@ func main() {
```go ```go
func TruncRound(x float64, n int) float64 func TruncRound(x float64, n int) float64
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -387,13 +462,17 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.TruncRound(0, 0)) //0 result1 := mathutil.TruncRound(0.124, 2)
fmt.Println(mathutil.TruncRound(0, 1)) //0 result2 := mathutil.TruncRound(0.125, 2)
fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12 result3 := mathutil.TruncRound(0.125, 3)
fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.12
// 0.125
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Mathutil # Mathutil
mathutil包实现了一些数学计算的函数.
mathutil 包实现了一些数学计算的函数.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) - [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.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/mathutil" "github.com/duke-git/lancet/v2/mathutil"
@@ -20,25 +21,26 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [Average](#Average)
- [Exponent](#Exponent) - [Average](#Average)
- [Fibonacci](#Fibonacci) - [Exponent](#Exponent)
- [Factorial](#Factorial) - [Fibonacci](#Fibonacci)
- [Max](#Max) - [Factorial](#Factorial)
- [MaxBy](#MaxBy) - [Max](#Max)
- [Min](#Min) - [MaxBy](#MaxBy)
- [MinBy](#MaxBy) - [Min](#Min)
- [Percent](#Percent) - [MinBy](#MaxBy)
- [RoundToFloat](#RoundToFloat) - [Percent](#Percent)
- [RoundToString](#RoundToString) - [RoundToFloat](#RoundToFloat)
- [TruncRound](#TruncRound) - [RoundToString](#RoundToString)
- [TruncRound](#TruncRound)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="Average">Average</span> ### <span id="Average">Average</span>
<p>计算平均数. 可能需要对结果调用RoundToFloat方法四舍五入</p> <p>计算平均数. 可能需要对结果调用RoundToFloat方法四舍五入</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -46,7 +48,8 @@ import (
```go ```go
func Average[T constraints.Integer | constraints.Float](numbers ...T) T func Average[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -57,15 +60,22 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Average(0, 0)) //0 result1 := mathutil.Average(1, 2)
fmt.Println(mathutil.Average(1, 1)) //1
avg := mathutil.Average(1.2, 1.4) //1.2999999998 avg := mathutil.Average(1.2, 1.4)
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3 result2 := mathutil.RoundToFloat(avg, 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.3
} }
``` ```
### <span id="Exponent">Exponent</span> ### <span id="Exponent">Exponent</span>
<p>指数计算x的n次方</p> <p>指数计算x的n次方</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -73,7 +83,8 @@ func main() {
```go ```go
func Exponent(x, n int64) int64 func Exponent(x, n int64) int64
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -84,15 +95,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Exponent(10, 0)) //1 result1 := mathutil.Exponent(10, 0)
fmt.Println(mathutil.Exponent(10, 1)) //10 result2 := mathutil.Exponent(10, 1)
fmt.Println(mathutil.Exponent(10, 2)) //100 result3 := mathutil.Exponent(10, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 10
// 100
} }
``` ```
### <span id="Fibonacci">Fibonacci</span> ### <span id="Fibonacci">Fibonacci</span>
<p>计算斐波那契数列的第n个数</p> <p>计算斐波那契数列的第n个数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -100,7 +119,8 @@ func main() {
```go ```go
func Fibonacci(first, second, n int) int func Fibonacci(first, second, n int) int
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -111,17 +131,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1 result1 := mathutil.Fibonacci(1, 1, 1)
fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1 result2 := mathutil.Fibonacci(1, 1, 2)
fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2 result3 := mathutil.Fibonacci(1, 1, 5)
fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3
fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 1
// 5
} }
``` ```
### <span id="Factorial">Factorial</span> ### <span id="Factorial">Factorial</span>
<p>计算阶乘</p> <p>计算阶乘</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -129,7 +155,8 @@ func main() {
```go ```go
func Factorial(x uint) uint func Factorial(x uint) uint
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -140,15 +167,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Factorial(0)) //1 result1 := mathutil.Factorial(1)
fmt.Println(mathutil.Factorial(1)) //1 result2 := mathutil.Factorial(2)
fmt.Println(mathutil.Factorial(2)) //2 result3 := mathutil.Factorial(3)
fmt.Println(mathutil.Factorial(3)) //6
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 1
// 2
// 6
} }
``` ```
### <span id="Max">Max</span> ### <span id="Max">Max</span>
<p>返回参数中的最大数</p> <p>返回参数中的最大数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -156,7 +191,8 @@ func main() {
```go ```go
func Max[T constraints.Integer | constraints.Float](numbers ...T) T func Max[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -167,15 +203,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Max(0, 0)) //0 result1 := mathutil.Max(1, 2, 3)
fmt.Println(mathutil.Max(1, 2, 3)) //3 result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4)
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 3
// 1.4
} }
``` ```
### <span id="MaxBy">MaxBy</span> ### <span id="MaxBy">MaxBy</span>
<p>使用给定的比较器函数返回切片的最大值</p> <p>使用给定的比较器函数返回切片的最大值</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -183,7 +224,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -194,27 +236,31 @@ import (
) )
func main() { func main() {
res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { result1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res1) //abc
res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { result2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res2) //abd
res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2) return len(v1) > len(v2)
}) })
fmt.Println(res3) //“”
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// abc
// abd
//
} }
``` ```
### <span id="Min">Min</span> ### <span id="Min">Min</span>
<p>返回参数中的最小数</p> <p>返回参数中的最小数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -222,7 +268,8 @@ func main() {
```go ```go
func Min[T constraints.Integer | constraints.Float](numbers ...T) T func Min[T constraints.Integer | constraints.Float](numbers ...T) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -233,15 +280,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Min(0, 0)) //0 result1 := mathutil.Min(1, 2, 3)
fmt.Println(mathutil.Min(1, 2, 3)) //1 result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4)
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 1.1
} }
``` ```
### <span id="MinBy">MinBy</span> ### <span id="MinBy">MinBy</span>
<p>使用给定的比较器函数返回切片的最小值</p> <p>使用给定的比较器函数返回切片的最小值</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -249,7 +301,8 @@ func main() {
```go ```go
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>示例:</b>
```go ```go
package main package main
@@ -260,27 +313,31 @@ import (
) )
func main() { func main() {
res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { result1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res1) //a
res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { result2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res2) //ab
res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2) return len(v1) < len(v2)
}) })
fmt.Println(res3) //“”
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// a
// ab
//
} }
``` ```
### <span id="Percent">Percent</span> ### <span id="Percent">Percent</span>
<p>计算百分比保留n位小数</p> <p>计算百分比保留n位小数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -288,7 +345,8 @@ func main() {
```go ```go
func Percent(val, total float64, n int) float64 func Percent(val, total float64, n int) float64
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -299,14 +357,20 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 result1 := mathutil.Percent(1, 2, 2)
fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 result2 := mathutil.Percent(0.1, 0.3, 2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0.5
// 0.33
} }
``` ```
### <span id="RoundToFloat">RoundToFloat</span> ### <span id="RoundToFloat">RoundToFloat</span>
<p>四舍五入保留n位小数</p> <p>四舍五入保留n位小数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -314,7 +378,8 @@ func main() {
```go ```go
func RoundToFloat(x float64, n int) float64 func RoundToFloat(x float64, n int) float64
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -325,18 +390,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.RoundToFloat(0, 0)) //0 result1 := mathutil.RoundToFloat(0.124, 2)
fmt.Println(mathutil.RoundToFloat(0, 1)) //0 result2 := mathutil.RoundToFloat(0.125, 2)
fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12 result3 := mathutil.RoundToFloat(0.125, 3)
fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13
fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
} }
``` ```
### <span id="RoundToString">RoundToString</span> ### <span id="RoundToString">RoundToString</span>
<p>四舍五入保留n位小数返回字符串</p> <p>四舍五入保留n位小数返回字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -344,7 +414,8 @@ func main() {
```go ```go
func RoundToString(x float64, n int) string func RoundToString(x float64, n int) string
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -355,17 +426,23 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.RoundToString(0, 0)) //"0" result1 := mathutil.RoundToString(0.124, 2)
fmt.Println(mathutil.RoundToString(0, 1)) //"0.0: result2 := mathutil.RoundToString(0.125, 2)
fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12" result3 := mathutil.RoundToString(0.125, 3)
fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13"
fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125" fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.13
// 0.125
} }
``` ```
### <span id="TruncRound">TruncRound</span> ### <span id="TruncRound">TruncRound</span>
<p>截短n位小数不进行四舍五入</p> <p>截短n位小数不进行四舍五入</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -373,7 +450,8 @@ func main() {
```go ```go
func TruncRound(x float64, n int) float64 func TruncRound(x float64, n int) float64
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -384,13 +462,17 @@ import (
) )
func main() { func main() {
fmt.Println(mathutil.TruncRound(0, 0)) //0 result1 := mathutil.TruncRound(0.124, 2)
fmt.Println(mathutil.TruncRound(0, 1)) //0 result2 := mathutil.TruncRound(0.125, 2)
fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12 result3 := mathutil.TruncRound(0.125, 3)
fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125 fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// 0.12
// 0.12
// 0.125
} }
``` ```

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -56,8 +56,8 @@ import (
) )
func main() { func main() {
randBytes := random.RandBytes(4) randBytes := random.RandBytes(4)
fmt.Println(randBytes) fmt.Println(randBytes)
} }
``` ```
@@ -82,8 +82,8 @@ import (
) )
func main() { func main() {
rInt := random.RandInt(1, 10) rInt := random.RandInt(1, 10)
fmt.Println(rInt) fmt.Println(rInt)
} }
``` ```
@@ -108,8 +108,8 @@ import (
) )
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze fmt.Println(randStr) //pGWsze
} }
``` ```
@@ -134,8 +134,8 @@ import (
) )
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF fmt.Println(randStr) //PACWGF
} }
``` ```
@@ -160,8 +160,8 @@ import (
) )
func main() { func main() {
randStr := random.RandLower(6) randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew fmt.Println(randStr) //siqbew
} }
``` ```
@@ -186,8 +186,8 @@ import (
) )
func main() { func main() {
randStr := random.RandNumeral(6) randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172 fmt.Println(randStr) //035172
} }
``` ```
@@ -212,8 +212,8 @@ import (
) )
func main() { func main() {
randStr := random.RandNumeralOrLetter(6) randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ fmt.Println(randStr) //0aW7cQ
} }
``` ```
@@ -238,10 +238,10 @@ import (
) )
func main() { func main() {
uuid, err := random.UUIdV4() uuid, err := random.UUIdV4()
if err != nil { if err != nil {
return return
} }
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -45,7 +45,7 @@ import (
func RandBytes(length int) []byte func RandBytes(length int) []byte
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -56,8 +56,8 @@ import (
) )
func main() { func main() {
randBytes := random.RandBytes(4) randBytes := random.RandBytes(4)
fmt.Println(randBytes) fmt.Println(randBytes)
} }
``` ```
@@ -71,7 +71,7 @@ func main() {
func RandInt(min, max int) int func RandInt(min, max int) int
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -82,8 +82,8 @@ import (
) )
func main() { func main() {
rInt := random.RandInt(1, 10) rInt := random.RandInt(1, 10)
fmt.Println(rInt) fmt.Println(rInt)
} }
``` ```
@@ -97,7 +97,7 @@ func main() {
func RandString(length int) string func RandString(length int) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -108,8 +108,8 @@ import (
) )
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) //pGWsze fmt.Println(randStr) //pGWsze
} }
``` ```
@@ -123,7 +123,7 @@ func main() {
func RandUpper(length int) string func RandUpper(length int) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -134,8 +134,8 @@ import (
) )
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF fmt.Println(randStr) //PACWGF
} }
``` ```
@@ -149,7 +149,7 @@ func main() {
func RandLower(length int) string func RandLower(length int) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -160,8 +160,8 @@ import (
) )
func main() { func main() {
randStr := random.RandLower(6) randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew fmt.Println(randStr) //siqbew
} }
``` ```
@@ -175,7 +175,7 @@ func main() {
func RandNumeral(length int) string func RandNumeral(length int) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -186,8 +186,8 @@ import (
) )
func main() { func main() {
randStr := random.RandNumeral(6) randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172 fmt.Println(randStr) //035172
} }
``` ```
@@ -201,7 +201,7 @@ func main() {
func RandNumeralOrLetter(length int) string func RandNumeralOrLetter(length int) string
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -212,8 +212,8 @@ import (
) )
func main() { func main() {
randStr := random.RandNumeralOrLetter(6) randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ fmt.Println(randStr) //0aW7cQ
} }
``` ```
@@ -227,7 +227,7 @@ func main() {
func UUIdV4() (string, error) func UUIdV4() (string, error)
``` ```
<b>例:</b> <b>例:</b>
```go ```go
package main package main
@@ -238,10 +238,10 @@ import (
) )
func main() { func main() {
uuid, err := random.UUIdV4() uuid, err := random.UUIdV4()
if err != nil { if err != nil {
return return
} }
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Retry # Retry
Package retry is for executing a function repeatedly until it was successful or canceled by the context. Package retry is for executing a function repeatedly until it was successful or canceled by the context.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go) - [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/retry" "github.com/duke-git/lancet/v2/retry"
@@ -20,18 +21,19 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [Context](#Context)
- [Retry](#Retry) - [Context](#Context)
- [RetryFunc](#RetryFunc) - [Retry](#Retry)
- [RetryDuration](#RetryDuration) - [RetryFunc](#RetryFunc)
- [RetryTimes](#RetryTimes) - [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="Context">Context</span> ### <span id="Context">Context</span>
<p>Set retry context config, can cancel the retry with context.</p> <p>Set retry context config, can cancel the retry with context.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -39,43 +41,46 @@ import (
```go ```go
func Context(ctx context.Context) func Context(ctx context.Context)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/duke-git/lancet/v2/retry" "github.com/duke-git/lancet/v2/retry"
"time" "time"
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(context.TODO())
var number int
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, number := 0
retry.RetryDuration(time.Microsecond*50), increaseNumber := func() error {
retry.Context(ctx), number++
) if number > 3 {
cancel()
}
return errors.New("error occurs")
}
if err != nil { duration := retry.RetryDuration(time.Microsecond*50)
fmt.Println(err) //retry is cancelled
} retry.Retry(increaseNumber,
duration,
retry.Context(ctx),
)
fmt.Println(number)
// Output:
// 4
} }
``` ```
### <span id="RetryFunc">RetryFunc</span> ### <span id="RetryFunc">RetryFunc</span>
<p>Function that retry executes.</p> <p>Function that retry executes.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -83,6 +88,7 @@ func main() {
```go ```go
type RetryFunc func() error type RetryFunc func() error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -96,28 +102,31 @@ import (
) )
func main() { func main() {
var number int number := 0
var increaseNumber retry.RetryFunc = func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
increaseNumber := func() error { duration := retry.RetryDuration(time.Microsecond*50)
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```
### <span id="RetryTimes">RetryTimes</span> ### <span id="RetryTimes">RetryTimes</span>
<p>Set times of retry. Default times is 5.</p> <p>Set times of retry. Default times is 5.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -125,6 +134,7 @@ func main() {
```go ```go
func RetryTimes(n uint) func RetryTimes(n uint)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -138,26 +148,28 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryTimes(2)) err := retry.Retry(increaseNumber, retry.RetryTimes(2))
if err != nil { if err != nil {
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1 fmt.Println(err)
} }
// Output:
// function main.main.func1 run failed after 2 times retry
} }
``` ```
### <span id="RetryDuration">RetryDuration</span> ### <span id="RetryDuration">RetryDuration</span>
<p>Set duration of retries. Default duration is 3 second.</p> <p>Set duration of retries. Default duration is 3 second.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -165,6 +177,7 @@ func main() {
```go ```go
func RetryDuration(d time.Duration) func RetryDuration(d time.Duration)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -178,26 +191,31 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```
### <span id="Retry">Retry</span> ### <span id="Retry">Retry</span>
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p> <p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -205,6 +223,7 @@ func main() {
```go ```go
func Retry(retryFunc RetryFunc, opts ...Option) error func Retry(retryFunc RetryFunc, opts ...Option) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -218,20 +237,25 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```

View File

@@ -1,16 +1,17 @@
# Retry # Retry
retry重试执行函数直到函数运行成功或被context cancel。
retry 重试执行函数直到函数运行成功或被 context cancel。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go) - [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.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/retry" "github.com/duke-git/lancet/v2/retry"
@@ -20,20 +21,19 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
- [Context](#Context)
- [Retry](#Retry)
- [RetryFunc](#RetryFunc)
- [RetryDuration](#RetryDuration)
- [RetryTimes](#RetryTimes)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Document 文档
## Document文档
### <span id="Context">Context</span> ### <span id="Context">Context</span>
<p>设置重试context参数</p> <p>设置重试context参数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -41,43 +41,46 @@ import (
```go ```go
func Context(ctx context.Context) func Context(ctx context.Context)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
import ( import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"lancet-demo/retry" "lancet-demo/retry"
"time" "time"
) )
func main() { func main() {
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(context.TODO())
var number int
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, number := 0
retry.RetryDuration(time.Microsecond*50), increaseNumber := func() error {
retry.Context(ctx), number++
) if number > 3 {
cancel()
}
return errors.New("error occurs")
}
if err != nil { duration := retry.RetryDuration(time.Microsecond*50)
fmt.Println(err) //retry is cancelled
} retry.Retry(increaseNumber,
duration,
retry.Context(ctx),
)
fmt.Println(number)
// Output:
// 4
} }
``` ```
### <span id="RetryFunc">RetryFunc</span> ### <span id="RetryFunc">RetryFunc</span>
<p>被重试执行的函数</p> <p>被重试执行的函数</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -85,7 +88,8 @@ func main() {
```go ```go
type RetryFunc func() error type RetryFunc func() error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -98,27 +102,31 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { var increaseNumber retry.RetryFunc = func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```
### <span id="RetryTimes">RetryTimes</span> ### <span id="RetryTimes">RetryTimes</span>
<p>设置重试次数默认5</p> <p>设置重试次数默认5</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -126,7 +134,8 @@ func main() {
```go ```go
func RetryTimes(n uint) func RetryTimes(n uint)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -139,25 +148,28 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryTimes(2)) increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
if err != nil { if err != nil {
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1 fmt.Println(err)
} }
// Output:
// function main.main.func1 run failed after 2 times retry
} }
``` ```
### <span id="RetryDuration">RetryDuration</span> ### <span id="RetryDuration">RetryDuration</span>
<p>设置重试间隔时间默认3秒</p> <p>设置重试间隔时间默认3秒</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -165,7 +177,8 @@ func main() {
```go ```go
func RetryDuration(d time.Duration) func RetryDuration(d time.Duration)
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -178,26 +191,31 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```
### <span id="Retry">Retry</span> ### <span id="Retry">Retry</span>
<p>重试执行函数retryFunc直到函数运行成功或被context停止</p> <p>重试执行函数retryFunc直到函数运行成功或被context停止</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -205,7 +223,8 @@ func main() {
```go ```go
func Retry(retryFunc RetryFunc, opts ...Option) error func Retry(retryFunc RetryFunc, opts ...Option) error
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -218,20 +237,25 @@ import (
) )
func main() { func main() {
var number int number := 0
increaseNumber := func() error { increaseNumber := func() error {
number++ number++
if number == 3 { if number == 3 {
return nil return nil
} }
return errors.New("error occurs") return errors.New("error occurs")
} }
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) duration := retry.RetryDuration(time.Microsecond*50)
err := retry.Retry(increaseNumber, duration)
if err != nil { if err != nil {
log.Fatal(err) return
} }
fmt.Println(number) //3 fmt.Println(number)
// Output:
// 3
} }
``` ```

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,17 @@
# System # System
Package system contains some functions about os, runtime, shell command. Package system contains some functions about os, runtime, shell command.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go) - [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
@@ -20,23 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [IsWindows](#IsWindows)
- [IsLinux](#IsLinux)
- [IsMac](#IsMac)
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
- [IsWindows](#IsWindows)
- [IsLinux](#IsLinux)
- [IsMac](#IsMac)
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="IsWindows">IsWindows</span> ### <span id="IsWindows">IsWindows</span>
<p>Check if current os is windows.</p> <p>Check if current os is windows.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -44,24 +45,23 @@ import (
```go ```go
func IsWindows() bool func IsWindows() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsWindows := system.IsWindows() isOsWindows := system.IsWindows()
fmt.Println(isOsWindows) fmt.Println(isOsWindows)
} }
``` ```
### <span id="IsLinux">IsLinux</span> ### <span id="IsLinux">IsLinux</span>
<p>Check if current os is linux.</p> <p>Check if current os is linux.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -69,23 +69,23 @@ func main() {
```go ```go
func IsLinux() bool func IsLinux() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsLinux := system.IsLinux() isOsLinux := system.IsLinux()
fmt.Println(isOsLinux) fmt.Println(isOsLinux)
} }
``` ```
### <span id="IsMac">IsMac</span> ### <span id="IsMac">IsMac</span>
<p>Check if current os is macos.</p> <p>Check if current os is macos.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -93,23 +93,23 @@ func main() {
```go ```go
func IsMac() bool func IsMac() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsMac := system.IsMac() isOsMac := system.IsMac()
fmt.Println(isOsMac) fmt.Println(isOsMac)
} }
``` ```
### <span id="GetOsEnv">GetOsEnv</span> ### <span id="GetOsEnv">GetOsEnv</span>
<p>Gets the value of the environment variable named by the key.</p> <p>Gets the value of the environment variable named by the key.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -117,23 +117,29 @@ func main() {
```go ```go
func GetOsEnv(key string) string func GetOsEnv(key string) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
fooEnv := system.GetOsEnv("foo") err := system.SetOsEnv("foo", "abc")
fmt.Println(fooEnv) result := system.GetOsEnv("foo")
fmt.Println(err)
fmt.Println(result)
// Output:
// <nil>
// abc
} }
``` ```
### <span id="SetOsEnv">SetOsEnv</span> ### <span id="SetOsEnv">SetOsEnv</span>
<p>Sets the value of the environment variable named by the key.</p> <p>Sets the value of the environment variable named by the key.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -141,24 +147,29 @@ func main() {
```go ```go
func SetOsEnv(key, value string) error func SetOsEnv(key, value string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
err := system.SetOsEnv("foo", "foo_value") err := system.SetOsEnv("foo", "abc")
fmt.Println(err) result := system.GetOsEnv("foo")
fmt.Println(err)
fmt.Println(result)
// Output:
// <nil>
// abc
} }
``` ```
### <span id="RemoveOsEnv">RemoveOsEnv</span> ### <span id="RemoveOsEnv">RemoveOsEnv</span>
<p>Remove a single environment variable.</p> <p>Remove a single environment variable.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -166,25 +177,37 @@ func main() {
```go ```go
func RemoveOsEnv(key string) error func RemoveOsEnv(key string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
err := system.RemoveOsEnv("foo") err1 := system.SetOsEnv("foo", "abc")
if err != nil { result1 := GetOsEnv("foo")
fmt.Println(err)
} err2 := system.RemoveOsEnv("foo")
result2 := GetOsEnv("foo")
fmt.Println(err1)
fmt.Println(err2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// <nil>
// <nil>
// abc
//
} }
``` ```
### <span id="CompareOsEnv">CompareOsEnv</span> ### <span id="CompareOsEnv">CompareOsEnv</span>
<p>Get env named by the key and compare it with comparedEnv.</p> <p>Get env named by the key and compare it with comparedEnv.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -192,25 +215,32 @@ func main() {
```go ```go
func CompareOsEnv(key, comparedEnv string) bool func CompareOsEnv(key, comparedEnv string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
system.SetOsEnv("foo", "foo_value") err := system.SetOsEnv("foo", "abc")
res := system.CompareOsEnv("foo", "foo_value") if err != nil {
fmt.Println(res) //true return
}
result := system.CompareOsEnv("foo", "abc")
fmt.Println(result)
// Output:
// true
} }
``` ```
### <span id="ExecCommand">CompareOsEnv</span> ### <span id="ExecCommand">CompareOsEnv</span>
<p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p> <p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -218,40 +248,39 @@ func main() {
```go ```go
func ExecCommand(command string) (stdout, stderr string, err error) func ExecCommand(command string) (stdout, stderr string, err error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
// linux or mac // linux or mac
stdout, stderr, err := system.ExecCommand("ls") stdout, stderr, err := system.ExecCommand("ls")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
assert.Equal("", stderr) assert.Equal("", stderr)
// windows // windows
stdout, stderr, err = system.ExecCommand("dir") stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
// error command // error command
stdout, stderr, err = system.ExecCommand("abc") stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
} }
``` ```
### <span id="GetOsBits">GetOsBits</span> ### <span id="GetOsBits">GetOsBits</span>
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p> <p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
<b>Signature:</b> <b>Signature:</b>
@@ -259,19 +288,17 @@ func main() {
```go ```go
func GetOsBits() int func GetOsBits() int
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
osBit := system.GetOsBits() osBit := system.GetOsBits()
fmt.Println(osBit) fmt.Println(osBit) // 32 or 64
} }
``` ```

View File

@@ -1,16 +1,17 @@
# System # System
system包含os, runtime, shell command相关函数。
system 包含 os, runtime, shell command 相关函数。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go) - [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.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/system" "github.com/duke-git/lancet/v2/system"
@@ -20,23 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [IsWindows](#IsWindows)
- [IsLinux](#IsLinux)
- [IsMac](#IsMac)
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
- [IsWindows](#IsWindows)
- [IsLinux](#IsLinux)
- [IsMac](#IsMac)
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation文档 ## Documentation 文档
### <span id="IsWindows">IsWindows</span> ### <span id="IsWindows">IsWindows</span>
<p>检查当前操作系统是否是windows</p> <p>检查当前操作系统是否是windows</p>
<b>Signature:</b> <b>Signature:</b>
@@ -44,24 +45,23 @@ import (
```go ```go
func IsWindows() bool func IsWindows() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsWindows := system.IsWindows() isOsWindows := system.IsWindows()
fmt.Println(isOsWindows) fmt.Println(isOsWindows)
} }
``` ```
### <span id="IsLinux">IsLinux</span> ### <span id="IsLinux">IsLinux</span>
<p>检查当前操作系统是否是linux</p> <p>检查当前操作系统是否是linux</p>
<b>Signature:</b> <b>Signature:</b>
@@ -69,23 +69,23 @@ func main() {
```go ```go
func IsLinux() bool func IsLinux() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsLinux := system.IsLinux() isOsLinux := system.IsLinux()
fmt.Println(isOsLinux) fmt.Println(isOsLinux)
} }
``` ```
### <span id="IsMac">IsMac</span> ### <span id="IsMac">IsMac</span>
<p>检查当前操作系统是否是macos</p> <p>检查当前操作系统是否是macos</p>
<b>Signature:</b> <b>Signature:</b>
@@ -93,23 +93,23 @@ func main() {
```go ```go
func IsMac() bool func IsMac() bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
isOsMac := system.IsMac() isOsMac := system.IsMac()
fmt.Println(isOsMac) fmt.Println(isOsMac)
} }
``` ```
### <span id="GetOsEnv">GetOsEnv</span> ### <span id="GetOsEnv">GetOsEnv</span>
<p>获取key命名的环境变量的值</p> <p>获取key命名的环境变量的值</p>
<b>Signature:</b> <b>Signature:</b>
@@ -117,23 +117,29 @@ func main() {
```go ```go
func GetOsEnv(key string) string func GetOsEnv(key string) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
fooEnv := system.GetOsEnv("foo") err := system.SetOsEnv("foo", "abc")
fmt.Println(fooEnv) result := system.GetOsEnv("foo")
fmt.Println(err)
fmt.Println(result)
// Output:
// <nil>
// abc
} }
``` ```
### <span id="SetOsEnv">SetOsEnv</span> ### <span id="SetOsEnv">SetOsEnv</span>
<p>设置由key命名的环境变量的值</p> <p>设置由key命名的环境变量的值</p>
<b>Signature:</b> <b>Signature:</b>
@@ -141,24 +147,29 @@ func main() {
```go ```go
func SetOsEnv(key, value string) error func SetOsEnv(key, value string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
err := system.SetOsEnv("foo", "foo_value") err := system.SetOsEnv("foo", "abc")
fmt.Println(err) result := system.GetOsEnv("foo")
fmt.Println(err)
fmt.Println(result)
// Output:
// <nil>
// abc
} }
``` ```
### <span id="RemoveOsEnv">RemoveOsEnv</span> ### <span id="RemoveOsEnv">RemoveOsEnv</span>
<p>删除单个环境变量</p> <p>删除单个环境变量</p>
<b>Signature:</b> <b>Signature:</b>
@@ -166,25 +177,37 @@ func main() {
```go ```go
func RemoveOsEnv(key string) error func RemoveOsEnv(key string) error
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
err := system.RemoveOsEnv("foo") err1 := system.SetOsEnv("foo", "abc")
if err != nil { result1 := GetOsEnv("foo")
fmt.Println(err)
} err2 := system.RemoveOsEnv("foo")
result2 := GetOsEnv("foo")
fmt.Println(err1)
fmt.Println(err2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// <nil>
// <nil>
// abc
//
} }
``` ```
### <span id="CompareOsEnv">CompareOsEnv</span> ### <span id="CompareOsEnv">CompareOsEnv</span>
<p>获取key命名的环境变量值并与compareEnv进行比较</p> <p>获取key命名的环境变量值并与compareEnv进行比较</p>
<b>Signature:</b> <b>Signature:</b>
@@ -192,25 +215,32 @@ func main() {
```go ```go
func CompareOsEnv(key, comparedEnv string) bool func CompareOsEnv(key, comparedEnv string) bool
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
system.SetOsEnv("foo", "foo_value") err := system.SetOsEnv("foo", "abc")
res := system.CompareOsEnv("foo", "foo_value") if err != nil {
fmt.Println(res) //true return
}
result := system.CompareOsEnv("foo", "abc")
fmt.Println(result)
// Output:
// true
} }
``` ```
### <span id="ExecCommand">ExecCommand</span> ### <span id="ExecCommand">ExecCommand</span>
<p>执行shell命令返回命令的stdout和stderr字符串如果出现错误则返回错误。参数`command`是一个完整的命令字符串如ls-alinuxdirwindowsping 127.0.0.1。在linux中使用/bin/bash-c执行命令在windows中使用powershell.exe执行命令。</p> <p>执行shell命令返回命令的stdout和stderr字符串如果出现错误则返回错误。参数`command`是一个完整的命令字符串如ls-alinuxdirwindowsping 127.0.0.1。在linux中使用/bin/bash-c执行命令在windows中使用powershell.exe执行命令。</p>
<b>Signature:</b> <b>Signature:</b>
@@ -218,40 +248,39 @@ func main() {
```go ```go
func ExecCommand(command string) (stdout, stderr string, err error) func ExecCommand(command string) (stdout, stderr string, err error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
// linux or mac // linux or mac
stdout, stderr, err := system.ExecCommand("ls") stdout, stderr, err := system.ExecCommand("ls")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
assert.Equal("", stderr) assert.Equal("", stderr)
// windows // windows
stdout, stderr, err = system.ExecCommand("dir") stdout, stderr, err = system.ExecCommand("dir")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
// error command // error command
stdout, stderr, err = system.ExecCommand("abc") stdout, stderr, err = system.ExecCommand("abc")
fmt.Println("std out: ", stdout) fmt.Println("std out: ", stdout)
fmt.Println("std err: ", stderr) fmt.Println("std err: ", stderr)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
} }
``` ```
### <span id="GetOsBits">GetOsBits</span> ### <span id="GetOsBits">GetOsBits</span>
<p>获取当前操作系统位数返回32或64</p> <p>获取当前操作系统位数返回32或64</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -259,23 +288,17 @@ func main() {
```go ```go
func GetOsBits() int func GetOsBits() int
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
import ( import (
"fmt" "fmt"
"github.com/duke-git/lancet/v2/system" "github.com/duke-git/lancet/v2/system"
) )
func main() { func main() {
osBit := system.GetOsBits() osBit := system.GetOsBits()
fmt.Println(osBit) fmt.Println(osBit) // 32 or 64
} }
``` ```

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,15 +1,17 @@
# Xerror # Xerror
Package xerror implements helpers for errors. Package xerror implements helpers for errors.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) - [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/xerror" "github.com/duke-git/lancet/v2/xerror"
@@ -19,15 +21,15 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [Unwrap](#Unwrap)
- [Unwrap](#Unwrap)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="Unwrap">Unwrap</span> ### <span id="Unwrap">Unwrap</span>
<p>Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.</p> <p>Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -35,6 +37,7 @@ import (
```go ```go
func Unwrap[T any](val T, err error) T func Unwrap[T any](val T, err error) T
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -46,12 +49,20 @@ import (
) )
func main() { func main() {
_, err := strconv.Atoi("4o2") result1 := xerror.Unwrap(strconv.Atoi("42"))
defer func() { fmt.Println(result1)
v := recover()
fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error()
}()
xerror.Unwrap(strconv.Atoi("4o2")) _, err := strconv.Atoi("4o2")
defer func() {
v := recover()
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
fmt.Println(result2)
}()
xerror.Unwrap(strconv.Atoi("4o2"))
// Output:
// 42
// true
} }
``` ```

View File

@@ -1,15 +1,17 @@
# Xerror # Xerror
xerror错误处理逻辑封装
xerror 错误处理逻辑封装
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) - [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.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/xerror" "github.com/duke-git/lancet/v2/xerror"
@@ -19,23 +21,24 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [Unwrap](#Unwrap)
- [Unwrap](#Unwrap)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="Unwrap">Unwrap</span> ### <span id="Unwrap">Unwrap</span>
<p>如果err为nil则展开则它返回一个有效值。 如果err不是nil则Unwrap使用err发生恐慌。</p>
<p>检查error, 如果err为nil则展开则它返回一个有效值如果err不是nil则Unwrap使用err发生panic。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Unwrap[T any](val T, err error) T func Unwrap[T any](val T, err error) T
``` ```
<b>例子:</b>
<b>示例:</b>
```go ```go
package main package main
@@ -46,12 +49,20 @@ import (
) )
func main() { func main() {
_, err := strconv.Atoi("4o2") result1 := xerror.Unwrap(strconv.Atoi("42"))
defer func() { fmt.Println(result1)
v := recover()
fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error()
}()
xerror.Unwrap(strconv.Atoi("4o2")) _, err := strconv.Atoi("4o2")
defer func() {
v := recover()
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
fmt.Println(result2)
}()
xerror.Unwrap(strconv.Atoi("4o2"))
// Output:
// 42
// true
} }
``` ```

View File

@@ -19,7 +19,7 @@ func ExampleIsExist() {
} }
func ExampleCreateFile() { func ExampleCreateFile() {
fname := "./test.txt" fname := "./a.txt"
result1 := IsExist(fname) result1 := IsExist(fname)
@@ -175,11 +175,11 @@ func ExampleReadFileByLine() {
} }
func ExampleListFileNames() { func ExampleListFileNames() {
fileList, _ := ListFileNames("./") fileList, _ := ListFileNames("../formatter/")
fmt.Println(fileList) fmt.Println(fileList)
// Output: // Output:
// [file.go file_example_test.go file_test.go] // [formatter.go formatter_example_test.go formatter_test.go]
} }
func ExampleZip() { func ExampleZip() {

View File

@@ -96,16 +96,6 @@ func TestCopyFile(t *testing.T) {
os.Remove(destFile) os.Remove(destFile)
} }
func TestListFileNames(t *testing.T) {
assert := internal.NewAssert(t, "TestListFileNames")
filesInPath, err := ListFileNames("./")
assert.IsNil(err)
expected := []string{"file.go", "file_test.go"}
assert.Equal(expected, filesInPath)
}
func TestReadFileToString(t *testing.T) { func TestReadFileToString(t *testing.T) {
assert := internal.NewAssert(t, "TestReadFileToString") assert := internal.NewAssert(t, "TestReadFileToString")
@@ -241,3 +231,13 @@ func TestMiMeType(t *testing.T) {
assert.Equal("text/plain; charset=utf-8", MiMeType(f)) assert.Equal("text/plain; charset=utf-8", MiMeType(f))
assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go")) assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go"))
} }
func TestListFileNames(t *testing.T) {
assert := internal.NewAssert(t, "TestListFileNames")
filesInPath, err := ListFileNames("../formatter/")
assert.IsNil(err)
expected := []string{"formatter.go", "formatter_example_test.go", "formatter_test.go"}
assert.Equal(expected, filesInPath)
}

View File

@@ -5,11 +5,13 @@
package function package function
import ( import (
"fmt"
"reflect" "reflect"
"time" "time"
) )
// After creates a function that invokes func once it's called n or more times // After creates a function that invokes func once it's called n or more times.
// Play: https://go.dev/play/p/8mQhkFmsgqs
func After(n int, fn any) func(args ...any) []reflect.Value { func After(n int, fn any) func(args ...any) []reflect.Value {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure
mustBeFunction(fn) mustBeFunction(fn)
@@ -23,7 +25,8 @@ func After(n int, fn any) func(args ...any) []reflect.Value {
} }
} }
// Before creates a function that invokes func once it's called less than n times // Before creates a function that invokes func once it's called less than n times.
// Play: https://go.dev/play/p/0HqUDIFZ3IL
func Before(n int, fn any) func(args ...any) []reflect.Value { func Before(n int, fn any) func(args ...any) []reflect.Value {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure
mustBeFunction(fn) mustBeFunction(fn)
@@ -40,41 +43,48 @@ func Before(n int, fn any) func(args ...any) []reflect.Value {
} }
} }
// Fn is for curry function which is func(...any) any // CurryFn is for make curry function
type Fn func(...any) any type CurryFn[T any] func(...T) T
// Curry make a curry function // New make a curry function for specific value.
func (f Fn) Curry(i any) func(...any) any { // Play: https://go.dev/play/p/5HopfDwANKX
return func(values ...any) any { func (cf CurryFn[T]) New(val T) func(...T) T {
v := append([]any{i}, values...) return func(vals ...T) T {
return f(v...) args := append([]T{val}, vals...)
return cf(args...)
} }
} }
// Compose compose the functions from right to left // Compose compose the functions from right to left.
func Compose(fnList ...func(...any) any) func(...any) any { // Play: https://go.dev/play/p/KKfugD4PKYF
return func(s ...any) any { func Compose[T any](fnList ...func(...T) T) func(...T) T {
f := fnList[0] return func(args ...T) T {
restFn := fnList[1:] firstFn := fnList[0]
restFns := fnList[1:]
if len(fnList) == 1 { if len(fnList) == 1 {
return f(s...) return firstFn(args...)
} }
return f(Compose(restFn...)(s...)) fn := Compose(restFns...)
arg := fn(args...)
return firstFn(arg)
} }
} }
// Delay make the function execution after delayed time // Delay make the function execution after delayed time.
// Play: https://go.dev/play/p/Ivtc2ZE-Tye
func Delay(delay time.Duration, fn any, args ...any) { func Delay(delay time.Duration, fn any, args ...any) {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure
mustBeFunction(fn) mustBeFunction(fn)
time.Sleep(delay) time.Sleep(delay)
invokeFunc(fn, args...) unsafeInvokeFunc(fn, args...)
} }
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked. // Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
// Play: https://go.dev/play/p/absuEGB_GN7
func Debounced(fn func(), duration time.Duration) func() { func Debounced(fn func(), duration time.Duration) func() {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure
mustBeFunction(fn) mustBeFunction(fn)
@@ -92,7 +102,8 @@ func Debounced(fn func(), duration time.Duration) func() {
return func() { timer.Reset(duration) } return func() { timer.Reset(duration) }
} }
// Schedule invoke function every duration time, util close the returned bool chan // Schedule invoke function every duration time, util close the returned bool channel.
// Play: https://go.dev/play/p/hbON-Xeyn5N
func Schedule(d time.Duration, fn any, args ...any) chan bool { func Schedule(d time.Duration, fn any, args ...any) chan bool {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure
mustBeFunction(fn) mustBeFunction(fn)
@@ -114,6 +125,7 @@ func Schedule(d time.Duration, fn any, args ...any) chan bool {
// Pipeline takes a list of functions and returns a function whose param will be passed into // Pipeline takes a list of functions and returns a function whose param will be passed into
// the functions one by one. // the functions one by one.
// Play: https://go.dev/play/p/mPdUVvj6HD6
func Pipeline[T any](funcs ...func(T) T) func(T) T { func Pipeline[T any](funcs ...func(T) T) func(T) T {
return func(arg T) (result T) { return func(arg T) (result T) {
result = arg result = arg
@@ -123,3 +135,19 @@ func Pipeline[T any](funcs ...func(T) T) func(T) T {
return return
} }
} }
func unsafeInvokeFunc(fn any, args ...any) []reflect.Value {
fv := reflect.ValueOf(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func mustBeFunction(function any) {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
}

View File

@@ -0,0 +1,147 @@
package function
import (
"fmt"
"strings"
"time"
)
func ExampleAfter() {
fn := After(2, func() {
fmt.Println("test")
})
fn()
fn()
// Output:
// test
}
func ExampleBefore() {
fn := Before(2, func() {
fmt.Println("test")
})
fn()
fn()
fn()
fn()
// Output:
// test
// test
}
func ExampleCurryFn_New() {
add := func(a, b int) int {
return a + b
}
var addCurry CurryFn[int] = func(values ...int) int {
return add(values[0], values[1])
}
add1 := addCurry.New(1)
result := add1(2)
fmt.Println(result)
// Output:
// 3
}
func ExampleCompose() {
toUpper := func(strs ...string) string {
return strings.ToUpper(strs[0])
}
toLower := func(strs ...string) string {
return strings.ToLower(strs[0])
}
transform := Compose(toUpper, toLower)
result := transform("aBCde")
fmt.Println(result)
// Output:
// ABCDE
}
func ExampleDelay() {
var print = func(s string) {
fmt.Println(s)
}
Delay(2*time.Second, print, "hello")
// Output:
// hello
}
func ExampleDebounced() {
count := 0
add := func() {
count++
}
debouncedAdd := Debounced(add, 50*time.Microsecond)
debouncedAdd()
debouncedAdd()
debouncedAdd()
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
// Output:
// 1
// 2
}
func ExampleSchedule() {
count := 0
increase := func() {
count++
}
stop := Schedule(2*time.Second, increase)
time.Sleep(2 * time.Second)
close(stop)
fmt.Println(count)
// Output:
// 2
}
func ExamplePipeline() {
addOne := func(x int) int {
return x + 1
}
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
fn := Pipeline(addOne, double, square)
result := fn(2)
fmt.Println(result)
// Output:
// 36
}

View File

@@ -1,39 +0,0 @@
package function
import (
"fmt"
"reflect"
)
func invokeFunc(fn any, args ...any) []reflect.Value {
fv := functionValue(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func unsafeInvokeFunc(fn any, args ...any) []reflect.Value {
fv := reflect.ValueOf(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func functionValue(function any) reflect.Value {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
return v
}
func mustBeFunction(function any) {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
}

View File

@@ -62,24 +62,26 @@ func TestCurry(t *testing.T) {
add := func(a, b int) int { add := func(a, b int) int {
return a + b return a + b
} }
var addCurry Fn = func(values ...any) any { var addCurry CurryFn[int] = func(values ...int) int {
return add(values[0].(int), values[1].(int)) return add(values[0], values[1])
} }
add1 := addCurry.Curry(1) add1 := addCurry.New(1)
assert.Equal(3, add1(2)) assert.Equal(3, add1(2))
} }
func TestCompose(t *testing.T) { func TestCompose(t *testing.T) {
assert := internal.NewAssert(t, "TestCompose") assert := internal.NewAssert(t, "TestCompose")
toUpper := func(a ...any) any { toUpper := func(strs ...string) string {
return strings.ToUpper(a[0].(string)) return strings.ToUpper(strs[0])
} }
toLower := func(a ...any) any { toLower := func(strs ...string) string {
return strings.ToLower(a[0].(string)) return strings.ToLower(strs[0])
} }
expected := toUpper(toLower("aBCde")) expected := toUpper(toLower("aBCde"))
cf := Compose(toUpper, toLower) cf := Compose(toUpper, toLower)
res := cf("aBCde") res := cf("aBCde")

View File

@@ -3,12 +3,18 @@ package function
import "time" import "time"
// Watcher is used for record code excution time // Watcher is used for record code excution time
// Play: https://go.dev/play/p/l2yrOpCLd1I
type Watcher struct { type Watcher struct {
startTime int64 startTime int64
stopTime int64 stopTime int64
excuting bool excuting bool
} }
// Start the watch timer.
func NewWatcher() *Watcher {
return &Watcher{}
}
// Start the watch timer. // Start the watch timer.
func (w *Watcher) Start() { func (w *Watcher) Start() {
w.startTime = time.Now().UnixNano() w.startTime = time.Now().UnixNano()

View File

@@ -0,0 +1,22 @@
package function
import "fmt"
func ExampleWatcher() {
w := NewWatcher()
w.Start()
longRunningTask()
w.Stop()
// eapsedTime := w.GetElapsedTime().Milliseconds()
fmt.Println("foo")
w.Reset()
// Output:
// foo
}

View File

@@ -9,7 +9,7 @@ import (
func TestWatcher(t *testing.T) { func TestWatcher(t *testing.T) {
assert := internal.NewAssert(t, "TestWatcher") assert := internal.NewAssert(t, "TestWatcher")
w := &Watcher{} w := NewWatcher()
w.Start() w.Start()
longRunningTask() longRunningTask()

View File

@@ -55,6 +55,7 @@ func Factorial(x uint) uint {
} }
// Percent calculate the percentage of value to total. // Percent calculate the percentage of value to total.
// Play: https://go.dev/play/p/QQM9B13coSP
func Percent(val, total float64, n int) float64 { func Percent(val, total float64, n int) float64 {
if total == 0 { if total == 0 {
return float64(0) return float64(0)

View File

@@ -13,40 +13,49 @@
package netutil package netutil
import ( import (
"bytes"
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url"
"reflect"
"regexp"
"sort" "sort"
"strings" "strings"
"time"
"github.com/duke-git/lancet/v2/slice"
) )
//HttpGet send get http request // HttpGet send get http request.
func HttpGet(url string, params ...any) (*http.Response, error) { func HttpGet(url string, params ...any) (*http.Response, error) {
return doHttpRequest(http.MethodGet, url, params...) return doHttpRequest(http.MethodGet, url, params...)
} }
//HttpPost send post http request // HttpPost send post http request.
func HttpPost(url string, params ...any) (*http.Response, error) { func HttpPost(url string, params ...any) (*http.Response, error) {
return doHttpRequest(http.MethodPost, url, params...) return doHttpRequest(http.MethodPost, url, params...)
} }
//HttpPut send put http request // HttpPut send put http request.
func HttpPut(url string, params ...any) (*http.Response, error) { func HttpPut(url string, params ...any) (*http.Response, error) {
return doHttpRequest(http.MethodPut, url, params...) return doHttpRequest(http.MethodPut, url, params...)
} }
//HttpDelete send delete http request // HttpDelete send delete http request.
func HttpDelete(url string, params ...any) (*http.Response, error) { func HttpDelete(url string, params ...any) (*http.Response, error) {
return doHttpRequest(http.MethodDelete, url, params...) return doHttpRequest(http.MethodDelete, url, params...)
} }
// HttpPatch send patch http request // HttpPatch send patch http request.
func HttpPatch(url string, params ...any) (*http.Response, error) { func HttpPatch(url string, params ...any) (*http.Response, error) {
return doHttpRequest(http.MethodPatch, url, params...) return doHttpRequest(http.MethodPatch, url, params...)
} }
// ParseHttpResponse decode http response to specified interface // ParseHttpResponse decode http response to specified interface.
func ParseHttpResponse(resp *http.Response, obj any) error { func ParseHttpResponse(resp *http.Response, obj any) error {
if resp == nil { if resp == nil {
return errors.New("InvalidResp") return errors.New("InvalidResp")
@@ -55,7 +64,8 @@ func ParseHttpResponse(resp *http.Response, obj any) error {
return json.NewDecoder(resp.Body).Decode(obj) return json.NewDecoder(resp.Body).Decode(obj)
} }
// ConvertMapToQueryString convert map to sorted url query string // ConvertMapToQueryString convert map to sorted url query string.
// Play: https://go.dev/play/p/jnNt_qoSnRi
func ConvertMapToQueryString(param map[string]any) string { func ConvertMapToQueryString(param map[string]any) string {
if param == nil { if param == nil {
return "" return ""
@@ -77,3 +87,220 @@ func ConvertMapToQueryString(param map[string]any) string {
} }
return build.String() return build.String()
} }
// HttpRequest struct is a composed http request
type HttpRequest struct {
RawURL string
Method string
Headers http.Header
QueryParams url.Values
FormData url.Values
Body []byte
}
// HttpClientConfig contains some configurations for http client
type HttpClientConfig struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseTimeout time.Duration
Verbose bool
}
// defaultHttpClientConfig defalut client config
var defaultHttpClientConfig = &HttpClientConfig{
Compressed: false,
HandshakeTimeout: 20 * time.Second,
ResponseTimeout: 40 * time.Second,
}
// HttpClient is used for sending http request
type HttpClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
Config HttpClientConfig
}
// NewHttpClient make a HttpClient instance
func NewHttpClient() *HttpClient {
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
DisableCompression: !defaultHttpClientConfig.Compressed,
},
},
Config: *defaultHttpClientConfig,
}
return client
}
// NewHttpClientWithConfig make a HttpClient instance with pass config
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
if config == nil {
config = defaultHttpClientConfig
}
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: config.HandshakeTimeout,
ResponseHeaderTimeout: config.ResponseTimeout,
DisableCompression: !config.Compressed,
},
},
Config: *config,
}
if config.SSLEnabled {
client.TLS = config.TLSConfig
}
return client
}
// SendRequest send http request.
// Play: https://go.dev/play/p/jUSgynekH7G
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
err := validateRequest(request)
if err != nil {
return nil, err
}
rawUrl := request.RawURL
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
if err != nil {
return nil, err
}
client.setTLS(rawUrl)
client.setHeader(req, request.Headers)
err = client.setQueryParam(req, rawUrl, request.QueryParams)
if err != nil {
return nil, err
}
if request.FormData != nil {
client.setFormData(req, request.FormData)
}
client.Request = req
resp, err := client.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
// DecodeResponse decode response into target object.
// Play: https://go.dev/play/p/jUSgynekH7G
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error {
if resp == nil {
return errors.New("invalid target param")
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(target)
}
// setTLS set http client transport TLSClientConfig
func (client *HttpClient) setTLS(rawUrl string) {
if strings.HasPrefix(rawUrl, "https") {
if transport, ok := client.Client.Transport.(*http.Transport); ok {
transport.TLSClientConfig = client.TLS
}
}
}
// setHeader set http rquest header
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
if headers == nil {
headers = make(http.Header)
}
if _, ok := headers["Accept"]; !ok {
headers["Accept"] = []string{"*/*"}
}
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
headers["Accept-Encoding"] = []string{"deflate, gzip"}
}
req.Header = headers
}
// setQueryParam set http request query string param
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
if queryParam != nil {
if !strings.Contains(reqUrl, "?") {
reqUrl = reqUrl + "?" + queryParam.Encode()
} else {
reqUrl = reqUrl + "&" + queryParam.Encode()
}
u, err := url.Parse(reqUrl)
if err != nil {
return err
}
req.URL = u
}
return nil
}
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
formData := []byte(values.Encode())
req.Body = io.NopCloser(bytes.NewReader(formData))
req.ContentLength = int64(len(formData))
}
// validateRequest check if a request has url, and valid method.
func validateRequest(req *HttpRequest) error {
if req.RawURL == "" {
return errors.New("invalid request url")
}
// common HTTP methods
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
return errors.New("invalid request method")
}
return nil
}
// StructToUrlValues convert struct to url valuse,
// only convert the field which is exported and has `json` tag.
// Play: https://go.dev/play/p/pFqMkM40w9z
func StructToUrlValues(targetStruct any) url.Values {
rv := reflect.ValueOf(targetStruct)
rt := reflect.TypeOf(targetStruct)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
}
result := url.Values{}
fieldNum := rt.NumField()
pattern := `^[A-Z]`
regex := regexp.MustCompile(pattern)
for i := 0; i < fieldNum; i++ {
name := rt.Field(i).Name
tag := rt.Field(i).Tag.Get("json")
if regex.MatchString(name) && tag != "" {
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
}
}
return result
}

View File

@@ -1,232 +0,0 @@
package netutil
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/duke-git/lancet/v2/slice"
)
// HttpRequest struct is a composed http request
type HttpRequest struct {
RawURL string
Method string
Headers http.Header
QueryParams url.Values
FormData url.Values
Body []byte
}
// HttpClientConfig contains some configurations for http client
type HttpClientConfig struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseTimeout time.Duration
Verbose bool
}
// defaultHttpClientConfig defalut client config
var defaultHttpClientConfig = &HttpClientConfig{
Compressed: false,
HandshakeTimeout: 20 * time.Second,
ResponseTimeout: 40 * time.Second,
}
// HttpClient is used for sending http request
type HttpClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
Config HttpClientConfig
}
// NewHttpClient make a HttpClient instance
func NewHttpClient() *HttpClient {
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
DisableCompression: !defaultHttpClientConfig.Compressed,
},
},
Config: *defaultHttpClientConfig,
}
return client
}
// NewHttpClientWithConfig make a HttpClient instance with pass config
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
if config == nil {
config = defaultHttpClientConfig
}
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: config.HandshakeTimeout,
ResponseHeaderTimeout: config.ResponseTimeout,
DisableCompression: !config.Compressed,
},
},
Config: *config,
}
if config.SSLEnabled {
client.TLS = config.TLSConfig
}
return client
}
// SendRequest send http request
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
err := validateRequest(request)
if err != nil {
return nil, err
}
rawUrl := request.RawURL
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
if err != nil {
return nil, err
}
client.setTLS(rawUrl)
client.setHeader(req, request.Headers)
err = client.setQueryParam(req, rawUrl, request.QueryParams)
if err != nil {
return nil, err
}
if request.FormData != nil {
client.setFormData(req, request.FormData)
}
client.Request = req
resp, err := client.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
// DecodeResponse decode response into target object
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error {
if resp == nil {
return errors.New("invalid target param")
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(target)
}
// setTLS set http client transport TLSClientConfig
func (client *HttpClient) setTLS(rawUrl string) {
if strings.HasPrefix(rawUrl, "https") {
if transport, ok := client.Client.Transport.(*http.Transport); ok {
transport.TLSClientConfig = client.TLS
}
}
}
// setHeader set http rquest header
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
if headers == nil {
headers = make(http.Header)
}
if _, ok := headers["Accept"]; !ok {
headers["Accept"] = []string{"*/*"}
}
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
headers["Accept-Encoding"] = []string{"deflate, gzip"}
}
req.Header = headers
}
// setQueryParam set http request query string param
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
if queryParam != nil {
if !strings.Contains(reqUrl, "?") {
reqUrl = reqUrl + "?" + queryParam.Encode()
} else {
reqUrl = reqUrl + "&" + queryParam.Encode()
}
u, err := url.Parse(reqUrl)
if err != nil {
return err
}
req.URL = u
}
return nil
}
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
formData := []byte(values.Encode())
req.Body = io.NopCloser(bytes.NewReader(formData))
req.ContentLength = int64(len(formData))
}
// validateRequest check if a request has url, and valid method.
func validateRequest(req *HttpRequest) error {
if req.RawURL == "" {
return errors.New("invalid request url")
}
// common HTTP methods
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
return errors.New("invalid request method")
}
return nil
}
// StructToUrlValues convert struct to url valuse,
// only convert the field which is exported and has `json` tag
func StructToUrlValues(targetStruct any) url.Values {
rv := reflect.ValueOf(targetStruct)
rt := reflect.TypeOf(targetStruct)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
}
result := url.Values{}
fieldNum := rt.NumField()
pattern := `^[A-Z]`
regex := regexp.MustCompile(pattern)
for i := 0; i < fieldNum; i++ {
name := rt.Field(i).Name
tag := rt.Field(i).Tag.Get("json")
if regex.MatchString(name) && tag != "" {
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
}
}
return result
}

View File

@@ -1,98 +0,0 @@
package netutil
import (
"io"
"log"
"net/http"
"net/url"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestHttpClient_Get(t *testing.T) {
assert := internal.NewAssert(t, "TestHttpClient_Get")
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
err = httpClient.DecodeResponse(resp, &todo)
if err != nil {
t.Log(err)
}
assert.Equal(1, todo.Id)
}
func TestHttpClent_Post(t *testing.T) {
header := http.Header{}
header.Add("Content-Type", "multipart/form-data")
postData := url.Values{}
postData.Add("userId", "1")
postData.Add("title", "testItem")
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "POST",
Headers: header,
FormData: postData,
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil {
log.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
func TestStructToUrlValues(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToUrlValues")
type TodoQuery struct {
Id int `json:"id"`
UserId int `json:"userId"`
}
todoQuery := TodoQuery{
Id: 1,
UserId: 1,
}
todoValues := StructToUrlValues(todoQuery)
assert.Equal("1", todoValues.Get("id"))
assert.Equal("1", todoValues.Get("userId"))
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "GET",
QueryParams: todoValues,
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", string(body))
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"log" "log"
"net/http"
"net/url" "net/url"
"testing" "testing"
@@ -156,3 +157,90 @@ func TestParseResponse(t *testing.T) {
} }
t.Log("response: ", toDoResp) t.Log("response: ", toDoResp)
} }
func TestHttpClient_Get(t *testing.T) {
assert := internal.NewAssert(t, "TestHttpClient_Get")
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
err = httpClient.DecodeResponse(resp, &todo)
if err != nil {
t.Log(err)
}
assert.Equal(1, todo.Id)
}
func TestHttpClent_Post(t *testing.T) {
header := http.Header{}
header.Add("Content-Type", "multipart/form-data")
postData := url.Values{}
postData.Add("userId", "1")
postData.Add("title", "testItem")
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "POST",
Headers: header,
FormData: postData,
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil {
log.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", resp.StatusCode, string(body))
}
func TestStructToUrlValues(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToUrlValues")
type TodoQuery struct {
Id int `json:"id"`
UserId int `json:"userId"`
}
todoQuery := TodoQuery{
Id: 1,
UserId: 1,
}
todoValues := StructToUrlValues(todoQuery)
assert.Equal("1", todoValues.Get("id"))
assert.Equal("1", todoValues.Get("userId"))
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "GET",
QueryParams: todoValues,
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
t.Log("response: ", string(body))
}

View File

@@ -9,7 +9,8 @@ import (
"strings" "strings"
) )
// GetInternalIp return internal ipv4 // GetInternalIp return internal ipv4.
// Play: https://go.dev/play/p/5mbu-gFp7ei
func GetInternalIp() string { func GetInternalIp() string {
addr, err := net.InterfaceAddrs() addr, err := net.InterfaceAddrs()
if err != nil { if err != nil {
@@ -26,7 +27,8 @@ func GetInternalIp() string {
return "" return ""
} }
// GetRequestPublicIp return the requested public ip // GetRequestPublicIp return the requested public ip.
// Play: https://go.dev/play/p/kxU-YDc_eBo
func GetRequestPublicIp(req *http.Request) string { func GetRequestPublicIp(req *http.Request) string {
var ip string var ip string
for _, ip = range strings.Split(req.Header.Get("X-Forwarded-For"), ",") { for _, ip = range strings.Split(req.Header.Get("X-Forwarded-For"), ",") {
@@ -47,7 +49,8 @@ func GetRequestPublicIp(req *http.Request) string {
} }
// GetPublicIpInfo return public ip information // GetPublicIpInfo return public ip information
// return the PublicIpInfo struct // return the PublicIpInfo struct.
// Play: https://go.dev/play/p/YDxIfozsRHR
func GetPublicIpInfo() (*PublicIpInfo, error) { func GetPublicIpInfo() (*PublicIpInfo, error) {
resp, err := http.Get("http://ip-api.com/json/") resp, err := http.Get("http://ip-api.com/json/")
if err != nil { if err != nil {
@@ -69,7 +72,8 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
return &ip, nil return &ip, nil
} }
// GetIps return all ipv4 of system // GetIps return all ipv4 of system.
// Play: https://go.dev/play/p/NUFfcEmukx1
func GetIps() []string { func GetIps() []string {
var ips []string var ips []string
@@ -89,7 +93,8 @@ func GetIps() []string {
return ips return ips
} }
// GetMacAddrs get mac address // GetMacAddrs get mac address.
// Play: https://go.dev/play/p/Rq9UUBS_Xp1
func GetMacAddrs() []string { func GetMacAddrs() []string {
var macAddrs []string var macAddrs []string
@@ -125,7 +130,8 @@ type PublicIpInfo struct {
Ip string `json:"query"` Ip string `json:"query"`
} }
// IsPublicIP verify a ip is public or not // IsPublicIP verify a ip is public or not.
// Play: https://go.dev/play/p/nmktSQpJZnn
func IsPublicIP(IP net.IP) bool { func IsPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() { if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false return false
@@ -145,7 +151,8 @@ func IsPublicIP(IP net.IP) bool {
return false return false
} }
// IsInternalIP verify an ip is intranet or not // IsInternalIP verify an ip is intranet or not.
// Play: https://go.dev/play/p/sYGhXbgO4Cb
func IsInternalIP(IP net.IP) bool { func IsInternalIP(IP net.IP) bool {
if IP.IsLoopback() { if IP.IsLoopback() {
return true return true
@@ -159,7 +166,8 @@ func IsInternalIP(IP net.IP) bool {
return false return false
} }
// EncodeUrl encode url // EncodeUrl encode url.
// Play: https://go.dev/play/p/bsZ6BRC4uKI
func EncodeUrl(urlStr string) (string, error) { func EncodeUrl(urlStr string) (string, error) {
URL, err := url.Parse(urlStr) URL, err := url.Parse(urlStr)
if err != nil { if err != nil {

156
netutil/net_example_test.go Normal file
View File

@@ -0,0 +1,156 @@
package netutil
import (
"fmt"
"net"
"net/http"
)
func ExampleGetInternalIp() {
internalIp := GetInternalIp()
result := IsInternalIP(net.ParseIP(internalIp))
fmt.Println(result)
// Output:
// true
}
func ExampleGetPublicIpInfo() {
ipInfo, err := GetPublicIpInfo()
if err != nil {
return
}
result := IsPublicIP(net.ParseIP(ipInfo.Ip))
fmt.Println(result)
// Output:
// true
}
func ExampleGetRequestPublicIp() {
ip := "36.112.24.10"
request := http.Request{
Method: "GET",
Header: http.Header{
"X-Forwarded-For": {ip},
},
}
publicIp := GetRequestPublicIp(&request)
fmt.Println(publicIp)
// Output:
// 36.112.24.10
}
func ExampleIsInternalIP() {
ip1 := IsInternalIP(net.ParseIP("127.0.0.1"))
ip2 := IsInternalIP(net.ParseIP("192.168.0.1"))
ip3 := IsInternalIP(net.ParseIP("36.112.24.10"))
fmt.Println(ip1)
fmt.Println(ip2)
fmt.Println(ip3)
// Output:
// true
// true
// false
}
func ExampleIsPublicIP() {
ip1 := IsPublicIP(net.ParseIP("127.0.0.1"))
ip2 := IsPublicIP(net.ParseIP("192.168.0.1"))
ip3 := IsPublicIP(net.ParseIP("36.112.24.10"))
fmt.Println(ip1)
fmt.Println(ip2)
fmt.Println(ip3)
// Output:
// false
// false
// true
}
func ExampleEncodeUrl() {
urlAddr := "http://www.lancet.com?a=1&b=[2]"
encodedUrl, err := EncodeUrl(urlAddr)
if err != nil {
return
}
fmt.Println(encodedUrl)
// Output:
// http://www.lancet.com?a=1&b=%5B2%5D
}
func ExampleHttpClient_DecodeResponse() {
request := &HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
return
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
err = httpClient.DecodeResponse(resp, &todo)
if err != nil {
return
}
fmt.Println(todo.Id)
// Output:
// 1
}
func ExampleStructToUrlValues() {
type TodoQuery struct {
Id int `json:"id"`
Name string `json:"name"`
}
todoQuery := TodoQuery{
Id: 1,
Name: "Test",
}
todoValues := StructToUrlValues(todoQuery)
fmt.Println(todoValues.Get("id"))
fmt.Println(todoValues.Get("name"))
// Output:
// 1
// Test
}
func ExampleConvertMapToQueryString() {
var m = map[string]any{
"c": 3,
"a": 1,
"b": 2,
}
qs := ConvertMapToQueryString(m)
fmt.Println(qs)
// Output:
// a=1&b=2&c=3
}

View File

@@ -6,7 +6,6 @@ import (
) )
func ExampleRandInt() { func ExampleRandInt() {
result := RandInt(1, 10) result := RandInt(1, 10)
if result >= 1 && result < 10 { if result >= 1 && result < 10 {

View File

@@ -34,21 +34,24 @@ type RetryFunc func() error
// Option is for adding retry config // Option is for adding retry config
type Option func(*RetryConfig) type Option func(*RetryConfig)
// RetryTimes set times of retry // RetryTimes set times of retry.
// Play: https://go.dev/play/p/ssfVeU2SwLO
func RetryTimes(n uint) Option { func RetryTimes(n uint) Option {
return func(rc *RetryConfig) { return func(rc *RetryConfig) {
rc.retryTimes = n rc.retryTimes = n
} }
} }
// RetryDuration set duration of retries // RetryDuration set duration of retries.
// Play: https://go.dev/play/p/nk2XRmagfVF
func RetryDuration(d time.Duration) Option { func RetryDuration(d time.Duration) Option {
return func(rc *RetryConfig) { return func(rc *RetryConfig) {
rc.retryDuration = d rc.retryDuration = d
} }
} }
// Context set retry context config // Context set retry context config.
// Play: https://go.dev/play/p/xnAOOXv9GkS
func Context(ctx context.Context) Option { func Context(ctx context.Context) Option {
return func(rc *RetryConfig) { return func(rc *RetryConfig) {
rc.context = ctx rc.context = ctx
@@ -56,7 +59,8 @@ func Context(ctx context.Context) Option {
} }
// Retry executes the retryFunc repeatedly until it was successful or canceled by the context // Retry executes the retryFunc repeatedly until it was successful or canceled by the context
// The default times of retries is 5 and the default duration between retries is 3 seconds // The default times of retries is 5 and the default duration between retries is 3 seconds.
// Play: https://go.dev/play/p/nk2XRmagfVF
func Retry(retryFunc RetryFunc, opts ...Option) error { func Retry(retryFunc RetryFunc, opts ...Option) error {
config := &RetryConfig{ config := &RetryConfig{
retryTimes: DefaultRetryTimes, retryTimes: DefaultRetryTimes,

View File

@@ -0,0 +1,93 @@
package retry
import (
"context"
"errors"
"fmt"
"time"
)
func ExampleContext() {
ctx, cancel := context.WithCancel(context.TODO())
number := 0
increaseNumber := func() error {
number++
if number > 3 {
cancel()
}
return errors.New("error occurs")
}
Retry(increaseNumber,
RetryDuration(time.Microsecond*50),
Context(ctx),
)
fmt.Println(number)
// Output:
// 4
}
func ExampleRetryDuration() {
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
if err != nil {
return
}
fmt.Println(number)
// Output:
// 3
}
func ExampleRetryTimes() {
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := Retry(increaseNumber, RetryTimes(2))
if err != nil {
fmt.Println(err)
}
// Output:
// function retry.ExampleRetryTimes.func1 run failed after 2 times retry
}
func ExampleRetry() {
number := 0
increaseNumber := func() error {
number++
if number == 3 {
return nil
}
return errors.New("error occurs")
}
err := Retry(increaseNumber, RetryDuration(time.Microsecond*50))
if err != nil {
return
}
fmt.Println(number)
// Output:
// 3
}

261
stream/stream.go Normal file
View File

@@ -0,0 +1,261 @@
// Copyright 2023 dudaodong@gmail.com. All rights resulterved.
// Use of this source code is governed by MIT license
// Package stream implements a sequence of elements supporting sequential and parallel aggregate operations.
// this package is an experiment to explore if stream in go can work as the way java does. it's complete, but not
// powerful like other libs
package stream
import (
"bytes"
"encoding/gob"
"golang.org/x/exp/constraints"
)
// A stream should implements methods:
// type StreamI[T any] interface {
// // part methods of Java Stream Specification.
// Distinct() StreamI[T]
// Filter(predicate func(item T) bool) StreamI[T]
// FlatMap(mapper func(item T) StreamI[T]) StreamI[T]
// Map(mapper func(item T) T) StreamI[T]
// Peek(consumer func(item T)) StreamI[T]
// Sort(less func(a, b T) bool) StreamI[T]
// Max(less func(a, b T) bool) (T, bool)
// Min(less func(a, b T) bool) (T, bool)
// Limit(maxSize int) StreamI[T]
// Skip(n int) StreamI[T]
// AllMatch(predicate func(item T) bool) bool
// AnyMatch(predicate func(item T) bool) bool
// NoneMatch(predicate func(item T) bool) bool
// ForEach(consumer func(item T))
// Reduce(init T, accumulator func(a, b T) T) T
// Count() int
// FindFirst() (T, bool)
// ToSlice() []T
// // part of methods custom extension
// Reverse() StreamI[T]
// Range(start, end int64) StreamI[T]
// Concat(streams ...StreamI[T]) StreamI[T]
// }
type stream[T any] struct {
source []T
}
// Of creates a stream stream whose elements are the specified values.
func Of[T any](elems ...T) stream[T] {
return FromSlice(elems)
}
// Generate stream where each element is generated by the provided generater function
// generater function: func() func() (item T, ok bool) {}
func Generate[T any](generator func() func() (item T, ok bool)) stream[T] {
source := make([]T, 0)
var zeroValue T
for next, item, ok := generator(), zeroValue, true; ok; {
item, ok = next()
if ok {
source = append(source, item)
}
}
return FromSlice(source)
}
// FromSlice create stream from slice.
func FromSlice[T any](source []T) stream[T] {
return stream[T]{source: source}
}
// FromChannel create stream from channel.
func FromChannel[T any](source <-chan T) stream[T] {
s := make([]T, 0)
for v := range source {
s = append(s, v)
}
return FromSlice(s)
}
// FromRange create a number stream from start to end. both start and end are included. [start, end]
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] {
if end < start {
panic("stream.FromRange: param start should be before param end")
} else if step <= 0 {
panic("stream.FromRange: param step should be positive")
}
l := int((end-start)/step) + 1
source := make([]T, l, l)
for i := 0; i < l; i++ {
source[i] = start + (T(i) * step)
}
return FromSlice(source)
}
// Distinct returns a stream that removes the duplicated items.
func (s stream[T]) Distinct() stream[T] {
source := make([]T, 0)
distinct := map[string]bool{}
for _, v := range s.source {
// todo: performance issue
k := hashKey(v)
if _, ok := distinct[k]; !ok {
distinct[k] = true
source = append(source, v)
}
}
return FromSlice(source)
}
func hashKey(data any) string {
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
if err != nil {
panic("stream.hashKey: get hashkey failed")
}
return buffer.String()
}
// Filter returns a stream consisting of the elements of this stream that match the given predicate.
func (s stream[T]) Filter(predicate func(item T) bool) stream[T] {
source := make([]T, 0)
for _, v := range s.source {
if predicate(v) {
source = append(source, v)
}
}
return FromSlice(source)
}
// Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream.
func (s stream[T]) Map(mapper func(item T) T) stream[T] {
source := make([]T, s.Count(), s.Count())
for i, v := range s.source {
source[i] = mapper(v)
}
return FromSlice(source)
}
// Peek returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
func (s stream[T]) Peek(consumer func(item T)) stream[T] {
for _, v := range s.source {
consumer(v)
}
return s
}
// Skip returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
// If this stream contains fewer than n elements then an empty stream will be returned.
func (s stream[T]) Skip(n int) stream[T] {
if n <= 0 {
return s
}
source := make([]T, 0)
l := len(s.source)
if n > l {
return FromSlice(source)
}
for i := n; i < l; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
func (s stream[T]) Limit(maxSize int) stream[T] {
if s.source == nil {
return s
}
if maxSize < 0 {
return FromSlice([]T{})
}
source := make([]T, 0, maxSize)
for i := 0; i < len(s.source) && i < maxSize; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// AllMatch returns whether all elements of this stream match the provided predicate.
func (s stream[T]) AllMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if !predicate(v) {
return false
}
}
return true
}
// AnyMatch returns whether any elements of this stream match the provided predicate.
func (s stream[T]) AnyMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if predicate(v) {
return true
}
}
return false
}
// NoneMatch returns whether no elements of this stream match the provided predicate.
func (s stream[T]) NoneMatch(predicate func(item T) bool) bool {
return !s.AnyMatch(predicate)
}
// ForEach performs an action for each element of this stream.
func (s stream[T]) ForEach(action func(item T)) {
for _, v := range s.source {
action(v)
}
}
// Reduce performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
func (s stream[T]) Reduce(init T, accumulator func(a, b T) T) T {
for _, v := range s.source {
init = accumulator(init, v)
}
return init
}
// Count returns the count of elements in the stream.
func (s stream[T]) Count() int {
return len(s.source)
}
// ToSlice return the elements in the stream.
func (s stream[T]) ToSlice() []T {
return s.source
}

249
stream/stream_test.go Normal file
View File

@@ -0,0 +1,249 @@
package stream
import (
"fmt"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestOf(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
stream := Of(1, 2, 3)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestGenerate(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
n := 0
max := 4
generator := func() func() (int, bool) {
return func() (int, bool) {
n++
return n, n < max
}
}
stream := Generate(generator)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
stream := FromSlice([]int{1, 2, 3})
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromChannel(t *testing.T) {
assert := internal.NewAssert(t, "TestFromChannel")
ch := make(chan int)
go func() {
for i := 1; i < 4; i++ {
ch <- i
}
close(ch)
}()
stream := FromChannel(ch)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromRange(t *testing.T) {
assert := internal.NewAssert(t, "TestFromRange")
s1 := FromRange(1, 5, 1)
s2 := FromRange(1.1, 5.0, 1.0)
assert.Equal([]int{1, 2, 3, 4, 5}, s1.ToSlice())
assert.Equal([]float64{1.1, 2.1, 3.1, 4.1}, s2.ToSlice())
}
func TestStream_Distinct(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Distinct")
nums := FromSlice([]int{1, 2, 2, 3, 3, 3})
distinctNums := nums.Distinct()
assert.Equal([]int{1, 2, 2, 3, 3, 3}, nums.ToSlice())
assert.Equal([]int{1, 2, 3}, distinctNums.ToSlice())
type Person struct {
Id string
Name string
Age uint
}
people := []Person{
{Id: "001", Name: "Tom", Age: 10},
{Id: "001", Name: "Tom", Age: 10},
{Id: "002", Name: "Jim", Age: 20},
{Id: "003", Name: "Mike", Age: 30},
}
stream := FromSlice(people)
distinctStream := stream.Distinct()
// {[{001 Tom 10} {001 Tom 10} {002 Jim 20} {003 Mike 30}]}
t.Log(stream)
// {[{001 Tom 10} {002 Jim 20} {003 Mike 30}]}
t.Log(distinctStream)
}
func TestStream_Filter(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Filter")
stream := FromSlice([]int{1, 2, 3, 4, 5})
isEven := func(n int) bool {
return n%2 == 0
}
even := stream.Filter(isEven)
assert.Equal([]int{2, 4}, even.ToSlice())
}
func TestStream_Map(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Map")
stream := FromSlice([]int{1, 2, 3})
addOne := func(n int) int {
return n + 1
}
s := stream.Map(addOne)
assert.Equal([]int{2, 3, 4}, s.ToSlice())
}
func TestStream_Peek(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Peek")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
result := []string{}
stream = stream.Filter(func(n int) bool {
return n <= 3
}).Peek(func(n int) {
result = append(result, fmt.Sprint("current: ", n))
})
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
assert.Equal([]string{
"current: 1", "current: 2", "current: 3",
}, result)
}
func TestStream_Skip(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Peek")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
s1 := stream.Skip(-1)
s2 := stream.Skip(0)
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s1.ToSlice())
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s2.ToSlice())
}
func TestStream_Limit(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Limit")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
s1 := stream.Limit(-1)
s2 := stream.Limit(0)
s3 := stream.Limit(1)
s4 := stream.Limit(6)
assert.Equal([]int{}, s1.ToSlice())
assert.Equal([]int{}, s2.ToSlice())
assert.Equal([]int{1}, s3.ToSlice())
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s4.ToSlice())
}
func TestStream_AllMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_AllMatch")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
result1 := stream.AllMatch(func(item int) bool {
return item > 0
})
result2 := stream.AllMatch(func(item int) bool {
return item > 1
})
assert.Equal(true, result1)
assert.Equal(false, result2)
}
func TestStream_AnyMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_AnyMatch")
stream := FromSlice([]int{1, 2, 3})
result1 := stream.AnyMatch(func(item int) bool {
return item > 3
})
result2 := stream.AnyMatch(func(item int) bool {
return item > 1
})
assert.Equal(false, result1)
assert.Equal(true, result2)
}
func TestStream_NoneMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_NoneMatch")
stream := FromSlice([]int{1, 2, 3})
result1 := stream.NoneMatch(func(item int) bool {
return item > 3
})
result2 := stream.NoneMatch(func(item int) bool {
return item > 1
})
assert.Equal(true, result1)
assert.Equal(false, result2)
}
func TestStream_ForEach(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_ForEach")
stream := FromSlice([]int{1, 2, 3})
result := 0
stream.ForEach(func(item int) {
result += item
})
assert.Equal(6, result)
}
func TestStream_Reduce(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Reduce")
stream := FromSlice([]int{1, 2, 3})
result := stream.Reduce(0, func(a, b int) int {
return a + b
})
assert.Equal(6, result)
}

View File

@@ -283,6 +283,7 @@ func SplitEx(s, sep string, removeEmptyString bool) []string {
} }
// Substring returns a substring of the specified length starting at the specified offset position. // Substring returns a substring of the specified length starting at the specified offset position.
// Play: https://go.dev/play/p/q3sM6ehnPDp
func Substring(s string, offset int, length uint) string { func Substring(s string, offset int, length uint) string {
rs := []rune(s) rs := []rune(s)
size := len(rs) size := len(rs)

View File

@@ -3,10 +3,10 @@ package system
import "fmt" import "fmt"
func ExampleSetOsEnv() { func ExampleSetOsEnv() {
ok := SetOsEnv("foo", "abc") err := SetOsEnv("foo", "abc")
result := GetOsEnv("foo") result := GetOsEnv("foo")
fmt.Println(ok) fmt.Println(err)
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// <nil> // <nil>
@@ -25,14 +25,14 @@ func ExampleGetOsEnv() {
} }
func ExampleRemoveOsEnv() { func ExampleRemoveOsEnv() {
ok1 := SetOsEnv("foo", "abc") err1 := SetOsEnv("foo", "abc")
result1 := GetOsEnv("foo") result1 := GetOsEnv("foo")
ok2 := RemoveOsEnv("foo") err2 := RemoveOsEnv("foo")
result2 := GetOsEnv("foo") result2 := GetOsEnv("foo")
fmt.Println(ok1) fmt.Println(err1)
fmt.Println(ok2) fmt.Println(err2)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -49,9 +49,10 @@ func ExampleCompareOsEnv() {
return return
} }
result1 := CompareOsEnv("foo", "abc") result := CompareOsEnv("foo", "abc")
fmt.Println(result)
fmt.Println(result1)
// Output: // Output:
// true // true
} }

View File

@@ -25,7 +25,7 @@ var (
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`) chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`) chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]") chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`) chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}|\d{4}-\d{8}`)
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`) creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`) base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
) )

View File

@@ -208,6 +208,7 @@ func TestIsChinesePhone(t *testing.T) {
assert.Equal(true, IsChinesePhone("010-32116675")) assert.Equal(true, IsChinesePhone("010-32116675"))
assert.Equal(true, IsChinesePhone("0464-8756213")) assert.Equal(true, IsChinesePhone("0464-8756213"))
assert.Equal(true, IsChinesePhone("0731-82251545")) //长沙晚报电话
assert.Equal(false, IsChinesePhone("123-87562")) assert.Equal(false, IsChinesePhone("123-87562"))
} }

View File

@@ -6,6 +6,7 @@ package xerror
// Unwrap if err is nil then it returns a valid value // Unwrap if err is nil then it returns a valid value
// If err is not nil, Unwrap panics with err. // If err is not nil, Unwrap panics with err.
// Play: https://go.dev/play/p/w84d7Mb3Afk
func Unwrap[T any](val T, err error) T { func Unwrap[T any](val T, err error) T {
if err != nil { if err != nil {
panic(err) panic(err)

View File

@@ -0,0 +1,25 @@
package xerror
import (
"fmt"
"reflect"
"strconv"
)
func ExampleUnwrap() {
result1 := Unwrap(strconv.Atoi("42"))
fmt.Println(result1)
_, err := strconv.Atoi("4o2")
defer func() {
v := recover()
result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
fmt.Println(result2)
}()
Unwrap(strconv.Atoi("4o2"))
// Output:
// 42
// true
}