mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-21 12:52:28 +08:00
Compare commits
21 Commits
v2.1.20
...
3beb769f09
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3beb769f09 | ||
|
|
8af02ff95b | ||
|
|
ba4485d8c0 | ||
|
|
8a1dd40738 | ||
|
|
85e2806531 | ||
|
|
1616d3d1be | ||
|
|
aa8e0d5c12 | ||
|
|
f05477d9cf | ||
|
|
cd91e16b26 | ||
|
|
9fcf046fb3 | ||
|
|
c3f1bc39d7 | ||
|
|
f5784b0f46 | ||
|
|
c86a8a479d | ||
|
|
e2aeb8ec07 | ||
|
|
654ba15aaf | ||
|
|
945c59896b | ||
|
|
219e31d929 | ||
|
|
82cb86b35c | ||
|
|
f93c561f5d | ||
|
|
4888909208 | ||
|
|
33c8875d14 |
47
README.md
47
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)
|
||||||
@@ -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,9 @@ 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)]
|
||||||
|
|
||||||
### 6. Cryptor package is for data encryption and decryption.
|
### 6. Cryptor package is for data encryption and decryption.
|
||||||
|
|
||||||
@@ -395,6 +403,8 @@ 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)]
|
||||||
- **<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 +486,8 @@ 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)]
|
||||||
|
|
||||||
### 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 +579,19 @@ 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)]
|
||||||
|
|
||||||
### 10. Formatter contains some functions for data formatting.
|
### 10. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
@@ -776,6 +793,10 @@ 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)]
|
||||||
|
- **<big>LCM</big>** : return Least Common Multiple (LCM) of integers.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#LCM)]
|
||||||
|
|
||||||
### 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 +866,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 +1007,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 +1203,8 @@ 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)]
|
||||||
- **<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)]
|
||||||
@@ -1314,16 +1341,28 @@ 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)]
|
||||||
|
- **<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)]
|
||||||
|
- **<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)]
|
||||||
|
|
||||||
### 21. System package contain some functions about os, runtime, shell command.
|
### 21. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -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,9 @@ 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)]
|
||||||
|
|
||||||
### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
@@ -394,6 +402,8 @@ 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)]
|
||||||
- **<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)]
|
||||||
@@ -469,12 +479,14 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- **<big>ToFormat</big>** : 返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。
|
- **<big>ToFormat</big>** : 返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)]
|
||||||
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
||||||
- **<big>ToFormatForTpl</big>** : 返回 tpl 格式指定的日期字符串。
|
- **<big>ToFormatForTpl</big>** : 返回tpl格式指定的日期字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)]
|
||||||
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
|
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
|
||||||
- **<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)]
|
||||||
|
|
||||||
### 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
### 8. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
@@ -564,16 +576,21 @@ 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)]
|
||||||
|
|
||||||
### 10. formatter 格式化器包含一些数据格式化处理方法。
|
### 10. formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
@@ -775,6 +792,10 @@ 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)]
|
||||||
|
- **<big>LCM</big>** : 求最小公倍数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#LCM)]
|
||||||
|
|
||||||
### 14. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
### 14. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
@@ -836,16 +857,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 +1006,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)]
|
||||||
@@ -1115,7 +1140,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
|
||||||
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
|
||||||
|
|
||||||
### 18. Stream 流,该包仅验证简单的 stream 实现,功能有限。
|
### 18. Stream流,该包仅验证简单的stream实现,功能有限。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/stream"
|
import "github.com/duke-git/lancet/v2/stream"
|
||||||
@@ -1174,9 +1199,11 @@ 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)]
|
||||||
- **<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)]
|
||||||
@@ -1316,16 +1343,28 @@ 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)]
|
||||||
|
- **<big>Trim</big>** : 从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Trim)]
|
||||||
|
- **<big>SplitAndTrim</big>** : 将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitAndTrim)]
|
||||||
|
|
||||||
### 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: todo
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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: todo
|
||||||
|
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,9 @@ 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: todo
|
||||||
|
func IsLeapYear(year int) bool {
|
||||||
|
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|
||||||
@@ -321,3 +338,15 @@ 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,19 @@ 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 TestAddDay(t *testing.T) {
|
func TestAddDay(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestAddDay")
|
assert := internal.NewAssert(t, "TestAddDay")
|
||||||
|
|
||||||
@@ -230,3 +243,13 @@ 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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
124
docs/datetime.md
124
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,7 @@ import (
|
|||||||
- [ToFormat](#ToFormat)
|
- [ToFormat](#ToFormat)
|
||||||
- [ToFormatForTpl](#ToFormatForTpl)
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
|
- [IsLeapYear](#IsLeapYear)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -198,6 +200,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 := AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := 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 +648,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 +712,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 +743,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 +776,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 +883,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -874,8 +915,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 +947,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 +979,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 +1008,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 +1037,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 +1067,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 +1097,42 @@ 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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -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,78 @@ 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)
|
||||||
|
|
||||||
<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 +125,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 +164,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 +199,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 := AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := 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 +274,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 +306,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 +338,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 +370,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 +402,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 +434,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 +466,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 +498,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 +530,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 +562,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 +594,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 +626,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 +647,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 +658,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 +690,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 +711,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 +722,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 +742,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 +754,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 +775,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 +786,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 +825,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 +861,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个当前时间的unix时间戳。</p>
|
<p>创建一个当前时间的unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -825,7 +870,7 @@ type theTime struct {
|
|||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -837,8 +882,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tm := datetime.NewUnixNow()
|
tm := datetime.NewUnixNow()
|
||||||
fmt.Println(tm)
|
fmt.Println(tm)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// &{1647597438}
|
// &{1647597438}
|
||||||
}
|
}
|
||||||
@@ -848,7 +893,7 @@ func main() {
|
|||||||
|
|
||||||
<p>创建一个unix时间戳。</p>
|
<p>创建一个unix时间戳。</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
@@ -857,7 +902,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 +914,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 +925,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 +934,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 +946,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 +957,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 +966,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 +978,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 +989,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 +1007,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 +1018,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 +1036,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 +1047,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 +1066,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 +1077,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 +1096,43 @@ 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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -522,7 +522,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isZip := IsZipFile("./zipfile.zip")
|
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||||
fmt.Println(isZip)
|
fmt.Println(isZip)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -522,7 +522,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isZip := IsZipFile("./zipfile.zip")
|
isZip := fileutil.IsZipFile("./zipfile.zip")
|
||||||
fmt.Println(isZip)
|
fmt.Println(isZip)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -363,15 +365,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Percent(1, 2, 2)
|
result1 := Percent(1, 2, 2)
|
||||||
result2 := mathutil.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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -702,4 +707,83 @@ func main() {
|
|||||||
// 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
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -40,6 +40,8 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -363,15 +365,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Percent(1, 2, 2)
|
result1 := Percent(1, 2, 2)
|
||||||
result2 := mathutil.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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -701,4 +706,84 @@ func main() {
|
|||||||
// 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
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -922,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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -924,7 +924,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 := 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 := 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>
|
||||||
|
|||||||
116
docs/strutil.md
116
docs/strutil.md
@@ -52,6 +52,9 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -968,6 +971,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello world
|
// hello world
|
||||||
// 你好😄
|
// 你好😄
|
||||||
@@ -998,6 +1002,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [97 98 99]
|
// [97 98 99]
|
||||||
// true
|
// true
|
||||||
@@ -1027,6 +1032,7 @@ func main() {
|
|||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// abc
|
// abc
|
||||||
}
|
}
|
||||||
@@ -1058,6 +1064,7 @@ func main() {
|
|||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// true
|
// true
|
||||||
@@ -1072,7 +1079,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>
|
||||||
@@ -1089,6 +1096,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
@@ -1119,6 +1127,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
@@ -1157,6 +1166,7 @@ func main() {
|
|||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 12
|
// 12
|
||||||
// 1
|
// 1
|
||||||
@@ -1165,3 +1175,107 @@ func main() {
|
|||||||
// -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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -968,6 +971,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello world
|
// hello world
|
||||||
// 你好😄
|
// 你好😄
|
||||||
@@ -1027,6 +1031,7 @@ func main() {
|
|||||||
result := strutil.BytesToString(bytes)
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// abc
|
// abc
|
||||||
}
|
}
|
||||||
@@ -1058,6 +1063,7 @@ func main() {
|
|||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// true
|
// true
|
||||||
@@ -1089,6 +1095,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
@@ -1119,6 +1126,7 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
@@ -1157,6 +1165,7 @@ func main() {
|
|||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
fmt.Println(result5)
|
fmt.Println(result5)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 12
|
// 12
|
||||||
// 1
|
// 1
|
||||||
@@ -1165,3 +1174,108 @@ func main() {
|
|||||||
// -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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ 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,48 @@ func IsPrime(n int) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GCD return greatest common divisor (GCD) of integers.
|
||||||
|
// Play: todo
|
||||||
|
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: todo
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,39 @@ 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,41 @@ 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))
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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/9xEf0-6C1e3
|
||||||
|
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,57 @@ 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: todo
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
func Trim(str string, characterMask ...string) string {
|
||||||
|
trimChars := DefaultTrimChars
|
||||||
|
|
||||||
|
if len(characterMask) > 0 {
|
||||||
|
trimChars += characterMask[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(str, trimChars)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,49 @@ 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]
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,38 @@ 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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user