mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-19 20:22:25 +08:00
Compare commits
47 Commits
99cb1b13a3
...
v2.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b787e99528 | ||
|
|
e54c9b2850 | ||
|
|
8944109c4c | ||
|
|
10e3732f32 | ||
|
|
a415597c6b | ||
|
|
69b32fd043 | ||
|
|
75ed359084 | ||
|
|
2c71b6375c | ||
|
|
2894bec80c | ||
|
|
09ec5b97a6 | ||
|
|
46ecb117a5 | ||
|
|
388171e739 | ||
|
|
6fbaf2b005 | ||
|
|
53a91cad71 | ||
|
|
a51a182fb2 | ||
|
|
64982f0c89 | ||
|
|
f38f69ce17 | ||
|
|
a33ea3d013 | ||
|
|
e35462fb14 | ||
|
|
af106a4a8e | ||
|
|
ec7232ec40 | ||
|
|
67c1b54b5a | ||
|
|
e149ae2f72 | ||
|
|
b1fcfce188 | ||
|
|
259dbce85e | ||
|
|
78aa679670 | ||
|
|
3beb769f09 | ||
|
|
8af02ff95b | ||
|
|
ba4485d8c0 | ||
|
|
8a1dd40738 | ||
|
|
85e2806531 | ||
|
|
1616d3d1be | ||
|
|
aa8e0d5c12 | ||
|
|
f05477d9cf | ||
|
|
cd91e16b26 | ||
|
|
9fcf046fb3 | ||
|
|
c3f1bc39d7 | ||
|
|
f5784b0f46 | ||
|
|
c86a8a479d | ||
|
|
e2aeb8ec07 | ||
|
|
654ba15aaf | ||
|
|
945c59896b | ||
|
|
219e31d929 | ||
|
|
82cb86b35c | ||
|
|
f93c561f5d | ||
|
|
4888909208 | ||
|
|
33c8875d14 |
82
README.md
82
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -25,7 +25,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 500+ go util functions, support string, slice, datetime, net, crypt...
|
- 💪 500+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
- 💅 Only depend on the go standard library.
|
- 💅 Only depends on two kinds of libraries: go standard library and golang.org/x.
|
||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -38,7 +38,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.3.8. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.3.9. </b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
||||||
@@ -128,16 +128,22 @@ import "github.com/duke-git/lancet/v2/compare"
|
|||||||
|
|
||||||
- **<big>Equal</big>** : Checks if two values are equal or not. (check both type and value)
|
- **<big>Equal</big>** : Checks if two values are equal or not. (check both type and value)
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#Equal)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#Equal)]
|
||||||
|
[[play](https://go.dev/play/p/wmVxR-to4lz)]
|
||||||
- **<big>EqualValue</big>** : Checks if two values are equal or not. (check value only)
|
- **<big>EqualValue</big>** : Checks if two values are equal or not. (check value only)
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#EqualValue)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#EqualValue)]
|
||||||
|
[[play](https://go.dev/play/p/fxnna_LLD9u)]
|
||||||
- **<big>LessThan</big>** : Checks if value `left` less than value `right`.
|
- **<big>LessThan</big>** : Checks if value `left` less than value `right`.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#LessThan)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#LessThan)]
|
||||||
|
[[play](https://go.dev/play/p/cYh7FQQj0ne)]
|
||||||
- **<big>GreaterThan</big>** : Checks if value `left` greater than value `right`.
|
- **<big>GreaterThan</big>** : Checks if value `left` greater than value `right`.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterThan)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterThan)]
|
||||||
|
[[play](https://go.dev/play/p/9-NYDFZmIMp)]
|
||||||
- **<big>LessOrEqual</big>** : Checks if value `left` less than or equal than value `right`.
|
- **<big>LessOrEqual</big>** : Checks if value `left` less than or equal than value `right`.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#LessOrEqual)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#LessOrEqual)]
|
||||||
|
[[play](https://go.dev/play/p/e4T_scwoQzp)]
|
||||||
- **<big>GreaterOrEqual</big>** : Checks if value `left` less greater or equal than value `right`.
|
- **<big>GreaterOrEqual</big>** : Checks if value `left` less greater or equal than value `right`.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterOrEqual)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare.md#GreaterOrEqual)]
|
||||||
|
[[play](https://go.dev/play/p/vx8mP0U8DFk)]
|
||||||
|
|
||||||
### 3. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
### 3. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||||
|
|
||||||
@@ -194,13 +200,13 @@ 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)]
|
||||||
- **<big>Nor</big>** : returns true if neither a nor b is truthy.
|
- **<big>Nor</big>** : returns true if neither a nor b is truthy.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Nor)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Nor)]
|
||||||
[[play](https://go.dev/play/p/g2j08F_zZky)
|
[[play](https://go.dev/play/p/g2j08F_zZky)]
|
||||||
- **<big>Xnor</big>** : returns true if both a and b or neither a nor b are truthy.
|
- **<big>Xnor</big>** : returns true if both a and b or neither a nor b are truthy.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Xnor)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Xnor)]
|
||||||
[[play](https://go.dev/play/p/OuDB9g51643)]
|
[[play](https://go.dev/play/p/OuDB9g51643)]
|
||||||
@@ -272,7 +278,10 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
||||||
- **<big>CopyProperties</big>** : copies each field from the source struct into the destination struct.
|
- **<big>CopyProperties</big>** : copies each field from the source struct into the destination struct.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#CopyProperties)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#CopyProperties)]
|
||||||
[[play](https://go.dev/play/p/FOVY3XJL-6B)]
|
[[play](https://go.dev/play/p/oZujoB5Sgg5)]
|
||||||
|
- **<big>ToInterface</big>** : converts reflect value to its interface type.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToInterface)]
|
||||||
|
[[play](https://go.dev/play/p/syqw0-WG7Xd)]
|
||||||
|
|
||||||
### 6. Cryptor package is for data encryption and decryption.
|
### 6. Cryptor package is for data encryption and decryption.
|
||||||
|
|
||||||
@@ -395,6 +404,9 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>AddMinute</big>** : add or sub day to the time.
|
- **<big>AddMinute</big>** : add or sub day to the time.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)]
|
||||||
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||||
|
- **<big>AddYear</big>** : add or sub year to the time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddYear)]
|
||||||
|
[[play](https://go.dev/play/p/MqW2ujnBx10)]
|
||||||
- **<big>BeginOfMinute</big>** : return the date time at the begin of minute of specific date.
|
- **<big>BeginOfMinute</big>** : return the date time at the begin of minute of specific date.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)]
|
||||||
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
||||||
@@ -476,6 +488,15 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>ToIso8601</big>** : return iso8601 time string.
|
- **<big>ToIso8601</big>** : return iso8601 time string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)]
|
||||||
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
- **<big>IsLeapYear</big>** : check if param `year` is leap year or not.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#IsLeapYear)]
|
||||||
|
[[play](https://go.dev/play/p/xS1eS2ejGew)]
|
||||||
|
- **<big>BetweenSeconds</big>** : returns the number of seconds between two times.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BetweenSeconds)]
|
||||||
|
- **<big>DayOfYear</big>** : returns which day of the year the parameter date `t` is.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#DayOfYear)]
|
||||||
|
- **<big>IsWeekend</big>** : checks if passed time is weekend or not.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#IsWeekend)]
|
||||||
|
|
||||||
### 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
### 8. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
@@ -567,14 +588,23 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
[[play](https://go.dev/play/p/s74a9iBGcSw)]
|
[[play](https://go.dev/play/p/s74a9iBGcSw)]
|
||||||
- **<big>IsZipFile</big>** : checks if file is zip file or not.
|
- **<big>IsZipFile</big>** : checks if file is zip file or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsZipFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsZipFile)]
|
||||||
|
[[play](https://go.dev/play/p/9M0g2j_uF_e)]
|
||||||
- **<big>FileSize</big>** : return file size in bytes.
|
- **<big>FileSize</big>** : return file size in bytes.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#FileSize)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#FileSize)]
|
||||||
|
[[play](https://go.dev/play/p/H9Z05uD-Jjc)]
|
||||||
- **<big>MTime</big>** : return file modified time(unix timestamp).
|
- **<big>MTime</big>** : return file modified time(unix timestamp).
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#MTime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#MTime)]
|
||||||
|
[[play](https://go.dev/play/p/s_Tl7lZoAaY)]
|
||||||
- **<big>Sha</big>** : return file sha value.
|
- **<big>Sha</big>** : return file sha value.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Sha)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Sha)]
|
||||||
|
[[play](https://go.dev/play/p/VfEEcO2MJYf)]
|
||||||
- **<big>ReadCsvFile</big>** : read file content into slice.
|
- **<big>ReadCsvFile</big>** : read file content into slice.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ReadCsvFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ReadCsvFile)]
|
||||||
|
[[play](https://go.dev/play/p/OExTkhGEd3_u)]
|
||||||
|
- **<big>WriteBytesToFile</big>** : write bytes to target file.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteBytesToFile)]
|
||||||
|
- **<big>WriteStringToFile</big>** : write string to target file.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteStringToFile)]
|
||||||
|
|
||||||
### 10. Formatter contains some functions for data formatting.
|
### 10. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
@@ -748,7 +778,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)]
|
[[play](https://go.dev/play/p/s0NdFCtwuyd)]
|
||||||
- **<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)]
|
||||||
@@ -776,6 +806,16 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
- **<big>IsPrime</big>** : checks if number is prime number.
|
- **<big>IsPrime</big>** : checks if number is prime number.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#IsPrime)]
|
||||||
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
||||||
|
- **<big>GCD</big>** : return greatest common divisor (GCD) of integers.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#GCD)]
|
||||||
|
[[play](https://go.dev/play/p/CiEceLSoAKB)]
|
||||||
|
- **<big>LCM</big>** : return Least Common Multiple (LCM) of integers.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#LCM)]
|
||||||
|
[[play](https://go.dev/play/p/EjcZxfY7G_g)]
|
||||||
|
- **<big>Cos</big>** : return the cosine of the radian argument.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Cos)]
|
||||||
|
- **<big>Sin</big>** : return the sine of the radian argument.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Sin)]
|
||||||
|
|
||||||
### 14. Netutil package contains functions to get net information and send http request.
|
### 14. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
@@ -845,8 +885,10 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#UploadFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#UploadFile)]
|
||||||
- **<big>IsPingConnected</big>** : checks if can ping the specified host or not.
|
- **<big>IsPingConnected</big>** : checks if can ping the specified host or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPingConnected)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPingConnected)]
|
||||||
|
[[play](https://go.dev/play/p/q8OzTijsA87)]
|
||||||
- **<big>IsTelnetConnected</big>** : checks if can if can telnet the specified host or not.
|
- **<big>IsTelnetConnected</big>** : checks if can if can telnet the specified host or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsTelnetConnected)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsTelnetConnected)]
|
||||||
|
[[play](https://go.dev/play/p/yiLCGtQv_ZG)]
|
||||||
|
|
||||||
### 15. Random package implements some basic functions to generate random int and string.
|
### 15. Random package implements some basic functions to generate random int and string.
|
||||||
|
|
||||||
@@ -984,11 +1026,13 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
|
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
|
||||||
- **<big>FindBy</big>** : iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
- **<big>FindBy</big>** : iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindBy)]
|
||||||
|
[[play](https://go.dev/play/p/n1lysBYl-GB)]
|
||||||
- **<big>FindLast<sup>deprecated</sup></big>** : return the last item that passes a truth test on predicate function.
|
- **<big>FindLast<sup>deprecated</sup></big>** : return the last item that passes a truth test on predicate function.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)]
|
||||||
[[play](https://go.dev/play/p/FFDPV_j7URd)]
|
[[play](https://go.dev/play/p/FFDPV_j7URd)]
|
||||||
- **<big>FindLastBy</big>** : iterates over elements of slice, returning the last one that passes a truth test on predicate function.
|
- **<big>FindLastBy</big>** : iterates over elements of slice, returning the last one that passes a truth test on predicate function.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLastBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLastBy)]
|
||||||
|
[[play](https://go.dev/play/p/8iqomzyCl_s)]
|
||||||
- **<big>Flatten</big>** : flattens slice one level.
|
- **<big>Flatten</big>** : flattens slice one level.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)]
|
||||||
[[play](https://go.dev/play/p/hYa3cBEevtm)]
|
[[play](https://go.dev/play/p/hYa3cBEevtm)]
|
||||||
@@ -1178,6 +1222,9 @@ import "github.com/duke-git/lancet/v2/stream"
|
|||||||
- **<big>FindFirst</big>** : returns the first element of this stream and true, or zero value and false if the stream is empty.
|
- **<big>FindFirst</big>** : returns the first element of this stream and true, or zero value and false if the stream is empty.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#FindFirst)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#FindFirst)]
|
||||||
[[play](https://go.dev/play/p/9xEf0-6C1e3)]
|
[[play](https://go.dev/play/p/9xEf0-6C1e3)]
|
||||||
|
- **<big>FindLast</big>** : returns the last element of this stream and true, or zero value and false if the stream is empty.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#FindLast)]
|
||||||
|
[[play](https://go.dev/play/p/WZD2rDAW-2h)]
|
||||||
- **<big>Max</big>** : returns the maximum element of this stream according to the provided less function. less fuction: a > b
|
- **<big>Max</big>** : returns the maximum element of this stream according to the provided less function. less fuction: a > b
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#Max)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream.md#Max)]
|
||||||
[[play](https://go.dev/play/p/fm-1KOPtGzn)]
|
[[play](https://go.dev/play/p/fm-1KOPtGzn)]
|
||||||
@@ -1258,6 +1305,10 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- **<big>Capitalize</big>** : converts the first character of source string to upper case and the remaining to lower case.
|
- **<big>Capitalize</big>** : converts the first character of source string to upper case and the remaining to lower case.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)]
|
||||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||||
|
- **<big>ContainsAll</big>** : return true if target string contains all the substrings.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ContainsAll)]
|
||||||
|
- **<big>ContainsAny</big>** : return true if target string contains any one of the substrings.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ContainsAny)]
|
||||||
- **<big>IsString</big>** : checks if the parameter value data type is string or not.
|
- **<big>IsString</big>** : checks if the parameter value data type is string or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)]
|
||||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||||
@@ -1314,16 +1365,33 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
||||||
- **<big>StringToBytes</big>** : converts a string to byte slice without a memory allocation.
|
- **<big>StringToBytes</big>** : converts a string to byte slice without a memory allocation.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#StringToBytes)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#StringToBytes)]
|
||||||
|
[[play](https://go.dev/play/p/7OyFBrf9AxA)]
|
||||||
- **<big>BytesToString</big>** : converts a byte slice to string without a memory allocation.
|
- **<big>BytesToString</big>** : converts a byte slice to string without a memory allocation.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BytesToString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BytesToString)]
|
||||||
|
[[play](https://go.dev/play/p/6c68HRvJecH)]
|
||||||
- **<big>IsBlank</big>** : checks if a string is whitespace or empty.
|
- **<big>IsBlank</big>** : checks if a string is whitespace or empty.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsBlank)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsBlank)]
|
||||||
|
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
||||||
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
|
- **<big>HasPrefixAny</big>** : checks if a string starts with any of an array of specified strings.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasPrefixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasPrefixAny)]
|
||||||
|
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||||
- **<big>HasSuffixAny</big>** : checks if a string ends with any of an array of specified strings.
|
- **<big>HasSuffixAny</big>** : checks if a string ends with any of an array of specified strings.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasSuffixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HasSuffixAny)]
|
||||||
|
[[play](https://go.dev/play/p/sKWpCQdOVkx)]
|
||||||
- **<big>IndexOffset</big>** : returns the index of the first instance of substr in string after offsetting the string by `idxFrom`.
|
- **<big>IndexOffset</big>** : returns the index of the first instance of substr in string after offsetting the string by `idxFrom`.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IndexOffset)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IndexOffset)]
|
||||||
|
[[play](https://go.dev/play/p/qZo4lV2fomB)]
|
||||||
|
- **<big>ReplaceWithMap</big>** : returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReplaceWithMap)]
|
||||||
|
[[play](https://go.dev/play/p/h3t7CNj2Vvu)]
|
||||||
|
- **<big>Trim</big>** : strips whitespace (or other characters) from the beginning and end of a string.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Trim)]
|
||||||
|
[[play](https://go.dev/play/p/Y0ilP0NRV3j)]
|
||||||
|
- **<big>SplitAndTrim</big>** : splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitAndTrim)]
|
||||||
|
[[play](https://go.dev/play/p/ZNL6o4SkYQ7)]
|
||||||
|
- **<big>HideString</big>** : Hide some chars in source string with param `replaceChar`.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#HideString)]
|
||||||
|
|
||||||
### 21. System package contain some functions about os, runtime, shell command.
|
### 21. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
|
|||||||
111
README_zh-CN.md
111
README_zh-CN.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 👏 全面、高效、可复用
|
- 👏 全面、高效、可复用。
|
||||||
- 💪 500+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
|
- 💪 500+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
|
||||||
- 💅 只依赖 go 标准库
|
- 💅 只依赖 go 标准库和 golang.org/x。
|
||||||
- 🌍 所有导出函数单元测试覆盖率 100%
|
- 🌍 所有导出函数单元测试覆盖率100%。
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.7。</b>
|
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.9。</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet// 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
@@ -127,16 +127,22 @@ import "github.com/duke-git/lancet/v2/compare"
|
|||||||
|
|
||||||
- **<big>Equal</big>** : 检查两个值是否相等(检查类型和值)。
|
- **<big>Equal</big>** : 检查两个值是否相等(检查类型和值)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#Equal)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#Equal)]
|
||||||
|
[[play](https://go.dev/play/p/wmVxR-to4lz)]
|
||||||
- **<big>EqualValue</big>** : 检查两个值是否相等(只检查值)。
|
- **<big>EqualValue</big>** : 检查两个值是否相等(只检查值)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#EqualValue)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#EqualValue)]
|
||||||
|
[[play](https://go.dev/play/p/fxnna_LLD9u)]
|
||||||
- **<big>LessThan</big>** : 验证参数`left`的值是否小于参数`right`的值。
|
- **<big>LessThan</big>** : 验证参数`left`的值是否小于参数`right`的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#LessThan)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#LessThan)]
|
||||||
|
[[play](https://go.dev/play/p/cYh7FQQj0ne)]
|
||||||
- **<big>GreaterThan</big>** : 验证参数`left`的值是否大于参数`right`的值。
|
- **<big>GreaterThan</big>** : 验证参数`left`的值是否大于参数`right`的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterThan)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterThan)]
|
||||||
|
[[play](https://go.dev/play/p/9-NYDFZmIMp)]
|
||||||
- **<big>LessOrEqual</big>** : 验证参数`left`的值是否小于或等于参数`right`的值。
|
- **<big>LessOrEqual</big>** : 验证参数`left`的值是否小于或等于参数`right`的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#LessOrEqual)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#LessOrEqual)]
|
||||||
|
[[play](https://go.dev/play/p/e4T_scwoQzp)]
|
||||||
- **<big>GreaterOrEqual</big>** : 验证参数`left`的值是否大于或等于参数`right`的值。
|
- **<big>GreaterOrEqual</big>** : 验证参数`left`的值是否大于或等于参数`right`的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterOrEqual)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/compare_zh-CN.md#GreaterOrEqual)]
|
||||||
|
[[play](https://go.dev/play/p/vx8mP0U8DFk)]
|
||||||
|
|
||||||
### 3. concurrency 包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
### 3. concurrency 包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
||||||
|
|
||||||
@@ -199,10 +205,10 @@ import "github.com/duke-git/lancet/v2/condition"
|
|||||||
[[play](https://go.dev/play/p/gObZrW7ZbG8)]
|
[[play](https://go.dev/play/p/gObZrW7ZbG8)]
|
||||||
- **<big>Nor</big>** : 异或的取反操作。
|
- **<big>Nor</big>** : 异或的取反操作。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nor)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nor)]
|
||||||
[[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)]
|
||||||
@@ -271,7 +277,10 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
[[play](https://go.dev/play/p/j4DP5dquxnk)]
|
||||||
- **<big>CopyProperties</big>** : 拷贝不同结构体之间的同名字段。
|
- **<big>CopyProperties</big>** : 拷贝不同结构体之间的同名字段。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#CopyProperties)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#CopyProperties)]
|
||||||
[[play](https://go.dev/play/p/FOVY3XJL-6B)]
|
[[play](https://go.dev/play/p/oZujoB5Sgg5)]
|
||||||
|
- **<big>ToInterface</big>** : 将反射值转换成对应的 interface 类型。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToInterface)]
|
||||||
|
[[play](https://go.dev/play/p/syqw0-WG7Xd)]
|
||||||
|
|
||||||
### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
@@ -394,6 +403,9 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>AddMinute</big>** : 将日期加/减分钟数。
|
- **<big>AddMinute</big>** : 将日期加/减分钟数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)]
|
||||||
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||||
|
- **<big>AddYear</big>** : 将日期加/减分年数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddYear)]
|
||||||
|
[[play](https://go.dev/play/p/MqW2ujnBx10)]
|
||||||
- **<big>BeginOfMinute</big>** : 返回指定时间的分钟开始时间。
|
- **<big>BeginOfMinute</big>** : 返回指定时间的分钟开始时间。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMinute)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMinute)]
|
||||||
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
||||||
@@ -475,6 +487,18 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>ToIso8601</big>** : 返回 iso8601 日期字符串。
|
- **<big>ToIso8601</big>** : 返回 iso8601 日期字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)]
|
||||||
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
- **<big>IsLeapYear</big>** :验证是否是闰年。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#IsLeapYear)]
|
||||||
|
[[play](https://go.dev/play/p/xS1eS2ejGew)]
|
||||||
|
- **<big>IsLeapYear</big>** : check if param `year` is leap year or not.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#IsLeapYear)]
|
||||||
|
[[play](https://go.dev/play/p/xS1eS2ejGew)]
|
||||||
|
- **<big>BetweenSeconds</big>** : 返回两个时间的间隔秒数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BetweenSeconds)]
|
||||||
|
- **<big>DayOfYear</big>** : 返回参数日期是一年中的第几天。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#DayOfYear)]
|
||||||
|
- **<big>IsWeekend</big>** : 判断日期是否是周末。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#IsWeekend)]
|
||||||
|
|
||||||
### 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
### 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
@@ -564,16 +588,25 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
- **<big>CurrentPath</big>** : 返回当前位置的绝对路径。
|
- **<big>CurrentPath</big>** : 返回当前位置的绝对路径。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CurrentPath)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CurrentPath)]
|
||||||
[[play](https://go.dev/play/p/s74a9iBGcSw)]
|
[[play](https://go.dev/play/p/s74a9iBGcSw)]
|
||||||
- **<big>IsZipFile</big>** : 判断文件是否是zip压缩文件。
|
- **<big>IsZipFile</big>** : 判断文件是否是 zip 压缩文件。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsZipFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsZipFile)]
|
||||||
|
[[play](https://go.dev/play/p/9M0g2j_uF_e)]
|
||||||
- **<big>FileSize</big>** : 返回文件字节大小。
|
- **<big>FileSize</big>** : 返回文件字节大小。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileSize)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileSize)]
|
||||||
|
[[play](https://go.dev/play/p/H9Z05uD-Jjc)]
|
||||||
- **<big>MTime</big>** : 返回文件修改时间(unix timestamp)。
|
- **<big>MTime</big>** : 返回文件修改时间(unix timestamp)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#MTime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#MTime)]
|
||||||
- **<big>Sha</big>** : 返回文件sha值。
|
[[play](https://go.dev/play/p/s_Tl7lZoAaY)]
|
||||||
|
- **<big>Sha</big>** : 返回文件 sha 值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Sha)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Sha)]
|
||||||
- **<big>ReadCsvFile</big>** : 读取csv文件内容到切片。
|
[[play](https://go.dev/play/p/VfEEcO2MJYf)]
|
||||||
|
- **<big>ReadCsvFile</big>** : 读取 csv 文件内容到切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ReadCsvFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ReadCsvFile)]
|
||||||
|
[[play](https://go.dev/play/p/OExTkhGEd3_u)]
|
||||||
|
- **<big>WriteBytesToFile</big>** : 将bytes写入文件。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteBytesToFile)]
|
||||||
|
- **<big>WriteStringToFile</big>** : 将字符串写入文件。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteStringToFile)]
|
||||||
|
|
||||||
### 10. formatter 格式化器包含一些数据格式化处理方法。
|
### 10. formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
@@ -747,7 +780,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)]
|
[[play](https://go.dev/play/p/s0NdFCtwuyd)]
|
||||||
- **<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)]
|
||||||
@@ -775,6 +808,16 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
- **<big>IsPrime</big>** : 判断质数。
|
- **<big>IsPrime</big>** : 判断质数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#IsPrime)]
|
||||||
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
[[play](https://go.dev/play/p/Rdd8UTHZJ7u)]
|
||||||
|
- **<big>GCD</big>** : 求最大公约数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#GCD)]
|
||||||
|
[[play](https://go.dev/play/p/CiEceLSoAKB)]
|
||||||
|
- **<big>LCM</big>** : 求最小公倍数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#LCM)]
|
||||||
|
[[play](https://go.dev/play/p/EjcZxfY7G_g)]
|
||||||
|
- **<big>Cos</big>** : 计算弧度的余弦值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Cos)]
|
||||||
|
- **<big>Sin</big>** : 计算弧度的正弦值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Sin)]
|
||||||
|
|
||||||
### 14. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
### 14. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
@@ -836,16 +879,18 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)]
|
||||||
- **<big>HttpPatch<sup>deprecated</sup></big>** : 发送 http patch 请求(已弃用:SendRequest 代替)。
|
- **<big>HttpPatch<sup>deprecated</sup></big>** : 发送 http patch 请求(已弃用:SendRequest 代替)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)]
|
||||||
- **<big>ParseHttpResponse</big>** : 解析http响应体到目标结构体。
|
- **<big>ParseHttpResponse</big>** : 解析 http 响应体到目标结构体。
|
||||||
[[doc](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#ParseHttpResponse)]
|
||||||
- **<big>DownloadFile</big>** : 从指定的server地址下载文件。
|
- **<big>DownloadFile</big>** : 从指定的 server 地址下载文件。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DownloadFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DownloadFile)]
|
||||||
- **<big>UploadFile</big>** : 将文件上传指定的server地址。
|
- **<big>UploadFile</big>** : 将文件上传指定的 server 地址。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#UploadFile)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#UploadFile)]
|
||||||
- **<big>IsPingConnected</big>** : 检查能否ping通主机。
|
- **<big>IsPingConnected</big>** : 检查能否 ping 通主机。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPingConnected)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPingConnected)]
|
||||||
- **<big>IsTelnetConnected</big>** : 检查能否telnet到主机。
|
[[play](https://go.dev/play/p/q8OzTijsA87)]
|
||||||
|
- **<big>IsTelnetConnected</big>** : 检查能否 telnet 到主机。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsTelnetConnected)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsTelnetConnected)]
|
||||||
|
[[play](https://go.dev/play/p/yiLCGtQv_ZG)]
|
||||||
|
|
||||||
### 15. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
### 15. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
|
|
||||||
@@ -983,11 +1028,13 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
|
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
|
||||||
- **<big>FindBy</big>** : 遍历切片的元素,返回第一个通过 predicate 函数真值测试的元素。
|
- **<big>FindBy</big>** : 遍历切片的元素,返回第一个通过 predicate 函数真值测试的元素。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindBy)]
|
||||||
|
[[play](https://go.dev/play/p/n1lysBYl-GB)]
|
||||||
- **<big>FindLast<sup>deprecated</sup></big>** : 从头到尾遍历 slice 的元素,返回最后一个通过 predicate 函数真值测试的元素。
|
- **<big>FindLast<sup>deprecated</sup></big>** : 从头到尾遍历 slice 的元素,返回最后一个通过 predicate 函数真值测试的元素。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)]
|
||||||
[[play](https://go.dev/play/p/FFDPV_j7URd)]
|
[[play](https://go.dev/play/p/FFDPV_j7URd)]
|
||||||
- **<big>FindLastBy</big>** : 从遍历slice的元素,返回最后一个通过predicate函数真值测试的元素。
|
- **<big>FindLastBy</big>** : 从遍历 slice 的元素,返回最后一个通过 predicate 函数真值测试的元素。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLastBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLastBy)]
|
||||||
|
[[play](https://go.dev/play/p/8iqomzyCl_s)]
|
||||||
- **<big>Flatten</big>** : 将多维切片展平一层。
|
- **<big>Flatten</big>** : 将多维切片展平一层。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)]
|
||||||
[[play](https://go.dev/play/p/hYa3cBEevtm)]
|
[[play](https://go.dev/play/p/hYa3cBEevtm)]
|
||||||
@@ -1174,9 +1221,12 @@ import "github.com/duke-git/lancet/v2/stream"
|
|||||||
- **<big>Reduce</big>** : 使用关联累加函数对 stream 的元素执行 reduce 操作,并 reduce 操作结果(如果有)。
|
- **<big>Reduce</big>** : 使用关联累加函数对 stream 的元素执行 reduce 操作,并 reduce 操作结果(如果有)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#Reduce)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#Reduce)]
|
||||||
[[play](https://go.dev/play/p/6uzZjq_DJLU)]
|
[[play](https://go.dev/play/p/6uzZjq_DJLU)]
|
||||||
- **<big>FindFirst</big>** : 返回此 stream 的第一个元素和 true,如果 stream 为空,则返回零值和 false。
|
- **<big>FindFirst</big>** : 返回此 stream 的第一个元素,如果 stream 为空,则返回零值和 false。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#FindFirst)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#FindFirst)]
|
||||||
[[play](https://go.dev/play/p/9xEf0-6C1e3)]
|
[[play](https://go.dev/play/p/9xEf0-6C1e3)]
|
||||||
|
- **<big>FindLast</big>** : 返回此 stream 的最后一个元素,如果 stream 为空,则返回零值和 false。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#FindLast)]
|
||||||
|
[[play](https://go.dev/play/p/WZD2rDAW-2h)]
|
||||||
- **<big>Max</big>** : 根据提供的 less 函数返回 stream 的最大元素。less 函数: a > b
|
- **<big>Max</big>** : 根据提供的 less 函数返回 stream 的最大元素。less 函数: a > b
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#Max)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/stream_zh-CN.md#Max)]
|
||||||
[[play](https://go.dev/play/p/fm-1KOPtGzn)]
|
[[play](https://go.dev/play/p/fm-1KOPtGzn)]
|
||||||
@@ -1260,6 +1310,10 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- **<big>Capitalize</big>** : 将字符串的第一个字符转换为大写。
|
- **<big>Capitalize</big>** : 将字符串的第一个字符转换为大写。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)]
|
||||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||||
|
- **<big>ContainsAll</big>** : 判断字符串是否包括全部给定的子字符串切片。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ContainsAll)]
|
||||||
|
- **<big>ContainsAny</big>** : 判断字符串是否包括给定的子字符串切片中任意一个子字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ContainsAny)]
|
||||||
- **<big>IsString</big>** : 判断传入参数的数据类型是否为字符串。
|
- **<big>IsString</big>** : 判断传入参数的数据类型是否为字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)]
|
||||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||||
@@ -1316,16 +1370,33 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
[[play](https://go.dev/play/p/og47F5x_jTZ)]
|
||||||
- **<big>StringToBytes</big>** : 在不分配内存的情况下将字符串转换为字节片。
|
- **<big>StringToBytes</big>** : 在不分配内存的情况下将字符串转换为字节片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#StringToBytes)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#StringToBytes)]
|
||||||
|
[[play](https://go.dev/play/p/7OyFBrf9AxA)]
|
||||||
- **<big>BytesToString</big>** : 在不分配内存的情况下将字节切片转换为字符串。
|
- **<big>BytesToString</big>** : 在不分配内存的情况下将字节切片转换为字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BytesToString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BytesToString)]
|
||||||
|
[[play](https://go.dev/play/p/6c68HRvJecH)]
|
||||||
- **<big>IsBlank</big>** : 检查字符串是否为空格或空。
|
- **<big>IsBlank</big>** : 检查字符串是否为空格或空。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsBlank)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsBlank)]
|
||||||
|
[[play](https://go.dev/play/p/6zXRH_c0Qd3)]
|
||||||
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
|
- **<big>HasPrefixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个开头。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasPrefixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasPrefixAny)]
|
||||||
|
[[play](https://go.dev/play/p/8UUTl2C5slo)]
|
||||||
- **<big>HasSuffixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个结尾。
|
- **<big>HasSuffixAny</big>** : 检查字符串是否以指定字符串数组中的任何一个结尾。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasSuffixAny)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HasSuffixAny)]
|
||||||
- **<big>IndexOffset</big>** : 将字符串偏移idxFrom后,返回字符串中第一个 substr 实例的索引。
|
[[play](https://go.dev/play/p/sKWpCQdOVkx)]
|
||||||
|
- **<big>IndexOffset</big>** : 将字符串偏移 idxFrom 后,返回字符串中第一个 substr 实例的索引。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IndexOffset)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IndexOffset)]
|
||||||
|
[[play](https://go.dev/play/p/qZo4lV2fomB)]
|
||||||
|
- **<big>ReplaceWithMap</big>** : 返回 string 的副本,以无序的方式被 map 替换,区分大小写。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReplaceWithMap)]
|
||||||
|
[[play](https://go.dev/play/p/h3t7CNj2Vvu)]
|
||||||
|
- **<big>Trim</big>** : 从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Trim)]
|
||||||
|
[[play](https://go.dev/play/p/Y0ilP0NRV3j)]
|
||||||
|
- **<big>SplitAndTrim</big>** : 将字符串 str 按字符串 delimiter 拆分为一个切片,并对该数组的每个元素调用 Trim。忽略 Trim 后为空的元素。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitAndTrim)]
|
||||||
|
[[play](https://go.dev/play/p/ZNL6o4SkYQ7)]
|
||||||
|
- **<big>HideString</big>** : 隐藏源字符串中的一些字符。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#HideString)]
|
||||||
|
|
||||||
### 21. system 包含 os, runtime, shell command 的相关函数。
|
### 21. system 包含 os, runtime, shell command 的相关函数。
|
||||||
|
|
||||||
|
|||||||
@@ -27,32 +27,38 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Equal checks if two values are equal or not. (check both type and value)
|
// Equal checks if two values are equal or not. (check both type and value)
|
||||||
|
// Play: https://go.dev/play/p/wmVxR-to4lz
|
||||||
func Equal(left, right any) bool {
|
func Equal(left, right any) bool {
|
||||||
return compareValue(equal, left, right)
|
return compareValue(equal, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EqualValue checks if two values are equal or not. (check value only)
|
// EqualValue checks if two values are equal or not. (check value only)
|
||||||
|
// Play: https://go.dev/play/p/fxnna_LLD9u
|
||||||
func EqualValue(left, right any) bool {
|
func EqualValue(left, right any) bool {
|
||||||
ls, rs := convertor.ToString(left), convertor.ToString(right)
|
ls, rs := convertor.ToString(left), convertor.ToString(right)
|
||||||
return ls == rs
|
return ls == rs
|
||||||
}
|
}
|
||||||
|
|
||||||
// LessThan checks if value `left` less than value `right`.
|
// LessThan checks if value `left` less than value `right`.
|
||||||
|
// Play: https://go.dev/play/p/cYh7FQQj0ne
|
||||||
func LessThan(left, right any) bool {
|
func LessThan(left, right any) bool {
|
||||||
return compareValue(lessThan, left, right)
|
return compareValue(lessThan, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GreaterThan checks if value `left` greater than value `right`.
|
// GreaterThan checks if value `left` greater than value `right`.
|
||||||
|
// Play: https://go.dev/play/p/9-NYDFZmIMp
|
||||||
func GreaterThan(left, right any) bool {
|
func GreaterThan(left, right any) bool {
|
||||||
return compareValue(greaterThan, left, right)
|
return compareValue(greaterThan, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LessOrEqual checks if value `left` less than or equal to value `right`.
|
// LessOrEqual checks if value `left` less than or equal to value `right`.
|
||||||
|
// Play: https://go.dev/play/p/e4T_scwoQzp
|
||||||
func LessOrEqual(left, right any) bool {
|
func LessOrEqual(left, right any) bool {
|
||||||
return compareValue(lessOrEqual, left, right)
|
return compareValue(lessOrEqual, left, right)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GreaterOrEqual checks if value `left` greater than or equal to value `right`.
|
// GreaterOrEqual checks if value `left` greater than or equal to value `right`.
|
||||||
|
// Play: https://go.dev/play/p/vx8mP0U8DFk
|
||||||
func GreaterOrEqual(left, right any) bool {
|
func GreaterOrEqual(left, right any) bool {
|
||||||
return compareValue(greaterOrEqual, left, right)
|
return compareValue(greaterOrEqual, left, right)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ func DeepClone[T any](src T) T {
|
|||||||
|
|
||||||
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
||||||
// use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.
|
// use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/oZujoB5Sgg5
|
||||||
func CopyProperties[T, U any](dst T, src U) error {
|
func CopyProperties[T, U any](dst T, src U) error {
|
||||||
dstType, srcType := reflect.TypeOf(dst), reflect.TypeOf(src)
|
dstType, srcType := reflect.TypeOf(dst), reflect.TypeOf(src)
|
||||||
|
|
||||||
@@ -347,3 +347,31 @@ func CopyProperties[T, U any](dst T, src U) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInterface converts reflect value to its interface type.
|
||||||
|
// Play: https://go.dev/play/p/syqw0-WG7Xd
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool) {
|
||||||
|
if v.IsValid() && v.CanInterface() {
|
||||||
|
return v.Interface(), true
|
||||||
|
}
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Bool:
|
||||||
|
return v.Bool(), true
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return v.Int(), true
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
return v.Uint(), true
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return v.Float(), true
|
||||||
|
case reflect.Complex64, reflect.Complex128:
|
||||||
|
return v.Complex(), true
|
||||||
|
case reflect.String:
|
||||||
|
return v.String(), true
|
||||||
|
case reflect.Ptr:
|
||||||
|
return ToInterface(v.Elem())
|
||||||
|
case reflect.Interface:
|
||||||
|
return ToInterface(v.Elem())
|
||||||
|
default:
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -349,3 +350,17 @@ func ExampleCopyProperties() {
|
|||||||
// 127.0.0.1
|
// 127.0.0.1
|
||||||
// 3
|
// 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleToInterface() {
|
||||||
|
val := reflect.ValueOf("abc")
|
||||||
|
iVal, ok := ToInterface(val)
|
||||||
|
|
||||||
|
fmt.Printf("%T\n", iVal)
|
||||||
|
fmt.Printf("%v\n", iVal)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// string
|
||||||
|
// abc
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|||||||
@@ -367,3 +367,32 @@ func TestCopyProperties(t *testing.T) {
|
|||||||
assert.Equal("127.0.0.1", indicatorVO.Ip)
|
assert.Equal("127.0.0.1", indicatorVO.Ip)
|
||||||
assert.Equal(3, len(indicatorVO.Disk))
|
assert.Equal(3, len(indicatorVO.Disk))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToInterface(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToInterface")
|
||||||
|
|
||||||
|
cases := []reflect.Value{
|
||||||
|
reflect.ValueOf("abc"),
|
||||||
|
reflect.ValueOf(int(0)), reflect.ValueOf(int8(1)), reflect.ValueOf(int16(-1)), reflect.ValueOf(int32(123)), reflect.ValueOf(int64(123)),
|
||||||
|
reflect.ValueOf(uint(123)), reflect.ValueOf(uint8(123)), reflect.ValueOf(uint16(123)), reflect.ValueOf(uint32(123)), reflect.ValueOf(uint64(123)),
|
||||||
|
reflect.ValueOf(float64(12.3)), reflect.ValueOf(float32(12.3)),
|
||||||
|
reflect.ValueOf(true), reflect.ValueOf(false),
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := []interface{}{
|
||||||
|
"abc",
|
||||||
|
0, int8(1), int16(-1), int32(123), int64(123),
|
||||||
|
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
|
||||||
|
float64(12.3), float32(12.3),
|
||||||
|
true, false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(cases); i++ {
|
||||||
|
actual, _ := ToInterface(cases[i])
|
||||||
|
assert.Equal(expected[i], actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
nilVal, ok := ToInterface(reflect.ValueOf(nil))
|
||||||
|
assert.EqualValues(nil, nilVal)
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func init() {
|
|||||||
timeFormat = map[string]string{
|
timeFormat = map[string]string{
|
||||||
"yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05",
|
"yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05",
|
||||||
"yyyy-mm-dd hh:mm": "2006-01-02 15:04",
|
"yyyy-mm-dd hh:mm": "2006-01-02 15:04",
|
||||||
"yyyy-mm-dd hh": "2006-01-02 15:04",
|
"yyyy-mm-dd hh": "2006-01-02 15",
|
||||||
"yyyy-mm-dd": "2006-01-02",
|
"yyyy-mm-dd": "2006-01-02",
|
||||||
"yyyy-mm": "2006-01",
|
"yyyy-mm": "2006-01",
|
||||||
"mm-dd": "01-02",
|
"mm-dd": "01-02",
|
||||||
@@ -72,6 +72,12 @@ 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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddYear add or sub year to the time.
|
||||||
|
// Play: https://go.dev/play/p/MqW2ujnBx10
|
||||||
|
func AddYear(t time.Time, year int64) time.Time {
|
||||||
|
return t.Add(365 * 24 * time.Hour * time.Duration(year))
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
// Play: https://go.dev/play/p/PvfkPpcpBBf
|
||||||
func GetNowDate() string {
|
func GetNowDate() string {
|
||||||
@@ -218,3 +224,32 @@ func BeginOfYear(t time.Time) time.Time {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsLeapYear check if param year is leap year or not.
|
||||||
|
// Play: https://go.dev/play/p/xS1eS2ejGew
|
||||||
|
func IsLeapYear(year int) bool {
|
||||||
|
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BetweenSeconds returns the number of seconds between two times.
|
||||||
|
// Play: todo
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64 {
|
||||||
|
index := t2.Unix() - t1.Unix()
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
// DayOfYear returns which day of the year the parameter date `t` is.
|
||||||
|
// Play: todo
|
||||||
|
func DayOfYear(t time.Time) int {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t.Location())
|
||||||
|
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
|
||||||
|
return int(nowDate.Sub(firstDay).Hours() / 24)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWeekend checks if passed time is weekend or not.
|
||||||
|
// Play: todo
|
||||||
|
func IsWeekend(t time.Time) bool {
|
||||||
|
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,6 +57,23 @@ func ExampleAddMinute() {
|
|||||||
// -2m0s
|
// -2m0s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleAddYear() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after1Year := AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := AddYear(now, -1)
|
||||||
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 8760h0m0s
|
||||||
|
// -8760h0m0s
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleGetNowDate() {
|
func ExampleGetNowDate() {
|
||||||
result := GetNowDate()
|
result := GetNowDate()
|
||||||
|
|
||||||
@@ -114,15 +131,18 @@ func ExampleFormatTimeToStr() {
|
|||||||
result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss")
|
result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss")
|
||||||
result2 := FormatTimeToStr(datetime, "yyyy-mm-dd")
|
result2 := FormatTimeToStr(datetime, "yyyy-mm-dd")
|
||||||
result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss")
|
result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss")
|
||||||
|
result4 := FormatTimeToStr(datetime, "yyyy-mm-dd hh")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2021-01-02 16:04:08
|
// 2021-01-02 16:04:08
|
||||||
// 2021-01-02
|
// 2021-01-02
|
||||||
// 02-01-21 16:04:08
|
// 02-01-21 16:04:08
|
||||||
|
// 2021-01-02 16
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleFormatStrToTime() {
|
func ExampleFormatStrToTime() {
|
||||||
@@ -321,3 +341,70 @@ func ExampleNewUnixNow() {
|
|||||||
// // Output:
|
// // Output:
|
||||||
// // 2006-01-02T23:04:05+08:00
|
// // 2006-01-02T23:04:05+08:00
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func ExampleIsLeapYear() {
|
||||||
|
result1 := IsLeapYear(2000)
|
||||||
|
result2 := IsLeapYear(2001)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBetweenSeconds() {
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 86400
|
||||||
|
// -86400
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleDayOfYear() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIsWeekend() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := IsWeekend(date1)
|
||||||
|
result2 := IsWeekend(date2)
|
||||||
|
result3 := IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,33 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestAddYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAddDay")
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
after2Years := AddYear(now, 1)
|
||||||
|
diff1 := after2Years.Sub(now)
|
||||||
|
assert.Equal(float64(8760), diff1.Hours())
|
||||||
|
|
||||||
|
before2Years := AddYear(now, -1)
|
||||||
|
diff2 := before2Years.Sub(now)
|
||||||
|
assert.Equal(float64(-8760), diff2.Hours())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBetweenSeconds(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBetweenSeconds")
|
||||||
|
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
assert.Equal(int64(86400), result1)
|
||||||
|
assert.Equal(int64(-86400), result2)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddDay(t *testing.T) {
|
func TestAddDay(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestAddDay")
|
assert := internal.NewAssert(t, "TestAddDay")
|
||||||
|
|
||||||
@@ -71,12 +98,16 @@ func TestFormatTimeToStr(t *testing.T) {
|
|||||||
cases := []string{
|
cases := []string{
|
||||||
"yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd",
|
"yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd",
|
||||||
"dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss",
|
"dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss",
|
||||||
"hh:mm:ss", "yyyy/mm"}
|
"hh:mm:ss", "yyyy/mm",
|
||||||
|
"yyyy-mm-dd hh",
|
||||||
|
}
|
||||||
|
|
||||||
expected := []string{
|
expected := []string{
|
||||||
"2021-01-02 16:04:08", "2021-01-02",
|
"2021-01-02 16:04:08", "2021-01-02",
|
||||||
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
||||||
"16:04:08", "2021/01"}
|
"16:04:08", "2021/01",
|
||||||
|
"2021-01-02 16",
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual := FormatTimeToStr(datetime, cases[i])
|
actual := FormatTimeToStr(datetime, cases[i])
|
||||||
@@ -230,3 +261,44 @@ func TestEndOfYear(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsLeapYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfYear")
|
||||||
|
|
||||||
|
result1 := IsLeapYear(2000)
|
||||||
|
result2 := IsLeapYear(2001)
|
||||||
|
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDayOfYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestDayOfYear")
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := DayOfYear(date1)
|
||||||
|
assert.Equal(31, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := DayOfYear(date2)
|
||||||
|
assert.Equal(1, result2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := DayOfYear(date3)
|
||||||
|
assert.Equal(0, result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsWeekend(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsWeekend")
|
||||||
|
date := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
result := IsWeekend(date)
|
||||||
|
assert.Equal(true, result)
|
||||||
|
|
||||||
|
date1 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
result1 := IsWeekend(date1)
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
result2 := IsWeekend(date2)
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import (
|
|||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
- [CopyProperties](#CopyProperties)
|
- [CopyProperties](#CopyProperties)
|
||||||
|
- [ToInterface](#ToInterface)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -629,7 +630,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="DeepClone">DeepClone</span>
|
### <span id="DeepClone">DeepClone</span>
|
||||||
|
|
||||||
<p>Creates a deep copy of passed item, can't clone unexported field of struct.</p>
|
<p>Creates a deep copy of passed item, can't clone unexported field of struct.</p>
|
||||||
@@ -694,7 +694,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyProperties">CopyProperties</span>
|
### <span id="CopyProperties">CopyProperties</span>
|
||||||
|
|
||||||
<p>Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.</p>
|
<p>Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.</p>
|
||||||
@@ -757,7 +756,11 @@ func main() {
|
|||||||
|
|
||||||
indicatorVO := IndicatorVO{}
|
indicatorVO := IndicatorVO{}
|
||||||
|
|
||||||
CopyProperties(&indicatorVO, indicator)
|
err := convertor.CopyProperties(&indicatorVO, indicator)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(indicatorVO.Id)
|
fmt.Println(indicatorVO.Id)
|
||||||
fmt.Println(indicatorVO.Ip)
|
fmt.Println(indicatorVO.Ip)
|
||||||
@@ -768,4 +771,39 @@ func main() {
|
|||||||
// 127.0.0.1
|
// 127.0.0.1
|
||||||
// 3
|
// 3
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ToInterface">ToInterface</span>
|
||||||
|
|
||||||
|
<p>Converts reflect value to its interface type.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
val := reflect.ValueOf("abc")
|
||||||
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
|
fmt.Printf("%T\n", iVal)
|
||||||
|
fmt.Printf("%v\n", iVal)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// string
|
||||||
|
// abc
|
||||||
|
// true
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -40,7 +40,7 @@ import (
|
|||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
- [CopyProperties](#CopyProperties)
|
- [CopyProperties](#CopyProperties)
|
||||||
|
- [ToInterface](#ToInterface)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -756,7 +756,11 @@ func main() {
|
|||||||
|
|
||||||
indicatorVO := IndicatorVO{}
|
indicatorVO := IndicatorVO{}
|
||||||
|
|
||||||
CopyProperties(&indicatorVO, indicator)
|
err := convertor.CopyProperties(&indicatorVO, indicator)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(indicatorVO.Id)
|
fmt.Println(indicatorVO.Id)
|
||||||
fmt.Println(indicatorVO.Ip)
|
fmt.Println(indicatorVO.Ip)
|
||||||
@@ -767,4 +771,39 @@ func main() {
|
|||||||
// 127.0.0.1
|
// 127.0.0.1
|
||||||
// 3
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ToInterface">ToInterface</span>
|
||||||
|
|
||||||
|
<p>将反射值转换成对应的interface类型。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
val := reflect.ValueOf("abc")
|
||||||
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
|
fmt.Printf("%T\n", iVal)
|
||||||
|
fmt.Printf("%v\n", iVal)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// string
|
||||||
|
// abc
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
248
docs/datetime.md
248
docs/datetime.md
@@ -26,6 +26,7 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [AddYear](#AddYear)
|
||||||
- [BeginOfMinute](#BeginOfMinute)
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
- [BeginOfHour](#BeginOfHour)
|
- [BeginOfHour](#BeginOfHour)
|
||||||
- [BeginOfDay](#BeginOfDay)
|
- [BeginOfDay](#BeginOfDay)
|
||||||
@@ -53,6 +54,10 @@ import (
|
|||||||
- [ToFormat](#ToFormat)
|
- [ToFormat](#ToFormat)
|
||||||
- [ToFormatForTpl](#ToFormatForTpl)
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
|
- [IsLeapYear](#IsLeapYear)
|
||||||
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -198,6 +203,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AddYear">AddYear</span>
|
||||||
|
|
||||||
|
<p>Add or sub year to the time.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AddYear(t time.Time, year int64) time.Time
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after1Year := datetime.AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := datetime.AddYear(now, -1)
|
||||||
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 8760h0m0s
|
||||||
|
// -8760h0m0s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <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>
|
||||||
@@ -607,8 +651,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
currentDate := datetime.GetNowDate()
|
currentDate := datetime.GetNowDate()
|
||||||
|
|
||||||
fmt.Println(currentDate)
|
fmt.Println(currentDate)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-01-28
|
// 2022-01-28
|
||||||
}
|
}
|
||||||
@@ -671,8 +715,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
current := datetime.GetNowDateTime()
|
current := datetime.GetNowDateTime()
|
||||||
|
|
||||||
fmt.Println(current)
|
fmt.Println(current)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-01-28 15:59:33
|
// 2022-01-28 15:59:33
|
||||||
}
|
}
|
||||||
@@ -702,9 +746,9 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
zeroTime := datetime.GetZeroHourTimestamp()
|
zeroTime := datetime.GetZeroHourTimestamp()
|
||||||
|
|
||||||
fmt.Println(zeroTime)
|
fmt.Println(zeroTime)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1643299200
|
// 1643299200
|
||||||
}
|
}
|
||||||
@@ -735,8 +779,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
nightTime := datetime.GetNightTimestamp()
|
nightTime := datetime.GetNightTimestamp()
|
||||||
|
|
||||||
fmt.Println(nightTime)
|
fmt.Println(nightTime)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1643385599
|
// 1643385599
|
||||||
}
|
}
|
||||||
@@ -842,8 +886,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -874,8 +918,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnix(1647597438)
|
tm := datetime.NewUnix(1647597438)
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -906,8 +950,8 @@ 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)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647594245}
|
// &{1647594245}
|
||||||
}
|
}
|
||||||
@@ -938,8 +982,8 @@ 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)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1136214245}
|
// &{1136214245}
|
||||||
}
|
}
|
||||||
@@ -967,8 +1011,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm.ToUnix())
|
fmt.Println(tm.ToUnix())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1647597438
|
// 1647597438
|
||||||
}
|
}
|
||||||
@@ -996,8 +1040,8 @@ 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())
|
fmt.Println(tm.ToFormat())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-03-18 17:04:05
|
// 2022-03-18 17:04:05
|
||||||
}
|
}
|
||||||
@@ -1026,8 +1070,8 @@ 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)
|
fmt.Println(ts)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022/03/18 17:04:05
|
// 2022/03/18 17:04:05
|
||||||
}
|
}
|
||||||
@@ -1056,9 +1100,163 @@ 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)
|
fmt.Println(ts)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2006-01-02T23:04:05+08:00
|
// 2006-01-02T23:04:05+08:00
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsLeapYear">IsLeapYear</span>
|
||||||
|
|
||||||
|
<p>check if param `year` is leap year or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsLeapYear(year int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.IsLeapYear(2000)
|
||||||
|
result2 := datetime.IsLeapYear(2001)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BetweenSeconds">BetweenSeconds</span>
|
||||||
|
|
||||||
|
<p>Return the number of seconds between two times.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := datetime.BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := datetime.BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 86400
|
||||||
|
// -86400
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>Returns which day of the year the parameter date `t` is.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>Checks if passed time is weekend or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
# Datetime
|
# Datetime
|
||||||
datetime日期时间处理包,格式化日期,比较日期。
|
|
||||||
|
datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [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)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
@@ -19,74 +21,82 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [AddDay](#AddDay)
|
|
||||||
- [AddHour](#AddHour)
|
- [AddDay](#AddDay)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddHour](#AddHour)
|
||||||
- [BeginOfMinute](#BeginOfMinute)
|
- [AddMinute](#AddMinute)
|
||||||
- [BeginOfHour](#BeginOfHour)
|
- [AddYear](#AddYear)
|
||||||
- [BeginOfDay](#BeginOfDay)
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfHour](#BeginOfHour)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfDay](#BeginOfDay)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfWeek](#EndOfWeek)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfMonth](#EndOfMonth)
|
- [EndOfDay](#EndOfDay)
|
||||||
- [EndOfYear](#EndOfYear)
|
- [EndOfWeek](#EndOfWeek)
|
||||||
- [GetNowDate](#GetNowDate)
|
- [EndOfMonth](#EndOfMonth)
|
||||||
- [GetNowTime](#GetNowTime)
|
- [EndOfYear](#EndOfYear)
|
||||||
- [GetNowDateTime](#GetNowDateTime)
|
- [GetNowDate](#GetNowDate)
|
||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetNowTime](#GetNowTime)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNowDateTime](#GetNowDateTime)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [NewUnixNow](#NewUnixNow)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [NewUnix](#NewUnix)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
- [NewFormat](#NewFormat)
|
- [NewUnixNow](#NewUnixNow)
|
||||||
- [NewISO8601](#NewISO8601)
|
- [NewUnix](#NewUnix)
|
||||||
- [ToUnix](#ToUnix)
|
- [NewFormat](#NewFormat)
|
||||||
- [ToFormat](#ToFormat)
|
- [NewISO8601](#NewISO8601)
|
||||||
- [ToFormatForTpl](#ToFormatForTpl)
|
- [ToUnix](#ToUnix)
|
||||||
- [ToIso8601](#ToIso8601)
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
- [IsLeapYear](#IsLeapYear)
|
||||||
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
## 注:
|
## 注:
|
||||||
1. 方法FormatTimeToStr和FormatStrToTime中的format参数值需要传以下类型之一:
|
|
||||||
- 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. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
||||||
|
|
||||||
|
- 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>将日期加/减天数。</p>
|
<p>将日期加/减天数。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func AddDay(t time.Time, day int64) time.Time
|
func AddDay(t time.Time, day int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -119,13 +129,13 @@ func main() {
|
|||||||
|
|
||||||
<p>将日期加/减小时数。</p>
|
<p>将日期加/减小时数。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func AddHour(t time.Time, hour int64) time.Time
|
func AddHour(t time.Time, hour int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -158,13 +168,13 @@ func main() {
|
|||||||
|
|
||||||
<p>将日期加/减分钟数。</p>
|
<p>将日期加/减分钟数。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func AddMinute(t time.Time, minute int64) time.Time
|
func AddMinute(t time.Time, minute int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -193,17 +203,56 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AddYear">AddYear</span>
|
||||||
|
|
||||||
|
<p>将日期加/减年数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AddYear(t time.Time, year int64) time.Time
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after1Year := datetime.AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := datetime.AddYear(now, -1)
|
||||||
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 8760h0m0s
|
||||||
|
// -8760h0m0s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟开始时间。</p>
|
<p>返回指定时间的分钟开始时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeginOfMinute(t time.Time) time.Time
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -229,13 +278,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的小时开始时间。</p>
|
<p>返回指定时间的小时开始时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeginOfHour(t time.Time) time.Time
|
func BeginOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -261,13 +310,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当天开始时间。</p>
|
<p>返回指定时间的当天开始时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeginOfDay(t time.Time) time.Time
|
func BeginOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -293,13 +342,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的每周开始时间,默认开始时间星期日。</p>
|
<p>返回指定时间的每周开始时间,默认开始时间星期日。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```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>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -325,13 +374,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当月开始时间。</p>
|
<p>返回指定时间的当月开始时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeginOfMonth(t time.Time) time.Time
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -357,13 +406,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当年开始时间</p>
|
<p>返回指定时间的当年开始时间</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeginOfYear(t time.Time) time.Time
|
func BeginOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -389,13 +438,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的分钟结束时间。</p>
|
<p>返回指定时间的分钟结束时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EndOfMinute(t time.Time) time.Time
|
func EndOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -421,13 +470,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的小时结束时间。</p>
|
<p>返回指定时间的小时结束时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EndOfHour(t time.Time) time.Time
|
func EndOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -453,13 +502,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当天结束时间。</p>
|
<p>返回指定时间的当天结束时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EndOfDay(t time.Time) time.Time
|
func EndOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -485,13 +534,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的星期结束时间,默认结束时间星期六。</p>
|
<p>返回指定时间的星期结束时间,默认结束时间星期六。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```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>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -517,13 +566,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当月结束时间。</p>
|
<p>返回指定时间的当月结束时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EndOfMonth(t time.Time) time.Time
|
func EndOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -549,13 +598,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回指定时间的当年结束时间。</p>
|
<p>返回指定时间的当年结束时间。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EndOfYear(t time.Time) time.Time
|
func EndOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -581,13 +630,13 @@ func main() {
|
|||||||
|
|
||||||
<p>获取当天日期,返回格式:yyyy-mm-dd。</p>
|
<p>获取当天日期,返回格式:yyyy-mm-dd。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetNowDate() string
|
func GetNowDate() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -602,8 +651,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
currentDate := datetime.GetNowDate()
|
currentDate := datetime.GetNowDate()
|
||||||
|
|
||||||
fmt.Println(currentDate)
|
fmt.Println(currentDate)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-01-28
|
// 2022-01-28
|
||||||
}
|
}
|
||||||
@@ -613,13 +662,13 @@ func main() {
|
|||||||
|
|
||||||
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetNowTime() string
|
func GetNowTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -645,13 +694,13 @@ func main() {
|
|||||||
|
|
||||||
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss。</p>
|
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetNowDateTime() string
|
func GetNowDateTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -666,8 +715,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
current := datetime.GetNowDateTime()
|
current := datetime.GetNowDateTime()
|
||||||
|
|
||||||
fmt.Println(current)
|
fmt.Println(current)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-01-28 15:59:33
|
// 2022-01-28 15:59:33
|
||||||
}
|
}
|
||||||
@@ -677,13 +726,13 @@ func main() {
|
|||||||
|
|
||||||
<p>获取零点时间戳(timestamp of 00:00)</p>
|
<p>获取零点时间戳(timestamp of 00:00)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetZeroHourTimestamp() int64
|
func GetZeroHourTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -697,9 +746,9 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
zeroTime := datetime.GetZeroHourTimestamp()
|
zeroTime := datetime.GetZeroHourTimestamp()
|
||||||
|
|
||||||
fmt.Println(zeroTime)
|
fmt.Println(zeroTime)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1643299200
|
// 1643299200
|
||||||
}
|
}
|
||||||
@@ -709,13 +758,13 @@ func main() {
|
|||||||
|
|
||||||
<p>获取午夜时间戳(timestamp of 23:59)。</p>
|
<p>获取午夜时间戳(timestamp of 23:59)。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetNightTimestamp() int64
|
func GetNightTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -730,8 +779,8 @@ func main() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
nightTime := datetime.GetNightTimestamp()
|
nightTime := datetime.GetNightTimestamp()
|
||||||
|
|
||||||
fmt.Println(nightTime)
|
fmt.Println(nightTime)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1643385599
|
// 1643385599
|
||||||
}
|
}
|
||||||
@@ -741,13 +790,13 @@ func main() {
|
|||||||
|
|
||||||
<p>将日期格式化成字符串,`format` 参数格式参考注1。</p>
|
<p>将日期格式化成字符串,`format` 参数格式参考注1。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -780,13 +829,13 @@ func main() {
|
|||||||
|
|
||||||
<p>将字符串格式化成时间,`format` 参数格式参考注1。</p>
|
<p>将字符串格式化成时间,`format` 参数格式参考注1。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -816,7 +865,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个当前时间的unix时间戳。</p>
|
<p>创建一个当前时间的unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -825,7 +874,7 @@ type theTime struct {
|
|||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -837,8 +886,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -848,7 +897,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个unix时间戳。</p>
|
<p>创建一个unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -857,7 +906,7 @@ type theTime struct {
|
|||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -869,8 +918,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnix(1647597438)
|
tm := datetime.NewUnix(1647597438)
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -880,7 +929,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。</p>
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -889,7 +938,7 @@ type theTime struct {
|
|||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -901,8 +950,8 @@ 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)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647594245}
|
// &{1647594245}
|
||||||
}
|
}
|
||||||
@@ -912,7 +961,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个iso8601格式时间字符串的unix时间戳。</p>
|
<p>创建一个iso8601格式时间字符串的unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -921,7 +970,7 @@ type theTime struct {
|
|||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -933,8 +982,8 @@ 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)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1136214245}
|
// &{1136214245}
|
||||||
}
|
}
|
||||||
@@ -944,13 +993,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回unix时间戳。</p>
|
<p>返回unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *theTime) ToUnix() int64
|
func (t *theTime) ToUnix() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -962,8 +1011,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm.ToUnix())
|
fmt.Println(tm.ToUnix())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1647597438
|
// 1647597438
|
||||||
}
|
}
|
||||||
@@ -973,13 +1022,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。</p>
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormat() string
|
func (t *theTime) ToFormat() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -991,8 +1040,8 @@ 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())
|
fmt.Println(tm.ToFormat())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022-03-18 17:04:05
|
// 2022-03-18 17:04:05
|
||||||
}
|
}
|
||||||
@@ -1002,13 +1051,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回tpl格式指定的日期字符串。</p>
|
<p>返回tpl格式指定的日期字符串。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1021,8 +1070,8 @@ 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)
|
fmt.Println(ts)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2022/03/18 17:04:05
|
// 2022/03/18 17:04:05
|
||||||
}
|
}
|
||||||
@@ -1032,13 +1081,13 @@ func main() {
|
|||||||
|
|
||||||
<p>返回iso8601日期字符串。</p>
|
<p>返回iso8601日期字符串。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *theTime) ToIso8601() string
|
func (t *theTime) ToIso8601() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -1051,9 +1100,160 @@ 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)
|
fmt.Println(ts)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2006-01-02T23:04:05+08:00
|
// 2006-01-02T23:04:05+08:00
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsLeapYear">IsLeapYear</span>
|
||||||
|
|
||||||
|
<p>验证是否是闰年。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsLeapYear(year int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.IsLeapYear(2000)
|
||||||
|
result2 := datetime.IsLeapYear(2001)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BetweenSeconds">BetweenSeconds</span>
|
||||||
|
|
||||||
|
<p>返回两个时间的间隔秒数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := datetime.BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := datetime.BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 86400
|
||||||
|
// -86400
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>返回参数日期是一年中的第几天。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>判断日期是否是周末。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
102
docs/fileutil.md
102
docs/fileutil.md
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -522,7 +524,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isZip := IsZipFile("./zipfile.zip")
|
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||||
fmt.Println(isZip)
|
fmt.Println(isZip)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -660,3 +662,101 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>Writes bytes to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>Writes string to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -522,7 +524,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isZip := IsZipFile("./zipfile.zip")
|
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||||
fmt.Println(isZip)
|
fmt.Println(isZip)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -660,3 +662,103 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>将bytes写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>将字符串写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
201
docs/mathutil.md
201
docs/mathutil.md
@@ -40,6 +40,10 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
- [Cos](#Cos)
|
||||||
|
- [Sin](#Sin)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -365,13 +369,16 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Percent(1, 2, 2)
|
result1 := mathutil.Percent(1, 2, 2)
|
||||||
result2 := mathutil.Percent(0.1, 0.3, 2)
|
result2 := mathutil.Percent(0.1, 0.3, 2)
|
||||||
|
result3 := mathutil.Percent(-30305, 408420, 2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 0.5
|
// 50
|
||||||
// 0.33
|
// 33.33
|
||||||
|
// -7.42
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -597,7 +604,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RadianToAngle">RadianToAngle</span>
|
### <span id="RadianToAngle">RadianToAngle</span>
|
||||||
|
|
||||||
<p>Converts radian value to angle value.</p>
|
<p>Converts radian value to angle value.</p>
|
||||||
@@ -664,7 +670,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPrime">IsPrime</span>
|
### <span id="IsPrime">IsPrime</span>
|
||||||
|
|
||||||
<p>Checks if number is prime number.</p>
|
<p>Checks if number is prime number.</p>
|
||||||
@@ -687,19 +692,183 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.IsPrime(-1)
|
result1 := mathutil.IsPrime(-1)
|
||||||
result2 := mathutil.IsPrime(0)
|
result2 := mathutil.IsPrime(0)
|
||||||
result3 := mathutil.IsPrime(1)
|
result3 := mathutil.IsPrime(1)
|
||||||
result4 := mathutil.IsPrime(2)
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GCD">GCD</span>
|
||||||
|
|
||||||
|
<p>Return greatest common divisor (GCD) of integers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GCD[T constraints.Integer](integers ...T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.GCD(1, 1)
|
||||||
|
result2 := mathutil.GCD(1, -1)
|
||||||
|
result3 := mathutil.GCD(-1, 1)
|
||||||
|
result4 := mathutil.GCD(-1, -1)
|
||||||
|
result5 := mathutil.GCD(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LCM">LCM</span>
|
||||||
|
|
||||||
|
<p>Return Least Common Multiple (LCM) of integers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LCM[T constraints.Integer](integers ...T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.LCM(1, 1)
|
||||||
|
result2 := mathutil.LCM(1, 2)
|
||||||
|
result3 := mathutil.LCM(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 18
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Cos">Cos</span>
|
||||||
|
|
||||||
|
<p>Returns the cosine of the radian argument.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Cos(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Cos(0)
|
||||||
|
result2 := mathutil.Cos(90)
|
||||||
|
result3 := mathutil.Cos(180)
|
||||||
|
result4 := mathutil.Cos(math.Pi)
|
||||||
|
result5 := mathutil.Cos(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// -0.447
|
||||||
|
// -0.598
|
||||||
|
// -1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Sin">Sin</span>
|
||||||
|
|
||||||
|
<p>Returns the sine of the radian argument.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sin(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Sin(0)
|
||||||
|
result2 := mathutil.Sin(90)
|
||||||
|
result3 := mathutil.Sin(180)
|
||||||
|
result4 := mathutil.Sin(math.Pi)
|
||||||
|
result5 := mathutil.Sin(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0.894
|
||||||
|
// -0.801
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -40,6 +40,10 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
- [Cos](#Cos)
|
||||||
|
- [Sin](#Sin)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -365,13 +369,16 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Percent(1, 2, 2)
|
result1 := mathutil.Percent(1, 2, 2)
|
||||||
result2 := mathutil.Percent(0.1, 0.3, 2)
|
result2 := mathutil.Percent(0.1, 0.3, 2)
|
||||||
|
result3 := mathutil.Percent(-30305, 408420, 2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 0.5
|
// 50
|
||||||
// 0.33
|
// 33.33
|
||||||
|
// -7.42
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -663,8 +670,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPrime">IsPrime</span>
|
### <span id="IsPrime">IsPrime</span>
|
||||||
|
|
||||||
<p>判断质数。</p>
|
<p>判断质数。</p>
|
||||||
@@ -687,18 +692,183 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.IsPrime(-1)
|
result1 := mathutil.IsPrime(-1)
|
||||||
result2 := mathutil.IsPrime(0)
|
result2 := mathutil.IsPrime(0)
|
||||||
result3 := mathutil.IsPrime(1)
|
result3 := mathutil.IsPrime(1)
|
||||||
result4 := mathutil.IsPrime(2)
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GCD">GCD</span>
|
||||||
|
|
||||||
|
<p>计算最大公约数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GCD[T constraints.Integer](integers ...T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.GCD(1, 1)
|
||||||
|
result2 := mathutil.GCD(1, -1)
|
||||||
|
result3 := mathutil.GCD(-1, 1)
|
||||||
|
result4 := mathutil.GCD(-1, -1)
|
||||||
|
result5 := mathutil.GCD(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LCM">LCM</span>
|
||||||
|
|
||||||
|
<p>计算最小公倍数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LCM[T constraints.Integer](integers ...T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.LCM(1, 1)
|
||||||
|
result2 := mathutil.LCM(1, 2)
|
||||||
|
result3 := mathutil.LCM(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 18
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Cos">Cos</span>
|
||||||
|
|
||||||
|
<p>计算弧度的余弦值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Cos(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Cos(0)
|
||||||
|
result2 := mathutil.Cos(90)
|
||||||
|
result3 := mathutil.Cos(180)
|
||||||
|
result4 := mathutil.Cos(math.Pi)
|
||||||
|
result5 := mathutil.Cos(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// -0.447
|
||||||
|
// -0.598
|
||||||
|
// -1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Sin">Sin</span>
|
||||||
|
|
||||||
|
<p>计算弧度的正弦值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sin(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Sin(0)
|
||||||
|
result2 := mathutil.Sin(90)
|
||||||
|
result3 := mathutil.Sin(180)
|
||||||
|
result4 := mathutil.Sin(math.Pi)
|
||||||
|
result5 := mathutil.Sin(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0.894
|
||||||
|
// -0.801
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -620,9 +620,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -662,9 +662,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request.(Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -692,28 +692,26 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
postData := url.Values{}
|
||||||
Title string `json:"title"`
|
postData.Add("userId", "1")
|
||||||
}
|
postData.Add("title", "TestToDo")
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -761,9 +759,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -800,9 +798,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -922,7 +920,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
err := netutil.DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
||||||
|
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -622,9 +622,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -664,9 +664,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -694,28 +694,26 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
postData := url.Values{}
|
||||||
Title string `json:"title"`
|
postData.Add("userId", "1")
|
||||||
}
|
postData.Add("title", "TestToDo")
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -763,9 +761,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -802,9 +800,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求。(已废弃:使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -924,7 +922,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
err := netutil.DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
||||||
|
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
- [FindFirst](#FindFirst)
|
- [FindFirst](#FindFirst)
|
||||||
|
- [FindLast](#FindLast)
|
||||||
- [Max](#Max)
|
- [Max](#Max)
|
||||||
- [Min](#Min)
|
- [Min](#Min)
|
||||||
- [AllMatch](#AllMatch)
|
- [AllMatch](#AllMatch)
|
||||||
@@ -667,6 +668,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
|
<p>Returns the last element of this stream and true, or zero value and false if the stream is empty.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s stream[T]) FindLast() (T, bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/stream"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
original := stream.FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := original.FindLast()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Max">Max</span>
|
### <span id="Max">Max</span>
|
||||||
|
|
||||||
<p>Returns the maximum element of this stream according to the provided less function. less fuction: a > b</p>
|
<p>Returns the maximum element of this stream according to the provided less function. less fuction: a > b</p>
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
- [FindFirst](#FindFirst)
|
- [FindFirst](#FindFirst)
|
||||||
|
- [FindLast](#FindLast)
|
||||||
- [Max](#Max)
|
- [Max](#Max)
|
||||||
- [Min](#Min)
|
- [Min](#Min)
|
||||||
- [AllMatch](#AllMatch)
|
- [AllMatch](#AllMatch)
|
||||||
@@ -667,6 +668,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
|
<p>返回此stream最后一个元素和true,如果stream为空,则返回零值和false。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s stream[T]) FindLast() (T, bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/stream"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
original := stream.FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := original.FindLast()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Max">Max</span>
|
### <span id="Max">Max</span>
|
||||||
|
|
||||||
<p>根据提供的less函数返回stream的最大元素。less 函数: a > b</p>
|
<p>根据提供的less函数返回stream的最大元素。less 函数: a > b</p>
|
||||||
|
|||||||
321
docs/strutil.md
321
docs/strutil.md
@@ -52,6 +52,12 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
- [HideString](#HideString)
|
||||||
|
- [ContainsAll](#ContainsAll)
|
||||||
|
- [ContainsAny](#ContainsAny)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -968,6 +974,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello world
|
// hello world
|
||||||
// 你好😄
|
// 你好😄
|
||||||
@@ -994,13 +1001,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.StringToBytes("abc")
|
result1 := strutil.StringToBytes("abc")
|
||||||
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
|
||||||
// [97 98 99]
|
// Output:
|
||||||
// true
|
// [97 98 99]
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1024,11 +1032,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytes := []byte{'a', 'b', 'c'}
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
|
||||||
// abc
|
// Output:
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1052,16 +1061,17 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.IsBlank("")
|
result1 := strutil.IsBlank("")
|
||||||
result2 := strutil.IsBlank("\t\v\f\n")
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
result3 := strutil.IsBlank(" 中文")
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1072,7 +1082,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HasPrefixAny(str string, prefixes []string) bool
|
func ReplaceWithMap(str string, replaces map[string]string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -1085,13 +1095,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1115,13 +1126,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1146,22 +1158,235 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "foo bar hello world"
|
str := "foo bar hello world"
|
||||||
|
|
||||||
result1 := strutil.IndexOffset(str, "o", 5)
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
result2 := strutil.IndexOffset(str, "o", 0)
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
result4 := strutil.IndexOffset(str, "d", len(str))
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
result5 := strutil.IndexOffset(str, "f", -1)
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
// Output:
|
|
||||||
// 12
|
// Output:
|
||||||
// 1
|
// 12
|
||||||
// 18
|
// 1
|
||||||
// -1
|
// 18
|
||||||
// -1
|
// -1
|
||||||
|
// -1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReplaceWithMap">ReplaceWithMap</span>
|
||||||
|
|
||||||
|
<p>Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// 1c 12 12 1c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Trim">Trim</span>
|
||||||
|
|
||||||
|
<p>Strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Trim(str string, characterMask ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := strutil.Trim(str)
|
||||||
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="SplitAndTrim">SplitAndTrim</span>
|
||||||
|
|
||||||
|
<p>Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice. It ignores the elements which are empty after Trim.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HideString">HideString</span>
|
||||||
|
|
||||||
|
<p>Hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 13242658976
|
||||||
|
// 132*2658976
|
||||||
|
// 132****8976
|
||||||
|
// 1324265****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains all the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains any one of the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -52,6 +52,13 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
- [HideString](#HideString)
|
||||||
|
- [ContainsAll](#ContainsAll)
|
||||||
|
- [ContainsAny](#ContainsAny)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -968,6 +975,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello world
|
// hello world
|
||||||
// 你好😄
|
// 你好😄
|
||||||
@@ -994,13 +1002,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.StringToBytes("abc")
|
result1 := strutil.StringToBytes("abc")
|
||||||
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
// Output:
|
||||||
// [97 98 99]
|
// [97 98 99]
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1024,11 +1032,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytes := []byte{'a', 'b', 'c'}
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
// Output:
|
|
||||||
// abc
|
// Output:
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1052,16 +1061,17 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.IsBlank("")
|
result1 := strutil.IsBlank("")
|
||||||
result2 := strutil.IsBlank("\t\v\f\n")
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
result3 := strutil.IsBlank(" 中文")
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1085,13 +1095,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1115,13 +1126,14 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
// Output:
|
|
||||||
// true
|
// Output:
|
||||||
// false
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1146,22 +1158,237 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
str := "foo bar hello world"
|
str := "foo bar hello world"
|
||||||
|
|
||||||
result1 := strutil.IndexOffset(str, "o", 5)
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
result2 := strutil.IndexOffset(str, "o", 0)
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
result4 := strutil.IndexOffset(str, "d", len(str))
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
result5 := strutil.IndexOffset(str, "f", -1)
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
// Output:
|
|
||||||
// 12
|
// Output:
|
||||||
// 1
|
// 12
|
||||||
// 18
|
// 1
|
||||||
// -1
|
// 18
|
||||||
// -1
|
// -1
|
||||||
|
// -1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ReplaceWithMap">ReplaceWithMap</span>
|
||||||
|
|
||||||
|
<p>返回`str`的副本,以无序的方式被map替换,区分大小写。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// 1c 12 12 1c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Trim">Trim</span>
|
||||||
|
|
||||||
|
<p>从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Trim(str string, characterMask ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := strutil.Trim(str)
|
||||||
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SplitAndTrim">SplitAndTrim</span>
|
||||||
|
|
||||||
|
<p>将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HideString">HideString</span>
|
||||||
|
|
||||||
|
<p>使用参数`replaceChar`隐藏源字符串中的一些字符。替换范围是 origin[start : end]。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 13242658976
|
||||||
|
// 132*2658976
|
||||||
|
// 132****8976
|
||||||
|
// 1324265****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括全部给定的子字符串切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括给定的子字符串切片中任意一个子字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ func ListFileNames(path string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsZipFile checks if file is zip or not.
|
// IsZipFile checks if file is zip or not.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/9M0g2j_uF_e
|
||||||
func IsZipFile(filepath string) bool {
|
func IsZipFile(filepath string) bool {
|
||||||
f, err := os.Open(filepath)
|
f, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -383,7 +383,7 @@ func CurrentPath() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FileSize returns file size in bytes.
|
// FileSize returns file size in bytes.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/H9Z05uD-Jjc
|
||||||
func FileSize(path string) (int64, error) {
|
func FileSize(path string) (int64, error) {
|
||||||
f, err := os.Stat(path)
|
f, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -393,7 +393,7 @@ func FileSize(path string) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MTime returns file modified time.
|
// MTime returns file modified time.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/s_Tl7lZoAaY
|
||||||
func MTime(filepath string) (int64, error) {
|
func MTime(filepath string) (int64, error) {
|
||||||
f, err := os.Stat(filepath)
|
f, err := os.Stat(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -403,7 +403,7 @@ func MTime(filepath string) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MTime returns file sha value, param `shaType` should be 1, 256 or 512.
|
// MTime returns file sha value, param `shaType` should be 1, 256 or 512.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/VfEEcO2MJYf
|
||||||
func Sha(filepath string, shaType ...int) (string, error) {
|
func Sha(filepath string, shaType ...int) (string, error) {
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -437,7 +437,7 @@ func Sha(filepath string, shaType ...int) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCsvFile read file content into slice.
|
// ReadCsvFile read file content into slice.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/OExTkhGEd3_u
|
||||||
func ReadCsvFile(filepath string) ([][]string, error) {
|
func ReadCsvFile(filepath string) ([][]string, error) {
|
||||||
f, err := os.Open(filepath)
|
f, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -453,3 +453,35 @@ func ReadCsvFile(filepath string) ([][]string, error) {
|
|||||||
|
|
||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteStringToFile write string to target file.
|
||||||
|
// Play: todo
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error {
|
||||||
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
if append {
|
||||||
|
flag = flag | os.O_APPEND
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(filepath, flag, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.WriteString(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytesToFile write bytes to target file.
|
||||||
|
// Play: todo
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error {
|
||||||
|
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.Write(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -285,3 +285,59 @@ func ExampleReadCsvFile() {
|
|||||||
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleWriteStringToFile() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWriteBytesToFile() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
|||||||
@@ -296,3 +296,68 @@ func TestReadCsvFile(t *testing.T) {
|
|||||||
assert.Equal(3, len(content[0]))
|
assert.Equal(3, len(content[0]))
|
||||||
assert.Equal("Bob", content[0][0])
|
assert.Equal("Bob", content[0][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteStringToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
||||||
|
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, "hello", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content1, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, " world", true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content2, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", content1)
|
||||||
|
assert.Equal("hello world", string(content2))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteBytesToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteBytesToFile")
|
||||||
|
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", string(content))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|||||||
@@ -55,12 +55,12 @@ 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
|
// Play: https://go.dev/play/p/s0NdFCtwuyd
|
||||||
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)
|
||||||
}
|
}
|
||||||
tmp := val / total
|
tmp := val / total * 100
|
||||||
result := RoundToFloat(tmp, n)
|
result := RoundToFloat(tmp, n)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@@ -256,3 +256,70 @@ func IsPrime(n int) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GCD return greatest common divisor (GCD) of integers.
|
||||||
|
// Play: https://go.dev/play/p/CiEceLSoAKB
|
||||||
|
func GCD[T constraints.Integer](integers ...T) T {
|
||||||
|
result := integers[0]
|
||||||
|
|
||||||
|
for k := range integers {
|
||||||
|
result = gcd(integers[k], result)
|
||||||
|
|
||||||
|
if result == 1 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// find greatest common divisor (GCD)
|
||||||
|
func gcd[T constraints.Integer](a, b T) T {
|
||||||
|
if b == 0 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return gcd(b, a%b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LCM return Least Common Multiple (LCM) of integers.
|
||||||
|
// Play: https://go.dev/play/p/EjcZxfY7G_g
|
||||||
|
func LCM[T constraints.Integer](integers ...T) T {
|
||||||
|
result := integers[0]
|
||||||
|
|
||||||
|
for k := range integers {
|
||||||
|
result = lcm(integers[k], result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// find Least Common Multiple (LCM) via GCD.
|
||||||
|
func lcm[T constraints.Integer](a, b T) T {
|
||||||
|
if a == 0 || b == 0 {
|
||||||
|
panic("lcm function: provide non zero integers only.")
|
||||||
|
}
|
||||||
|
return a * b / gcd(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cos returns the cosine of the radian argument.
|
||||||
|
// Play: todo
|
||||||
|
func Cos(radian float64, precision ...int) float64 {
|
||||||
|
t := 1.0 / (2.0 * math.Pi)
|
||||||
|
radian *= t
|
||||||
|
radian -= 0.25 + math.Floor(radian+0.25)
|
||||||
|
radian *= 16.0 * (math.Abs(radian) - 0.5)
|
||||||
|
radian += 0.225 * radian * (math.Abs(radian) - 1.0)
|
||||||
|
|
||||||
|
if len(precision) == 1 {
|
||||||
|
return TruncRound(radian, precision[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return TruncRound(radian, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cos returns the sine of the radian argument.
|
||||||
|
// Play: todo
|
||||||
|
func Sin(radian float64, precision ...int) float64 {
|
||||||
|
return Cos((math.Pi / 2) - radian)
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,13 +53,16 @@ func ExampleFactorial() {
|
|||||||
func ExamplePercent() {
|
func ExamplePercent() {
|
||||||
result1 := Percent(1, 2, 2)
|
result1 := Percent(1, 2, 2)
|
||||||
result2 := Percent(0.1, 0.3, 2)
|
result2 := Percent(0.1, 0.3, 2)
|
||||||
|
result3 := Percent(-30305, 408420, 2)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 0.5
|
// 50
|
||||||
// 0.33
|
// 33.33
|
||||||
|
// -7.42
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRoundToFloat() {
|
func ExampleRoundToFloat() {
|
||||||
@@ -281,3 +284,81 @@ func ExampleIsPrime() {
|
|||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleGCD() {
|
||||||
|
result1 := GCD(1, 1)
|
||||||
|
result2 := GCD(1, -1)
|
||||||
|
result3 := GCD(-1, 1)
|
||||||
|
result4 := GCD(-1, -1)
|
||||||
|
result5 := GCD(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleLCM() {
|
||||||
|
result1 := LCM(1, 1)
|
||||||
|
result2 := LCM(1, 2)
|
||||||
|
result3 := LCM(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 18
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleCos() {
|
||||||
|
result1 := Cos(0)
|
||||||
|
result2 := Cos(90)
|
||||||
|
result3 := Cos(180)
|
||||||
|
result4 := Cos(math.Pi)
|
||||||
|
result5 := Cos(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// -0.447
|
||||||
|
// -0.598
|
||||||
|
// -1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleSin() {
|
||||||
|
result1 := Sin(0)
|
||||||
|
result2 := Sin(90)
|
||||||
|
result3 := Sin(180)
|
||||||
|
result4 := Sin(math.Pi)
|
||||||
|
result5 := Sin(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0.894
|
||||||
|
// -0.801
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,8 +36,9 @@ func TestFactorial(t *testing.T) {
|
|||||||
func TestPercent(t *testing.T) {
|
func TestPercent(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestPercent")
|
assert := internal.NewAssert(t, "TestPercent")
|
||||||
|
|
||||||
assert.Equal(0.5, Percent(1, 2, 2))
|
assert.EqualValues(50, Percent(1, 2, 2))
|
||||||
assert.Equal(0.33, Percent(0.1, 0.3, 2))
|
assert.EqualValues(33.33, Percent(0.1, 0.3, 2))
|
||||||
|
assert.EqualValues(-7.42, Percent(-30305, 408420, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRoundToFloat(t *testing.T) {
|
func TestRoundToFloat(t *testing.T) {
|
||||||
@@ -210,3 +211,61 @@ func TestIsPrime(t *testing.T) {
|
|||||||
assert.Equal(true, IsPrime(3))
|
assert.Equal(true, IsPrime(3))
|
||||||
assert.Equal(false, IsPrime(4))
|
assert.Equal(false, IsPrime(4))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGCD(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGCD")
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, 1))
|
||||||
|
assert.Equal(1, GCD(1, -1))
|
||||||
|
assert.Equal(-1, GCD(-1, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, -1))
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, 0))
|
||||||
|
assert.Equal(1, GCD(0, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, 0))
|
||||||
|
assert.Equal(-1, GCD(0, -1))
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, -2))
|
||||||
|
assert.Equal(1, GCD(-2, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, 2))
|
||||||
|
assert.Equal(-1, GCD(2, -1))
|
||||||
|
|
||||||
|
assert.Equal(-1, GCD(-1, -2))
|
||||||
|
assert.Equal(-1, GCD(-2, -1))
|
||||||
|
|
||||||
|
assert.Equal(-9, GCD(-27, -36))
|
||||||
|
assert.Equal(3, GCD(3, 6, 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLCM(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLCM")
|
||||||
|
|
||||||
|
assert.Equal(1, LCM(1))
|
||||||
|
assert.Equal(-1, LCM(-1))
|
||||||
|
assert.Equal(-1, LCM(1, -1))
|
||||||
|
assert.Equal(1, LCM(-1, 1))
|
||||||
|
assert.Equal(1, LCM(1, 1))
|
||||||
|
assert.Equal(-1, LCM(-1, -1))
|
||||||
|
assert.Equal(2, LCM(1, 2))
|
||||||
|
assert.Equal(18, LCM(3, 6, 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCos(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestCos")
|
||||||
|
|
||||||
|
assert.EqualValues(1, Cos(0))
|
||||||
|
assert.EqualValues(-0.447, Cos(90))
|
||||||
|
assert.EqualValues(-0.598, Cos(180))
|
||||||
|
assert.EqualValues(-1, Cos(math.Pi))
|
||||||
|
assert.EqualValues(0, Cos(math.Pi/2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSin(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSin")
|
||||||
|
|
||||||
|
assert.EqualValues(0, Sin(0))
|
||||||
|
assert.EqualValues(0.894, Sin(90))
|
||||||
|
assert.EqualValues(-0.801, Sin(180))
|
||||||
|
assert.EqualValues(0, Sin(math.Pi))
|
||||||
|
assert.EqualValues(1, Sin(math.Pi/2))
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func TestHttpPost(t *testing.T) {
|
|||||||
func TestHttpPostFormData(t *testing.T) {
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
// "Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
"Content-Type": "multipart/form-data",
|
// "Content-Type": "multipart/form-data",
|
||||||
}
|
}
|
||||||
|
|
||||||
postData := url.Values{}
|
postData := url.Values{}
|
||||||
@@ -61,7 +61,7 @@ func TestHttpPostFormData(t *testing.T) {
|
|||||||
// postData["userId"] = "1"
|
// postData["userId"] = "1"
|
||||||
// postData["title"] = "title"
|
// postData["title"] = "title"
|
||||||
|
|
||||||
resp, err := HttpPost(apiUrl, header, postData, nil)
|
resp, err := HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -249,9 +250,14 @@ func DownloadFile(filepath string, url string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsPingConnected checks if can ping specified host or not.
|
// IsPingConnected checks if can ping specified host or not.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/q8OzTijsA87
|
||||||
func IsPingConnected(host string) bool {
|
func IsPingConnected(host string) bool {
|
||||||
cmd := exec.Command("ping", host, "-c", "1", "-W", "6")
|
cmd := exec.Command("ping", host, "-c", "4", "-W", "6")
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("ping", host, "-n", "4", "-w", "6")
|
||||||
|
}
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@@ -260,7 +266,7 @@ func IsPingConnected(host string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsTelnetConnected checks if can telnet specified host or not.
|
// IsTelnetConnected checks if can telnet specified host or not.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/yiLCGtQv_ZG
|
||||||
func IsTelnetConnected(host string, port string) bool {
|
func IsTelnetConnected(host string, port string) bool {
|
||||||
adder := host + ":" + port
|
adder := host + ":" + port
|
||||||
conn, err := net.DialTimeout("tcp", adder, 5*time.Second)
|
conn, err := net.DialTimeout("tcp", adder, 5*time.Second)
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package netutil
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -72,52 +74,34 @@ func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body any) error {
|
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body any) error {
|
||||||
err := setHeader(req, header)
|
if err := setHeader(req, header); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
|
} else if err = setQueryParam(req, reqUrl, queryParam); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err = setBodyByte(req, body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = setQueryParam(req, reqUrl, queryParam)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
|
||||||
if formData, ok := queryParam.(url.Values); ok {
|
|
||||||
err = setBodyByte(req, []byte(formData.Encode()))
|
|
||||||
}
|
|
||||||
if formData, ok := queryParam.(map[string]string); ok {
|
|
||||||
postData := url.Values{}
|
|
||||||
for k, v := range formData {
|
|
||||||
postData.Set(k, v)
|
|
||||||
}
|
|
||||||
err = setBodyByte(req, []byte(postData.Encode()))
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
err = setBodyByte(req, body)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setHeader(req *http.Request, header any) error {
|
func setHeader(req *http.Request, header any) error {
|
||||||
if header != nil {
|
if header == nil {
|
||||||
switch v := header.(type) {
|
return nil
|
||||||
case map[string]string:
|
}
|
||||||
for k := range v {
|
|
||||||
req.Header.Add(k, v[k])
|
switch v := header.(type) {
|
||||||
}
|
case map[string]string:
|
||||||
case http.Header:
|
for k := range v {
|
||||||
for k, vv := range v {
|
req.Header.Add(k, v[k])
|
||||||
for _, vvv := range vv {
|
|
||||||
req.Header.Add(k, vvv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.New("header params type should be http.Header or map[string]string")
|
|
||||||
}
|
}
|
||||||
|
case http.Header:
|
||||||
|
for k, vv := range v {
|
||||||
|
for _, vvv := range vv {
|
||||||
|
req.Header.Add(k, vvv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.New("header params type should be http.Header or map[string]string")
|
||||||
}
|
}
|
||||||
|
|
||||||
if host := req.Header.Get("Host"); host != "" {
|
if host := req.Header.Get("Host"); host != "" {
|
||||||
@@ -137,19 +121,21 @@ func setUrl(req *http.Request, reqUrl string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
||||||
|
if queryParam == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var values url.Values
|
var values url.Values
|
||||||
if queryParam != nil {
|
switch v := queryParam.(type) {
|
||||||
switch v := queryParam.(type) {
|
case map[string]string:
|
||||||
case map[string]string:
|
values = url.Values{}
|
||||||
values = url.Values{}
|
for k := range v {
|
||||||
for k := range v {
|
values.Set(k, v[k])
|
||||||
values.Set(k, v[k])
|
|
||||||
}
|
|
||||||
case url.Values:
|
|
||||||
values = v
|
|
||||||
default:
|
|
||||||
return errors.New("query string params type should be url.Values or map[string]string")
|
|
||||||
}
|
}
|
||||||
|
case url.Values:
|
||||||
|
values = v
|
||||||
|
default:
|
||||||
|
return errors.New("query string params type should be url.Values or map[string]string")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set url
|
// set url
|
||||||
@@ -170,14 +156,36 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setBodyByte(req *http.Request, body any) error {
|
func setBodyByte(req *http.Request, body any) error {
|
||||||
if body != nil {
|
if body == nil {
|
||||||
switch b := body.(type) {
|
return nil
|
||||||
case []byte:
|
}
|
||||||
req.Body = io.NopCloser(bytes.NewReader(b))
|
var bodyReader *bytes.Reader
|
||||||
req.ContentLength = int64(len(b))
|
switch b := body.(type) {
|
||||||
default:
|
case io.Reader:
|
||||||
return errors.New("body type should be []byte")
|
buf := bytes.NewBuffer(nil)
|
||||||
|
if _, err := io.Copy(buf, b); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
req.Body = ioutil.NopCloser(buf)
|
||||||
|
req.ContentLength = int64(buf.Len())
|
||||||
|
case []byte:
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case map[string]interface{}:
|
||||||
|
values := url.Values{}
|
||||||
|
for k := range b {
|
||||||
|
values.Set(k, fmt.Sprintf("%v", b[k]))
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader([]byte(values.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case url.Values:
|
||||||
|
bodyReader = bytes.NewReader([]byte(b.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid body type: %T", b)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func TestIsPingConnected(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestIsPingConnected")
|
assert := internal.NewAssert(t, "TestIsPingConnected")
|
||||||
|
|
||||||
// in github action env, this will fail
|
// in github action env, this will fail
|
||||||
// result1 := IsPingConnected("bing.com")
|
// result1 := IsPingConnected("www.baidu.com")
|
||||||
// assert.Equal(true, result1)
|
// assert.Equal(true, result1)
|
||||||
|
|
||||||
result2 := IsPingConnected("www.!@#&&&.com")
|
result2 := IsPingConnected("www.!@#&&&.com")
|
||||||
|
|||||||
@@ -338,7 +338,7 @@ func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, boo
|
|||||||
// FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// FindBy iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
// If return T is nil or zero value then no items matched the predicate func.
|
// If return T is nil or zero value then no items matched the predicate func.
|
||||||
// In contrast to Find or FindLast, its return value no longer requires dereferencing
|
// In contrast to Find or FindLast, its return value no longer requires dereferencing
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/n1lysBYl-GB
|
||||||
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
||||||
index := -1
|
index := -1
|
||||||
|
|
||||||
@@ -359,7 +359,7 @@ func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok b
|
|||||||
// FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function.
|
// FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function.
|
||||||
// If return T is nil or zero value then no items matched the predicate func.
|
// If return T is nil or zero value then no items matched the predicate func.
|
||||||
// In contrast to Find or FindLast, its return value no longer requires dereferencing
|
// In contrast to Find or FindLast, its return value no longer requires dereferencing
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/8iqomzyCl_s
|
||||||
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
||||||
index := -1
|
index := -1
|
||||||
|
|
||||||
|
|||||||
@@ -295,6 +295,18 @@ func (s stream[T]) FindFirst() (T, bool) {
|
|||||||
return s.source[0], true
|
return s.source[0], true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindLast returns the last element of this stream and true, or zero value and false if the stream is empty.
|
||||||
|
// Play: https://go.dev/play/p/WZD2rDAW-2h
|
||||||
|
func (s stream[T]) FindLast() (T, bool) {
|
||||||
|
var result T
|
||||||
|
|
||||||
|
if s.source == nil || len(s.source) == 0 {
|
||||||
|
return result, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.source[len(s.source)-1], true
|
||||||
|
}
|
||||||
|
|
||||||
// Reverse returns a stream whose elements are reverse order of given stream.
|
// Reverse returns a stream whose elements are reverse order of given stream.
|
||||||
// Play: https://go.dev/play/p/A8_zkJnLHm4
|
// Play: https://go.dev/play/p/A8_zkJnLHm4
|
||||||
func (s stream[T]) Reverse() stream[T] {
|
func (s stream[T]) Reverse() stream[T] {
|
||||||
|
|||||||
@@ -290,6 +290,19 @@ func ExampleStream_FindFirst() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleStream_FindLast() {
|
||||||
|
original := FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := original.FindLast()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleStream_Reverse() {
|
func ExampleStream_Reverse() {
|
||||||
original := FromSlice([]int{1, 2, 3})
|
original := FromSlice([]int{1, 2, 3})
|
||||||
|
|
||||||
|
|||||||
@@ -273,6 +273,24 @@ func TestStream_FindFirst(t *testing.T) {
|
|||||||
assert.Equal(true, ok)
|
assert.Equal(true, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStream_FindLast(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestStream_FindLast")
|
||||||
|
|
||||||
|
stream := FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := stream.FindLast()
|
||||||
|
|
||||||
|
assert.Equal(1, result)
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
|
||||||
|
stream2 := FromSlice([]int{})
|
||||||
|
|
||||||
|
result, ok = stream2.FindLast()
|
||||||
|
|
||||||
|
assert.Equal(0, result)
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
}
|
||||||
|
|
||||||
func TestStream_Reverse(t *testing.T) {
|
func TestStream_Reverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestStream_Reverse")
|
assert := internal.NewAssert(t, "TestStream_Reverse")
|
||||||
|
|
||||||
|
|||||||
@@ -377,7 +377,7 @@ func RemoveNonPrintable(str string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StringToBytes converts a string to byte slice without a memory allocation.
|
// StringToBytes converts a string to byte slice without a memory allocation.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/7OyFBrf9AxA
|
||||||
func StringToBytes(str string) (b []byte) {
|
func StringToBytes(str string) (b []byte) {
|
||||||
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
|
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||||
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||||
@@ -386,13 +386,13 @@ func StringToBytes(str string) (b []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BytesToString converts a byte slice to string without a memory allocation.
|
// BytesToString converts a byte slice to string without a memory allocation.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/6c68HRvJecH
|
||||||
func BytesToString(bytes []byte) string {
|
func BytesToString(bytes []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&bytes))
|
return *(*string)(unsafe.Pointer(&bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsBlank checks if a string is whitespace, empty.
|
// IsBlank checks if a string is whitespace, empty.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/6zXRH_c0Qd3
|
||||||
func IsBlank(str string) bool {
|
func IsBlank(str string) bool {
|
||||||
if len(str) == 0 {
|
if len(str) == 0 {
|
||||||
return true
|
return true
|
||||||
@@ -407,8 +407,8 @@ func IsBlank(str string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasPrefixAny check if a string starts with any of an array of specified strings.
|
// HasPrefixAny check if a string starts with any of a slice of specified strings.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/8UUTl2C5slo
|
||||||
func HasPrefixAny(str string, prefixes []string) bool {
|
func HasPrefixAny(str string, prefixes []string) bool {
|
||||||
if len(str) == 0 || len(prefixes) == 0 {
|
if len(str) == 0 || len(prefixes) == 0 {
|
||||||
return false
|
return false
|
||||||
@@ -421,8 +421,8 @@ func HasPrefixAny(str string, prefixes []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasSuffixAny check if a string ends with any of an array of specified strings.
|
// HasSuffixAny check if a string ends with any of a slice of specified strings.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/sKWpCQdOVkx
|
||||||
func HasSuffixAny(str string, suffixes []string) bool {
|
func HasSuffixAny(str string, suffixes []string) bool {
|
||||||
if len(str) == 0 || len(suffixes) == 0 {
|
if len(str) == 0 || len(suffixes) == 0 {
|
||||||
return false
|
return false
|
||||||
@@ -437,7 +437,7 @@ func HasSuffixAny(str string, suffixes []string) bool {
|
|||||||
|
|
||||||
// IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`,
|
// IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`,
|
||||||
// or -1 if substr is not present in string.
|
// or -1 if substr is not present in string.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/qZo4lV2fomB
|
||||||
func IndexOffset(str string, substr string, idxFrom int) int {
|
func IndexOffset(str string, substr string, idxFrom int) int {
|
||||||
if idxFrom > len(str)-1 || idxFrom < 0 {
|
if idxFrom > len(str)-1 || idxFrom < 0 {
|
||||||
return -1
|
return -1
|
||||||
@@ -445,3 +445,110 @@ func IndexOffset(str string, substr string, idxFrom int) int {
|
|||||||
|
|
||||||
return strings.Index(str[idxFrom:], substr) + idxFrom
|
return strings.Index(str[idxFrom:], substr) + idxFrom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplaceWithMap returns a copy of `str`,
|
||||||
|
// which is replaced by a map in unordered way, case-sensitively.
|
||||||
|
// Play: https://go.dev/play/p/h3t7CNj2Vvu
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string {
|
||||||
|
for k, v := range replaces {
|
||||||
|
str = strings.ReplaceAll(str, k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
// SplitAndTrim splits string `str` by a string `delimiter` to a slice,
|
||||||
|
// and calls Trim to every element of this slice. It ignores the elements
|
||||||
|
// which are empty after Trim.
|
||||||
|
// Play: https://go.dev/play/p/ZNL6o4SkYQ7
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
|
||||||
|
result := make([]string, 0)
|
||||||
|
|
||||||
|
for _, v := range strings.Split(str, delimiter) {
|
||||||
|
v = Trim(v, characterMask...)
|
||||||
|
if v != "" {
|
||||||
|
result = append(result, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultTrimChars are the characters which are stripped by Trim* functions in default.
|
||||||
|
DefaultTrimChars = string([]byte{
|
||||||
|
'\t', // Tab.
|
||||||
|
'\v', // Vertical tab.
|
||||||
|
'\n', // New line (line feed).
|
||||||
|
'\r', // Carriage return.
|
||||||
|
'\f', // New page.
|
||||||
|
' ', // Ordinary space.
|
||||||
|
0x00, // NUL-byte.
|
||||||
|
0x85, // Delete.
|
||||||
|
0xA0, // Non-breaking space.
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
// Trim strips whitespace (or other characters) from the beginning and end of a string.
|
||||||
|
// The optional parameter `characterMask` specifies the additional stripped characters.
|
||||||
|
// Play: https://go.dev/play/p/Y0ilP0NRV3j
|
||||||
|
func Trim(str string, characterMask ...string) string {
|
||||||
|
trimChars := DefaultTrimChars
|
||||||
|
|
||||||
|
if len(characterMask) > 0 {
|
||||||
|
trimChars += characterMask[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(str, trimChars)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HideString hide some chars in source string with param `replaceChar`.
|
||||||
|
// replace range is origin[start : end]. [start, end)
|
||||||
|
// Play: todo
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string {
|
||||||
|
size := len(origin)
|
||||||
|
|
||||||
|
if start > size-1 || start < 0 || end < 0 || start > end {
|
||||||
|
return origin
|
||||||
|
}
|
||||||
|
|
||||||
|
if end > size {
|
||||||
|
end = size
|
||||||
|
}
|
||||||
|
|
||||||
|
if replaceChar == "" {
|
||||||
|
return origin
|
||||||
|
}
|
||||||
|
|
||||||
|
startStr := origin[0:start]
|
||||||
|
endStr := origin[end:size]
|
||||||
|
|
||||||
|
replaceSize := end - start
|
||||||
|
replaceStr := strings.Repeat(replaceChar, replaceSize)
|
||||||
|
|
||||||
|
return startStr + replaceStr + endStr
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainsAll return true if target string contains all the substrs.
|
||||||
|
// Play: todo
|
||||||
|
func ContainsAll(str string, substrs []string) bool {
|
||||||
|
for _, v := range substrs {
|
||||||
|
if !strings.Contains(str, v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainsAny return true if target string contains any one of the substrs.
|
||||||
|
// Play: todo
|
||||||
|
func ContainsAny(str string, substrs []string) bool {
|
||||||
|
for _, v := range substrs {
|
||||||
|
if strings.Contains(str, v) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -394,22 +394,19 @@ func ExampleSplitWords() {
|
|||||||
|
|
||||||
result1 := SplitWords("a word")
|
result1 := SplitWords("a word")
|
||||||
result2 := SplitWords("I'am a programmer")
|
result2 := SplitWords("I'am a programmer")
|
||||||
result3 := SplitWords("Bonjour, je suis programmeur")
|
result3 := SplitWords("a -b-c' 'd'e")
|
||||||
result4 := SplitWords("a -b-c' 'd'e")
|
result4 := SplitWords("你好,我是一名码农")
|
||||||
result5 := SplitWords("你好,我是一名码农")
|
result5 := SplitWords("こんにちは,私はプログラマーです")
|
||||||
result6 := SplitWords("こんにちは,私はプログラマーです")
|
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
fmt.Println(result6)
|
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [a word]
|
// [a word]
|
||||||
// [I'am a programmer]
|
// [I'am a programmer]
|
||||||
// [Bonjour je suis programmeur]
|
|
||||||
// [a b-c' d'e]
|
// [a b-c' d'e]
|
||||||
// []
|
// []
|
||||||
// []
|
// []
|
||||||
@@ -528,3 +525,100 @@ func ExampleIndexOffset() {
|
|||||||
// -1
|
// -1
|
||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleReplaceWithMap() {
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// 1c 12 12 1c
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleTrim() {
|
||||||
|
result1 := Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := Trim(str)
|
||||||
|
result3 := Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleSplitAndTrim() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := SplitAndTrim(str, ",")
|
||||||
|
result2 := SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleHideString() {
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
result1 := HideString(str, 3, 3, "*")
|
||||||
|
result2 := HideString(str, 3, 4, "*")
|
||||||
|
result3 := HideString(str, 3, 7, "*")
|
||||||
|
result4 := HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 13242658976
|
||||||
|
// 132*2658976
|
||||||
|
// 132****8976
|
||||||
|
// 1324265****
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleContainsAll() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleContainsAny() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|||||||
@@ -314,11 +314,10 @@ func TestSplitWords(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSplitWords")
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
cases := map[string][]string{
|
cases := map[string][]string{
|
||||||
"a word": {"a", "word"},
|
"a word": {"a", "word"},
|
||||||
"I'am a programmer": {"I'am", "a", "programmer"},
|
"I'am a programmer": {"I'am", "a", "programmer"},
|
||||||
"Bonjour, je suis programmeur": {"Bonjour", "je", "suis", "programmeur"},
|
"a -b-c' 'd'e": {"a", "b-c'", "d'e"},
|
||||||
"a -b-c' 'd'e": {"a", "b-c'", "d'e"},
|
"你好,我是一名码农": nil,
|
||||||
"你好,我是一名码农": nil,
|
|
||||||
"こんにちは,私はプログラマーです": nil,
|
"こんにちは,私はプログラマーです": nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,11 +330,10 @@ func TestWordCount(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestSplitWords")
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
cases := map[string]int{
|
cases := map[string]int{
|
||||||
"a word": 2, // {"a", "word"},
|
"a word": 2, // {"a", "word"},
|
||||||
"I'am a programmer": 3, // {"I'am", "a", "programmer"},
|
"I'am a programmer": 3, // {"I'am", "a", "programmer"},
|
||||||
"Bonjour, je suis programmeur": 4, // {"Bonjour", "je", "suis", "programmeur"},
|
"a -b-c' 'd'e": 3, // {"a", "b-c'", "d'e"},
|
||||||
"a -b-c' 'd'e": 3, // {"a", "b-c'", "d'e"},
|
"你好,我是一名码农": 0, // nil,
|
||||||
"你好,我是一名码农": 0, // nil,
|
|
||||||
"こんにちは,私はプログラマーです": 0, // nil,
|
"こんにちは,私はプログラマーです": 0, // nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,3 +397,72 @@ func TestIndexOffset(t *testing.T) {
|
|||||||
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
||||||
assert.Equal(IndexOffset(str, "f", -1), -1)
|
assert.Equal(IndexOffset(str, "f", -1), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplaceWithMap(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestReplaceWithMap")
|
||||||
|
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(str, "ac ab ab ac")
|
||||||
|
assert.Equal(ReplaceWithMap(str, replaces), "1c 12 12 1c")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str1 := "$ ab cd $ "
|
||||||
|
|
||||||
|
assert.Equal("$ ab cd $", Trim(str1))
|
||||||
|
assert.Equal("ab cd", Trim(str1, "$"))
|
||||||
|
assert.Equal("abcd", Trim("\nabcd"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitAndTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := SplitAndTrim(str, ",")
|
||||||
|
result2 := SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "$1"}, result1)
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "1"}, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHideString(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
assert.Equal("13242658976", HideString(str, 0, -1, "*"))
|
||||||
|
assert.Equal("13242658976", HideString(str, 0, 0, "*"))
|
||||||
|
assert.Equal("****2658976", HideString(str, 0, 4, "*"))
|
||||||
|
|
||||||
|
assert.Equal("13242658976", HideString(str, 3, 3, "*"))
|
||||||
|
assert.Equal("132*2658976", HideString(str, 3, 4, "*"))
|
||||||
|
assert.Equal("132****8976", HideString(str, 3, 7, "*"))
|
||||||
|
assert.Equal("1324265****", HideString(str, 7, 11, "*"))
|
||||||
|
|
||||||
|
assert.Equal("1324265****", HideString(str, 7, 100, "*"))
|
||||||
|
assert.Equal("13242658976", HideString(str, 100, 100, "*"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsAll(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestContainsAll")
|
||||||
|
|
||||||
|
assert.Equal(true, ContainsAll("hello world", []string{"hello", "world"}))
|
||||||
|
assert.Equal(true, ContainsAll("hello world", []string{""}))
|
||||||
|
assert.Equal(false, ContainsAll("hello world", []string{"hello", "abc"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsAny(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestContainsAny")
|
||||||
|
|
||||||
|
assert.Equal(true, ContainsAny("hello world", []string{"hello", "world"}))
|
||||||
|
assert.Equal(true, ContainsAny("hello world", []string{"hello", "abc"}))
|
||||||
|
assert.Equal(false, ContainsAny("hello world", []string{"123", "abc"}))
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||||
|
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
||||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||||
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
||||||
@@ -111,6 +112,12 @@ func ContainLetter(str string) bool {
|
|||||||
return letterRegexMatcher.MatchString(str)
|
return letterRegexMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ContainLetter check if the string contain at least one number.
|
||||||
|
func ContainNumber(input string) bool {
|
||||||
|
return numberRegexMatcher.MatchString(input)
|
||||||
|
}
|
||||||
|
|
||||||
// IsJSON checks if the string is valid JSON.
|
// IsJSON checks if the string is valid JSON.
|
||||||
// Play: https://go.dev/play/p/8Kip1Itjiil
|
// Play: https://go.dev/play/p/8Kip1Itjiil
|
||||||
func IsJSON(str string) bool {
|
func IsJSON(str string) bool {
|
||||||
|
|||||||
@@ -36,6 +36,24 @@ func ExampleContainLetter() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleContainNumber() {
|
||||||
|
result1 := ContainNumber("你好")
|
||||||
|
result2 := ContainNumber("&@#$%^&*")
|
||||||
|
result3 := ContainNumber("ab1")
|
||||||
|
result4 := ContainNumber("1234")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleContainLower() {
|
func ExampleContainLower() {
|
||||||
result1 := ContainLower("abc")
|
result1 := ContainLower("abc")
|
||||||
result2 := ContainLower("aBC")
|
result2 := ContainLower("aBC")
|
||||||
|
|||||||
@@ -86,6 +86,23 @@ func TestContainLetter(t *testing.T) {
|
|||||||
assert.Equal(false, ContainLetter("&@#$%^&*"))
|
assert.Equal(false, ContainLetter("&@#$%^&*"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContainNumber(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestContainNumber")
|
||||||
|
|
||||||
|
assert.Equal(true, ContainNumber("123"))
|
||||||
|
assert.Equal(true, ContainNumber("1Bc"))
|
||||||
|
assert.Equal(true, ContainNumber("a2c"))
|
||||||
|
assert.Equal(true, ContainNumber("ab3"))
|
||||||
|
assert.Equal(true, ContainNumber("a23"))
|
||||||
|
assert.Equal(true, ContainNumber("a23c"))
|
||||||
|
assert.Equal(true, ContainNumber("1%%%"))
|
||||||
|
|
||||||
|
assert.Equal(false, ContainNumber("ABC"))
|
||||||
|
assert.Equal(false, ContainNumber(""))
|
||||||
|
assert.Equal(false, ContainNumber("你好"))
|
||||||
|
assert.Equal(false, ContainNumber("&@#$%^&*"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsJSON(t *testing.T) {
|
func TestIsJSON(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsJSON")
|
assert := internal.NewAssert(t, "TestIsJSON")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user