mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 23:22:28 +08:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cacedf2a05 | ||
|
|
4fc391895b | ||
|
|
b419319e66 | ||
|
|
db34b5f69c | ||
|
|
ee44526d9e | ||
|
|
6576b1f0cb | ||
|
|
93108bafb9 | ||
|
|
07f5e0697f | ||
|
|
4ba91d7e4c | ||
|
|
ab81d9c283 | ||
|
|
a74038466f | ||
|
|
02daa7f6cb | ||
|
|
fef6fd7b9d | ||
|
|
f6cd98086f | ||
|
|
24eb2bbacd | ||
|
|
15c1537bf0 | ||
|
|
c02654559a | ||
|
|
634ca09e8c | ||
|
|
f2e743dcf4 | ||
|
|
f8f58cae10 | ||
|
|
215b79140d | ||
|
|
0bd675340f | ||
|
|
f3d73899b1 | ||
|
|
d4a20b239a | ||
|
|
4c28431451 | ||
|
|
168ed096c7 | ||
|
|
a060769635 | ||
|
|
0a99492cf6 | ||
|
|
fb3de03f37 | ||
|
|
43c2fd2a22 | ||
|
|
279d0754ba | ||
|
|
f133b32faa |
91
README.md
91
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)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -24,7 +24,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 200+ go util functions, support string, slice, datetime, net, crypt...
|
- 💪 400+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
- 💅 Only depend on the go standard library.
|
- 💅 Only depend on the go standard library.
|
||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
@@ -63,7 +63,22 @@ func main() {
|
|||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
### 1. Convertor package contains some functions for data convertion.
|
### 1. Compare package provides a lightweight comparison function on any type.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/compare"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
|
||||||
|
- [Equal](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#Equal)
|
||||||
|
- [EqualValue](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#EqualValue)
|
||||||
|
- [LessThan](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#LessThan)
|
||||||
|
- [GreaterThan](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#GreaterThan)
|
||||||
|
- [LessOrEqual](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](https://github.com/duke-git/lancet/blob/v1/docs/compare.md#GreaterOrEqual)
|
||||||
|
|
||||||
|
### 2. Convertor package contains some functions for data convertion.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/convertor"
|
import "github.com/duke-git/lancet/convertor"
|
||||||
@@ -83,8 +98,10 @@ import "github.com/duke-git/lancet/convertor"
|
|||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#StructToMap)
|
||||||
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#EncodeByte)
|
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#EncodeByte)
|
||||||
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DecodeByte)
|
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DecodeByte)
|
||||||
|
- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DeepClone)
|
||||||
|
- [CopyProperties](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#CopyProperties)
|
||||||
|
|
||||||
### 2. Cryptor package is for data encryption and decryption.
|
### 3. Cryptor package is for data encryption and decryption.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/cryptor"
|
import "github.com/duke-git/lancet/cryptor"
|
||||||
@@ -125,7 +142,7 @@ import "github.com/duke-git/lancet/cryptor"
|
|||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#RsaDecrypt)
|
||||||
|
|
||||||
### 3. Datetime package supports date and time format and compare.
|
### 4. Datetime package supports date and time format and compare.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/datetime"
|
import "github.com/duke-git/lancet/datetime"
|
||||||
@@ -164,7 +181,7 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
### 4. Fileutil package implements some basic functions for file operations.
|
### 5. Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/fileutil"
|
import "github.com/duke-git/lancet/fileutil"
|
||||||
@@ -187,8 +204,14 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [ReadFileByLine](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadFileByLine)
|
- [ReadFileByLine](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadFileByLine)
|
||||||
- [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#Zip)
|
- [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#Zip)
|
||||||
- [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#UnZip)
|
- [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#UnZip)
|
||||||
|
- [CurrentPath](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#CurrentPath)
|
||||||
|
- [IsZipFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#IsZipFile)
|
||||||
|
- [FileSize](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#FileSize)
|
||||||
|
- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#MTime)
|
||||||
|
- [Sha](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#Sha)
|
||||||
|
- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadCsvFile)
|
||||||
|
|
||||||
### 5. Formatter contains some functions for data formatting.
|
### 6. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/formatter"
|
import "github.com/duke-git/lancet/formatter"
|
||||||
@@ -197,8 +220,14 @@ import "github.com/duke-git/lancet/formatter"
|
|||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma)
|
||||||
|
- [Pretty](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Pretty)
|
||||||
|
- [PrettyToWriter](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#PrettyToWriter)
|
||||||
|
- [DecimalBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#DecimalBytes)
|
||||||
|
- [BinaryBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#BinaryBytes)
|
||||||
|
- [ParseDecimalBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#ParseDecimalBytes)
|
||||||
|
- [ParseBinaryBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#ParseBinaryBytes)
|
||||||
|
|
||||||
### Function package can control the flow of function execution and support part of functional programming
|
### 7. Function package can control the flow of function execution and support part of functional programming
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/function"
|
import "github.com/duke-git/lancet/function"
|
||||||
@@ -212,9 +241,11 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Debounced)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay)
|
||||||
|
- [Pipeline](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Pipeline)
|
||||||
|
- [Schedule](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Schedule)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher)
|
||||||
|
|
||||||
### 6. Mathutil package implements some functions for math calculation.
|
### 8. Mathutil package implements some functions for math calculation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/mathutil"
|
import "github.com/duke-git/lancet/mathutil"
|
||||||
@@ -229,8 +260,12 @@ import "github.com/duke-git/lancet/mathutil"
|
|||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString)
|
||||||
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound)
|
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound)
|
||||||
|
- [AngleToRadian](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#AngleToRadian)
|
||||||
|
- [RadianToAngle](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RadianToAngle)
|
||||||
|
- [PointDistance](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#PointDistance)
|
||||||
|
- [IsPrime](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#IsPrime)
|
||||||
|
|
||||||
### 7. Netutil package contains functions to get net information and send http request.
|
### 9. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/netutil"
|
import "github.com/duke-git/lancet/netutil"
|
||||||
@@ -253,8 +288,12 @@ import "github.com/duke-git/lancet/netutil"
|
|||||||
- [HttpPut](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPut)
|
- [HttpPut](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPut)
|
||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch)
|
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse)
|
||||||
|
- [UploadFile](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#UploadFile)
|
||||||
|
- [DownloadFile](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#DownloadFile)
|
||||||
|
- [IsPingConnected](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#IsPingConnected)
|
||||||
|
- [IsTelnetConnected](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#IsTelnetConnected)
|
||||||
|
|
||||||
### 8. Random package implements some basic functions to generate random int and string.
|
### 10. Random package implements some basic functions to generate random int and string.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/random"
|
import "github.com/duke-git/lancet/random"
|
||||||
@@ -271,7 +310,7 @@ import "github.com/duke-git/lancet/random"
|
|||||||
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeralOrLetter)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### 9. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
### 11. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/retry"
|
import "github.com/duke-git/lancet/retry"
|
||||||
@@ -285,7 +324,7 @@ import "github.com/duke-git/lancet/retry"
|
|||||||
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryDuration)
|
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryDuration)
|
||||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryTimes)
|
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryTimes)
|
||||||
|
|
||||||
### 10. Slice contains some functions to manipulate slice.
|
### 12. Slice contains some functions to manipulate slice.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/slice"
|
import "github.com/duke-git/lancet/slice"
|
||||||
@@ -334,7 +373,7 @@ import "github.com/duke-git/lancet/slice"
|
|||||||
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#UpdateByIndex)
|
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#UpdateByIndex)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Without)
|
||||||
|
|
||||||
### 11. Strutil package contains some functions to manipulate string.
|
### 13. Strutil package contains some functions to manipulate string.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/strutil"
|
import "github.com/duke-git/lancet/strutil"
|
||||||
@@ -350,17 +389,29 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [Capitalize](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Capitalize)
|
- [Capitalize](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Capitalize)
|
||||||
- [IsString](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#IsString)
|
- [IsString](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#IsString)
|
||||||
- [KebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#KebabCase)
|
- [KebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#KebabCase)
|
||||||
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperKebabCase)
|
||||||
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#LowerFirst)
|
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#LowerFirst)
|
||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperFirst)
|
||||||
|
- [Pad](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Pad)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadStart)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Reverse)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SnakeCase)
|
||||||
|
- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperSnakeCase)
|
||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Unwrap)
|
||||||
|
- [SplitWords](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitWords)
|
||||||
|
- [WordCount](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#WordCount)
|
||||||
|
- [RemoveNonPrintable](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#StringToBytes)
|
||||||
|
- [BytesToString](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#BytesToString)
|
||||||
|
- [IsBlank](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#IsBlank)
|
||||||
|
- [HasPrefixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#HasSuffixAny)
|
||||||
|
- [IndexOffset](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#IndexOffset)
|
||||||
|
|
||||||
### 12. System package contain some functions about os, runtime, shell command.
|
### 14. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/system"
|
import "github.com/duke-git/lancet/system"
|
||||||
@@ -378,7 +429,7 @@ import "github.com/duke-git/lancet/system"
|
|||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system.md#ExecCommand)
|
||||||
- [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system.md#GetOsBits)
|
- [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system.md#GetOsBits)
|
||||||
|
|
||||||
### 13. Validator package contains some functions for data validation.
|
### 15. Validator package contains some functions for data validation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/validator"
|
import "github.com/duke-git/lancet/validator"
|
||||||
@@ -401,11 +452,14 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsDns](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsDns)
|
- [IsDns](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsDns)
|
||||||
- [IsEmail](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsEmail)
|
- [IsEmail](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsEmail)
|
||||||
- [IsEmptyString](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsEmptyString)
|
- [IsEmptyString](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsEmptyString)
|
||||||
|
- [IsInt](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsInt)
|
||||||
|
- [IsFloat](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsFloat)
|
||||||
|
- [IsNumber](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsNumber)
|
||||||
|
- [IsIntStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIntStr)
|
||||||
- [IsFloatStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsFloatStr)
|
- [IsFloatStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsFloatStr)
|
||||||
- [IsNumberStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsNumberStr)
|
- [IsNumberStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsNumberStr)
|
||||||
- [IsJSON](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsJSON)
|
- [IsJSON](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsJSON)
|
||||||
- [IsRegexMatch](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsRegexMatch)
|
- [IsRegexMatch](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsRegexMatch)
|
||||||
- [IsIntStr](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIntStr)
|
|
||||||
- [IsIp](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIp)
|
- [IsIp](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIp)
|
||||||
- [IsIpV4](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIpV4)
|
- [IsIpV4](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIpV4)
|
||||||
- [IsIpV6](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIpV6)
|
- [IsIpV6](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsIpV6)
|
||||||
@@ -413,6 +467,9 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword)
|
||||||
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue)
|
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue)
|
||||||
|
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsGBK)
|
||||||
|
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsASCII)
|
||||||
|
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsPrintable)
|
||||||
|
|
||||||
## How to Contribute
|
## How to Contribute
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 👏 全面、高效、可复用
|
- 👏 全面、高效、可复用
|
||||||
- 💪 200+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
|
- 💪 400+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
|
||||||
- 💅 只依赖 go 标准库
|
- 💅 只依赖 go 标准库
|
||||||
- 🌍 所有导出函数单元测试覆盖率 100%
|
- 🌍 所有导出函数单元测试覆盖率 100%
|
||||||
|
|
||||||
@@ -62,7 +62,22 @@ func main() {
|
|||||||
|
|
||||||
## API 文档
|
## API 文档
|
||||||
|
|
||||||
### 1. convertor 转换器包支持一些常见的数据类型转换。
|
|
||||||
|
### 1. compare包提供几个轻量级的类型比较函数。
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/compare"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
|
||||||
|
- [Equal](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#Equal)
|
||||||
|
- [EqualValue](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#EqualValue)
|
||||||
|
- [LessThan](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#LessThan)
|
||||||
|
- [GreaterThan](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#GreaterThan)
|
||||||
|
- [LessOrEqual](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](https://github.com/duke-git/lancet/blob/v1/docs/compare_zh-CN.md#GreaterOrEqual)
|
||||||
|
|
||||||
|
### 2. convertor转换器包支持一些常见的数据类型转换。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/convertor"
|
import "github.com/duke-git/lancet/convertor"
|
||||||
@@ -82,8 +97,10 @@ import "github.com/duke-git/lancet/convertor"
|
|||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#StructToMap)
|
||||||
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#EncodeByte)
|
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#EncodeByte)
|
||||||
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DecodeByte)
|
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DecodeByte)
|
||||||
|
- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DeepClone)
|
||||||
|
- [CopyProperties](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#CopyProperties)
|
||||||
|
|
||||||
### 2. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
### 3. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/cryptor"
|
import "github.com/duke-git/lancet/cryptor"
|
||||||
@@ -124,7 +141,7 @@ import "github.com/duke-git/lancet/cryptor"
|
|||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt)
|
||||||
|
|
||||||
### 3. datetime 日期时间处理包,格式化日期,比较日期。
|
### 4. datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/datetime"
|
import "github.com/duke-git/lancet/datetime"
|
||||||
@@ -163,7 +180,7 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
|
|
||||||
### 4. fileutil 包支持文件基本操作。
|
### 5. fileutil 包支持文件基本操作。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/fileutil"
|
import "github.com/duke-git/lancet/fileutil"
|
||||||
@@ -186,8 +203,14 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [ReadFileByLine](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadFileByLine)
|
- [ReadFileByLine](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadFileByLine)
|
||||||
- [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Zip)
|
- [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Zip)
|
||||||
- [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#UnZip)
|
- [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#UnZip)
|
||||||
|
- [CurrentPath](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#CurrentPath)
|
||||||
|
- [IsZipFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#IsZipFile)
|
||||||
|
- [FileSize](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#FileSize)
|
||||||
|
- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#MTime)
|
||||||
|
- [Sha](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Sha)
|
||||||
|
- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadCsvFile)
|
||||||
|
|
||||||
### 5. formatter 格式化器包含一些数据格式化处理方法。
|
### 6. formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/formatter"
|
import "github.com/duke-git/lancet/formatter"
|
||||||
@@ -196,8 +219,14 @@ import "github.com/duke-git/lancet/formatter"
|
|||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma)
|
||||||
|
- [Pretty](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Pretty)
|
||||||
|
- [PrettyToWriter](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#PrettyToWriter)
|
||||||
|
- [DecimalBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#DecimalBytes)
|
||||||
|
- [BinaryBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#BinaryBytes)
|
||||||
|
- [ParseDecimalBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#ParseDecimalBytes)
|
||||||
|
- [ParseBinaryBytes](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#ParseBinaryBytes)
|
||||||
|
|
||||||
### function 函数包控制函数执行流程,包含部分函数式编程。
|
### 7. function 函数包控制函数执行流程,包含部分函数式编程。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/function"
|
import "github.com/duke-git/lancet/function"
|
||||||
@@ -211,9 +240,11 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Debounced)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Delay)
|
||||||
|
- [Pipeline](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Pipeline)
|
||||||
|
- [Schedule](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Schedule)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Watcher)
|
||||||
|
|
||||||
### 6. mathutil 包实现了一些数学计算的函数。
|
### 8. mathutil 包实现了一些数学计算的函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/mathutil"
|
import "github.com/duke-git/lancet/mathutil"
|
||||||
@@ -228,8 +259,12 @@ import "github.com/duke-git/lancet/mathutil"
|
|||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToString)
|
||||||
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#TruncRound)
|
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#TruncRound)
|
||||||
|
- [AngleToRadian](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#AngleToRadian)
|
||||||
|
- [RadianToAngle](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RadianToAngle)
|
||||||
|
- [PointDistance](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#PointDistance)
|
||||||
|
- [IsPrime](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#IsPrime)
|
||||||
|
|
||||||
### 7. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
### 9. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/netutil"
|
import "github.com/duke-git/lancet/netutil"
|
||||||
@@ -252,8 +287,12 @@ import "github.com/duke-git/lancet/netutil"
|
|||||||
- [HttpPut](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPut)
|
- [HttpPut](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPut)
|
||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPatch)
|
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ParseHttpResponse)
|
||||||
|
- [UploadFile](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#UploadFile)
|
||||||
|
- [DownloadFile](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#DownloadFile)
|
||||||
|
- [IsPingConnected](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#IsPingConnected)
|
||||||
|
- [IsTelnetConnected](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#IsTelnetConnected)
|
||||||
|
|
||||||
### 8. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
### 10. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/random"
|
import "github.com/duke-git/lancet/random"
|
||||||
@@ -270,7 +309,7 @@ import "github.com/duke-git/lancet/random"
|
|||||||
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeralOrLetter)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### 9. retry 重试执行函数直到函数运行成功或被 context cancel。
|
### 11. retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/retry"
|
import "github.com/duke-git/lancet/retry"
|
||||||
@@ -284,7 +323,7 @@ import "github.com/duke-git/lancet/retry"
|
|||||||
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration)
|
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration)
|
||||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes)
|
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes)
|
||||||
|
|
||||||
### 10. slice 包包含操作切片的方法集合。
|
### 12. slice 包包含操作切片的方法集合。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/slice"
|
import "github.com/duke-git/lancet/slice"
|
||||||
@@ -333,7 +372,7 @@ import "github.com/duke-git/lancet/slice"
|
|||||||
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex)
|
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without)
|
||||||
|
|
||||||
### 12. strutil 包含处理字符串的相关函数。
|
### 13. strutil 包含处理字符串的相关函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/strutil"
|
import "github.com/duke-git/lancet/strutil"
|
||||||
@@ -349,17 +388,29 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [Capitalize](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Capitalize)
|
- [Capitalize](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Capitalize)
|
||||||
- [IsString](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#IsString)
|
- [IsString](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#IsString)
|
||||||
- [KebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#KebabCase)
|
- [KebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#KebabCase)
|
||||||
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperKebabCase)
|
||||||
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#LowerFirst)
|
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#LowerFirst)
|
||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperFirst)
|
||||||
|
- [Pad](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Pad)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadStart)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Reverse)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SnakeCase)
|
||||||
|
- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperSnakeCase)
|
||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap)
|
||||||
|
- [SplitWords](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitWords)
|
||||||
|
- [WordCount](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#WordCount)
|
||||||
|
- [RemoveNonPrintable](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#StringToBytes)
|
||||||
|
- [BytesToString](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#BytesToString)
|
||||||
|
- [IsBlank](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#IsBlank)
|
||||||
|
- [HasPrefixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#HasSuffixAny)
|
||||||
|
- [IndexOffset](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#IndexOffset)
|
||||||
|
|
||||||
### 13. system 包含 os, runtime, shell command 相关函数。
|
### 14. system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/system"
|
import "github.com/duke-git/lancet/system"
|
||||||
@@ -377,7 +428,7 @@ import "github.com/duke-git/lancet/system"
|
|||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#ExecCommand)
|
||||||
- [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#GetOsBits)
|
- [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#GetOsBits)
|
||||||
|
|
||||||
### 14. validator 验证器包,包含常用字符串格式验证函数。
|
### 15. validator 验证器包,包含常用字符串格式验证函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/validator"
|
import "github.com/duke-git/lancet/validator"
|
||||||
@@ -400,11 +451,14 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsDns](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsDns)
|
- [IsDns](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsDns)
|
||||||
- [IsEmail](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsEmail)
|
- [IsEmail](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsEmail)
|
||||||
- [IsEmptyString](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsEmptyString)
|
- [IsEmptyString](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsEmptyString)
|
||||||
|
- [IsInt](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsInt)
|
||||||
|
- [IsFloat](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsFloat)
|
||||||
|
- [IsNumber](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsNumber)
|
||||||
|
- [IsIntStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIntStr)
|
||||||
- [IsFloatStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsFloatStr)
|
- [IsFloatStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsFloatStr)
|
||||||
- [IsNumberStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsNumberStr)
|
- [IsNumberStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsNumberStr)
|
||||||
- [IsJSON](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsJSON)
|
- [IsJSON](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsJSON)
|
||||||
- [IsRegexMatch](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsRegexMatch)
|
- [IsRegexMatch](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsRegexMatch)
|
||||||
- [IsIntStr](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIntStr)
|
|
||||||
- [IsIp](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIp)
|
- [IsIp](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIp)
|
||||||
- [IsIpV4](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIpV4)
|
- [IsIpV4](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIpV4)
|
||||||
- [IsIpV6](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIpV6)
|
- [IsIpV6](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsIpV6)
|
||||||
@@ -412,6 +466,9 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword)
|
||||||
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue)
|
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue)
|
||||||
|
- [IsGBK](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsGBK)
|
||||||
|
- [IsASCII](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsASCII)
|
||||||
|
- [IsPrintable](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsPrintable)
|
||||||
|
|
||||||
## 如何贡献代码
|
## 如何贡献代码
|
||||||
|
|
||||||
|
|||||||
58
compare/compare.go
Normal file
58
compare/compare.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
// Copyright 2023 dudaodong@gmail.com. All rights resulterved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package compare provides a lightweight comparison function on interface{} type.
|
||||||
|
// reference: https://github.com/stretchr/testify
|
||||||
|
package compare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
// operator type
|
||||||
|
const (
|
||||||
|
equal = "eq"
|
||||||
|
lessThan = "lt"
|
||||||
|
greaterThan = "gt"
|
||||||
|
lessOrEqual = "le"
|
||||||
|
greaterOrEqual = "ge"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
timeType = reflect.TypeOf(time.Time{})
|
||||||
|
bytesType = reflect.TypeOf([]byte{})
|
||||||
|
)
|
||||||
|
|
||||||
|
// Equal checks if two values are equal or not. (check both type and value)
|
||||||
|
func Equal(left, right interface{}) bool {
|
||||||
|
return compareValue(equal, left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EqualValue checks if two values are equal or not. (check value only)
|
||||||
|
func EqualValue(left, right interface{}) bool {
|
||||||
|
ls, rs := convertor.ToString(left), convertor.ToString(right)
|
||||||
|
return ls == rs
|
||||||
|
}
|
||||||
|
|
||||||
|
// LessThan checks if value `left` less than value `right`.
|
||||||
|
func LessThan(left, right interface{}) bool {
|
||||||
|
return compareValue(lessThan, left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GreaterThan checks if value `left` greater than value `right`.
|
||||||
|
func GreaterThan(left, right interface{}) bool {
|
||||||
|
return compareValue(greaterThan, left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LessOrEqual checks if value `left` less than or equal to value `right`.
|
||||||
|
func LessOrEqual(left, right interface{}) bool {
|
||||||
|
return compareValue(lessOrEqual, left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GreaterOrEqual checks if value `left` greater than or equal to value `right`.
|
||||||
|
func GreaterOrEqual(left, right interface{}) bool {
|
||||||
|
return compareValue(greaterOrEqual, left, right)
|
||||||
|
}
|
||||||
170
compare/compare_example_test.go
Normal file
170
compare/compare_example_test.go
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
package compare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleEqual() {
|
||||||
|
result1 := Equal(1, 1)
|
||||||
|
result2 := Equal("1", "1")
|
||||||
|
result3 := Equal([]int{1, 2, 3}, []int{1, 2, 3})
|
||||||
|
result4 := Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"})
|
||||||
|
|
||||||
|
result5 := Equal(1, "1")
|
||||||
|
result6 := Equal(1, int64(1))
|
||||||
|
result7 := Equal([]int{1, 2}, []int{1, 2, 3})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEqualValue() {
|
||||||
|
result1 := EqualValue(1, 1)
|
||||||
|
result2 := EqualValue(int(1), int64(1))
|
||||||
|
result3 := EqualValue(1, "1")
|
||||||
|
result4 := EqualValue(1, "2")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleLessThan() {
|
||||||
|
result1 := LessThan(1, 2)
|
||||||
|
result2 := LessThan(1.1, 2.2)
|
||||||
|
result3 := LessThan("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := LessThan(time1, time2)
|
||||||
|
|
||||||
|
result5 := LessThan(2, 1)
|
||||||
|
result6 := LessThan(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGreaterThan() {
|
||||||
|
result1 := GreaterThan(2, 1)
|
||||||
|
result2 := GreaterThan(2.2, 1.1)
|
||||||
|
result3 := GreaterThan("b", "a")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := GreaterThan(time2, time1)
|
||||||
|
|
||||||
|
result5 := GreaterThan(1, 2)
|
||||||
|
result6 := GreaterThan(int64(2), 1)
|
||||||
|
result7 := GreaterThan("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleLessOrEqual() {
|
||||||
|
result1 := LessOrEqual(1, 1)
|
||||||
|
result2 := LessOrEqual(1.1, 2.2)
|
||||||
|
result3 := LessOrEqual("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := LessOrEqual(time1, time2)
|
||||||
|
|
||||||
|
result5 := LessOrEqual(2, 1)
|
||||||
|
result6 := LessOrEqual(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGreaterOrEqual() {
|
||||||
|
result1 := GreaterOrEqual(1, 1)
|
||||||
|
result2 := GreaterOrEqual(2.2, 1.1)
|
||||||
|
result3 := GreaterOrEqual("b", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := GreaterOrEqual(time2, time1)
|
||||||
|
|
||||||
|
result5 := GreaterOrEqual(1, 2)
|
||||||
|
result6 := GreaterOrEqual(int64(2), 1)
|
||||||
|
result7 := GreaterOrEqual("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
323
compare/compare_internal.go
Normal file
323
compare/compare_internal.go
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
package compare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func compareValue(operator string, left, right interface{}) bool {
|
||||||
|
leftType, rightType := reflect.TypeOf(left), reflect.TypeOf(right)
|
||||||
|
|
||||||
|
if leftType.Kind() != rightType.Kind() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch leftType.Kind() {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||||
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||||
|
reflect.Float32, reflect.Float64, reflect.Bool, reflect.String:
|
||||||
|
return compareBasicValue(operator, left, right)
|
||||||
|
|
||||||
|
case reflect.Struct, reflect.Slice, reflect.Map:
|
||||||
|
return compareRefValue(operator, left, right, leftType.Kind())
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareRefValue(operator string, leftObj, rightObj interface{}, kind reflect.Kind) bool {
|
||||||
|
leftVal, rightVal := reflect.ValueOf(leftObj), reflect.ValueOf(rightObj)
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case reflect.Struct:
|
||||||
|
|
||||||
|
// compare time
|
||||||
|
if leftVal.CanConvert(timeType) {
|
||||||
|
timeObj1, ok := leftObj.(time.Time)
|
||||||
|
if !ok {
|
||||||
|
timeObj1 = leftVal.Convert(timeType).Interface().(time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeObj2, ok := rightObj.(time.Time)
|
||||||
|
if !ok {
|
||||||
|
timeObj2 = rightVal.Convert(timeType).Interface().(time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
return compareBasicValue(operator, timeObj1.UnixNano(), timeObj2.UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
// for other struct type, only process equal operator
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
return objectsAreEqualValues(leftObj, rightObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
case reflect.Slice:
|
||||||
|
// compare []byte
|
||||||
|
if leftVal.CanConvert(bytesType) {
|
||||||
|
bytesObj1, ok := leftObj.([]byte)
|
||||||
|
if !ok {
|
||||||
|
bytesObj1 = leftVal.Convert(bytesType).Interface().([]byte)
|
||||||
|
}
|
||||||
|
bytesObj2, ok := rightObj.([]byte)
|
||||||
|
if !ok {
|
||||||
|
bytesObj2 = rightVal.Convert(bytesType).Interface().([]byte)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if bytes.Compare(bytesObj1, bytesObj2) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if bytes.Compare(bytesObj1, bytesObj2) == -1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if bytes.Compare(bytesObj1, bytesObj2) == 1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if bytes.Compare(bytesObj1, bytesObj2) <= 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if bytes.Compare(bytesObj1, bytesObj2) >= 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// for other type slice, only process equal operator
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
return reflect.DeepEqual(leftObj, rightObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
case reflect.Map:
|
||||||
|
// only process equal operator
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
return reflect.DeepEqual(leftObj, rightObj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func objectsAreEqualValues(expected, actual interface{}) bool {
|
||||||
|
if objectsAreEqual(expected, actual) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
actualType := reflect.TypeOf(actual)
|
||||||
|
if actualType == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
expectedValue := reflect.ValueOf(expected)
|
||||||
|
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
|
||||||
|
// Attempt comparison after type conversion
|
||||||
|
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func objectsAreEqual(expected, actual interface{}) bool {
|
||||||
|
if expected == nil || actual == nil {
|
||||||
|
return expected == actual
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, ok := expected.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return reflect.DeepEqual(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
act, ok := actual.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if exp == nil || act == nil {
|
||||||
|
return exp == nil && act == nil
|
||||||
|
}
|
||||||
|
return bytes.Equal(exp, act)
|
||||||
|
}
|
||||||
|
|
||||||
|
// compareBasic compare basic value: integer, float, string, bool
|
||||||
|
func compareBasicValue(operator string, leftValue, rightValue interface{}) bool {
|
||||||
|
if leftValue == nil && rightValue == nil && operator == equal {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
switch leftVal := leftValue.(type) {
|
||||||
|
case json.Number:
|
||||||
|
if left, err := leftVal.Float64(); err == nil {
|
||||||
|
switch rightVal := rightValue.(type) {
|
||||||
|
case json.Number:
|
||||||
|
if right, err := rightVal.Float64(); err == nil {
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if left > right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if left <= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if left >= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
|
||||||
|
right, err := convertor.ToFloat(rightValue)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if left > right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if left <= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if left >= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
|
||||||
|
left, err := convertor.ToFloat(leftValue)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch rightVal := rightValue.(type) {
|
||||||
|
case json.Number:
|
||||||
|
if right, err := rightVal.Float64(); err == nil {
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if left > right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if left <= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if left >= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case float32, float64, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
|
||||||
|
right, err := convertor.ToFloat(rightValue)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if left > right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if left <= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if left >= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case string:
|
||||||
|
left := leftVal
|
||||||
|
switch right := rightValue.(type) {
|
||||||
|
case string:
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessThan:
|
||||||
|
if left < right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterThan:
|
||||||
|
if left > right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case lessOrEqual:
|
||||||
|
if left <= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case greaterOrEqual:
|
||||||
|
if left >= right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case bool:
|
||||||
|
left := leftVal
|
||||||
|
switch right := rightValue.(type) {
|
||||||
|
case bool:
|
||||||
|
switch operator {
|
||||||
|
case equal:
|
||||||
|
if left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
134
compare/compare_test.go
Normal file
134
compare/compare_test.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
package compare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEqual(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEqual")
|
||||||
|
|
||||||
|
assert.Equal(true, Equal(1, 1))
|
||||||
|
assert.Equal(true, Equal(int64(1), int64(1)))
|
||||||
|
assert.Equal(true, Equal("a", "a"))
|
||||||
|
assert.Equal(true, Equal(true, true))
|
||||||
|
assert.Equal(true, Equal([]int{1, 2, 3}, []int{1, 2, 3}))
|
||||||
|
assert.Equal(true, Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"}))
|
||||||
|
|
||||||
|
assert.Equal(false, Equal(1, 2))
|
||||||
|
assert.Equal(false, Equal(1, int64(1)))
|
||||||
|
assert.Equal(false, Equal("a", "b"))
|
||||||
|
assert.Equal(false, Equal(true, false))
|
||||||
|
assert.Equal(false, Equal([]int{1, 2}, []int{1, 2, 3}))
|
||||||
|
assert.Equal(false, Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a"}))
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
time3 := time1.Add(time.Second)
|
||||||
|
|
||||||
|
assert.Equal(false, Equal(time1, time2))
|
||||||
|
assert.Equal(true, Equal(time2, time3))
|
||||||
|
|
||||||
|
st1 := struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}{
|
||||||
|
A: "a",
|
||||||
|
B: "b",
|
||||||
|
}
|
||||||
|
|
||||||
|
st2 := struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}{
|
||||||
|
A: "a",
|
||||||
|
B: "b",
|
||||||
|
}
|
||||||
|
|
||||||
|
st3 := struct {
|
||||||
|
A string
|
||||||
|
B string
|
||||||
|
}{
|
||||||
|
A: "a1",
|
||||||
|
B: "b",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(true, Equal(st1, st2))
|
||||||
|
assert.Equal(false, Equal(st1, st3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEqualValue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEqualValue")
|
||||||
|
|
||||||
|
assert.Equal(true, EqualValue(1, 1))
|
||||||
|
assert.Equal(true, EqualValue(int(1), int64(1)))
|
||||||
|
assert.Equal(true, EqualValue(1, "1"))
|
||||||
|
|
||||||
|
assert.Equal(false, EqualValue(1, "2"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLessThan(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLessThan")
|
||||||
|
|
||||||
|
assert.Equal(true, LessThan(1, 2))
|
||||||
|
assert.Equal(true, LessThan(1.1, 2.2))
|
||||||
|
assert.Equal(true, LessThan("a", "b"))
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
assert.Equal(true, LessThan(time1, time2))
|
||||||
|
|
||||||
|
assert.Equal(false, LessThan(1, 1))
|
||||||
|
assert.Equal(false, LessThan(1, int64(1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGreaterThan(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGreaterThan")
|
||||||
|
|
||||||
|
assert.Equal(true, GreaterThan(2, 1))
|
||||||
|
assert.Equal(true, GreaterThan(2.2, 1.1))
|
||||||
|
assert.Equal(true, GreaterThan("b", "a"))
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
assert.Equal(true, GreaterThan(time2, time1))
|
||||||
|
|
||||||
|
assert.Equal(false, GreaterThan(1, 2))
|
||||||
|
assert.Equal(false, GreaterThan(int64(2), 1))
|
||||||
|
assert.Equal(false, GreaterThan("b", "c"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLessOrEqual(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLessOrEqual")
|
||||||
|
|
||||||
|
assert.Equal(true, LessOrEqual(1, 2))
|
||||||
|
assert.Equal(true, LessOrEqual(1, 1))
|
||||||
|
assert.Equal(true, LessOrEqual(1.1, 2.2))
|
||||||
|
assert.Equal(true, LessOrEqual("a", "b"))
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
assert.Equal(true, LessOrEqual(time1, time2))
|
||||||
|
|
||||||
|
assert.Equal(false, LessOrEqual(2, 1))
|
||||||
|
assert.Equal(false, LessOrEqual(1, int64(2)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGreaterOrEqual(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGreaterThan")
|
||||||
|
|
||||||
|
assert.Equal(true, GreaterOrEqual(2, 1))
|
||||||
|
assert.Equal(true, GreaterOrEqual(1, 1))
|
||||||
|
assert.Equal(true, GreaterOrEqual(2.2, 1.1))
|
||||||
|
assert.Equal(true, GreaterOrEqual("b", "b"))
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
assert.Equal(true, GreaterOrEqual(time2, time1))
|
||||||
|
|
||||||
|
assert.Equal(false, GreaterOrEqual(1, 2))
|
||||||
|
assert.Equal(false, GreaterOrEqual(int64(2), 1))
|
||||||
|
assert.Equal(false, GreaterOrEqual("b", "c"))
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -262,3 +263,59 @@ func DecodeByte(data []byte, target interface{}) error {
|
|||||||
decoder := gob.NewDecoder(buffer)
|
decoder := gob.NewDecoder(buffer)
|
||||||
return decoder.Decode(target)
|
return decoder.Decode(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeepClone creates a deep copy of passed item.
|
||||||
|
// can't clone unexported field of struct
|
||||||
|
func DeepClone(src interface{}) interface{} {
|
||||||
|
c := cloner{
|
||||||
|
ptrs: map[reflect.Type]map[uintptr]reflect.Value{},
|
||||||
|
}
|
||||||
|
result := c.clone(reflect.ValueOf(src))
|
||||||
|
if result.Kind() == reflect.Invalid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
||||||
|
func CopyProperties(dst, src interface{}) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
err = errors.New(fmt.Sprintf("%v", e))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst)
|
||||||
|
srcType, srcValue := reflect.TypeOf(src), reflect.ValueOf(src)
|
||||||
|
|
||||||
|
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct {
|
||||||
|
return errors.New("CopyProperties: param dst should be struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if srcType.Kind() == reflect.Ptr {
|
||||||
|
srcType, srcValue = srcType.Elem(), srcValue.Elem()
|
||||||
|
}
|
||||||
|
if srcType.Kind() != reflect.Struct {
|
||||||
|
return errors.New("CopyProperties: param src should be a struct or struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
dstType, dstValue = dstType.Elem(), dstValue.Elem()
|
||||||
|
|
||||||
|
propertyNums := dstType.NumField()
|
||||||
|
|
||||||
|
for i := 0; i < propertyNums; i++ {
|
||||||
|
property := dstType.Field(i)
|
||||||
|
propertyValue := srcValue.FieldByName(property.Name)
|
||||||
|
|
||||||
|
if !propertyValue.IsValid() || property.Type != propertyValue.Type() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if dstValue.Field(i).CanSet() {
|
||||||
|
dstValue.Field(i).Set(propertyValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
216
convertor/convertor_internal.go
Normal file
216
convertor/convertor_internal.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
// Copyright 2023 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package convertor implements some functions to convert data.
|
||||||
|
package convertor
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
type cloner struct {
|
||||||
|
ptrs map[reflect.Type]map[uintptr]reflect.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// clone return a duplicate of passed item.
|
||||||
|
func (c *cloner) clone(v reflect.Value) reflect.Value {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Invalid:
|
||||||
|
return reflect.ValueOf(nil)
|
||||||
|
|
||||||
|
// bool
|
||||||
|
case reflect.Bool:
|
||||||
|
return reflect.ValueOf(v.Bool())
|
||||||
|
|
||||||
|
//int
|
||||||
|
case reflect.Int:
|
||||||
|
return reflect.ValueOf(int(v.Int()))
|
||||||
|
case reflect.Int8:
|
||||||
|
return reflect.ValueOf(int8(v.Int()))
|
||||||
|
case reflect.Int16:
|
||||||
|
return reflect.ValueOf(int16(v.Int()))
|
||||||
|
case reflect.Int32:
|
||||||
|
return reflect.ValueOf(int32(v.Int()))
|
||||||
|
case reflect.Int64:
|
||||||
|
return reflect.ValueOf(v.Int())
|
||||||
|
|
||||||
|
// uint
|
||||||
|
case reflect.Uint:
|
||||||
|
return reflect.ValueOf(uint(v.Uint()))
|
||||||
|
case reflect.Uint8:
|
||||||
|
return reflect.ValueOf(uint8(v.Uint()))
|
||||||
|
case reflect.Uint16:
|
||||||
|
return reflect.ValueOf(uint16(v.Uint()))
|
||||||
|
case reflect.Uint32:
|
||||||
|
return reflect.ValueOf(uint32(v.Uint()))
|
||||||
|
case reflect.Uint64:
|
||||||
|
return reflect.ValueOf(v.Uint())
|
||||||
|
|
||||||
|
// float
|
||||||
|
case reflect.Float32:
|
||||||
|
return reflect.ValueOf(float32(v.Float()))
|
||||||
|
case reflect.Float64:
|
||||||
|
return reflect.ValueOf(v.Float())
|
||||||
|
|
||||||
|
// complex
|
||||||
|
case reflect.Complex64:
|
||||||
|
return reflect.ValueOf(complex64(v.Complex()))
|
||||||
|
case reflect.Complex128:
|
||||||
|
return reflect.ValueOf(v.Complex())
|
||||||
|
|
||||||
|
// string
|
||||||
|
case reflect.String:
|
||||||
|
return reflect.ValueOf(v.String())
|
||||||
|
|
||||||
|
// array
|
||||||
|
case reflect.Array, reflect.Slice:
|
||||||
|
return c.cloneArray(v)
|
||||||
|
|
||||||
|
// map
|
||||||
|
case reflect.Map:
|
||||||
|
return c.cloneMap(v)
|
||||||
|
|
||||||
|
// Ptr
|
||||||
|
case reflect.Ptr:
|
||||||
|
return c.clonePtr(v)
|
||||||
|
|
||||||
|
// struct
|
||||||
|
case reflect.Struct:
|
||||||
|
return c.cloneStruct(v)
|
||||||
|
|
||||||
|
// func
|
||||||
|
case reflect.Func:
|
||||||
|
return v
|
||||||
|
|
||||||
|
// interface
|
||||||
|
case reflect.Interface:
|
||||||
|
return c.clone(v.Elem())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneArray(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := reflect.MakeSlice(v.Type(), v.Len(), v.Len())
|
||||||
|
|
||||||
|
for i := 0; i < v.Len(); i++ {
|
||||||
|
val := c.clone(v.Index(i))
|
||||||
|
|
||||||
|
if val.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item := arr.Index(i)
|
||||||
|
if !item.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item.Set(val.Convert(item.Type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneMap(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedMap := reflect.MakeMap(v.Type())
|
||||||
|
|
||||||
|
for _, key := range v.MapKeys() {
|
||||||
|
value := v.MapIndex(key)
|
||||||
|
clonedKey := c.clone(key)
|
||||||
|
clonedValue := c.clone(value)
|
||||||
|
|
||||||
|
if !isNillable(clonedKey) || !clonedKey.IsNil() {
|
||||||
|
clonedKey = clonedKey.Convert(key.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isNillable(clonedValue) || !clonedValue.IsNil()) && clonedValue.IsValid() {
|
||||||
|
clonedValue = clonedValue.Convert(value.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !clonedValue.IsValid() {
|
||||||
|
clonedValue = reflect.Zero(clonedMap.Type().Elem())
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedMap.SetMapIndex(clonedKey, clonedValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return clonedMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func isNillable(v reflect.Value) bool {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Chan, reflect.Interface, reflect.Ptr, reflect.Func:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) clonePtr(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
var newVal reflect.Value
|
||||||
|
|
||||||
|
if v.Elem().CanAddr() {
|
||||||
|
ptrs, exists := c.ptrs[v.Type()]
|
||||||
|
if exists {
|
||||||
|
if newVal, exists := ptrs[v.Elem().UnsafeAddr()]; exists {
|
||||||
|
return newVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newVal = c.clone(v.Elem())
|
||||||
|
|
||||||
|
if v.Elem().CanAddr() {
|
||||||
|
ptrs, exists := c.ptrs[v.Type()]
|
||||||
|
if exists {
|
||||||
|
if newVal, exists := ptrs[v.Elem().UnsafeAddr()]; exists {
|
||||||
|
return newVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedPtr := reflect.New(newVal.Type())
|
||||||
|
clonedPtr.Elem().Set(newVal)
|
||||||
|
|
||||||
|
return clonedPtr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneStruct(v reflect.Value) reflect.Value {
|
||||||
|
clonedStructPtr := reflect.New(v.Type())
|
||||||
|
clonedStruct := clonedStructPtr.Elem()
|
||||||
|
|
||||||
|
if v.CanAddr() {
|
||||||
|
ptrs := c.ptrs[clonedStructPtr.Type()]
|
||||||
|
if ptrs == nil {
|
||||||
|
ptrs = make(map[uintptr]reflect.Value)
|
||||||
|
c.ptrs[clonedStructPtr.Type()] = ptrs
|
||||||
|
}
|
||||||
|
ptrs[v.UnsafeAddr()] = clonedStructPtr
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
newStructValue := clonedStruct.Field(i)
|
||||||
|
if !newStructValue.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedVal := c.clone(v.Field(i))
|
||||||
|
if !clonedVal.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newStructValue.Set(clonedVal.Convert(newStructValue.Type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return clonedStruct
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -213,3 +214,97 @@ func TestDecodeByte(t *testing.T) {
|
|||||||
DecodeByte(byteData, &obj)
|
DecodeByte(byteData, &obj)
|
||||||
assert.Equal("abc", obj)
|
assert.Equal("abc", obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeepClone(t *testing.T) {
|
||||||
|
// assert := internal.NewAssert(t, "TestDeepClone")
|
||||||
|
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, item := range cases {
|
||||||
|
cloned := DeepClone(item)
|
||||||
|
|
||||||
|
t.Log(cloned)
|
||||||
|
if &cloned == &item {
|
||||||
|
t.Fatalf("[TestDeepClone case #%d failed]: equal pointer", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(item, cloned) {
|
||||||
|
t.Fatalf("[TestDeepClone case #%d failed] unequal objects", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCopyProperties(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestCopyProperties")
|
||||||
|
|
||||||
|
type Address struct {
|
||||||
|
Country string
|
||||||
|
ZipCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Role string
|
||||||
|
Addr Address
|
||||||
|
Hobbys []string
|
||||||
|
salary int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Employee struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Role string
|
||||||
|
Addr Address
|
||||||
|
Hobbys []string
|
||||||
|
salary int
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000}
|
||||||
|
|
||||||
|
employee1 := Employee{}
|
||||||
|
|
||||||
|
err := CopyProperties(&employee1, &user)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal("user001", employee1.Name)
|
||||||
|
assert.Equal("Admin", employee1.Role)
|
||||||
|
assert.Equal("CN", employee1.Addr.Country)
|
||||||
|
assert.Equal(0, employee1.salary)
|
||||||
|
|
||||||
|
employee2 := Employee{Name: "employee001", Age: 20, Role: "User",
|
||||||
|
Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500}
|
||||||
|
|
||||||
|
err = CopyProperties(&employee2, &user)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal("user001", employee2.Name)
|
||||||
|
assert.Equal("Admin", employee2.Role)
|
||||||
|
assert.Equal("CN", employee2.Addr.Country)
|
||||||
|
assert.Equal(500, employee2.salary)
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,14 +17,23 @@ import (
|
|||||||
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesEcbEncrypt(data, key []byte) []byte {
|
func AesEcbEncrypt(data, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
size := len(key)
|
||||||
|
if size != 16 && size != 24 && size != 32 {
|
||||||
|
panic("key length shoud be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
|
||||||
|
cipher, _ := aes.NewCipher(generateAesKey(key, size))
|
||||||
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
||||||
|
|
||||||
plain := make([]byte, length*aes.BlockSize)
|
plain := make([]byte, length*aes.BlockSize)
|
||||||
|
|
||||||
copy(plain, data)
|
copy(plain, data)
|
||||||
|
|
||||||
pad := byte(len(plain) - len(data))
|
pad := byte(len(plain) - len(data))
|
||||||
for i := len(data); i < len(plain); i++ {
|
for i := len(data); i < len(plain); i++ {
|
||||||
plain[i] = pad
|
plain[i] = pad
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, len(plain))
|
encrypted := make([]byte, len(plain))
|
||||||
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
||||||
@@ -36,9 +45,14 @@ func AesEcbEncrypt(data, key []byte) []byte {
|
|||||||
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
|
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
size := len(key)
|
||||||
|
if size != 16 && size != 24 && size != 32 {
|
||||||
|
panic("key length shoud be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
|
||||||
|
cipher, _ := aes.NewCipher(generateAesKey(key, size))
|
||||||
decrypted := make([]byte, len(encrypted))
|
decrypted := make([]byte, len(encrypted))
|
||||||
//
|
|
||||||
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ package cryptor
|
|||||||
|
|
||||||
import "bytes"
|
import "bytes"
|
||||||
|
|
||||||
func generateAesKey(key []byte) []byte {
|
func generateAesKey(key []byte, size int) []byte {
|
||||||
genKey := make([]byte, 16)
|
aesKey := make([]byte, size)
|
||||||
copy(genKey, key)
|
copy(aesKey, key)
|
||||||
for i := 16; i < len(key); {
|
for i := size; i < len(key); {
|
||||||
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
|
for j := 0; j < size && i < len(key); j, i = j+1, i+1 {
|
||||||
genKey[j] ^= key[i]
|
aesKey[j] ^= key[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return genKey
|
return aesKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDesKey(key []byte) []byte {
|
func generateDesKey(key []byte) []byte {
|
||||||
|
|||||||
326
docs/compare.md
Normal file
326
docs/compare.md
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
# Compare
|
||||||
|
|
||||||
|
Package compare provides a lightweight comparison function on any type.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/v1/compare/compare.go](https://github.com/duke-git/lancet/blob/v1/compare/compare.go)
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/v1/compare/compare_internal.go](https://github.com/duke-git/lancet/blob/v1/compare/compare_internal.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/condition"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
|
||||||
|
- [Equal](#Equal)
|
||||||
|
- [EqualValue](#EqualValue)
|
||||||
|
- [LessThan](#LessThan)
|
||||||
|
- [GreaterThan](#GreaterThan)
|
||||||
|
- [LessOrEqual](#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](#GreaterOrEqual)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
|
<p>Checks if two values are equal or not. (check both type and value)</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Equal(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.Equal(1, 1)
|
||||||
|
result2 := compare.Equal("1", "1")
|
||||||
|
result3 := compare.Equal([]int{1, 2, 3}, []int{1, 2, 3})
|
||||||
|
result4 := compare.Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"})
|
||||||
|
|
||||||
|
result5 := compare.Equal(1, "1")
|
||||||
|
result6 := compare.Equal(1, int64(1))
|
||||||
|
result7 := compare.Equal([]int{1, 2}, []int{1, 2, 3})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EqualValue">EqualValue</span>
|
||||||
|
|
||||||
|
<p>Checks if two values are equal or not. (check value only)</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EqualValue(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.EqualValue(1, 1)
|
||||||
|
result2 := compare.EqualValue(int(1), int64(1))
|
||||||
|
result3 := compare.EqualValue(1, "1")
|
||||||
|
result4 := compare.EqualValue(1, "2")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessThan">LessThan</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.LessThan(1, 2)
|
||||||
|
result2 := compare.LessThan(1.1, 2.2)
|
||||||
|
result3 := compare.LessThan("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.LessThan(time1, time2)
|
||||||
|
|
||||||
|
result5 := compare.LessThan(2, 1)
|
||||||
|
result6 := compare.LessThan(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterThan">GreaterThan</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` greater than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.GreaterThan(2, 1)
|
||||||
|
result2 := compare.GreaterThan(2.2, 1.1)
|
||||||
|
result3 := compare.GreaterThan("b", "a")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.GreaterThan(time2, time1)
|
||||||
|
|
||||||
|
result5 := compare.GreaterThan(1, 2)
|
||||||
|
result6 := compare.GreaterThan(int64(2), 1)
|
||||||
|
result7 := compare.GreaterThan("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessOrEqual">LessOrEqual</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less than or equal than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.LessOrEqual(1, 1)
|
||||||
|
result2 := compare.LessOrEqual(1.1, 2.2)
|
||||||
|
result3 := compare.LessOrEqual("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.LessOrEqual(time1, time2)
|
||||||
|
|
||||||
|
result5 := compare.LessOrEqual(2, 1)
|
||||||
|
result6 := compare.LessOrEqual(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterOrEqual">GreaterOrEqual</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less greater or equal than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.GreaterOrEqual(1, 1)
|
||||||
|
result2 := compare.GreaterOrEqual(2.2, 1.1)
|
||||||
|
result3 := compare.GreaterOrEqual("b", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.GreaterOrEqual(time2, time1)
|
||||||
|
|
||||||
|
result5 := compare.GreaterOrEqual(1, 2)
|
||||||
|
result6 := compare.GreaterOrEqual(int64(2), 1)
|
||||||
|
result7 := compare.GreaterOrEqual("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
326
docs/compare_zh-CN.md
Normal file
326
docs/compare_zh-CN.md
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
# Compare
|
||||||
|
|
||||||
|
compare包提供几个轻量级的类型比较函数。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/v1/compare/compare.go](https://github.com/duke-git/lancet/blob/v1/compare/compare.go)
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/v1/compare/compare_internal.go](https://github.com/duke-git/lancet/blob/v1/compare/compare_internal.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/condition"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [Equal](#Equal)
|
||||||
|
- [EqualValue](#EqualValue)
|
||||||
|
- [LessThan](#LessThan)
|
||||||
|
- [GreaterThan](#GreaterThan)
|
||||||
|
- [LessOrEqual](#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](#GreaterOrEqual)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
|
<p>检查两个值是否相等(检查类型和值)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Equal(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.Equal(1, 1)
|
||||||
|
result2 := compare.Equal("1", "1")
|
||||||
|
result3 := compare.Equal([]int{1, 2, 3}, []int{1, 2, 3})
|
||||||
|
result4 := compare.Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"})
|
||||||
|
|
||||||
|
result5 := compare.Equal(1, "1")
|
||||||
|
result6 := compare.Equal(1, int64(1))
|
||||||
|
result7 := compare.Equal([]int{1, 2}, []int{1, 2, 3})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EqualValue">EqualValue</span>
|
||||||
|
|
||||||
|
<p>检查两个值是否相等(只检查值)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EqualValue(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.EqualValue(1, 1)
|
||||||
|
result2 := compare.EqualValue(int(1), int64(1))
|
||||||
|
result3 := compare.EqualValue(1, "1")
|
||||||
|
result4 := compare.EqualValue(1, "2")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessThan">LessThan</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否小于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.LessThan(1, 2)
|
||||||
|
result2 := compare.LessThan(1.1, 2.2)
|
||||||
|
result3 := compare.LessThan("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.LessThan(time1, time2)
|
||||||
|
|
||||||
|
result5 := compare.LessThan(2, 1)
|
||||||
|
result6 := compare.LessThan(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterThan">GreaterThan</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否大于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.GreaterThan(2, 1)
|
||||||
|
result2 := compare.GreaterThan(2.2, 1.1)
|
||||||
|
result3 := compare.GreaterThan("b", "a")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.GreaterThan(time2, time1)
|
||||||
|
|
||||||
|
result5 := compare.GreaterThan(1, 2)
|
||||||
|
result6 := compare.GreaterThan(int64(2), 1)
|
||||||
|
result7 := compare.GreaterThan("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessOrEqual">LessOrEqual</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否小于或等于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.LessOrEqual(1, 1)
|
||||||
|
result2 := compare.LessOrEqual(1.1, 2.2)
|
||||||
|
result3 := compare.LessOrEqual("a", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.LessOrEqual(time1, time2)
|
||||||
|
|
||||||
|
result5 := compare.LessOrEqual(2, 1)
|
||||||
|
result6 := compare.LessOrEqual(1, int64(2))
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterOrEqual">GreaterOrEqual</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否大于或参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/compare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := compare.GreaterOrEqual(1, 1)
|
||||||
|
result2 := compare.GreaterOrEqual(2.2, 1.1)
|
||||||
|
result3 := compare.GreaterOrEqual("b", "b")
|
||||||
|
|
||||||
|
time1 := time.Now()
|
||||||
|
time2 := time1.Add(time.Second)
|
||||||
|
result4 := compare.GreaterOrEqual(time2, time1)
|
||||||
|
|
||||||
|
result5 := compare.GreaterOrEqual(1, 2)
|
||||||
|
result6 := compare.GreaterOrEqual(int64(2), 1)
|
||||||
|
result7 := compare.GreaterOrEqual("b", "c")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
# Convertor
|
# Convertor
|
||||||
|
|
||||||
Package convertor contains some functions for data type convertion.
|
Package convertor contains some functions for data type convertion.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package convertor contains some functions for data type convertion.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/convertor"
|
"github.com/duke-git/lancet/convertor"
|
||||||
@@ -19,26 +21,28 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ColorHexToRGB](#ColorHexToRGB)
|
- [ColorHexToRGB](#ColorHexToRGB)
|
||||||
- [ColorRGBToHex](#ColorRGBToHex)
|
- [ColorRGBToHex](#ColorRGBToHex)
|
||||||
- [ToBool](#ToBool)
|
- [ToBool](#ToBool)
|
||||||
- [ToBytes](#ToBytes)
|
- [ToBytes](#ToBytes)
|
||||||
- [ToChar](#ToChar)
|
- [ToChar](#ToChar)
|
||||||
- [ToChannel](#ToChannel)
|
- [ToChannel](#ToChannel)
|
||||||
|
- [ToFloat](#ToFloat)
|
||||||
- [ToInt](#ToInt)
|
- [ToInt](#ToInt)
|
||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
- [ToString](#ToString)
|
- [ToString](#ToString)
|
||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
- [EncodeByte](#EncodeByte)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
|
- [DeepClone](#DeepClone)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
|
|
||||||
<p>Convert color hex to color rgb.</p>
|
<p>Convert color hex to color rgb.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -46,6 +50,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -63,8 +68,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>Convert color rgb to color hex.</p>
|
<p>Convert color rgb to color hex.</p>
|
||||||
@@ -74,6 +77,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ColorRGBToHex(red, green, blue int) string
|
func ColorRGBToHex(red, green, blue int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -94,8 +98,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>Convert string to a boolean value. Use strconv.ParseBool</p>
|
<p>Convert string to a boolean value. Use strconv.ParseBool</p>
|
||||||
@@ -105,6 +107,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBool(s string) (bool, error)
|
func ToBool(s string) (bool, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -130,8 +133,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>Convert interface to byte slice.</p>
|
<p>Convert interface to byte slice.</p>
|
||||||
@@ -141,6 +142,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBytes(data interface{}) ([]byte, error)
|
func ToBytes(data interface{}) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -160,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>Convert string to char slice.</p>
|
<p>Convert string to char slice.</p>
|
||||||
@@ -171,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChar(s string) []string
|
func ToChar(s string) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -193,7 +194,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>Convert a collection of elements to a read-only channels.</p>
|
<p>Convert a collection of elements to a read-only channels.</p>
|
||||||
@@ -203,6 +203,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChannel(array []interface{}) <-chan interface{}
|
func ToChannel(array []interface{}) <-chan interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,8 +231,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error. </p>
|
<p>Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error. </p>
|
||||||
@@ -241,6 +240,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToFloat(value interface{}) (float64, error)
|
func ToFloat(value interface{}) (float64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -263,8 +263,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>Convert interface to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
<p>Convert interface to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
||||||
@@ -274,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToInt(value interface{}) (int64, error)
|
func ToInt(value interface{}) (int64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -296,8 +295,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
||||||
@@ -307,6 +304,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToJson(value interface{}) (string, error)
|
func ToJson(value interface{}) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -324,8 +322,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>Convert interface to string. </p>
|
<p>Convert interface to string. </p>
|
||||||
@@ -335,6 +331,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToString(value interface{}) string
|
func ToString(value interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -352,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
||||||
@@ -363,6 +358,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -388,7 +384,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>Encode data to byte slice.</p>
|
<p>Encode data to byte slice.</p>
|
||||||
@@ -398,6 +393,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeByte(data any) ([]byte, error)
|
func EncodeByte(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -414,8 +410,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
||||||
@@ -425,6 +419,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DecodeByte(data []byte, target any) error
|
func DecodeByte(data []byte, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -442,3 +437,67 @@ func main() {
|
|||||||
fmt.Println(result) //"abc"
|
fmt.Println(result) //"abc"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DeepClone">DeepClone</span>
|
||||||
|
|
||||||
|
<p>Creates a deep copy of passed item, can't clone unexported field of struct.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DeepClone[T any](src T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range cases {
|
||||||
|
cloned := convertor.DeepClone(item)
|
||||||
|
|
||||||
|
isPointerEqual := &cloned == &item
|
||||||
|
fmt.Println(cloned, isPointerEqual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true false
|
||||||
|
// 1 false
|
||||||
|
// 0.1 false
|
||||||
|
// map[a:1 b:2] false
|
||||||
|
// &{test 1 0.1 true <nil> } false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Convertor
|
# Convertor
|
||||||
|
|
||||||
convertor 转换器包支持一些常见的数据类型转换
|
convertor 转换器包支持一些常见的数据类型转换
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -21,33 +22,27 @@ import (
|
|||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Convertor](#convertor)
|
- [ColorHexToRGB](#ColorHexToRGB)
|
||||||
- [源码:](#源码)
|
- [ColorRGBToHex](#ColorRGBToHex)
|
||||||
- [用法:](#用法)
|
- [ToBool](#ToBool)
|
||||||
- [目录](#目录)
|
- [ToBytes](#ToBytes)
|
||||||
- [文档](#文档)
|
- [ToChar](#ToChar)
|
||||||
- [<span id="ColorHexToRGB">ColorHexToRGB</span>](#colorhextorgb)
|
- [ToChannel](#ToChannel)
|
||||||
- [<span id="ColorRGBToHex">ColorRGBToHex</span>](#colorrgbtohex)
|
- [ToFloat](#ToFloat)
|
||||||
- [<span id="ToBool">ToBool</span>](#tobool)
|
- [ToInt](#ToInt)
|
||||||
- [<span id="ToBytes">ToBytes</span>](#tobytes)
|
- [ToJson](#ToJson)
|
||||||
- [<span id="ToChar">ToChar</span>](#tochar)
|
- [ToString](#ToString)
|
||||||
- [<span id="ToChannel">ToChannel</span>](#tochannel)
|
- [StructToMap](#StructToMap)
|
||||||
- [<span id="ToFloat">ToFloat</span>](#tofloat)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [<span id="ToInt">ToInt</span>](#toint)
|
- [DecodeByte](#DecodeByte)
|
||||||
- [<span id="ToJson">ToJson</span>](#tojson)
|
- [DeepClone](#DeepClone)
|
||||||
- [<span id="ToString">ToString</span>](#tostring)
|
|
||||||
- [<span id="StructToMap">StructToMap</span>](#structtomap)
|
|
||||||
- [<span id="EncodeByte">EncodeByte</span>](#encodebyte)
|
|
||||||
- [<span id="DecodeByte">DecodeByte</span>](#decodebyte)
|
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
|
|
||||||
<p>颜色值十六进制转rgb</p>
|
<p>颜色值十六进制转rgb</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -55,6 +50,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -72,8 +68,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>颜色值rgb转十六进制</p>
|
<p>颜色值rgb转十六进制</p>
|
||||||
@@ -83,6 +77,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ColorRGBToHex(red, green, blue int) string
|
func ColorRGBToHex(red, green, blue int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -103,8 +98,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>字符串转布尔类型,使用strconv.ParseBool</p>
|
<p>字符串转布尔类型,使用strconv.ParseBool</p>
|
||||||
@@ -114,6 +107,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBool(s string) (bool, error)
|
func ToBool(s string) (bool, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -139,8 +133,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>interface转字节切片.</p>
|
<p>interface转字节切片.</p>
|
||||||
@@ -150,6 +142,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBytes(data interface{}) ([]byte, error)
|
func ToBytes(data interface{}) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -169,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>字符串转字符切片</p>
|
<p>字符串转字符切片</p>
|
||||||
@@ -180,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChar(s string) []string
|
func ToChar(s string) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -202,8 +194,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>将切片转为只读channel</p>
|
<p>将切片转为只读channel</p>
|
||||||
@@ -213,6 +203,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChannel(array []interface{}) <-chan interface{}
|
func ToChannel(array []interface{}) <-chan interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -220,7 +211,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
"github.com/duke-git/lancet/convertor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -240,8 +231,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>将interface转成float64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成float64类型,如果参数无法转换,会返回0和error</p>
|
||||||
@@ -251,6 +240,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToFloat(value interface{}) (float64, error)
|
func ToFloat(value interface{}) (float64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -273,8 +263,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>将interface转成intt64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成intt64类型,如果参数无法转换,会返回0和error</p>
|
||||||
@@ -284,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToInt(value interface{}) (int64, error)
|
func ToInt(value interface{}) (int64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -306,8 +295,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>将interface转成json字符串,如果参数无法转换,会返回""和error</p>
|
<p>将interface转成json字符串,如果参数无法转换,会返回""和error</p>
|
||||||
@@ -317,6 +304,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToJson(value interface{}) (string, error)
|
func ToJson(value interface{}) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -334,8 +322,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>将interface转成字符串</p>
|
<p>将interface转成字符串</p>
|
||||||
@@ -345,6 +331,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToString(value interface{}) string
|
func ToString(value interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -362,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
|
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
|
||||||
@@ -373,6 +358,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -398,7 +384,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>将data编码成字节切片</p>
|
<p>将data编码成字节切片</p>
|
||||||
@@ -408,6 +393,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeByte(data any) ([]byte, error)
|
func EncodeByte(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -415,7 +401,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
"github.com/duke-git/lancet/convertor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -424,8 +410,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
|
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
|
||||||
@@ -435,6 +419,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DecodeByte(data []byte, target any) error
|
func DecodeByte(data []byte, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -442,7 +427,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/convertor"
|
"github.com/duke-git/lancet/convertor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -452,3 +437,67 @@ func main() {
|
|||||||
fmt.Println(result) //"abc"
|
fmt.Println(result) //"abc"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DeepClone">DeepClone</span>
|
||||||
|
|
||||||
|
<p>创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DeepClone[T any](src T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range cases {
|
||||||
|
cloned := convertor.DeepClone(item)
|
||||||
|
|
||||||
|
isPointerEqual := &cloned == &item
|
||||||
|
fmt.Println(cloned, isPointerEqual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true false
|
||||||
|
// 1 false
|
||||||
|
// 0.1 false
|
||||||
|
// map[a:1 b:2] false
|
||||||
|
// &{test 1 0.1 true <nil> } false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Cryptor
|
# Cryptor
|
||||||
|
|
||||||
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
|
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ Package cryptor contains some functions for data encryption and decryption. Supp
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/cryptor"
|
"github.com/duke-git/lancet/cryptor"
|
||||||
@@ -47,7 +49,6 @@ import (
|
|||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
- [HmacSha512](#HmacSha512)
|
- [HmacSha512](#HmacSha512)
|
||||||
|
|
||||||
- [Md5String](#Md5String)
|
- [Md5String](#Md5String)
|
||||||
- [Md5File](#Md5File)
|
- [Md5File](#Md5File)
|
||||||
- [Sha1](#Sha1)
|
- [Sha1](#Sha1)
|
||||||
@@ -57,13 +58,10 @@ import (
|
|||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -73,6 +71,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AesEcbEncrypt(data, key []byte) []byte
|
func AesEcbEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -92,8 +91,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -103,6 +100,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte
|
func AesEcbDecrypt(encrypted, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -122,8 +120,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -133,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesCbcEncrypt(data, key []byte) []byte
|
func AesCbcEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -152,8 +149,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -183,8 +178,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -215,8 +208,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -245,8 +236,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -276,8 +265,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -306,8 +293,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -338,8 +323,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdEncode">Base64StdEncode</span>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>Encode string with base64 encoding.</p>
|
<p>Encode string with base64 encoding.</p>
|
||||||
@@ -349,6 +332,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Base64StdEncode(s string) string
|
func Base64StdEncode(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -365,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdDecode">Base64StdDecode</span>
|
### <span id="Base64StdDecode">Base64StdDecode</span>
|
||||||
|
|
||||||
<p>Decode a base64 encoded string.</p>
|
<p>Decode a base64 encoded string.</p>
|
||||||
@@ -393,8 +375,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -424,8 +404,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -456,8 +434,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -487,8 +463,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -519,8 +493,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -551,8 +523,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -581,8 +551,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -612,8 +580,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -642,8 +608,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -674,8 +638,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacMd5">HmacMd5</span>
|
### <span id="HmacMd5">HmacMd5</span>
|
||||||
|
|
||||||
<p>Get the md5 hmac hash of string.</p>
|
<p>Get the md5 hmac hash of string.</p>
|
||||||
@@ -702,8 +664,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 hmac hash of string.</p>
|
<p>Get the sha1 hmac hash of string.</p>
|
||||||
@@ -730,8 +690,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 hmac hash of string</p>
|
<p>Get the sha256 hmac hash of string</p>
|
||||||
@@ -758,8 +716,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 hmac hash of string.</p>
|
<p>Get the sha512 hmac hash of string.</p>
|
||||||
@@ -782,13 +738,10 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha512("hello world", "12345"))
|
s := cryptor.HmacSha512("hello world", "12345"))
|
||||||
fmt.Println(s)
|
fmt.Println(s) //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
||||||
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>Get the md5 value of string.</p>
|
<p>Get the md5 value of string.</p>
|
||||||
@@ -815,8 +768,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>Get the md5 value of file.</p>
|
<p>Get the md5 value of file.</p>
|
||||||
@@ -843,8 +794,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha1">Sha1</span>
|
### <span id="Sha1">Sha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 value of string.</p>
|
<p>Get the sha1 value of string.</p>
|
||||||
@@ -871,8 +820,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 value of string.</p>
|
<p>Get the sha256 value of string.</p>
|
||||||
@@ -899,8 +846,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 value of string.</p>
|
<p>Get the sha512 value of string.</p>
|
||||||
@@ -927,8 +872,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>Create the rsa public and private key file in current directory.</p>
|
<p>Create the rsa public and private key file in current directory.</p>
|
||||||
@@ -957,8 +900,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaEncrypt">RsaEncrypt</span>
|
### <span id="RsaEncrypt">RsaEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with public key file useing ras algorithm.</p>
|
<p>Encrypt data with public key file useing ras algorithm.</p>
|
||||||
@@ -993,8 +934,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaDecrypt">RsaDecrypt</span>
|
### <span id="RsaDecrypt">RsaDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with private key file useing ras algorithm.</p>
|
<p>Decrypt data with private key file useing ras algorithm.</p>
|
||||||
@@ -1028,6 +967,3 @@ func main() {
|
|||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Cryptor
|
# Cryptor
|
||||||
|
|
||||||
cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/cryptor"
|
"github.com/duke-git/lancet/cryptor"
|
||||||
@@ -22,6 +24,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AesEcbEncrypt](#AesEcbEncrypt)
|
- [AesEcbEncrypt](#AesEcbEncrypt)
|
||||||
- [AesEcbDecrypt](#AesEcbDecrypt)
|
- [AesEcbDecrypt](#AesEcbDecrypt)
|
||||||
- [AesCbcEncrypt](#AesCbcEncrypt)
|
- [AesCbcEncrypt](#AesCbcEncrypt)
|
||||||
@@ -33,7 +36,6 @@ import (
|
|||||||
- [AesOfbDecrypt](#AesOfbDecrypt)
|
- [AesOfbDecrypt](#AesOfbDecrypt)
|
||||||
- [Base64StdEncode](#Base64StdEncode)
|
- [Base64StdEncode](#Base64StdEncode)
|
||||||
- [Base64StdDecode](#Base64StdDecode)
|
- [Base64StdDecode](#Base64StdDecode)
|
||||||
|
|
||||||
- [DesEcbEncrypt](#DesEcbEncrypt)
|
- [DesEcbEncrypt](#DesEcbEncrypt)
|
||||||
- [DesEcbDecrypt](#DesEcbDecrypt)
|
- [DesEcbDecrypt](#DesEcbDecrypt)
|
||||||
- [DesCbcEncrypt](#DesCbcEncrypt)
|
- [DesCbcEncrypt](#DesCbcEncrypt)
|
||||||
@@ -43,7 +45,6 @@ import (
|
|||||||
- [DesCfbDecrypt](#DesCfbDecrypt)
|
- [DesCfbDecrypt](#DesCfbDecrypt)
|
||||||
- [DesOfbEncrypt](#DesOfbEncrypt)
|
- [DesOfbEncrypt](#DesOfbEncrypt)
|
||||||
- [DesOfbDecrypt](#DesOfbDecrypt)
|
- [DesOfbDecrypt](#DesOfbDecrypt)
|
||||||
|
|
||||||
- [HmacMd5](#HmacMd5)
|
- [HmacMd5](#HmacMd5)
|
||||||
- [HmacSha1](#HmacSha1)
|
- [HmacSha1](#HmacSha1)
|
||||||
- [HmacSha256](#HmacSha256)
|
- [HmacSha256](#HmacSha256)
|
||||||
@@ -57,13 +58,10 @@ import (
|
|||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -73,6 +71,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AesEcbEncrypt(data, key []byte) []byte
|
func AesEcbEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -92,8 +91,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。
|
<p>使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。
|
||||||
@@ -103,6 +100,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte
|
func AesEcbDecrypt(encrypted, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -122,8 +120,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -133,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesCbcEncrypt(data, key []byte) []byte
|
func AesCbcEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -152,8 +149,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -184,8 +179,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
||||||
|
|
||||||
<p>使用AES CTR算法模式加密/解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CTR算法模式加密/解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -216,8 +209,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -246,8 +237,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -277,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32</p>
|
<p>使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32</p>
|
||||||
@@ -307,8 +294,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32</p>
|
<p>使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32</p>
|
||||||
@@ -338,8 +323,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdEncode">Base64StdEncode</span>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>将字符串base64编码</p>
|
<p>将字符串base64编码</p>
|
||||||
@@ -349,6 +332,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Base64StdEncode(s string) string
|
func Base64StdEncode(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -365,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdDecode">Base64StdDecode</span>
|
### <span id="Base64StdDecode">Base64StdDecode</span>
|
||||||
|
|
||||||
<p>解码base64字符串</p>
|
<p>解码base64字符串</p>
|
||||||
@@ -393,8 +375,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES ECB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES ECB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -424,8 +404,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES ECB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES ECB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -456,8 +434,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES CBC算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES CBC算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -487,8 +463,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES CBC算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CBC算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -519,8 +493,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
||||||
|
|
||||||
<p>使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -551,8 +523,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES CFB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES CFB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -581,8 +551,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES CFB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CFB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -612,8 +580,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES OFB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES OFB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -642,8 +608,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES OFB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES OFB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -673,8 +637,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacMd5">HmacMd5</span>
|
### <span id="HmacMd5">HmacMd5</span>
|
||||||
|
|
||||||
<p>获取字符串md5 hmac值</p>
|
<p>获取字符串md5 hmac值</p>
|
||||||
@@ -701,8 +663,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>获取字符串sha1 hmac值</p>
|
<p>获取字符串sha1 hmac值</p>
|
||||||
@@ -729,8 +689,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256 hmac值</p>
|
<p>获取字符串sha256 hmac值</p>
|
||||||
@@ -757,8 +715,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512 hmac值</p>
|
<p>获取字符串sha512 hmac值</p>
|
||||||
@@ -786,8 +742,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>获取字符串md5值</p>
|
<p>获取字符串md5值</p>
|
||||||
@@ -814,8 +768,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>获取文件md5值.</p>
|
<p>获取文件md5值.</p>
|
||||||
@@ -842,8 +794,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha1">Sha1</span>
|
### <span id="Sha1">Sha1</span>
|
||||||
|
|
||||||
<p>获取字符串sha1值</p>
|
<p>获取字符串sha1值</p>
|
||||||
@@ -870,8 +820,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256值</p>
|
<p>获取字符串sha256值</p>
|
||||||
@@ -898,8 +846,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512值</p>
|
<p>获取字符串sha512值</p>
|
||||||
@@ -926,8 +872,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>在当前目录下创建rsa私钥文件和公钥文件</p>
|
<p>在当前目录下创建rsa私钥文件和公钥文件</p>
|
||||||
@@ -956,8 +900,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaEncrypt">RsaEncrypt</span>
|
### <span id="RsaEncrypt">RsaEncrypt</span>
|
||||||
|
|
||||||
<p>用公钥文件ras加密数据</p>
|
<p>用公钥文件ras加密数据</p>
|
||||||
@@ -983,6 +925,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
@@ -990,8 +933,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaDecrypt">RsaDecrypt</span>
|
### <span id="RsaDecrypt">RsaDecrypt</span>
|
||||||
|
|
||||||
<p>用私钥文件rsa解密数据</p>
|
<p>用私钥文件rsa解密数据</p>
|
||||||
@@ -1017,12 +958,10 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
118
docs/datetime.md
118
docs/datetime.md
@@ -1,4 +1,5 @@
|
|||||||
# Datetime
|
# Datetime
|
||||||
|
|
||||||
Package datetime supports date and time format and compare.
|
Package datetime supports date and time format and compare.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package datetime supports date and time format and compare.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
@@ -28,7 +31,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -41,7 +43,6 @@ import (
|
|||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
|
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
- [NewUnixNow](#NewUnixNow)
|
- [NewUnixNow](#NewUnixNow)
|
||||||
- [NewUnix](#NewUnix)
|
- [NewUnix](#NewUnix)
|
||||||
@@ -57,7 +58,9 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
## Note:
|
## Note:
|
||||||
|
|
||||||
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
|
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy-mm-dd hh
|
||||||
@@ -77,8 +80,8 @@ import (
|
|||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
|
|
||||||
<p>Add or sub days to time.</p>
|
<p>Add or sub days to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -86,6 +89,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AddDay(t time.Time, day int64) time.Time
|
func AddDay(t time.Time, day int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -106,8 +110,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddHour">AddHour</span>
|
### <span id="AddHour">AddHour</span>
|
||||||
|
|
||||||
<p>Add or sub hours to time.</p>
|
<p>Add or sub hours to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -115,6 +119,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddHour(t time.Time, hour int64) time.Time
|
func AddHour(t time.Time, hour int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -136,6 +141,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="AddMinute">AddMinute</span>
|
### <span id="AddMinute">AddMinute</span>
|
||||||
|
|
||||||
<p>Add or sub minutes to time.</p>
|
<p>Add or sub minutes to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -143,6 +149,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddMinute(t time.Time, minute int64) time.Time
|
func AddMinute(t time.Time, minute int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -164,6 +171,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>Return beginning minute time of day.</p>
|
<p>Return beginning minute time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -171,6 +179,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMinute(t time.Time) time.Time
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -190,6 +199,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfHour">BeginOfHour</span>
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
|
||||||
<p>Return zero time of day.</p>
|
<p>Return zero time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -197,6 +207,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfHour(t time.Time) time.Time
|
func BeginOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -216,6 +227,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfDay">BeginOfDay</span>
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
|
||||||
<p>Return begin time of day.</p>
|
<p>Return begin time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -223,6 +235,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfDay(t time.Time) time.Time
|
func BeginOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -241,9 +254,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfWeek">BeginOfWeek</span>
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
|
||||||
<p>Return beginning time of week, week begin from Sunday.</p>
|
<p>Return beginning time of week, week begin from Sunday.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -251,6 +263,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfWeek(t time.Time) time.Time
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -269,9 +282,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfMonth</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
|
||||||
<p>Return beginning time of month</p>
|
<p>Return beginning time of month</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -279,6 +291,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMonth(t time.Time) time.Time
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -297,8 +310,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfYear">BeginOfYear</span>
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
|
||||||
<p>Return beginning time of year.</p>
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -306,6 +319,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfYear(t time.Time) time.Time
|
func BeginOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -324,9 +338,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMinute">EndOfMinute</span>
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
|
||||||
<p>Return end time minute of day.</p>
|
<p>Return end time minute of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -334,6 +347,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMinute(t time.Time) time.Time
|
func EndOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -353,6 +367,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfHour">EndOfHour</span>
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
|
||||||
<p>Return end time hour of day.</p>
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -360,6 +375,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfHour(t time.Time) time.Time
|
func EndOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -379,6 +395,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfDay">EndOfDay</span>
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
|
||||||
<p>Return end time hour of day.</p>
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -386,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfDay(t time.Time) time.Time
|
func EndOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -404,9 +422,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfWeek">EndOfWeek</span>
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
|
||||||
<p>Return end time of week, week end with Saturday.</p>
|
<p>Return end time of week, week end with Saturday.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -414,6 +431,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfWeek(t time.Time) time.Time
|
func EndOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -432,9 +450,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMonth">EndOfMonth</span>
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
|
||||||
<p>Return end time of month</p>
|
<p>Return end time of month</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -442,6 +459,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMonth(t time.Time) time.Time
|
func EndOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -460,8 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfYear">EndOfYear</span>
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
|
||||||
<p>Return beginning time of year.</p>
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -469,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfYear(t time.Time) time.Time
|
func EndOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -487,8 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDate">GetNowDate</span>
|
### <span id="GetNowDate">GetNowDate</span>
|
||||||
|
|
||||||
<p>Get current date string, format is yyyy-mm-dd.</p>
|
<p>Get current date string, format is yyyy-mm-dd.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -496,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDate() string
|
func GetNowDate() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -514,8 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowTime">GetNowTime</span>
|
### <span id="GetNowTime">GetNowTime</span>
|
||||||
|
|
||||||
<p>Get current time string, format is hh:mm:ss.</p>
|
<p>Get current time string, format is hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -523,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowTime() string
|
func GetNowTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -541,8 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDateTime">GetNowDateTime</span>
|
### <span id="GetNowDateTime">GetNowDateTime</span>
|
||||||
|
|
||||||
<p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p>
|
<p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -550,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDateTime() string
|
func GetNowDateTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -568,8 +590,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||||
|
|
||||||
<p>Return timestamp of zero hour (timestamp of 00:00).</p>
|
<p>Return timestamp of zero hour (timestamp of 00:00).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -577,6 +599,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetZeroHourTimestamp() int64
|
func GetZeroHourTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -595,8 +618,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
||||||
|
|
||||||
<p>Return timestamp of zero hour (timestamp of 23:59).</p>
|
<p>Return timestamp of zero hour (timestamp of 23:59).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -604,6 +627,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNightTimestamp() int64
|
func GetNightTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -623,6 +647,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
||||||
|
|
||||||
<p>Format time to string, `format` param specification see note 1.</p>
|
<p>Format time to string, `format` param specification see note 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -630,6 +655,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -648,8 +674,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="FormatStrToTime">FormatStrToTime</span>
|
### <span id="FormatStrToTime">FormatStrToTime</span>
|
||||||
|
|
||||||
<p>Format string to time, `format` param specification see note 1.</p>
|
<p>Format string to time, `format` param specification see note 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -657,6 +683,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -673,9 +700,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnixNow">NewUnixNow</span>
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of current time</p>
|
<p>Return unix timestamp of current time</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -686,6 +712,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -693,7 +720,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -702,8 +729,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnix">NewUnix</span>
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified int64 value.</p>
|
<p>Return unix timestamp of specified int64 value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -714,6 +741,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -721,7 +749,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -730,9 +758,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewFormat">NewFormat</span>
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -743,6 +770,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -750,7 +778,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -759,10 +787,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewISO8601">NewISO8601</span>
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified iso8601 time string.</p>
|
<p>Return unix timestamp of specified iso8601 time string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -773,6 +799,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -780,7 +807,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -789,9 +816,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToUnix">ToUnix</span>
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
|
||||||
<p>Return unix timestamp.</p>
|
<p>Return unix timestamp.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -799,6 +825,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToUnix() int64
|
func (t *theTime) ToUnix() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -806,7 +833,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -815,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormat">ToFormat</span>
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
|
||||||
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -825,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormat() string
|
func (t *theTime) ToFormat() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -832,7 +859,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -841,9 +868,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
|
||||||
<p>Return the time string which format is specified tpl.</p>
|
<p>Return the time string which format is specified tpl.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -851,6 +877,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -858,7 +885,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -868,8 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToIso8601">ToIso8601</span>
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
|
||||||
<p>Return iso8601 time string.</p>
|
<p>Return iso8601 time string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -877,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToIso8601() string
|
func (t *theTime) ToIso8601() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -884,7 +912,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Datetime
|
# Datetime
|
||||||
|
|
||||||
datetime 日期时间处理包,格式化日期,比较日期。
|
datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ datetime日期时间处理包,格式化日期,比较日期。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
@@ -28,7 +31,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -56,7 +58,9 @@ import (
|
|||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
## 注:
|
## 注:
|
||||||
|
|
||||||
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy-mm-dd hh
|
||||||
@@ -76,8 +80,8 @@ import (
|
|||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
|
|
||||||
<p>将日期加/减天数</p>
|
<p>将日期加/减天数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -85,6 +89,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AddDay(t time.Time, day int64) time.Time
|
func AddDay(t time.Time, day int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -105,8 +110,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddHour">AddHour</span>
|
### <span id="AddHour">AddHour</span>
|
||||||
|
|
||||||
<p>将日期加/减小时数</p>
|
<p>将日期加/减小时数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -114,6 +119,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddHour(t time.Time, hour int64) time.Time
|
func AddHour(t time.Time, hour int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -135,6 +141,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="AddMinute">AddMinute</span>
|
### <span id="AddMinute">AddMinute</span>
|
||||||
|
|
||||||
<p>将日期加/减分钟数</p>
|
<p>将日期加/减分钟数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -142,6 +149,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddMinute(t time.Time, minute int64) time.Time
|
func AddMinute(t time.Time, minute int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -163,6 +171,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟开始时间</p>
|
<p>返回指定时间的分钟开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -170,6 +179,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMinute(t time.Time) time.Time
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -189,6 +199,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfHour">BeginOfHour</span>
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
|
||||||
<p>返回指定时间的小时开始时间</p>
|
<p>返回指定时间的小时开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -196,6 +207,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfHour(t time.Time) time.Time
|
func BeginOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -215,6 +227,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfDay">BeginOfDay</span>
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
|
||||||
<p>返回指定时间的当天开始时间</p>
|
<p>返回指定时间的当天开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -222,6 +235,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfDay(t time.Time) time.Time
|
func BeginOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -240,9 +254,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfWeek">BeginOfWeek</span>
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
|
||||||
<p>返回指定时间的星期开始时间</p>
|
<p>返回指定时间的星期开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -250,6 +263,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfWeek(t time.Time) time.Time
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -268,9 +282,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfMonth</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
|
||||||
<p>返回指定时间的当月开始时间</p>
|
<p>返回指定时间的当月开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -278,6 +291,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMonth(t time.Time) time.Time
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -296,8 +310,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfYear">BeginOfYear</span>
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
|
||||||
<p>返回指定时间的当年开始时间</p>
|
<p>返回指定时间的当年开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -305,6 +319,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfYear(t time.Time) time.Time
|
func BeginOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -323,9 +338,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMinute">EndOfMinute</span>
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟结束时间</p>
|
<p>返回指定时间的分钟结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -333,6 +347,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMinute(t time.Time) time.Time
|
func EndOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -352,6 +367,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfHour">EndOfHour</span>
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
|
||||||
<p>返回指定时间的小时结束时间</p>
|
<p>返回指定时间的小时结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -359,6 +375,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfHour(t time.Time) time.Time
|
func EndOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -378,6 +395,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfDay">EndOfDay</span>
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
|
||||||
<p>返回指定时间的当天结束时间.</p>
|
<p>返回指定时间的当天结束时间.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -385,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfDay(t time.Time) time.Time
|
func EndOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -403,9 +422,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfWeek">EndOfWeek</span>
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
|
||||||
<p>返回指定时间的星期结束时间</p>
|
<p>返回指定时间的星期结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -413,6 +431,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfWeek(t time.Time) time.Time
|
func EndOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -431,9 +450,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMonth">EndOfMonth</span>
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
|
||||||
<p>返回指定时间的月份结束时间</p>
|
<p>返回指定时间的月份结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -441,6 +459,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMonth(t time.Time) time.Time
|
func EndOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -459,8 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfYear">EndOfYear</span>
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
|
||||||
<p>返回指定时间的年份结束时间</p>
|
<p>返回指定时间的年份结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -468,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfYear(t time.Time) time.Time
|
func EndOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -486,8 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDate">GetNowDate</span>
|
### <span id="GetNowDate">GetNowDate</span>
|
||||||
|
|
||||||
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -495,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDate() string
|
func GetNowDate() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -513,8 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowTime">GetNowTime</span>
|
### <span id="GetNowTime">GetNowTime</span>
|
||||||
|
|
||||||
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -522,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowTime() string
|
func GetNowTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -540,8 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDateTime">GetNowDateTime</span>
|
### <span id="GetNowDateTime">GetNowDateTime</span>
|
||||||
|
|
||||||
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.</p>
|
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -549,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDateTime() string
|
func GetNowDateTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -567,8 +590,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||||
|
|
||||||
<p>获取零时时间戳(timestamp of 00:00).</p>
|
<p>获取零时时间戳(timestamp of 00:00).</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -576,6 +599,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetZeroHourTimestamp() int64
|
func GetZeroHourTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -594,8 +618,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
||||||
|
|
||||||
<p>获取午夜时间戳(timestamp of 23:59).</p>
|
<p>获取午夜时间戳(timestamp of 23:59).</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -603,6 +627,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNightTimestamp() int64
|
func GetNightTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -622,6 +647,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
||||||
|
|
||||||
<p>将日期格式化成字符串,`format` 参数格式参考注<sup>1</sup></p>
|
<p>将日期格式化成字符串,`format` 参数格式参考注<sup>1</sup></p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -629,6 +655,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -647,8 +674,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="FormatStrToTime">FormatStrToTime</span>
|
### <span id="FormatStrToTime">FormatStrToTime</span>
|
||||||
|
|
||||||
<p>将字符串格式化成时间,`format` 参数格式参考注<sup>1</sup></p>
|
<p>将字符串格式化成时间,`format` 参数格式参考注<sup>1</sup></p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -656,6 +683,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -672,9 +700,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnixNow">NewUnixNow</span>
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
|
||||||
<p>创建一个当前时间的unix时间戳</p>
|
<p>创建一个当前时间的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -685,6 +712,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -692,7 +720,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -701,8 +729,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnix">NewUnix</span>
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
|
||||||
<p>创建一个unix时间戳</p>
|
<p>创建一个unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -713,6 +741,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -720,7 +749,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -729,9 +758,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewFormat">NewFormat</span>
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
|
||||||
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -742,6 +770,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -749,7 +778,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -758,10 +787,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewISO8601">NewISO8601</span>
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
|
||||||
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -772,6 +799,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -779,7 +807,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -788,9 +816,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToUnix">ToUnix</span>
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
|
||||||
<p>返回unix时间戳</p>
|
<p>返回unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -798,6 +825,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToUnix() int64
|
func (t *theTime) ToUnix() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -805,7 +833,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -814,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormat">ToFormat</span>
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
|
||||||
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -824,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormat() string
|
func (t *theTime) ToFormat() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -831,7 +859,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -840,9 +868,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
|
||||||
<p>返回tpl格式指定的日期字符串</p>
|
<p>返回tpl格式指定的日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -850,6 +877,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -857,7 +885,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -867,8 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToIso8601">ToIso8601</span>
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
|
||||||
<p>返回iso8601日期字符串</p>
|
<p>返回iso8601日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -876,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToIso8601() string
|
func (t *theTime) ToIso8601() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -883,7 +912,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -892,4 +921,3 @@ func main() {
|
|||||||
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
252
docs/fileutil.md
252
docs/fileutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Fileutil
|
# Fileutil
|
||||||
|
|
||||||
Package fileutil implements some basic functions for file operations.
|
Package fileutil implements some basic functions for file operations.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package fileutil implements some basic functions for file operations.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/fileutil"
|
"github.com/duke-git/lancet/fileutil"
|
||||||
@@ -19,9 +21,9 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ClearFile](#ClearFile)
|
- [ClearFile](#ClearFile)
|
||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
|
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
- [CopyFile](#CopyFile)
|
- [CopyFile](#CopyFile)
|
||||||
- [FileMode](#FileMode)
|
- [FileMode](#FileMode)
|
||||||
@@ -35,14 +37,19 @@ import (
|
|||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
- [UnZip](#UnZip)
|
- [UnZip](#UnZip)
|
||||||
|
- [CurrentPath](#CurrentPath)
|
||||||
|
- [IsZipFile](#IsZipFile)
|
||||||
|
- [FileSize](#FileSize)
|
||||||
|
- [MTime](#MTime)
|
||||||
|
- [Sha](#Sha)
|
||||||
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ClearFile">ClearFile</span>
|
### <span id="ClearFile">ClearFile</span>
|
||||||
|
|
||||||
<p>Clear the file content, write empty string to the file.</p>
|
<p>Clear the file content, write empty string to the file.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -50,6 +57,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ClearFile(path string) error
|
func ClearFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -68,9 +76,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateDir">CreateDir</span>
|
### <span id="CreateDir">CreateDir</span>
|
||||||
|
|
||||||
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
|
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -78,6 +85,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateDir(absPath string) error
|
func CreateDir(absPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -94,9 +102,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateFile">CreateFile</span>
|
### <span id="CreateFile">CreateFile</span>
|
||||||
|
|
||||||
<p>Create file in path. return true if create succeed.</p>
|
<p>Create file in path. return true if create succeed.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -104,6 +111,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateFile(path string) bool
|
func CreateFile(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -120,8 +128,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyFile">CopyFile</span>
|
### <span id="CopyFile">CopyFile</span>
|
||||||
|
|
||||||
<p>Copy src file to dest file. If dest file exist will overwrite it.</p>
|
<p>Copy src file to dest file. If dest file exist will overwrite it.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -129,6 +137,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CopyFile(srcFilePath string, dstFilePath string) error
|
func CopyFile(srcFilePath string, dstFilePath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -147,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>Return file mode infomation.</p>
|
<p>Return file mode infomation.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -157,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FileMode(path string) (fs.FileMode, error)
|
func FileMode(path string) (fs.FileMode, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -176,9 +185,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MiMeType">MiMeType</span>
|
### <span id="MiMeType">MiMeType</span>
|
||||||
|
|
||||||
<p>Get file mime type, 'file' param's type should be string or *os.File.</p>
|
<p>Get file mime type, 'file' param's type should be string or *os.File.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -186,6 +194,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func MiMeType(file interface{}) string
|
func MiMeType(file interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,10 +216,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsExist">IsExist</span>
|
### <span id="IsExist">IsExist</span>
|
||||||
|
|
||||||
<p>Checks if a file or directory exists.</p>
|
<p>Checks if a file or directory exists.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -218,6 +225,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsExist(path string) bool
|
func IsExist(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -235,9 +243,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLink">IsLink</span>
|
### <span id="IsLink">IsLink</span>
|
||||||
|
|
||||||
<p>Checks if a file is symbol link or not.</p>
|
<p>Checks if a file is symbol link or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -245,6 +252,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLink(path string) bool
|
func IsLink(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -261,9 +269,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDir">IsDir</span>
|
### <span id="IsDir">IsDir</span>
|
||||||
|
|
||||||
<p>Checks if the path is directy or not.</p>
|
<p>Checks if the path is directy or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -271,6 +278,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDir(path string) bool
|
func IsDir(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -290,9 +298,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ListFileNames">ListFileNames</span>
|
### <span id="ListFileNames">ListFileNames</span>
|
||||||
|
|
||||||
<p>List all file names in given path.</p>
|
<p>List all file names in given path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -300,6 +307,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ListFileNames(path string) ([]string, error)
|
func ListFileNames(path string) ([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -316,9 +324,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveFile">RemoveFile</span>
|
### <span id="RemoveFile">RemoveFile</span>
|
||||||
|
|
||||||
<p>Remove the file of path.</p>
|
<p>Remove the file of path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -326,6 +333,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -344,8 +352,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>Return string of file content.</p>
|
<p>Return string of file content.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -353,6 +361,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileToString(path string) (string, error)
|
func ReadFileToString(path string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -376,9 +385,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileByLine">ReadFileByLine</span>
|
### <span id="ReadFileByLine">ReadFileByLine</span>
|
||||||
|
|
||||||
<p>Read file line by line, and return slice of lines</p>
|
<p>Read file line by line, and return slice of lines</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -386,6 +394,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileByLine(path string)([]string, error)
|
func ReadFileByLine(path string)([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -410,9 +419,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Zip">Zip</span>
|
### <span id="Zip">Zip</span>
|
||||||
|
|
||||||
<p>Create a zip file of fpath, fpath could be a file or a directory.</p>
|
<p>Create a zip file of fpath, fpath could be a file or a directory.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -420,6 +428,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Zip(fpath string, destPath string) error
|
func Zip(fpath string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -438,10 +447,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UnZip">UnZip</span>
|
### <span id="UnZip">UnZip</span>
|
||||||
|
|
||||||
<p>Unzip the file and save it to dest path.</p>
|
<p>Unzip the file and save it to dest path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -449,6 +456,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -467,7 +475,189 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="CurrentPath">CurrentPath</span>
|
||||||
|
|
||||||
|
<p>return current absolute path.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CurrentPath() string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
fmt.Println(absPath)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
|
<p>Checks if file is zip file or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsZipFile(filepath string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
isZip := IsZipFile("./zipfile.zip")
|
||||||
|
fmt.Println(isZip)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FileSize">FileSize</span>
|
||||||
|
|
||||||
|
<p>Returns file size in bytes.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FileSize(path string) (int64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
size, err := fileutil.FileSize("./testdata/test.txt")
|
||||||
|
|
||||||
|
fmt.Println(size)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 20
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="MTime">MTime</span>
|
||||||
|
|
||||||
|
<p>Returns file modified time(unix timestamp).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func MTime(filepath string) (int64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mtime, err := fileutil.MTime("./testdata/test.txt")
|
||||||
|
|
||||||
|
fmt.Println(mtime)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1682391110
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Sha">Sha</span>
|
||||||
|
|
||||||
|
<p>returns file sha value, param `shaType` should be 1, 256 or 512.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha(filepath string, shaType ...int) (string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
sha1, err := fileutil.Sha("./testdata/test.txt", 1)
|
||||||
|
sha256, _ := fileutil.Sha("./testdata/test.txt", 256)
|
||||||
|
sha512, _ := fileutil.Sha("./testdata/test.txt", 512)
|
||||||
|
|
||||||
|
fmt.Println(sha1)
|
||||||
|
fmt.Println(sha256)
|
||||||
|
fmt.Println(sha512)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// dda3cf10c5a6ff6c6659a497bf7261b287af2bc7
|
||||||
|
// aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35
|
||||||
|
// d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReadCsvFile">ReadCsvFile</span>
|
||||||
|
|
||||||
|
<p>Reads file content into slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReadCsvFile(filepath string) ([][]string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
content, err := fileutil.ReadCsvFile("./testdata/test.csv")
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Fileutil
|
# Fileutil
|
||||||
|
|
||||||
fileutil 包支持文件基本操作。
|
fileutil 包支持文件基本操作。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ fileutil包支持文件基本操作。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/fileutil"
|
"github.com/duke-git/lancet/fileutil"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ClearFile](#ClearFile)
|
- [ClearFile](#ClearFile)
|
||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
@@ -28,21 +31,25 @@ import (
|
|||||||
- [IsExist](#IsExist)
|
- [IsExist](#IsExist)
|
||||||
- [IsLink](#IsLink)
|
- [IsLink](#IsLink)
|
||||||
- [IsDir](#IsDir)
|
- [IsDir](#IsDir)
|
||||||
|
|
||||||
- [ListFileNames](#ListFileNames)
|
- [ListFileNames](#ListFileNames)
|
||||||
- [RemoveFile](#RemoveFile)
|
- [RemoveFile](#RemoveFile)
|
||||||
- [ReadFileToString](#ReadFileToString)
|
- [ReadFileToString](#ReadFileToString)
|
||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
- [UnZip](#UnZip)
|
- [UnZip](#UnZip)
|
||||||
|
- [CurrentPath](#CurrentPath)
|
||||||
|
- [IsZipFile](#IsZipFile)
|
||||||
|
- [FileSize](#FileSize)
|
||||||
|
- [MTime](#MTime)
|
||||||
|
- [Sha](#Sha)
|
||||||
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ClearFile">ClearFile</span>
|
### <span id="ClearFile">ClearFile</span>
|
||||||
|
|
||||||
<p>清空文件内容</p>
|
<p>清空文件内容</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -50,6 +57,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ClearFile(path string) error
|
func ClearFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -68,8 +76,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateDir">CreateDir</span>
|
### <span id="CreateDir">CreateDir</span>
|
||||||
|
|
||||||
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
|
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -77,6 +85,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateDir(absPath string) error
|
func CreateDir(absPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -93,9 +102,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateFile">CreateFile</span>
|
### <span id="CreateFile">CreateFile</span>
|
||||||
|
|
||||||
<p>创建文件,创建成功返回true, 否则返回false</p>
|
<p>创建文件,创建成功返回true, 否则返回false</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -103,6 +111,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateFile(path string) bool
|
func CreateFile(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -119,8 +128,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyFile">CopyFile</span>
|
### <span id="CopyFile">CopyFile</span>
|
||||||
|
|
||||||
<p>拷贝文件,会覆盖原有的拷贝文件</p>
|
<p>拷贝文件,会覆盖原有的拷贝文件</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -128,6 +137,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CopyFile(srcFilePath string, dstFilePath string) error
|
func CopyFile(srcFilePath string, dstFilePath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -146,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>获取文件mode信息</p>
|
<p>获取文件mode信息</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -156,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FileMode(path string) (fs.FileMode, error)
|
func FileMode(path string) (fs.FileMode, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -175,9 +185,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MiMeType">MiMeType</span>
|
### <span id="MiMeType">MiMeType</span>
|
||||||
|
|
||||||
<p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p>
|
<p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -185,6 +194,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func MiMeType(file interface{}) string
|
func MiMeType(file interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -206,10 +216,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsExist">IsExist</span>
|
### <span id="IsExist">IsExist</span>
|
||||||
|
|
||||||
<p>判断文件或目录是否存在</p>
|
<p>判断文件或目录是否存在</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -217,6 +225,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsExist(path string) bool
|
func IsExist(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,9 +243,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLink">IsLink</span>
|
### <span id="IsLink">IsLink</span>
|
||||||
|
|
||||||
<p>判断文件是否是符号链接</p>
|
<p>判断文件是否是符号链接</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -244,6 +252,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLink(path string) bool
|
func IsLink(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -260,9 +269,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDir">IsDir</span>
|
### <span id="IsDir">IsDir</span>
|
||||||
|
|
||||||
<p>判断目录是否存在</p>
|
<p>判断目录是否存在</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -270,6 +278,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDir(path string) bool
|
func IsDir(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -289,9 +298,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ListFileNames">ListFileNames</span>
|
### <span id="ListFileNames">ListFileNames</span>
|
||||||
|
|
||||||
<p>返回目录下所有文件名</p>
|
<p>返回目录下所有文件名</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -299,6 +307,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ListFileNames(path string) ([]string, error)
|
func ListFileNames(path string) ([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -315,9 +324,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveFile">RemoveFile</span>
|
### <span id="RemoveFile">RemoveFile</span>
|
||||||
|
|
||||||
<p>删除文件</p>
|
<p>删除文件</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -325,6 +333,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -343,8 +352,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>读取文件内容并返回字符串</p>
|
<p>读取文件内容并返回字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -352,6 +361,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileToString(path string) (string, error)
|
func ReadFileToString(path string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -375,9 +385,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileByLine">ReadFileByLine</span>
|
### <span id="ReadFileByLine">ReadFileByLine</span>
|
||||||
|
|
||||||
<p>按行读取文件内容,返回字符串切片包含每一行</p>
|
<p>按行读取文件内容,返回字符串切片包含每一行</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -385,6 +394,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileByLine(path string)([]string, error)
|
func ReadFileByLine(path string)([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -409,9 +419,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Zip">Zip</span>
|
### <span id="Zip">Zip</span>
|
||||||
|
|
||||||
<p>zip压缩文件, fpath参数可以是文件或目录</p>
|
<p>zip压缩文件, fpath参数可以是文件或目录</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -419,6 +428,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Zip(fpath string, destPath string) error
|
func Zip(fpath string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -437,10 +447,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UnZip">UnZip</span>
|
### <span id="UnZip">UnZip</span>
|
||||||
|
|
||||||
<p>zip解压缩文件并保存在目录中</p>
|
<p>zip解压缩文件并保存在目录中</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -448,6 +456,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -466,7 +475,189 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="CurrentPath">CurrentPath</span>
|
||||||
|
|
||||||
|
<p>返回当前位置的绝对路径。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CurrentPath() string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
fmt.Println(absPath)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
|
<p>判断文件是否是zip压缩文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsZipFile(filepath string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
isZip := IsZipFile("./zipfile.zip")
|
||||||
|
fmt.Println(isZip)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FileSize">FileSize</span>
|
||||||
|
|
||||||
|
<p>返回文件字节大小。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FileSize(path string) (int64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
size, err := fileutil.FileSize("./testdata/test.txt")
|
||||||
|
|
||||||
|
fmt.Println(size)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 20
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="MTime">MTime</span>
|
||||||
|
|
||||||
|
<p>返回文件修改时间(unix timestamp).</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func MTime(filepath string) (int64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mtime, err := fileutil.MTime("./testdata/test.txt")
|
||||||
|
|
||||||
|
fmt.Println(mtime)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1682391110
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Sha">Sha</span>
|
||||||
|
|
||||||
|
<p>返回文件sha值,参数`shaType` 应传值为: 1, 256,512.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sha(filepath string, shaType ...int) (string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
sha1, err := fileutil.Sha("./testdata/test.txt", 1)
|
||||||
|
sha256, _ := fileutil.Sha("./testdata/test.txt", 256)
|
||||||
|
sha512, _ := fileutil.Sha("./testdata/test.txt", 512)
|
||||||
|
|
||||||
|
fmt.Println(sha1)
|
||||||
|
fmt.Println(sha256)
|
||||||
|
fmt.Println(sha512)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// dda3cf10c5a6ff6c6659a497bf7261b287af2bc7
|
||||||
|
// aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35
|
||||||
|
// d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReadCsvFile">ReadCsvFile</span>
|
||||||
|
|
||||||
|
<p>读取csv文件内容到切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReadCsvFile(filepath string) ([][]string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
content, err := fileutil.ReadCsvFile("./testdata/test.csv")
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [[Bob 12 male] [Duke 14 male] [Lucy 16 female]]
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Formatter
|
# Formatter
|
||||||
|
|
||||||
formatter contains some functions for data formatting.
|
formatter contains some functions for data formatting.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ formatter contains some functions for data formatting.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/formatter"
|
"github.com/duke-git/lancet/formatter"
|
||||||
@@ -19,23 +21,30 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Comma](#Comma)
|
- [Comma](#Comma)
|
||||||
|
- [Pretty](#Pretty)
|
||||||
|
- [PrettyToWriter](#PrettyToWriter)
|
||||||
|
- [DecimalBytes](#DecimalBytes)
|
||||||
|
- [BinaryBytes](#BinaryBytes)
|
||||||
|
- [ParseDecimalBytes](#ParseDecimalBytes)
|
||||||
|
- [ParseBinaryBytes](#ParseBinaryBytes)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Comma">Comma</span>
|
### <span id="Comma">Comma</span>
|
||||||
|
|
||||||
<p>Add comma to number by every 3 numbers from right. ahead by symbol char.
|
<p>Add comma to number by every 3 numbers from right. ahead by symbol char.
|
||||||
Param should be number or numberic string.</p>
|
Param should be number or numberic string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Comma(v interface{}, symbol string) string
|
func Comma(value interface{}, symbol string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -51,3 +60,241 @@ func main() {
|
|||||||
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67"
|
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pretty">Pretty</span>
|
||||||
|
|
||||||
|
<p>Pretty data to JSON string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pretty(v interface{}) (string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.Pretty([]string{"a", "b", "c"})
|
||||||
|
result2, _ := formatter.Pretty(map[string]int{"a": 1})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [
|
||||||
|
// "a",
|
||||||
|
// "b",
|
||||||
|
// "c"
|
||||||
|
// ]
|
||||||
|
// {
|
||||||
|
// "a": 1
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PrettyToWriter">PrettyToWriter</span>
|
||||||
|
|
||||||
|
<p>Pretty encode data to writer.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PrettyToWriter(v interface{}, out io.Writer) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Aage uint `json:"age"`
|
||||||
|
}
|
||||||
|
user := User{Name: "King", Aage: 10000}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
err := formatter.PrettyToWriter(user, buf)
|
||||||
|
|
||||||
|
fmt.Println(buf)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "name": "King",
|
||||||
|
// "age": 10000
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DecimalBytes">DecimalBytes</span>
|
||||||
|
|
||||||
|
<p>Returns a human readable byte size under decimal standard (base 1000). The precision parameter specifies the number of digits after the decimal point, which is 4 for default.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DecimalBytes(size float64, precision ...int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := formatter.DecimalBytes(1000)
|
||||||
|
result2 := formatter.DecimalBytes(1024)
|
||||||
|
result3 := formatter.DecimalBytes(1234567)
|
||||||
|
result4 := formatter.DecimalBytes(1234567, 3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1KB
|
||||||
|
// 1.024KB
|
||||||
|
// 1.2346MB
|
||||||
|
// 1.235MB
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinaryBytes">BinaryBytes</span>
|
||||||
|
|
||||||
|
<p>Returns a human readable byte size under binary standard (base 1024). The precision parameter specifies the number of digits after the decimal point, which is 4 for default.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinaryBytes(size float64, precision ...int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := formatter.BinaryBytes(1024)
|
||||||
|
result2 := formatter.BinaryBytes(1024 * 1024)
|
||||||
|
result3 := formatter.BinaryBytes(1234567)
|
||||||
|
result4 := formatter.BinaryBytes(1234567, 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1KiB
|
||||||
|
// 1MiB
|
||||||
|
// 1.1774MiB
|
||||||
|
// 1.18MiB
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ParseDecimalBytes">ParseDecimalBytes</span>
|
||||||
|
|
||||||
|
<p>Returns the human readable bytes size string into the amount it represents(base 1000).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ParseDecimalBytes(size string) (uint64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.ParseDecimalBytes("12")
|
||||||
|
result2, _ := formatter.ParseDecimalBytes("12k")
|
||||||
|
result3, _ := formatter.ParseDecimalBytes("12 Kb")
|
||||||
|
result4, _ := formatter.ParseDecimalBytes("12.2 kb")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 12000
|
||||||
|
// 12000
|
||||||
|
// 12200
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ParseBinaryBytes">ParseBinaryBytes</span>
|
||||||
|
|
||||||
|
<p>Returns the human readable bytes size string into the amount it represents(base 1024).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ParseBinaryBytes(size string) (uint64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.ParseBinaryBytes("12")
|
||||||
|
result2, _ := formatter.ParseBinaryBytes("12ki")
|
||||||
|
result3, _ := formatter.ParseBinaryBytes("12 KiB")
|
||||||
|
result4, _ := formatter.ParseBinaryBytes("12.2 kib")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 12288
|
||||||
|
// 12288
|
||||||
|
// 12492
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Formatter
|
# Formatter
|
||||||
|
|
||||||
formatter 格式化器包含一些数据格式化处理方法。
|
formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ formatter格式化器包含一些数据格式化处理方法。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/formatter"
|
"github.com/duke-git/lancet/formatter"
|
||||||
@@ -19,15 +21,21 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Comma](#Comma)
|
- [Comma](#Comma)
|
||||||
|
- [Pretty](#Pretty)
|
||||||
|
- [PrettyToWriter](#PrettyToWriter)
|
||||||
|
- [DecimalBytes](#DecimalBytes)
|
||||||
|
- [BinaryBytes](#BinaryBytes)
|
||||||
|
- [ParseDecimalBytes](#ParseDecimalBytes)
|
||||||
|
- [ParseBinaryBytes](#ParseBinaryBytes)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Comma">Comma</span>
|
### <span id="Comma">Comma</span>
|
||||||
|
|
||||||
<p>用逗号每隔3位分割数字/字符串,签名添加符号。参数必须是数字或者可以转为数字的字符串</p>
|
<p>用逗号每隔3位分割数字/字符串,签名添加符号。参数必须是数字或者可以转为数字的字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -35,6 +43,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Comma(v interface{}, symbol string) string
|
func Comma(v interface{}, symbol string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -50,3 +59,241 @@ func main() {
|
|||||||
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67"
|
fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pretty">Pretty</span>
|
||||||
|
|
||||||
|
<p>返回pretty JSON字符串.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pretty(v interface{}) (string, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.Pretty([]string{"a", "b", "c"})
|
||||||
|
result2, _ := formatter.Pretty(map[string]int{"a": 1})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [
|
||||||
|
// "a",
|
||||||
|
// "b",
|
||||||
|
// "c"
|
||||||
|
// ]
|
||||||
|
// {
|
||||||
|
// "a": 1
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PrettyToWriter">PrettyToWriter</span>
|
||||||
|
|
||||||
|
<p>Pretty encode数据到writer。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PrettyToWriter(v interface{}, out io.Writer) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Aage uint `json:"age"`
|
||||||
|
}
|
||||||
|
user := User{Name: "King", Aage: 10000}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
err := formatter.PrettyToWriter(user, buf)
|
||||||
|
|
||||||
|
fmt.Println(buf)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "name": "King",
|
||||||
|
// "age": 10000
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// <nil>
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DecimalBytes">DecimalBytes</span>
|
||||||
|
|
||||||
|
<p>返回十进制标准(以1000为基数)下的可读字节单位字符串。precision参数指定小数点后的位数,默认为4。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DecimalBytes(size float64, precision ...int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := formatter.DecimalBytes(1000)
|
||||||
|
result2 := formatter.DecimalBytes(1024)
|
||||||
|
result3 := formatter.DecimalBytes(1234567)
|
||||||
|
result4 := formatter.DecimalBytes(1234567, 3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1KB
|
||||||
|
// 1.024KB
|
||||||
|
// 1.2346MB
|
||||||
|
// 1.235MB
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinaryBytes">BinaryBytes</span>
|
||||||
|
|
||||||
|
<p>返回binary标准(以1024为基数)下的可读字节单位字符串。precision参数指定小数点后的位数,默认为4。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinaryBytes(size float64, precision ...int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := formatter.BinaryBytes(1024)
|
||||||
|
result2 := formatter.BinaryBytes(1024 * 1024)
|
||||||
|
result3 := formatter.BinaryBytes(1234567)
|
||||||
|
result4 := formatter.BinaryBytes(1234567, 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1KiB
|
||||||
|
// 1MiB
|
||||||
|
// 1.1774MiB
|
||||||
|
// 1.18MiB
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ParseDecimalBytes">ParseDecimalBytes</span>
|
||||||
|
|
||||||
|
<p>将字节单位字符串转换成其所表示的字节数(以1000为基数)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ParseDecimalBytes(size string) (uint64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.ParseDecimalBytes("12")
|
||||||
|
result2, _ := formatter.ParseDecimalBytes("12k")
|
||||||
|
result3, _ := formatter.ParseDecimalBytes("12 Kb")
|
||||||
|
result4, _ := formatter.ParseDecimalBytes("12.2 kb")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 12000
|
||||||
|
// 12000
|
||||||
|
// 12200
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ParseBinaryBytes">ParseBinaryBytes</span>
|
||||||
|
|
||||||
|
<p>将字节单位字符串转换成其所表示的字节数(以1024为基数)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ParseBinaryBytes(size string) (uint64, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/formatter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1, _ := formatter.ParseBinaryBytes("12")
|
||||||
|
result2, _ := formatter.ParseBinaryBytes("12ki")
|
||||||
|
result3, _ := formatter.ParseBinaryBytes("12 KiB")
|
||||||
|
result4, _ := formatter.ParseBinaryBytes("12.2 kib")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 12288
|
||||||
|
// 12288
|
||||||
|
// 12492
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Function
|
# Function
|
||||||
|
|
||||||
Package function can control the flow of function execution and support part of functional programming.
|
Package function can control the flow of function execution and support part of functional programming.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -11,6 +12,7 @@ Package function can control the flow of function execution and support part of
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/function"
|
"github.com/duke-git/lancet/function"
|
||||||
@@ -20,21 +22,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
- [Curry](#Curry)
|
- [Curry](#Curry)
|
||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
|
- [Pipeline](#Pipeline)
|
||||||
|
- [Schedule](#Schedule)
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>Creates a function that invokes given func once it's called n or more times.</p>
|
<p>Creates a function that invokes given func once it's called n or more times.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -42,6 +46,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -78,8 +83,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>creates a function that invokes func once it's called less than n times.</p>
|
<p>creates a function that invokes func once it's called less than n times.</p>
|
||||||
@@ -89,6 +92,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -124,8 +128,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Curry">Curry</span>
|
### <span id="Curry">Curry</span>
|
||||||
|
|
||||||
<p>Make a curry function.</p>
|
<p>Make a curry function.</p>
|
||||||
@@ -136,6 +138,7 @@ func main() {
|
|||||||
type Fn func(...interface{}) interface{}
|
type Fn func(...interface{}) interface{}
|
||||||
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -159,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compose">Compose</span>
|
### <span id="Compose">Compose</span>
|
||||||
|
|
||||||
<p>Compose the function list from right to left, then return the composed function.</p>
|
<p>Compose the function list from right to left, then return the composed function.</p>
|
||||||
@@ -170,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Debounced">Debounced</span>
|
### <span id="Debounced">Debounced</span>
|
||||||
|
|
||||||
<p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p>
|
<p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Debounced(fn func(), duration time.Duration) func()
|
func Debounced(fn func(), duration time.Duration) func()
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -237,8 +238,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Delay">Delay</span>
|
### <span id="Delay">Delay</span>
|
||||||
|
|
||||||
<p>Invoke function after delayed time.</p>
|
<p>Invoke function after delayed time.</p>
|
||||||
@@ -248,6 +247,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -266,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Schedule">Schedule</span>
|
### <span id="Schedule">Schedule</span>
|
||||||
|
|
||||||
<p>Invoke function every duration time, until close the returned bool chan.</p>
|
<p>Invoke function every duration time, until close the returned bool chan.</p>
|
||||||
@@ -277,6 +275,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -301,7 +300,48 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pipeline">Pipeline</span>
|
||||||
|
|
||||||
|
<p>Pipeline takes a list of functions and returns a function whose param will be passed into
|
||||||
|
the functions one by one.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{}
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/function"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
result := fn(2)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 36
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Watcher">Watcher</span>
|
### <span id="Watcher">Watcher</span>
|
||||||
|
|
||||||
@@ -320,6 +360,7 @@ func (w *Watcher) Stop() //stop the watcher
|
|||||||
func (w *Watcher) Reset() //reset the watcher
|
func (w *Watcher) Reset() //reset the watcher
|
||||||
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -359,6 +400,3 @@ func longRunningTask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Function
|
# Function
|
||||||
|
|
||||||
function 函数包控制函数执行流程,包含部分函数式编程。
|
function 函数包控制函数执行流程,包含部分函数式编程。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -11,6 +12,7 @@ function函数包控制函数执行流程,包含部分函数式编程。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/function"
|
"github.com/duke-git/lancet/function"
|
||||||
@@ -20,21 +22,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
- [Curry](#Curry)
|
- [Curry](#Curry)
|
||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
|
- [Pipeline](#Pipeline)
|
||||||
|
- [Schedule](#Schedule)
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>创建一个函数,当他被调用n或更多次之后将马上触发fn</p>
|
<p>创建一个函数,当他被调用n或更多次之后将马上触发fn</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -42,6 +46,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -78,8 +83,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果</p>
|
<p>创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果</p>
|
||||||
@@ -89,6 +92,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -124,8 +128,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Curry">Curry</span>
|
### <span id="Curry">Curry</span>
|
||||||
|
|
||||||
<p>创建一个柯里化的函数</p>
|
<p>创建一个柯里化的函数</p>
|
||||||
@@ -136,6 +138,7 @@ func main() {
|
|||||||
type Fn func(...interface{}) interface{}
|
type Fn func(...interface{}) interface{}
|
||||||
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -159,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compose">Compose</span>
|
### <span id="Compose">Compose</span>
|
||||||
|
|
||||||
<p>从右至左组合函数列表fnList, 返回组合后的函数</p>
|
<p>从右至左组合函数列表fnList, 返回组合后的函数</p>
|
||||||
@@ -170,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Debounced">Debounced</span>
|
### <span id="Debounced">Debounced</span>
|
||||||
|
|
||||||
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p>
|
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Debounced(fn func(), duration time.Duration) func()
|
func Debounced(fn func(), duration time.Duration) func()
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -237,8 +238,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Delay">Delay</span>
|
### <span id="Delay">Delay</span>
|
||||||
|
|
||||||
<p>延迟delay时间后调用函数</p>
|
<p>延迟delay时间后调用函数</p>
|
||||||
@@ -248,6 +247,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -266,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Schedule">Schedule</span>
|
### <span id="Schedule">Schedule</span>
|
||||||
|
|
||||||
<p>每次持续时间调用函数,直到关闭返回的 bool chan</p>
|
<p>每次持续时间调用函数,直到关闭返回的 bool chan</p>
|
||||||
@@ -277,6 +275,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -301,7 +300,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pipeline">Pipeline</span>
|
||||||
|
|
||||||
|
<p>管道执行多个函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{}
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/function"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
result := fn(2)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 36
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Watcher">Watcher</span>
|
### <span id="Watcher">Watcher</span>
|
||||||
|
|
||||||
@@ -320,6 +359,7 @@ func (w *Watcher) Stop() //stop the watcher
|
|||||||
func (w *Watcher) Reset() //reset the watcher
|
func (w *Watcher) Reset() //reset the watcher
|
||||||
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -359,6 +399,3 @@ func longRunningTask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
174
docs/mathutil.md
174
docs/mathutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Mathutil
|
# Mathutil
|
||||||
|
|
||||||
Package mathutil implements some functions for math calculation.
|
Package mathutil implements some functions for math calculation.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package mathutil implements some functions for math calculation.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/mathutil"
|
"github.com/duke-git/lancet/mathutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
@@ -27,13 +29,17 @@ import (
|
|||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
- [TruncRound](#TruncRound)
|
- [TruncRound](#TruncRound)
|
||||||
|
- [AngleToRadian](#AngleToRadian)
|
||||||
|
- [RadianToAngle](#RadianToAngle)
|
||||||
|
- [PointDistance](#PointDistance)
|
||||||
|
- [IsPrime](#IsPrime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
|
|
||||||
<p>Calculate x to the nth power.</p>
|
<p>Calculate x to the nth power.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -41,6 +47,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Exponent(x, n int64) int64
|
func Exponent(x, n int64) int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,9 +65,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Fibonacci">Fibonacci</span>
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
|
||||||
<p>Calculate the nth number of fibonacci sequence.</p>
|
<p>Calculate the nth number of fibonacci sequence.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -68,6 +74,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Fibonacci(first, second, n int) int
|
func Fibonacci(first, second, n int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -87,9 +94,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Factorial">Factorial</span>
|
### <span id="Factorial">Factorial</span>
|
||||||
|
|
||||||
<p>Calculate the factorial of x.</p>
|
<p>Calculate the factorial of x.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -97,6 +103,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Factorial(x uint) uint
|
func Factorial(x uint) uint
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -115,9 +122,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
|
|
||||||
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -125,6 +131,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Percent(val, total float64, n int) float64
|
func Percent(val, total float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -141,9 +148,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToFloat">RoundToFloat</span>
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
|
||||||
<p>Round float up to n decimal places.</p>
|
<p>Round float up to n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -151,6 +157,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToFloat(x float64, n int) float64
|
func RoundToFloat(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -170,10 +177,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToString">RoundToString</span>
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
|
||||||
<p>Round float up to n decimal places. will return string.</p>
|
<p>Round float up to n decimal places. will return string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -181,6 +186,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToString(x float64, n int) string
|
func RoundToString(x float64, n int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -200,9 +206,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="TruncRound">TruncRound</span>
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
|
||||||
<p>Round float off n decimal places.</p>
|
<p>Round float off n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -210,6 +215,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func TruncRound(x float64, n int) float64
|
func TruncRound(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -229,5 +235,143 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AngleToRadian">AngleToRadian</span>
|
||||||
|
|
||||||
|
<p>Converts angle value to radian value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AngleToRadian(angle float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.AngleToRadian(45)
|
||||||
|
result2 := mathutil.AngleToRadian(90)
|
||||||
|
result3 := mathutil.AngleToRadian(180)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0.7853981633974483
|
||||||
|
// 1.5707963267948966
|
||||||
|
// 3.141592653589793
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RadianToAngle">RadianToAngle</span>
|
||||||
|
|
||||||
|
<p>Converts radian value to angle value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RadianToAngle(radian float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.RadianToAngle(math.Pi)
|
||||||
|
result2 := mathutil.RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := mathutil.RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 180
|
||||||
|
// 90
|
||||||
|
// 45
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PointDistance">PointDistance</span>
|
||||||
|
|
||||||
|
<p>Caculates two points distance.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPrime">IsPrime</span>
|
||||||
|
|
||||||
|
<p>Checks if number is prime number.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPrime(n int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.IsPrime(-1)
|
||||||
|
result2 := mathutil.IsPrime(0)
|
||||||
|
result3 := mathutil.IsPrime(1)
|
||||||
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Mathutil
|
# Mathutil
|
||||||
|
|
||||||
mathutil 包实现了一些数学计算的函数.
|
mathutil 包实现了一些数学计算的函数.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ mathutil包实现了一些数学计算的函数.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/mathutil"
|
"github.com/duke-git/lancet/mathutil"
|
||||||
@@ -20,21 +21,25 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
- [TruncRound](#TruncRound)
|
- [TruncRound](#TruncRound)
|
||||||
|
- [AngleToRadian](#AngleToRadian)
|
||||||
|
- [RadianToAngle](#RadianToAngle)
|
||||||
|
- [PointDistance](#PointDistance)
|
||||||
|
- [IsPrime](#IsPrime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
|
|
||||||
<p>指数计算(x的n次方)</p>
|
<p>指数计算(x的n次方)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -42,6 +47,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Exponent(x, n int64) int64
|
func Exponent(x, n int64) int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -59,9 +65,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Fibonacci">Fibonacci</span>
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
|
||||||
<p>计算斐波那契数列的第n个数</p>
|
<p>计算斐波那契数列的第n个数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -69,6 +74,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Fibonacci(first, second, n int) int
|
func Fibonacci(first, second, n int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -88,9 +94,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Factorial">Factorial</span>
|
### <span id="Factorial">Factorial</span>
|
||||||
|
|
||||||
<p>计算阶乘</p>
|
<p>计算阶乘</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -98,6 +103,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Factorial(x uint) uint
|
func Factorial(x uint) uint
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -116,9 +122,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
|
|
||||||
<p>计算百分比,保留n位小数</p>
|
<p>计算百分比,保留n位小数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -126,6 +131,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Percent(val, total float64, n int) float64
|
func Percent(val, total float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -142,9 +148,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToFloat">RoundToFloat</span>
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
|
||||||
<p>四舍五入,保留n位小数</p>
|
<p>四舍五入,保留n位小数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -152,6 +157,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToFloat(x float64, n int) float64
|
func RoundToFloat(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -171,10 +177,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToString">RoundToString</span>
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
|
||||||
<p>四舍五入,保留n位小数,返回字符串</p>
|
<p>四舍五入,保留n位小数,返回字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -182,6 +186,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToString(x float64, n int) string
|
func RoundToString(x float64, n int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -201,9 +206,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="TruncRound">TruncRound</span>
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
|
||||||
<p>截短n位小数(不进行四舍五入)</p>
|
<p>截短n位小数(不进行四舍五入)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -211,6 +215,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func TruncRound(x float64, n int) float64
|
func TruncRound(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,5 +235,143 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AngleToRadian">AngleToRadian</span>
|
||||||
|
|
||||||
|
<p>将角度值转为弧度值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AngleToRadian(angle float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.AngleToRadian(45)
|
||||||
|
result2 := mathutil.AngleToRadian(90)
|
||||||
|
result3 := mathutil.AngleToRadian(180)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0.7853981633974483
|
||||||
|
// 1.5707963267948966
|
||||||
|
// 3.141592653589793
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RadianToAngle">RadianToAngle</span>
|
||||||
|
|
||||||
|
<p>将弧度值转为角度值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RadianToAngle(radian float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.RadianToAngle(math.Pi)
|
||||||
|
result2 := mathutil.RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := mathutil.RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 180
|
||||||
|
// 90
|
||||||
|
// 45
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PointDistance">PointDistance</span>
|
||||||
|
|
||||||
|
<p>计算两个坐标点的距离</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPrime">IsPrime</span>
|
||||||
|
|
||||||
|
<p>判断质数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPrime(n int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.IsPrime(-1)
|
||||||
|
result2 := mathutil.IsPrime(0)
|
||||||
|
result3 := mathutil.IsPrime(1)
|
||||||
|
result4 := mathutil.IsPrime(2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
224
docs/netutil.md
224
docs/netutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
|
|
||||||
Package netutil contains functions to get net information and send http request.
|
Package netutil contains functions to get net information and send http request.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -14,6 +15,7 @@ Package netutil contains functions to get net information and send http request.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/netutil"
|
"github.com/duke-git/lancet/netutil"
|
||||||
@@ -23,6 +25,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -37,20 +40,23 @@ import (
|
|||||||
- [SendRequest](#SendRequest)
|
- [SendRequest](#SendRequest)
|
||||||
- [DecodeResponse](#DecodeResponse)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [StructToUrlValues](#StructToUrlValues)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
||||||
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
- [UploadFile](#UploadFile)
|
||||||
|
- [DownloadFile](#DownloadFile)
|
||||||
|
- [IsPingConnected](#IsPingConnected)
|
||||||
|
- [IsTelnetConnected](#IsTelnetConnected)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>Convert map to url query string.</p>
|
<p>Convert map to url query string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -58,6 +64,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -80,9 +87,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>Encode url query string values.</p>
|
<p>Encode url query string values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -90,6 +96,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -110,8 +117,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>Get internal ip information.</p>
|
<p>Get internal ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -119,6 +126,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -138,9 +146,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>Get all ipv4 list.</p>
|
<p>Get all ipv4 list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -148,6 +155,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -165,9 +173,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>Get all mac addresses list.</p>
|
<p>Get all mac addresses list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -175,6 +182,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -192,9 +200,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>Get public ip information.</p>
|
<p>Get public ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -216,6 +223,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -236,8 +244,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>Get http request public ip.</p>
|
<p>Get http request public ip.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -245,6 +253,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -278,9 +287,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>Checks if a ip is public or not.</p>
|
<p>Checks if a ip is public or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -288,6 +296,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -308,9 +317,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>Checks if an ip is intranet or not.</p>
|
<p>Checks if an ip is intranet or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -318,6 +326,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -338,9 +347,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -355,6 +363,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -383,8 +392,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
|
||||||
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -411,6 +420,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -432,9 +442,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>Use HttpClient to send HTTP request.</p>
|
<p>Use HttpClient to send HTTP request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -442,6 +451,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -480,9 +490,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>Decode http response into target object.</p>
|
<p>Decode http response into target object.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -490,6 +499,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -528,8 +538,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -537,6 +547,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct interface{}) url.Values
|
func StructToUrlValues(targetStruct interface{}) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -551,20 +562,32 @@ func main() {
|
|||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 2,
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id")) //1
|
fmt.Println(todoValues.Get("id"))
|
||||||
fmt.Println(todoValues.Get("userId")) //2
|
fmt.Println(todoValues.Get("userId"))
|
||||||
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -576,6 +599,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -604,9 +628,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -618,6 +641,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -653,9 +677,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -667,6 +690,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -703,9 +727,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -717,6 +740,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -742,9 +766,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -756,6 +779,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -792,9 +816,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>Decode http response to specified interface.</p>
|
<p>Decode http response to specified interface.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -802,6 +825,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -843,3 +867,123 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UploadFile">UploadFile</span>
|
||||||
|
|
||||||
|
<p>upload the file to a server.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UploadFile(filepath string, server string) (bool, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ok, err := netutil.UploadFile("./a.jpg", "http://www.xxx.com/bucket/test")
|
||||||
|
|
||||||
|
fmt.Println(ok)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DownloadFile">DownloadFile</span>
|
||||||
|
|
||||||
|
<p>download the file exist in url to a local file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DownloadFile(filepath string, url string) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPingConnected">IsPingConnected</span>
|
||||||
|
|
||||||
|
<p>checks if can ping the specified host or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPingConnected(host string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := netutil.IsPingConnected("www.baidu.com")
|
||||||
|
result2 := netutil.IsPingConnected("www.!@#&&&.com")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsTelnetConnected">IsTelnetConnected</span>
|
||||||
|
|
||||||
|
<p>Checks if can telnet the specified host or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsTelnetConnected(host string, port string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := netutil.IsTelnetConnected("www.baidu.com", "80")
|
||||||
|
result2 := netutil.IsTelnetConnected("www.baidu.com", "123")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
|
|
||||||
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ netutil网络包支持获取ip地址,发送http请求。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/netutil"
|
"github.com/duke-git/lancet/netutil"
|
||||||
@@ -22,6 +24,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -36,20 +39,23 @@ import (
|
|||||||
- [SendRequest](#SendRequest)
|
- [SendRequest](#SendRequest)
|
||||||
- [DecodeResponse](#DecodeResponse)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [StructToUrlValues](#StructToUrlValues)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
||||||
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
- [UploadFile](#UploadFile)
|
||||||
|
- [DownloadFile](#DownloadFile)
|
||||||
|
- [IsPingConnected](#IsPingConnected)
|
||||||
|
- [IsTelnetConnected](#IsTelnetConnected)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>将map转换成http查询字符串.</p>
|
<p>将map转换成http查询字符串.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -57,6 +63,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -79,9 +86,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>编码url query string的值</p>
|
<p>编码url query string的值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -89,6 +95,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -109,9 +116,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>获取内部ip</p>
|
<p>获取内部ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -119,6 +125,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -138,8 +145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>获取ipv4地址列表</p>
|
<p>获取ipv4地址列表</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -147,6 +154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -164,9 +172,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>获取mac地址列</p>
|
<p>获取mac地址列</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -174,6 +181,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -191,9 +199,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>获取公网ip信息</p>
|
<p>获取公网ip信息</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -215,6 +222,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -235,9 +243,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>获取http请求ip</p>
|
<p>获取http请求ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -245,6 +252,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -278,9 +286,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是公共ip</p>
|
<p>判断ip是否是公共ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -288,6 +295,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -308,9 +316,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是局域网ip.</p>
|
<p>判断ip是否是局域网ip.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -318,6 +325,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -338,8 +346,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -354,6 +362,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -382,8 +391,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
|
||||||
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -410,6 +419,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -431,9 +441,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>HttpClient发送http请求</p>
|
<p>HttpClient发送http请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -441,6 +450,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -479,9 +489,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>解析http响应体到目标结构体</p>
|
<p>解析http响应体到目标结构体</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -489,6 +498,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -527,8 +537,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
|
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -536,6 +546,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct interface{}) url.Values
|
func StructToUrlValues(targetStruct interface{}) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -550,21 +561,32 @@ func main() {
|
|||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
item := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 2,
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
queryValues := netutil.StructToUrlValues(item)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id")) //1
|
fmt.Println(todoValues.Get("id"))
|
||||||
fmt.Println(todoValues.Get("userId")) //2
|
fmt.Println(todoValues.Get("userId"))
|
||||||
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
fmt.Println(todoValues.Get("status"))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 123
|
||||||
|
// test
|
||||||
|
//
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -576,6 +598,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -604,9 +627,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -618,6 +640,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -653,9 +676,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -667,6 +689,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -703,9 +726,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -717,6 +739,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -742,9 +765,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -756,6 +778,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -792,9 +815,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>将http请求响应解码成特定struct值</p>
|
<p>将http请求响应解码成特定struct值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -802,6 +824,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -843,3 +866,123 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UploadFile">UploadFile</span>
|
||||||
|
|
||||||
|
<p>将文件上传指定的server地址。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UploadFile(filepath string, server string) (bool, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ok, err := netutil.UploadFile("./a.jpg", "http://www.xxx.com/bucket/test")
|
||||||
|
|
||||||
|
fmt.Println(ok)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DownloadFile">DownloadFile</span>
|
||||||
|
|
||||||
|
<p>从指定的server地址下载文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DownloadFile(filepath string, url string) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPingConnected">IsPingConnected</span>
|
||||||
|
|
||||||
|
<p>检查能否ping通主机。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPingConnected(host string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := netutil.IsPingConnected("www.baidu.com")
|
||||||
|
result2 := netutil.IsPingConnected("www.!@#&&&.com")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsTelnetConnected">IsTelnetConnected</span>
|
||||||
|
|
||||||
|
<p>检查能否telnet到主机。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsTelnetConnected(host string, port string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := netutil.IsTelnetConnected("www.baidu.com", "80")
|
||||||
|
result2 := netutil.IsTelnetConnected("www.baidu.com", "123")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
- [RandUpper](#RandUpper)
|
- [RandUpper](#RandUpper)
|
||||||
|
|
||||||
- [RandLower](#RandLower)
|
- [RandLower](#RandLower)
|
||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
- [RandUpper](#RandUpper)
|
- [RandUpper](#RandUpper)
|
||||||
|
|
||||||
- [RandLower](#RandLower)
|
- [RandLower](#RandLower)
|
||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Retry
|
# Retry
|
||||||
|
|
||||||
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package retry is for executing a function repeatedly until it was successful or
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/retry"
|
"github.com/duke-git/lancet/retry"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
@@ -30,8 +32,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>Set retry context config, can cancel the retry with context.</p>
|
<p>Set retry context config, can cancel the retry with context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -39,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -72,10 +75,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>Function that retry executes.</p>
|
<p>Function that retry executes.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -83,6 +84,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -115,9 +117,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>Set times of retry. Default times is 5.</p>
|
<p>Set times of retry. Default times is 5.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -125,6 +126,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -154,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>Set duration of retries. Default duration is 3 second.</p>
|
<p>Set duration of retries. Default duration is 3 second.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -164,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -204,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Retry
|
# Retry
|
||||||
|
|
||||||
retry 重试执行函数直到函数运行成功或被 context cancel。
|
retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ retry重试执行函数直到函数运行成功或被context cancel。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/retry"
|
"github.com/duke-git/lancet/retry"
|
||||||
@@ -20,20 +21,19 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
- [RetryDuration](#RetryDuration)
|
- [RetryDuration](#RetryDuration)
|
||||||
- [RetryTimes](#RetryTimes)
|
- [RetryTimes](#RetryTimes)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|
||||||
## Document 文档
|
## Document 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>设置重试context参数</p>
|
<p>设置重试context参数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -41,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -74,10 +75,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>被重试执行的函数</p>
|
<p>被重试执行的函数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -85,6 +84,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -117,9 +117,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>设置重试次数,默认5</p>
|
<p>设置重试次数,默认5</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -127,6 +126,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -156,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>设置重试间隔时间,默认3秒</p>
|
<p>设置重试间隔时间,默认3秒</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -197,8 +197,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
188
docs/slice.md
188
docs/slice.md
@@ -1,4 +1,5 @@
|
|||||||
# Slice
|
# Slice
|
||||||
|
|
||||||
Package slice implements some functions to manipulate slice.
|
Package slice implements some functions to manipulate slice.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package slice implements some functions to manipulate slice.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
@@ -39,7 +41,6 @@ import (
|
|||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
@@ -67,11 +68,11 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
## Note:
|
## Note:
|
||||||
|
|
||||||
1. param which type is interface{} in below functions should be slice.
|
1. param which type is interface{} in below functions should be slice.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
|
||||||
<p>If slice doesn't contain the value, append it to the slice.</p>
|
<p>If slice doesn't contain the value, append it to the slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -79,6 +80,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -97,8 +99,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
|
|
||||||
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -106,6 +108,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Contain(iterableType interface{}, value interface{}) bool
|
func Contain(iterableType interface{}, value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -120,8 +123,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainSubSlice">ContainSubSlice</span>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>Check if the slice contain subslice or not.</p>
|
<p>Check if the slice contain subslice or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -129,6 +132,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -143,10 +147,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Chunk">Chunk</span>
|
### <span id="Chunk">Chunk</span>
|
||||||
|
|
||||||
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -154,6 +156,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Chunk(slice []interface{}, size int) [][]interface{}
|
func Chunk(slice []interface{}, size int) [][]interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -169,9 +172,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compact">Compact</span>
|
### <span id="Compact">Compact</span>
|
||||||
|
|
||||||
<p>Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.</p>
|
<p>Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -179,6 +181,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compact(slice interface{}) interface{}
|
func Compact(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -193,8 +196,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Concat">Concat</span>
|
### <span id="Concat">Concat</span>
|
||||||
|
|
||||||
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
|
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -202,6 +205,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Concat(slice interface{}, values ...interface{}) interface{}
|
func Concat(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -219,9 +223,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Count">Count</span>
|
### <span id="Count">Count</span>
|
||||||
|
|
||||||
<p>Count iterates over elements of slice, returns a count of all matched elements. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Count iterates over elements of slice, returns a count of all matched elements. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -229,6 +232,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Count(slice, function interface{}) int
|
func Count(slice, function interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -248,10 +252,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Difference">Difference</span>
|
### <span id="Difference">Difference</span>
|
||||||
|
|
||||||
<p>Creates an slice of whose element not included in the other given slice.</p>
|
<p>Creates an slice of whose element not included in the other given slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -259,6 +261,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Difference(slice1, slice2 interface{}) interface{}
|
func Difference(slice1, slice2 interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -276,10 +279,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DifferenceBy">DifferenceBy</span>
|
### <span id="DifferenceBy">DifferenceBy</span>
|
||||||
|
|
||||||
<p>DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.</p>
|
<p>DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -287,6 +288,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -307,10 +309,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DeleteByIndex">DeleteByIndex</span>
|
### <span id="DeleteByIndex">DeleteByIndex</span>
|
||||||
|
|
||||||
<p>Delete the element of slice from start index to end index - 1.</p>
|
<p>Delete the element of slice from start index to end index - 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -318,6 +318,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -336,10 +337,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -347,6 +346,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Drop(slice interface{}, n int) interface{}
|
func Drop(slice interface{}, n int) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -367,9 +367,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Equal">Equal</span>
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
|
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -377,6 +376,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Equal(slice1, slice2 interface{}) bool
|
func Equal(slice1, slice2 interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -398,9 +398,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EqualWith">EqualWith</span>
|
### <span id="EqualWith">EqualWith</span>
|
||||||
|
|
||||||
<p>Check if two slices are equal with comparator funcation.comparator signature: func(a interface{}, b interface{}) bool</p>
|
<p>Check if two slices are equal with comparator funcation.comparator signature: func(a interface{}, b interface{}) bool</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -408,6 +407,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -430,9 +430,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Every">Every</span>
|
### <span id="Every">Every</span>
|
||||||
|
|
||||||
<p>Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -440,6 +439,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Every(slice, function interface{}) bool
|
func Every(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -459,10 +459,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Filter">Filter</span>
|
### <span id="Filter">Filter</span>
|
||||||
|
|
||||||
<p>Return all elements which match the function. Function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return all elements which match the function. Function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -470,6 +468,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Filter(slice, function interface{}) interface{}
|
func Filter(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -489,9 +488,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Find">Find</span>
|
### <span id="Find">Find</span>
|
||||||
|
|
||||||
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool.</p>
|
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -499,6 +497,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Find(slice, function interface{}) (interface{}, bool)
|
func Find(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -519,10 +518,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FindLast">FindLast</span>
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -530,6 +527,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FindLast(slice, function interface{}) (interface{}, bool)
|
func FindLast(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -550,9 +548,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
|
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -560,6 +557,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FlattenDeep(slice interface{}) interface{}
|
func FlattenDeep(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -575,9 +573,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ForEach">ForEach</span>
|
### <span id="ForEach">ForEach</span>
|
||||||
|
|
||||||
<p>Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).</p>
|
<p>Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -585,6 +582,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ForEach(slice, function interface{})
|
func ForEach(slice, function interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -603,10 +601,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GroupBy">GroupBy</span>
|
### <span id="GroupBy">GroupBy</span>
|
||||||
|
|
||||||
<p>Iterates over elements of the slice, each element will be group by criteria, returns two slices. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Iterates over elements of the slice, each element will be group by criteria, returns two slices. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -614,6 +610,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -634,10 +631,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
|
|
||||||
<p>Convert interface slice to int slice.</p>
|
<p>Convert interface slice to int slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -645,6 +640,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IntSlice(slice interface{}) []int
|
func IntSlice(slice interface{}) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -660,10 +656,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InterfaceSlice">InterfaceSlice</span>
|
### <span id="InterfaceSlice">InterfaceSlice</span>
|
||||||
|
|
||||||
<p>Convert value to interface slice.</p>
|
<p>Convert value to interface slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -671,6 +665,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InterfaceSlice(slice interface{}) []interface{}
|
func InterfaceSlice(slice interface{}) []interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -686,10 +681,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Intersection">Intersection</span>
|
### <span id="Intersection">Intersection</span>
|
||||||
|
|
||||||
<p>Creates a slice of unique values that included by all slices.</p>
|
<p>Creates a slice of unique values that included by all slices.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -697,6 +690,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Intersection(slices ...interface{}) interface{}
|
func Intersection(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -714,9 +708,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IndexOf">IndexOf</span>
|
### <span id="IndexOf">IndexOf</span>
|
||||||
|
|
||||||
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -724,6 +717,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IndexOf(slice, value interface{}) int
|
func IndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -742,9 +736,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LastIndexOf">LastIndexOf</span>
|
### <span id="LastIndexOf">LastIndexOf</span>
|
||||||
|
|
||||||
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -752,6 +745,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LastIndexOf(slice, value interface{}) int
|
func LastIndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -770,8 +764,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertByIndex">InsertByIndex</span>
|
### <span id="InsertByIndex">InsertByIndex</span>
|
||||||
|
|
||||||
<p>insert the element into slice at index.</p>
|
<p>insert the element into slice at index.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -779,6 +773,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -798,10 +793,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Map">Map</span>
|
### <span id="Map">Map</span>
|
||||||
|
|
||||||
<p>Creates an slice of values by running each element in slice thru function, function signature should be func(index int, value interface{}) interface{}.</p>
|
<p>Creates an slice of values by running each element in slice thru function, function signature should be func(index int, value interface{}) interface{}.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -809,6 +802,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Map(slice, function interface{}) interface{}
|
func Map(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -827,10 +821,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseSlice">ReverseSlice</span>
|
### <span id="ReverseSlice">ReverseSlice</span>
|
||||||
|
|
||||||
<p>Reverse the elements order in slice.</p>
|
<p>Reverse the elements order in slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -838,6 +830,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseSlice(slice interface{})
|
func ReverseSlice(slice interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -853,9 +846,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reduce">Reduce</span>
|
### <span id="Reduce">Reduce</span>
|
||||||
|
|
||||||
<p>Reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}.</p>
|
<p>Reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -863,6 +855,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Reduce(slice, function, zero interface{}) interface{}
|
func Reduce(slice, function, zero interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -881,10 +874,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Shuffle">Shuffle</span>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
|
|
||||||
<p>Creates an slice of shuffled values.</p>
|
<p>Creates an slice of shuffled values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -892,6 +883,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Shuffle(slice interface{}) interface{}
|
func Shuffle(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -907,9 +899,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SortByField">SortByField</span>
|
### <span id="SortByField">SortByField</span>
|
||||||
|
|
||||||
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
|
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -917,6 +908,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -950,9 +942,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Some">Some</span>
|
### <span id="Some">Some</span>
|
||||||
|
|
||||||
<p>Return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -960,6 +951,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Some(slice, function interface{}) bool
|
func Some(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -979,9 +971,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StringSlice">StringSlice</span>
|
### <span id="StringSlice">StringSlice</span>
|
||||||
|
|
||||||
<p>Convert interface slice to string slice.</p>
|
<p>Convert interface slice to string slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -989,6 +980,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StringSlice(slice interface{}) []string
|
func StringSlice(slice interface{}) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1004,9 +996,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlice">ToSlice</span>
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
<p>Returns a slices of a variable parameter transformation</p>
|
<p>Returns a slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1014,6 +1005,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlice(value ...interface{}) interface{}
|
func ToSlice(value ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1028,9 +1020,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlicePointer</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
|
||||||
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1038,6 +1029,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlicePointer(value ...interface{}) []*interface{}
|
func ToSlicePointer(value ...interface{}) []*interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1054,9 +1046,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
|
|
||||||
<p>Remove duplicate elements in slice.</p>
|
<p>Remove duplicate elements in slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1064,6 +1055,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unique(slice interface{}) interface{}
|
func Unique(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1078,9 +1070,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UniqueBy">UniqueBy</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
|
||||||
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1088,6 +1079,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UniqueBy(slice, iteratee interface{}) interface{}
|
func UniqueBy(slice, iteratee interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1104,9 +1096,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1114,6 +1105,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Union(slices ...interface{}) interface{}
|
func Union(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1131,9 +1123,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpdateByIndex">UpdateByIndex</span>
|
### <span id="UpdateByIndex">UpdateByIndex</span>
|
||||||
|
|
||||||
<p>Update the slice element at index. if param index < 0 or index >= len(slice), will return error. </p>
|
<p>Update the slice element at index. if param index < 0 or index >= len(slice), will return error. </p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1141,6 +1132,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1157,10 +1149,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Without">Without</span>
|
### <span id="Without">Without</span>
|
||||||
|
|
||||||
<p>Creates a slice excluding all given values. </p>
|
<p>Creates a slice excluding all given values. </p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1168,6 +1158,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Without(slice interface{}, values ...interface{}) interface{}
|
func Without(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1181,14 +1172,3 @@ func main() {
|
|||||||
fmt.Println(res) //[]int{3, 4, 5}
|
fmt.Println(res) //[]int{3, 4, 5}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Slice
|
# Slice
|
||||||
|
|
||||||
slice 包包含操作切片的方法集合。
|
slice 包包含操作切片的方法集合。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ slice包包含操作切片的方法集合。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
@@ -39,7 +41,6 @@ import (
|
|||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
@@ -67,6 +68,7 @@ import (
|
|||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
|
||||||
<p>当前切片中不包含值时,将该值追加到切片中</p>
|
<p>当前切片中不包含值时,将该值追加到切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -74,6 +76,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -92,9 +95,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
|
|
||||||
<p>判断slice是否包含value</p>
|
<p>判断slice是否包含value</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -102,6 +104,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Contain(iterableType interface{}, value interface{}) bool
|
func Contain(iterableType interface{}, value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -116,8 +119,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainSubSlice">ContainSubSlice</span>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>判断slice是否包含subslice</p>
|
<p>判断slice是否包含subslice</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -125,6 +128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -139,10 +143,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Chunk">Chunk</span>
|
### <span id="Chunk">Chunk</span>
|
||||||
|
|
||||||
<p>按照size参数均分slice</p>
|
<p>按照size参数均分slice</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -150,6 +152,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Chunk(slice []interface{}, size int) [][]interface{}
|
func Chunk(slice []interface{}, size int) [][]interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -165,9 +168,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compact">Compact</span>
|
### <span id="Compact">Compact</span>
|
||||||
|
|
||||||
<p>去除slice中的假值(false values are false, nil, 0, "")</p>
|
<p>去除slice中的假值(false values are false, nil, 0, "")</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -175,6 +177,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compact(slice interface{}) interface{}
|
func Compact(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -189,8 +192,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Concat">Concat</span>
|
### <span id="Concat">Concat</span>
|
||||||
|
|
||||||
<p>连接values到slice中,values类型可以是切片或多个值</p>
|
<p>连接values到slice中,values类型可以是切片或多个值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -198,6 +201,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Concat(slice interface{}, values ...interface{}) interface{}
|
func Concat(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -215,9 +219,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Count">Count</span>
|
### <span id="Count">Count</span>
|
||||||
|
|
||||||
<p>遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool</p>
|
<p>遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -225,6 +228,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Count(slice, function interface{}) int
|
func Count(slice, function interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -244,10 +248,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Difference">Difference</span>
|
### <span id="Difference">Difference</span>
|
||||||
|
|
||||||
<p>创建一个切片,其元素不包含在另一个给定切片中</p>
|
<p>创建一个切片,其元素不包含在另一个给定切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -255,6 +257,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Difference(slice1, slice2 interface{}) interface{}
|
func Difference(slice1, slice2 interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -272,10 +275,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DifferenceBy">DifferenceBy</span>
|
### <span id="DifferenceBy">DifferenceBy</span>
|
||||||
|
|
||||||
<p>在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值</p>
|
<p>在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -283,6 +284,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -303,10 +305,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DeleteByIndex">DeleteByIndex</span>
|
### <span id="DeleteByIndex">DeleteByIndex</span>
|
||||||
|
|
||||||
<p>删除切片中从开始索引到结束索引-1的元素</p>
|
<p>删除切片中从开始索引到结束索引-1的元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -314,6 +314,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -332,10 +333,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素</p>
|
<p>创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -343,6 +342,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Drop(slice interface{}, n int) interface{}
|
func Drop(slice interface{}, n int) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -363,9 +363,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Equal">Equal</span>
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
<p>检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同</p>
|
<p>检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -373,6 +372,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Equal(slice1, slice2 interface{}) bool
|
func Equal(slice1, slice2 interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -394,9 +394,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EqualWith">EqualWith</span>
|
### <span id="EqualWith">EqualWith</span>
|
||||||
|
|
||||||
<p>检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool</p>
|
<p>检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -404,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -426,9 +426,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Every">Every</span>
|
### <span id="Every">Every</span>
|
||||||
|
|
||||||
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool</p>
|
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -436,6 +435,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Every(slice, function interface{}) bool
|
func Every(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -455,10 +455,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Filter">Filter</span>
|
### <span id="Filter">Filter</span>
|
||||||
|
|
||||||
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool</p>
|
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -466,6 +464,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Filter(slice, function interface{}) interface{}
|
func Filter(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -485,9 +484,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Find">Find</span>
|
### <span id="Find">Find</span>
|
||||||
|
|
||||||
<p>遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool</p>
|
<p>遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -495,6 +493,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Find(slice, function interface{}) (interface{}, bool)
|
func Find(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -515,10 +514,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FindLast">FindLast</span>
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
<p>从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。</p>
|
<p>从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -526,6 +523,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FindLast(slice, function interface{}) (interface{}, bool)
|
func FindLast(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -546,9 +544,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
|
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -556,6 +553,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FlattenDeep(slice interface{}) interface{}
|
func FlattenDeep(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -571,11 +569,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ForEach">ForEach</span>
|
### <span id="ForEach">ForEach</span>
|
||||||
|
|
||||||
<p>遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})</p>
|
<p>遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -583,6 +578,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ForEach(slice, function interface{})
|
func ForEach(slice, function interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -601,10 +597,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GroupBy">GroupBy</span>
|
### <span id="GroupBy">GroupBy</span>
|
||||||
|
|
||||||
<p>迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool</p>
|
<p>迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -612,6 +606,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -632,10 +627,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
|
|
||||||
<p>将接口切片转换为int切片</p>
|
<p>将接口切片转换为int切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -643,6 +636,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IntSlice(slice interface{}) []int
|
func IntSlice(slice interface{}) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -658,10 +652,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InterfaceSlice">InterfaceSlice</span>
|
### <span id="InterfaceSlice">InterfaceSlice</span>
|
||||||
|
|
||||||
<p>将值转换为接口切片</p>
|
<p>将值转换为接口切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -669,6 +661,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InterfaceSlice(slice interface{}) []interface{}
|
func InterfaceSlice(slice interface{}) []interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -684,10 +677,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Intersection">Intersection</span>
|
### <span id="Intersection">Intersection</span>
|
||||||
|
|
||||||
<p>多个切片的交集</p>
|
<p>多个切片的交集</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -695,6 +686,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Intersection(slices ...interface{}) interface{}
|
func Intersection(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -712,9 +704,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IndexOf">IndexOf</span>
|
### <span id="IndexOf">IndexOf</span>
|
||||||
|
|
||||||
<p>返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1</p>
|
<p>返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -722,6 +713,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IndexOf(slice, value interface{}) int
|
func IndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -740,9 +732,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LastIndexOf">LastIndexOf</span>
|
### <span id="LastIndexOf">LastIndexOf</span>
|
||||||
|
|
||||||
<p>返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1</p>
|
<p>返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -750,6 +741,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LastIndexOf(slice, value interface{}) int
|
func LastIndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -768,9 +760,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertByIndex">InsertByIndex</span>
|
### <span id="InsertByIndex">InsertByIndex</span>
|
||||||
|
|
||||||
<p>将元素插入到索引处的切片中</p>
|
<p>将元素插入到索引处的切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -778,6 +769,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -797,10 +789,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Map">Map</span>
|
### <span id="Map">Map</span>
|
||||||
|
|
||||||
<p>通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。</p>
|
<p>通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -808,6 +798,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Map(slice, function interface{}) interface{}
|
func Map(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -826,10 +817,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseSlice">ReverseSlice</span>
|
### <span id="ReverseSlice">ReverseSlice</span>
|
||||||
|
|
||||||
<p>反转切片中的元素顺序</p>
|
<p>反转切片中的元素顺序</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -837,6 +826,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseSlice(slice interface{})
|
func ReverseSlice(slice interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -852,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reduce">Reduce</span>
|
### <span id="Reduce">Reduce</span>
|
||||||
|
|
||||||
<p>将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。</p>
|
<p>将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -862,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Reduce(slice, function, zero interface{}) interface{}
|
func Reduce(slice, function, zero interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -880,10 +870,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Shuffle">Shuffle</span>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
|
|
||||||
<p>随机打乱切片中的元素顺序</p>
|
<p>随机打乱切片中的元素顺序</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -891,6 +879,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Shuffle(slice interface{}) interface{}
|
func Shuffle(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -906,9 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SortByField">SortByField</span>
|
### <span id="SortByField">SortByField</span>
|
||||||
|
|
||||||
<p>按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc</p>
|
<p>按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -916,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -949,9 +938,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Some">Some</span>
|
### <span id="Some">Some</span>
|
||||||
|
|
||||||
<p>如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .</p>
|
<p>如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -959,6 +947,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Some(slice, function interface{}) bool
|
func Some(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -978,9 +967,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StringSlice">StringSlice</span>
|
### <span id="StringSlice">StringSlice</span>
|
||||||
|
|
||||||
<p>将接口切片转换为字符串切片</p>
|
<p>将接口切片转换为字符串切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -988,6 +976,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StringSlice(slice interface{}) []string
|
func StringSlice(slice interface{}) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1003,9 +992,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlice">ToSlice</span>
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
<p>将可变参数转为切片</p>
|
<p>将可变参数转为切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1013,12 +1001,13 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlice(value ...interface{}) interface{}
|
func ToSlice(value ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -1027,9 +1016,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlicePointer</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
|
||||||
<p>将可变参数转为指针切片</p>
|
<p>将可变参数转为指针切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1037,12 +1025,13 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlicePointer(value ...interface{}) []*interface{}
|
func ToSlicePointer(value ...interface{}) []*interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -1053,9 +1042,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
|
|
||||||
<p>删除切片中的重复元素</p>
|
<p>删除切片中的重复元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1063,6 +1051,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unique(slice interface{}) interface{}
|
func Unique(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1077,9 +1066,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UniqueBy">UniqueBy</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
|
||||||
<p>对切片的每个项目调用iteratee函数,然后删除重复的</p>
|
<p>对切片的每个项目调用iteratee函数,然后删除重复的</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1087,6 +1075,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UniqueBy(slice, iteratee interface{}) interface{}
|
func UniqueBy(slice, iteratee interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1103,8 +1092,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。</p>
|
<p>从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1112,6 +1101,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Union(slices ...interface{}) interface{}
|
func Union(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1129,9 +1119,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpdateByIndex">UpdateByIndex</span>
|
### <span id="UpdateByIndex">UpdateByIndex</span>
|
||||||
|
|
||||||
<p>更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误</p>
|
<p>更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1139,6 +1128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1155,10 +1145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Without">Without</span>
|
### <span id="Without">Without</span>
|
||||||
|
|
||||||
<p>创建一个不包括所有给定值的切片</p>
|
<p>创建一个不包括所有给定值的切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1166,6 +1154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Without(slice interface{}, values ...interface{}) interface{}
|
func Without(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1179,14 +1168,3 @@ func main() {
|
|||||||
fmt.Println(res) //[]int{3, 4, 5}
|
fmt.Println(res) //[]int{3, 4, 5}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
547
docs/strutil.md
547
docs/strutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Strutil
|
# Strutil
|
||||||
|
|
||||||
Package strutil contains some functions to manipulate string.
|
Package strutil contains some functions to manipulate string.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package strutil contains some functions to manipulate string.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/strutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [AfterLast](#AfterLast)
|
- [AfterLast](#AfterLast)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
@@ -28,24 +30,34 @@ import (
|
|||||||
- [Capitalize](#Capitalize)
|
- [Capitalize](#Capitalize)
|
||||||
- [IsString](#IsString)
|
- [IsString](#IsString)
|
||||||
- [KebabCase](#KebabCase)
|
- [KebabCase](#KebabCase)
|
||||||
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
- [LowerFirst](#LowerFirst)
|
- [LowerFirst](#LowerFirst)
|
||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
|
- [Pad](#Pad)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [Reverse](#Reverse)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
|
- [UpperSnakeCase](#UpperSnakeCase)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
|
- [SplitWords](#SplitWords)
|
||||||
|
- [WordCount](#WordCount)
|
||||||
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](#StringToBytes)
|
||||||
|
- [BytesToString](#BytesToString)
|
||||||
|
- [IsBlank](#IsBlank)
|
||||||
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
|
- [IndexOffset](#IndexOffset)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>Creates substring in source string after position when char first appear.</p>
|
<p>Creates substring in source string after position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -53,6 +65,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(s, char string) string
|
func After(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -73,9 +86,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
|
|
||||||
<p>Creates substring in source string after position when char last appear.</p>
|
<p>Creates substring in source string after position when char last appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -83,6 +95,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AfterLast(s, char string) string
|
func AfterLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -103,10 +116,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>Creates substring in source string before position when char first appear.</p>
|
<p>Creates substring in source string before position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -114,6 +125,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(s, char string) string
|
func Before(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -134,10 +146,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
|
|
||||||
<p>Creates substring in source string before position when char first appear.</p>
|
<p>Creates substring in source string before position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -145,6 +155,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeforeLast(s, char string) string
|
func BeforeLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -165,17 +176,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
<p>Covert string to camelCase string.</p>
|
|
||||||
|
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CamelCase(s string) string
|
func CamelCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -196,13 +206,13 @@ func main() {
|
|||||||
|
|
||||||
s4 := strutil.CamelCase("foo bar")
|
s4 := strutil.CamelCase("foo bar")
|
||||||
fmt.Println(s4) //fooBar
|
fmt.Println(s4) //fooBar
|
||||||
}
|
|
||||||
|
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
|
||||||
|
fmt.Println(s4) //foo11Bar
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
|
|
||||||
<p>Convert the first character of a string to upper case.</p>
|
<p>Convert the first character of a string to upper case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -210,6 +220,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Capitalize(s string) string
|
func Capitalize(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,9 +241,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
|
|
||||||
<p>Check if the value's data type is string.</p>
|
<p>Check if the value's data type is string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -240,6 +250,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsString(v interface{}) bool
|
func IsString(v interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -258,16 +269,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
### <span id="KebabCase">KebabCase</span>
|
||||||
<p>Covert string to kebab-case.</p>
|
|
||||||
|
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -287,14 +298,45 @@ func main() {
|
|||||||
fmt.Println(s3) //foo-bar
|
fmt.Println(s3) //foo-bar
|
||||||
|
|
||||||
s4 := strutil.KebabCase("__FOO_BAR__")
|
s4 := strutil.KebabCase("__FOO_BAR__")
|
||||||
fmt.Println(s4) //f-o-o-b-a-r
|
fmt.Println(s4) //foo-bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
|
|
||||||
|
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func KebabCase(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := strutil.UpperKebabCase("Foo Bar-")
|
||||||
|
fmt.Println(s1) //FOO-BAR
|
||||||
|
|
||||||
|
s2 := strutil.UpperKebabCase("foo_Bar")
|
||||||
|
fmt.Println(s2) //FOO-BAR
|
||||||
|
|
||||||
|
s3 := strutil.UpperKebabCase("fooBar")
|
||||||
|
fmt.Println(s3) //FOO-BAR
|
||||||
|
|
||||||
|
s4 := strutil.UpperKebabCase("__FOO_BAR__")
|
||||||
|
fmt.Println(s4) //FOO-BAR
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
|
|
||||||
<p>Convert the first character of string to lower case.</p>
|
<p>Convert the first character of string to lower case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -302,6 +344,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LowerFirst(s string) string
|
func LowerFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -325,10 +368,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
|
|
||||||
<p>Convert the first character of string to upper case.</p>
|
<p>Convert the first character of string to upper case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -336,6 +377,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpperFirst(s string) string
|
func UpperFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -359,10 +401,53 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>Pads string on the left and right side if it's shorter than size.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
|
|
||||||
<p>Pads string on the right side if it's shorter than size.</p>
|
<p>Pads string on the right side if it's shorter than size.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -370,6 +455,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func PadEnd(source string, size int, padStr string) string
|
func PadEnd(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -379,24 +465,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
|
|
||||||
<p>Pads string on the left side if it's shorter than size.</p>
|
<p>Pads string on the left side if it's shorter than size.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -404,6 +501,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func PadStart(source string, size int, padStr string) string
|
func PadStart(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -413,24 +511,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="ReverseStr">ReverseStr</span>
|
||||||
|
|
||||||
<p>Return string whose char order is reversed to the given string.</p>
|
<p>Return string whose char order is reversed to the given string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -438,6 +547,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func ReverseStr(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -455,16 +565,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
<p>Covert string to snake_case.</p>
|
|
||||||
|
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -484,17 +594,51 @@ func main() {
|
|||||||
fmt.Println(s3) //foo_bar
|
fmt.Println(s3) //foo_bar
|
||||||
|
|
||||||
s4 := strutil.SnakeCase("__FOO_BAR__")
|
s4 := strutil.SnakeCase("__FOO_BAR__")
|
||||||
fmt.Println(s4) //f_o_o_b_a_r
|
fmt.Println(s4) //foo_bar
|
||||||
|
|
||||||
s5 := strutil.SnakeCase("aBbc-s$@a&%_B.B^C")
|
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
|
||||||
fmt.Println(s5) //a_bbc_s_a_b_b_c
|
fmt.Println(s5) //foo_1_1_bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
|
|
||||||
|
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SnakeCase(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := strutil.UpperSnakeCase("Foo Bar-")
|
||||||
|
fmt.Println(s1) //FOO_BAR
|
||||||
|
|
||||||
|
s2 := strutil.UpperSnakeCase("foo_Bar")
|
||||||
|
fmt.Println(s2) //FOO_BAR
|
||||||
|
|
||||||
|
s3 := strutil.UpperSnakeCase("fooBar")
|
||||||
|
fmt.Println(s3) //FOO_BAR
|
||||||
|
|
||||||
|
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
|
||||||
|
fmt.Println(s4) //FOO_BAR
|
||||||
|
|
||||||
|
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
|
||||||
|
fmt.Println(s5) //FOO_1_1_BAR
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>Wrap a string with another string.</p>
|
<p>Wrap a string with another string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -502,6 +646,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Wrap(str string, wrapWith string) string
|
func Wrap(str string, wrapWith string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -528,10 +673,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>Unwrap a given string from anther string. will change str value.</p>
|
<p>Unwrap a given string from anther string. will change str value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -539,6 +682,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unwrap(str string, wrapToken string) string
|
func Unwrap(str string, wrapToken string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -565,9 +709,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
|
||||||
<p>Split a given string whether the result contains empty string.</p>
|
<p>Split a given string whether the result contains empty string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -575,6 +718,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -601,7 +745,280 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="SplitWords">SplitWords</span>
|
||||||
|
|
||||||
|
<p>Splits a string into words, word only contains alphabetic characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitWords(s string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.SplitWords("a word")
|
||||||
|
result2 := strutil.SplitWords("I'am a programmer")
|
||||||
|
result3 := strutil.SplitWords("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.SplitWords("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.SplitWords("你好,我是一名码农")
|
||||||
|
result6 := strutil.SplitWords("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a word]
|
||||||
|
// [I'am a programmer]
|
||||||
|
// [Bonjour je suis programmeur]
|
||||||
|
// [a b-c' d'e]
|
||||||
|
// []
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
|
<p>Return the number of meaningful word, word only contains alphabetic characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WordCount(s string) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.WordCount("a word")
|
||||||
|
result2 := strutil.WordCount("I'am a programmer")
|
||||||
|
result3 := strutil.WordCount("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.WordCount("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.WordCount("你好,我是一名码农")
|
||||||
|
result6 := strutil.WordCount("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// 3
|
||||||
|
// 0
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="StringToBytes">StringToBytes</span>
|
||||||
|
|
||||||
|
<p>Converts a string to byte slice without a memory allocation.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StringToBytes(str string) (b []byte)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.StringToBytes("abc")
|
||||||
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// [97 98 99]
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BytesToString">BytesToString</span>
|
||||||
|
|
||||||
|
<p>Converts a byte slice to string without a memory allocation.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BytesToString(bytes []byte) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsBlank">IsBlank</span>
|
||||||
|
|
||||||
|
<p>Checks if a string is whitespace or empty.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBlank(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.IsBlank("")
|
||||||
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HasPrefixAny">HasPrefixAny</span>
|
||||||
|
|
||||||
|
<p>Checks if a string starts with any of an array of specified strings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HasPrefixAny(str string, prefixes []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HasSuffixAny">HasSuffixAny</span>
|
||||||
|
|
||||||
|
<p>Checks if a string ends with any of an array of specified strings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HasSuffixAny(str string, suffixes []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IndexOffset">IndexOffset</span>
|
||||||
|
|
||||||
|
<p>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.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IndexOffset(str string, substr string, idxFrom int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "foo bar hello world"
|
||||||
|
|
||||||
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 1
|
||||||
|
// 18
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
# Strutil
|
# Strutil
|
||||||
|
|
||||||
strutil 包含处理字符串的相关函数。
|
strutil 包含处理字符串的相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ strutil包含处理字符串的相关函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/strutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [AfterLast](#AfterLast)
|
- [AfterLast](#AfterLast)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
@@ -28,25 +30,34 @@ import (
|
|||||||
- [Capitalize](#Capitalize)
|
- [Capitalize](#Capitalize)
|
||||||
- [IsString](#IsString)
|
- [IsString](#IsString)
|
||||||
- [KebabCase](#KebabCase)
|
- [KebabCase](#KebabCase)
|
||||||
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
- [LowerFirst](#LowerFirst)
|
- [LowerFirst](#LowerFirst)
|
||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
|
- [Pad](#Pad)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [Reverse](#Reverse)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
|
- [UpperSnakeCase](#UpperSnakeCase)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
|
- [SplitWords](#SplitWords)
|
||||||
|
- [WordCount](#WordCount)
|
||||||
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
- [StringToBytes](#StringToBytes)
|
||||||
|
- [BytesToString](#BytesToString)
|
||||||
|
- [IsBlank](#IsBlank)
|
||||||
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
|
- [IndexOffset](#IndexOffset)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|
||||||
## Documentation 文档
|
## Documentation 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>截取源字符串中char首次出现时的位置之后的子字符串</p>
|
<p>截取源字符串中char首次出现时的位置之后的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -54,6 +65,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(s, char string) string
|
func After(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -74,9 +86,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
|
|
||||||
<p>截取源字符串中char最后一次出现时的位置之后的子字符串</p>
|
<p>截取源字符串中char最后一次出现时的位置之后的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -84,6 +95,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AfterLast(s, char string) string
|
func AfterLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -104,10 +116,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>截取源字符串中char首次出现时的位置之前的子字符串</p>
|
<p>截取源字符串中char首次出现时的位置之前的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -115,6 +125,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(s, char string) string
|
func Before(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -135,10 +146,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
|
|
||||||
<p>截取源字符串中char最后一次出现时的位置之前的子字符串</p>
|
<p>截取源字符串中char最后一次出现时的位置之前的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -146,6 +155,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeforeLast(s, char string) string
|
func BeforeLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -166,17 +176,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
<p>将字符串转换为驼峰式字符串</p>
|
|
||||||
|
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CamelCase(s string) string
|
func CamelCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -197,13 +206,14 @@ func main() {
|
|||||||
|
|
||||||
s4 := strutil.CamelCase("foo bar")
|
s4 := strutil.CamelCase("foo bar")
|
||||||
fmt.Println(s4) //fooBar
|
fmt.Println(s4) //fooBar
|
||||||
|
|
||||||
|
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
|
||||||
|
fmt.Println(s4) //foo11Bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为大写</p>
|
<p>将字符串的第一个字符转换为大写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -211,6 +221,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Capitalize(s string) string
|
func Capitalize(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -231,9 +242,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
|
|
||||||
<p>检查值的数据类型是否为字符串</p>
|
<p>检查值的数据类型是否为字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -241,6 +251,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsString(v interface{}) bool
|
func IsString(v interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,16 +270,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
### <span id="KebabCase">KebabCase</span>
|
||||||
<p>将字符串转换为kebab-case</p>
|
|
||||||
|
<p>将字符串转换为kebab-case, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -288,14 +299,45 @@ func main() {
|
|||||||
fmt.Println(s3) //foo-bar
|
fmt.Println(s3) //foo-bar
|
||||||
|
|
||||||
s4 := strutil.KebabCase("__FOO_BAR__")
|
s4 := strutil.KebabCase("__FOO_BAR__")
|
||||||
fmt.Println(s4) //f-o-o-b-a-r
|
fmt.Println(s4) //foo-bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
|
|
||||||
|
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func KebabCase(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := strutil.UpperKebabCase("Foo Bar-")
|
||||||
|
fmt.Println(s1) //FOO-BAR
|
||||||
|
|
||||||
|
s2 := strutil.UpperKebabCase("foo_Bar")
|
||||||
|
fmt.Println(s2) //FOO-BAR
|
||||||
|
|
||||||
|
s3 := strutil.UpperKebabCase("fooBar")
|
||||||
|
fmt.Println(s3) //FOO-BAR
|
||||||
|
|
||||||
|
s4 := strutil.UpperKebabCase("__FOO_BAR__")
|
||||||
|
fmt.Println(s4) //FOO-BAR
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为小写</p>
|
<p>将字符串的第一个字符转换为小写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -303,6 +345,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LowerFirst(s string) string
|
func LowerFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -326,10 +369,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为大写</p>
|
<p>将字符串的第一个字符转换为大写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -337,6 +378,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpperFirst(s string) string
|
func UpperFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -360,18 +402,62 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>如果字符串长度短于size,则在左右两侧填充字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
<p>如果字符串长度短于size,则在右侧填充字符串</p>
|
|
||||||
|
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadEnd(source string, size int, padStr string) string
|
func PadEnd(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -380,32 +466,44 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
<p>如果字符串长度短于size,则在左侧填充字符串</p>
|
|
||||||
|
<p>如果字符串长度短于size,则在左侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadStart(source string, size int, padStr string) string
|
func PadStart(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -414,24 +512,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="ReverseStr">ReverseStr</span>
|
||||||
|
|
||||||
<p>返回字符顺序与给定字符串相反的字符串</p>
|
<p>返回字符顺序与给定字符串相反的字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -439,6 +548,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func ReverseStr(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -456,16 +566,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
<p>将字符串转换为snake_case形式</p>
|
|
||||||
|
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -485,17 +595,51 @@ func main() {
|
|||||||
fmt.Println(s3) //foo_bar
|
fmt.Println(s3) //foo_bar
|
||||||
|
|
||||||
s4 := strutil.SnakeCase("__FOO_BAR__")
|
s4 := strutil.SnakeCase("__FOO_BAR__")
|
||||||
fmt.Println(s4) //f_o_o_b_a_r
|
fmt.Println(s4) //foo_bar
|
||||||
|
|
||||||
s5 := strutil.SnakeCase("aBbc-s$@a&%_B.B^C")
|
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
|
||||||
fmt.Println(s5) //a_bbc_s_a_b_b_c
|
fmt.Println(s5) //foo_1_1_bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
|
|
||||||
|
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SnakeCase(s string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := strutil.UpperSnakeCase("Foo Bar-")
|
||||||
|
fmt.Println(s1) //FOO_BAR
|
||||||
|
|
||||||
|
s2 := strutil.UpperSnakeCase("foo_Bar")
|
||||||
|
fmt.Println(s2) //FOO_BAR
|
||||||
|
|
||||||
|
s3 := strutil.UpperSnakeCase("fooBar")
|
||||||
|
fmt.Println(s3) //FOO_BAR
|
||||||
|
|
||||||
|
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
|
||||||
|
fmt.Println(s4) //FOO_BAR
|
||||||
|
|
||||||
|
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
|
||||||
|
fmt.Println(s5) //FOO_1_1_BAR
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>用另一个字符串包裹一个字符串</p>
|
<p>用另一个字符串包裹一个字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -503,6 +647,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Wrap(str string, wrapWith string) string
|
func Wrap(str string, wrapWith string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -529,10 +674,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unwrap">Unwrap</span>
|
### <span id="Unwrap">Unwrap</span>
|
||||||
|
|
||||||
<p>用另一个字符串解开包裹一个字符串</p>
|
<p>用另一个字符串解开包裹一个字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -540,6 +683,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unwrap(str string, wrapToken string) string
|
func Unwrap(str string, wrapToken string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -566,9 +710,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
|
||||||
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -576,6 +719,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -602,8 +746,311 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="SplitWords">SplitWords</span>
|
||||||
|
|
||||||
|
<p>将字符串拆分为单词,只支持字母字符单词。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitWords(s string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.SplitWords("a word")
|
||||||
|
result2 := strutil.SplitWords("I'am a programmer")
|
||||||
|
result3 := strutil.SplitWords("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.SplitWords("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.SplitWords("你好,我是一名码农")
|
||||||
|
result6 := strutil.SplitWords("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a word]
|
||||||
|
// [I'am a programmer]
|
||||||
|
// [Bonjour je suis programmeur]
|
||||||
|
// [a b-c' d'e]
|
||||||
|
// []
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
|
<p>返回有意义单词的数量,只支持字母字符单词。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WordCount(s string) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.WordCount("a word")
|
||||||
|
result2 := strutil.WordCount("I'am a programmer")
|
||||||
|
result3 := strutil.WordCount("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.WordCount("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.WordCount("你好,我是一名码农")
|
||||||
|
result6 := strutil.WordCount("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// 3
|
||||||
|
// 0
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
|
<p>删除字符串中不可打印的字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveNonPrintable(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
|
||||||
|
result2 := strutil.RemoveNonPrintable("你好😄")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// hello world
|
||||||
|
// 你好😄
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="StringToBytes">StringToBytes</span>
|
||||||
|
|
||||||
|
<p>在不分配内存的情况下将字符串转换为字节片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StringToBytes(str string) (b []byte)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.StringToBytes("abc")
|
||||||
|
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// [97 98 99]
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BytesToString">BytesToString</span>
|
||||||
|
|
||||||
|
<p>在不分配内存的情况下将字节切片转换为字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BytesToString(bytes []byte) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
|
result := strutil.BytesToString(bytes)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsBlank">IsBlank</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否为空格或空。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsBlank(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.IsBlank("")
|
||||||
|
result2 := strutil.IsBlank("\t\v\f\n")
|
||||||
|
result3 := strutil.IsBlank(" 中文")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HasPrefixAny">HasPrefixAny</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否以指定字符串数组中的任何一个开头。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HasPrefixAny(str string, prefixes []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
|
||||||
|
result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HasSuffixAny">HasSuffixAny</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否以指定字符串数组中的任何一个结尾。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HasSuffixAny(str string, suffixes []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
|
||||||
|
result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IndexOffset">IndexOffset</span>
|
||||||
|
|
||||||
|
<p>将字符串偏移idxFrom后,返回字符串中第一个 substr 实例的索引,如果字符串中不存在 substr,则返回 -1。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IndexOffset(str string, substr string, idxFrom int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "foo bar hello world"
|
||||||
|
|
||||||
|
result1 := strutil.IndexOffset(str, "o", 5)
|
||||||
|
result2 := strutil.IndexOffset(str, "o", 0)
|
||||||
|
result3 := strutil.IndexOffset(str, "d", len(str)-1)
|
||||||
|
result4 := strutil.IndexOffset(str, "d", len(str))
|
||||||
|
result5 := strutil.IndexOffset(str, "f", -1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
// Output:
|
||||||
|
// 12
|
||||||
|
// 1
|
||||||
|
// 18
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
Package system contains some functions about os, runtime, shell command.
|
Package system contains some functions about os, runtime, shell command.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package system contains some functions about os, runtime, shell command.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/system"
|
"github.com/duke-git/lancet/system"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [IsWindows](#IsWindows)
|
- [IsWindows](#IsWindows)
|
||||||
- [IsLinux](#IsLinux)
|
- [IsLinux](#IsLinux)
|
||||||
- [IsMac](#IsMac)
|
- [IsMac](#IsMac)
|
||||||
@@ -30,13 +32,12 @@ import (
|
|||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>Check if current os is windows.</p>
|
<p>Check if current os is windows.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,6 +45,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,10 +60,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>Check if current os is linux.</p>
|
<p>Check if current os is linux.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,6 +69,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +84,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>Check if current os is macos.</p>
|
<p>Check if current os is macos.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,6 +93,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -107,9 +108,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>Gets the value of the environment variable named by the key.</p>
|
<p>Gets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,6 +117,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -131,9 +132,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>Sets the value of the environment variable named by the key.</p>
|
<p>Sets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,6 +141,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -155,10 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>Remove a single environment variable.</p>
|
<p>Remove a single environment variable.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -182,9 +182,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>Get env named by the key and compare it with comparedEnv.</p>
|
<p>Get env named by the key and compare it with comparedEnv.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,6 +191,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,17 +207,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ExecCommand">ExecCommand</span>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
|
||||||
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
type (
|
||||||
|
Option func(*exec.Cmd)
|
||||||
|
)
|
||||||
|
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -227,16 +229,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
out, errout, err := system.ExecCommand("ls")
|
out, errout, err := system.ExecCommand("ls", system.WithForeground())
|
||||||
fmt.Println(out)
|
fmt.Println(out)
|
||||||
fmt.Println(errout)
|
fmt.Println(errout)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>获取当前操作系统位数,返回32或64</p>
|
<p>获取当前操作系统位数,返回32或64</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -244,6 +245,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -257,7 +259,3 @@ func main() {
|
|||||||
fmt.Println(osBit)
|
fmt.Println(osBit)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
system 包含 os, runtime, shell command 相关函数。
|
system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ system包含os, runtime, shell command相关函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/system"
|
"github.com/duke-git/lancet/system"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [IsWindows](#IsWindows)
|
- [IsWindows](#IsWindows)
|
||||||
- [IsLinux](#IsLinux)
|
- [IsLinux](#IsLinux)
|
||||||
- [IsMac](#IsMac)
|
- [IsMac](#IsMac)
|
||||||
@@ -30,13 +32,12 @@ import (
|
|||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation 文档
|
## Documentation 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是windows</p>
|
<p>检查当前操作系统是否是windows</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,6 +45,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,10 +60,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是linux</p>
|
<p>检查当前操作系统是否是linux</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,6 +69,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +84,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是macos</p>
|
<p>检查当前操作系统是否是macos</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,6 +93,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -107,9 +108,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量的值</p>
|
<p>获取key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,6 +117,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -131,9 +132,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>设置由key命名的环境变量的值</p>
|
<p>设置由key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,6 +141,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -155,10 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>删除单个环境变量</p>
|
<p>删除单个环境变量</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -182,9 +182,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,6 +191,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,17 +207,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ExecCommand">ExecCommand</span>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
|
||||||
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
|
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
type (
|
||||||
|
Option func(*exec.Cmd)
|
||||||
|
)
|
||||||
|
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -227,16 +229,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
out, errout, err := system.ExecCommand("ls")
|
out, errout, err := system.ExecCommand("ls", system.WithForeground())
|
||||||
fmt.Println(out)
|
fmt.Println(out)
|
||||||
fmt.Println(errout)
|
fmt.Println(errout)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -244,6 +245,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -257,7 +259,3 @@ func main() {
|
|||||||
fmt.Println(osBit)
|
fmt.Println(osBit)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Validator
|
# Validator
|
||||||
|
|
||||||
Package validator contains some functions for data validation.
|
Package validator contains some functions for data validation.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package validator contains some functions for data validation.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/validator"
|
"github.com/duke-git/lancet/validator"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ContainChinese](#ContainChinese)
|
- [ContainChinese](#ContainChinese)
|
||||||
- [ContainLetter](#ContainLetter)
|
- [ContainLetter](#ContainLetter)
|
||||||
- [ContainLower](#ContainLower)
|
- [ContainLower](#ContainLower)
|
||||||
@@ -34,13 +36,15 @@ import (
|
|||||||
- [IsCreditCard](#IsCreditCard)
|
- [IsCreditCard](#IsCreditCard)
|
||||||
- [IsDns](#IsDns)
|
- [IsDns](#IsDns)
|
||||||
- [IsEmail](#IsEmail)
|
- [IsEmail](#IsEmail)
|
||||||
|
|
||||||
- [IsEmptyString](#IsEmptyString)
|
- [IsEmptyString](#IsEmptyString)
|
||||||
|
- [IsInt](#IsInt)
|
||||||
|
- [IsFloat](#IsFloat)
|
||||||
|
- [IsNumber](#IsNumber)
|
||||||
|
- [IsIntStr](#IsIntStr)
|
||||||
- [IsFloatStr](#IsFloatStr)
|
- [IsFloatStr](#IsFloatStr)
|
||||||
- [IsNumberStr](#IsNumberStr)
|
- [IsNumberStr](#IsNumberStr)
|
||||||
- [IsJSON](#IsJSON)
|
- [IsJSON](#IsJSON)
|
||||||
- [IsRegexMatch](#IsRegexMatch)
|
- [IsRegexMatch](#IsRegexMatch)
|
||||||
- [IsIntStr](#IsIntStr)
|
|
||||||
- [IsIp](#IsIp)
|
- [IsIp](#IsIp)
|
||||||
- [IsIpV4](#IsIpV4)
|
- [IsIpV4](#IsIpV4)
|
||||||
- [IsIpV6](#IsIpV6)
|
- [IsIpV6](#IsIpV6)
|
||||||
@@ -48,14 +52,16 @@ import (
|
|||||||
- [IsUrl](#IsUrl)
|
- [IsUrl](#IsUrl)
|
||||||
- [IsWeakPassword](#IsWeakPassword)
|
- [IsWeakPassword](#IsWeakPassword)
|
||||||
- [IsZeroValue](#IsZeroValue)
|
- [IsZeroValue](#IsZeroValue)
|
||||||
|
- [IsGBK](#IsGBK)
|
||||||
|
- [IsASCII](#IsASCII)
|
||||||
|
- [IsAIsPrintableSCII](#IsPrintable)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainChinese">ContainChinese</span>
|
### <span id="ContainChinese">ContainChinese</span>
|
||||||
|
|
||||||
<p>Check if the string contain mandarin chinese.</p>
|
<p>Check if the string contain mandarin chinese.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -63,6 +69,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ContainChinese(s string) bool
|
func ContainChinese(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +90,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLetter">ContainLetter</span>
|
### <span id="ContainLetter">ContainLetter</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one letter.</p>
|
<p>Check if the string contain at least one letter.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,6 +99,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLetter(str string) bool
|
func ContainLetter(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -113,10 +120,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLower">ContainLower</span>
|
### <span id="ContainLower">ContainLower</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one lower case letter a-z.</p>
|
<p>Check if the string contain at least one lower case letter a-z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -124,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLower(str string) bool
|
func ContainLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -144,10 +150,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainUpper">ContainUpper</span>
|
### <span id="ContainUpper">ContainUpper</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one upper case letter A-Z.</p>
|
<p>Check if the string contain at least one upper case letter A-Z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -155,6 +159,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainUpper(str string) bool
|
func ContainUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -175,10 +180,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAlpha">IsAlpha</span>
|
### <span id="IsAlpha">IsAlpha</span>
|
||||||
|
|
||||||
<p>Check if the string contains only letters (a-zA-Z).</p>
|
<p>Check if the string contains only letters (a-zA-Z).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -186,6 +189,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAlpha(s string) bool
|
func IsAlpha(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,6 +211,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsAllUpper">IsAllUpper</span>
|
### <span id="IsAllUpper">IsAllUpper</span>
|
||||||
|
|
||||||
<p>Check if string is all upper case letters A-Z.</p>
|
<p>Check if string is all upper case letters A-Z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -214,6 +219,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllUpper(str string) bool
|
func IsAllUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -231,10 +237,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAllLower">IsAllLower</span>
|
### <span id="IsAllLower">IsAllLower</span>
|
||||||
|
|
||||||
<p>Check if string is all lower case letters a-z.</p>
|
<p>Check if string is all lower case letters a-z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -242,6 +246,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllLower(str string) bool
|
func IsAllLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,10 +264,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsBase64">IsBase64</span>
|
### <span id="IsBase64">IsBase64</span>
|
||||||
|
|
||||||
<p>Check if the string is base64 string.</p>
|
<p>Check if the string is base64 string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -270,6 +273,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsBase64(base64 string) bool
|
func IsBase64(base64 string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -287,10 +291,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseMobile">IsChineseMobile</span>
|
### <span id="IsChineseMobile">IsChineseMobile</span>
|
||||||
|
|
||||||
<p>Check if the string is valid chinese mobile number.</p>
|
<p>Check if the string is valid chinese mobile number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -298,6 +300,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseMobile(mobileNum string) bool
|
func IsChineseMobile(mobileNum string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -315,9 +318,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
||||||
|
|
||||||
<p>Check if the string is chinese id number.</p>
|
<p>Check if the string is chinese id number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -325,6 +327,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseIdNum(id string) bool
|
func IsChineseIdNum(id string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -342,10 +345,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChinesePhone">IsChinesePhone</span>
|
### <span id="IsChinesePhone">IsChinesePhone</span>
|
||||||
|
|
||||||
<p>Check if the string is chinese phone number.</p>
|
<p>Check if the string is chinese phone number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -353,6 +354,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChinesePhone(phone string) bool
|
func IsChinesePhone(phone string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -370,10 +372,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsCreditCard">IsCreditCard</span>
|
### <span id="IsCreditCard">IsCreditCard</span>
|
||||||
|
|
||||||
<p>Check if the string is credit card.</p>
|
<p>Check if the string is credit card.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -381,6 +381,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsCreditCard(creditCart string) bool
|
func IsCreditCard(creditCart string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -398,10 +399,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDns">IsDns</span>
|
### <span id="IsDns">IsDns</span>
|
||||||
|
|
||||||
<p>Check if the string is valid dns.</p>
|
<p>Check if the string is valid dns.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -409,6 +408,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDns(dns string) bool
|
func IsDns(dns string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -429,10 +429,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmail">IsEmail</span>
|
### <span id="IsEmail">IsEmail</span>
|
||||||
|
|
||||||
<p>Check if the string is email address.</p>
|
<p>Check if the string is email address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -440,6 +438,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmail(email string) bool
|
func IsEmail(email string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -457,11 +456,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmptyString">IsEmptyString</span>
|
### <span id="IsEmptyString">IsEmptyString</span>
|
||||||
|
|
||||||
<p>Check if the string is empty or not.</p>
|
<p>Check if the string is empty or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -469,6 +465,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmptyString(s string) bool
|
func IsEmptyString(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -486,10 +483,145 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsInt">IsInt</span>
|
||||||
|
|
||||||
|
<p>Check if the value is integer(int, unit) or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsInt(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsInt("")
|
||||||
|
result2 := validator.IsInt("3")
|
||||||
|
result3 := validator.IsInt(0.1)
|
||||||
|
result4 := validator.IsInt(0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsFloat">IsFloat</span>
|
||||||
|
|
||||||
|
<p>Check if the value is float(float32, float34) or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsFloat(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsFloat("")
|
||||||
|
result2 := validator.IsFloat("3")
|
||||||
|
result3 := validator.IsFloat(0)
|
||||||
|
result4 := validator.IsFloat(0.1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsNumber">IsNumber</span>
|
||||||
|
|
||||||
|
<p>Check if the value is number(integer, float) or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsNumber(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsNumber("")
|
||||||
|
result2 := validator.IsNumber("3")
|
||||||
|
result3 := validator.IsNumber(0.1)
|
||||||
|
result4 := validator.IsNumber(0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsIntStr">IsIntStr</span>
|
||||||
|
|
||||||
|
<p>Check if the string can convert to a integer.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsIntStr(s string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(validator.IsIntStr("+3")) //true
|
||||||
|
fmt.Println(validator.IsIntStr("-3")) //true
|
||||||
|
fmt.Println(validator.IsIntStr("3.")) //false
|
||||||
|
fmt.Println(validator.IsIntStr("abc")) //false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="IsFloatStr">IsFloatStr</span>
|
### <span id="IsFloatStr">IsFloatStr</span>
|
||||||
|
|
||||||
<p>Check if the string can convert to a float.</p>
|
<p>Check if the string can convert to a float.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -497,6 +629,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsFloatStr(s string) bool
|
func IsFloatStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -515,10 +648,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsNumberStr">IsNumberStr</span>
|
### <span id="IsNumberStr">IsNumberStr</span>
|
||||||
|
|
||||||
<p>Check if the string can convert to a number.</p>
|
<p>Check if the string can convert to a number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -526,6 +657,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsNumberStr(s string) bool
|
func IsNumberStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -544,10 +676,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsJSON">IsJSON</span>
|
### <span id="IsJSON">IsJSON</span>
|
||||||
|
|
||||||
<p>Check if the string is valid JSON.</p>
|
<p>Check if the string is valid JSON.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -555,6 +685,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsJSON(str string) bool
|
func IsJSON(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -573,10 +704,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsRegexMatch">IsRegexMatch</span>
|
### <span id="IsRegexMatch">IsRegexMatch</span>
|
||||||
|
|
||||||
<p>Check if the string match the regexp.</p>
|
<p>Check if the string match the regexp.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -584,6 +713,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsRegexMatch(s, regex string) bool
|
func IsRegexMatch(s, regex string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -599,37 +729,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIntStr">IsIntStr</span>
|
|
||||||
<p>Check if the string can convert to a integer.</p>
|
|
||||||
|
|
||||||
<b>Signature:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func IsIntStr(s string) bool
|
|
||||||
```
|
|
||||||
<b>Example:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duke-git/lancet/validator"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println(validator.IsIntStr("+3")) //true
|
|
||||||
fmt.Println(validator.IsIntStr("-3")) //true
|
|
||||||
fmt.Println(validator.IsIntStr("3.")) //false
|
|
||||||
fmt.Println(validator.IsIntStr("abc")) //false
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIp">IsIp</span>
|
### <span id="IsIp">IsIp</span>
|
||||||
|
|
||||||
<p>Check if the string is a ip address.</p>
|
<p>Check if the string is a ip address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -637,6 +738,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIp(ipstr string) bool
|
func IsIp(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -653,10 +755,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV4">IsIpV4</span>
|
### <span id="IsIpV4">IsIpV4</span>
|
||||||
|
|
||||||
<p>Check if the string is a ipv4 address.</p>
|
<p>Check if the string is a ipv4 address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -664,6 +764,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV4(ipstr string) bool
|
func IsIpV4(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -680,10 +781,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV6">IsIpV6</span>
|
### <span id="IsIpV6">IsIpV6</span>
|
||||||
|
|
||||||
<p>Check if the string is a ipv6 address.</p>
|
<p>Check if the string is a ipv6 address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -691,6 +790,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV6(ipstr string) bool
|
func IsIpV6(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -707,10 +807,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsStrongPassword">IsStrongPassword</span>
|
### <span id="IsStrongPassword">IsStrongPassword</span>
|
||||||
|
|
||||||
<p>Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).</p>
|
<p>Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -718,6 +816,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsStrongPassword(password string, length int) bool
|
func IsStrongPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -735,10 +834,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsUrl">IsUrl</span>
|
### <span id="IsUrl">IsUrl</span>
|
||||||
|
|
||||||
<p>Check if the string is url.</p>
|
<p>Check if the string is url.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -746,6 +843,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsUrl(str string) bool
|
func IsUrl(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -762,10 +860,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWeakPassword">IsWeakPassword</span>
|
### <span id="IsWeakPassword">IsWeakPassword</span>
|
||||||
|
|
||||||
<p>Check if the string is weak password(only letter or only number or letter + number)
|
<p>Check if the string is weak password(only letter or only number or letter + number)
|
||||||
.</p>
|
.</p>
|
||||||
|
|
||||||
@@ -774,6 +870,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsWeakPassword(password string, length int) bool
|
func IsWeakPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -791,13 +888,15 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsZeroValue">IsZeroValue</span>
|
### <span id="IsZeroValue">IsZeroValue</span>
|
||||||
|
|
||||||
<p>Checks if passed value is a zero value.</p>
|
<p>Checks if passed value is a zero value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func IsZeroValue(value any) bool
|
func IsZeroValue(value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -818,14 +917,113 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsGBK">IsGBK</span>
|
||||||
|
|
||||||
|
<p>Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsGBK(data []byte) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := []byte("你好")
|
||||||
|
|
||||||
|
// check utf8 first
|
||||||
|
if utf8.Valid(data) {
|
||||||
|
fmt.Println("data encoding is utf-8")
|
||||||
|
}else if(validator.IsGBK(data)) {
|
||||||
|
fmt.Println("data encoding is GBK")
|
||||||
|
}
|
||||||
|
fmt.Println("data encoding is unknown")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsASCII">IsASCII</span>
|
||||||
|
|
||||||
|
<p>Checks if string is all ASCII char.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsASCII(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsASCII("ABC")
|
||||||
|
result2 := validator.IsASCII("123")
|
||||||
|
result3 := validator.IsASCII("")
|
||||||
|
result4 := validator.IsASCII("😄")
|
||||||
|
result5 := validator.IsASCII("你好")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPrintable">IsPrintable</span>
|
||||||
|
|
||||||
|
<p>Checks if string is all printable chars.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPrintable(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsPrintable("ABC")
|
||||||
|
result2 := validator.IsPrintable("{id: 123}")
|
||||||
|
result3 := validator.IsPrintable("")
|
||||||
|
result4 := validator.IsPrintable("😄")
|
||||||
|
result5 := validator.IsPrintable("\u0000")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
# Validator
|
# Validator
|
||||||
|
|
||||||
validator 验证器包,包含常用字符串格式验证函数。
|
validator 验证器包,包含常用字符串格式验证函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ validator验证器包,包含常用字符串格式验证函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/validator"
|
"github.com/duke-git/lancet/validator"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录:
|
## 目录:
|
||||||
|
|
||||||
- [ContainChinese](#ContainChinese)
|
- [ContainChinese](#ContainChinese)
|
||||||
- [ContainLetter](#ContainLetter)
|
- [ContainLetter](#ContainLetter)
|
||||||
- [ContainLower](#ContainLower)
|
- [ContainLower](#ContainLower)
|
||||||
@@ -34,13 +36,15 @@ import (
|
|||||||
- [IsCreditCard](#IsCreditCard)
|
- [IsCreditCard](#IsCreditCard)
|
||||||
- [IsDns](#IsDns)
|
- [IsDns](#IsDns)
|
||||||
- [IsEmail](#IsEmail)
|
- [IsEmail](#IsEmail)
|
||||||
|
|
||||||
- [IsEmptyString](#IsEmptyString)
|
- [IsEmptyString](#IsEmptyString)
|
||||||
|
- [IsInt](#IsInt)
|
||||||
|
- [IsFloat](#IsFloat)
|
||||||
|
- [IsNumber](#IsNumber)
|
||||||
|
- [IsIntStr](#IsIntStr)
|
||||||
- [IsFloatStr](#IsFloatStr)
|
- [IsFloatStr](#IsFloatStr)
|
||||||
- [IsNumberStr](#IsNumberStr)
|
- [IsNumberStr](#IsNumberStr)
|
||||||
- [IsJSON](#IsJSON)
|
- [IsJSON](#IsJSON)
|
||||||
- [IsRegexMatch](#IsRegexMatch)
|
- [IsRegexMatch](#IsRegexMatch)
|
||||||
- [IsIntStr](#IsIntStr)
|
|
||||||
- [IsIp](#IsIp)
|
- [IsIp](#IsIp)
|
||||||
- [IsIpV4](#IsIpV4)
|
- [IsIpV4](#IsIpV4)
|
||||||
- [IsIpV6](#IsIpV6)
|
- [IsIpV6](#IsIpV6)
|
||||||
@@ -48,14 +52,16 @@ import (
|
|||||||
- [IsUrl](#IsUrl)
|
- [IsUrl](#IsUrl)
|
||||||
- [IsWeakPassword](#IsWeakPassword)
|
- [IsWeakPassword](#IsWeakPassword)
|
||||||
- [IsZeroValue](#IsZeroValue)
|
- [IsZeroValue](#IsZeroValue)
|
||||||
|
- [IsGBK](#IsGBK)
|
||||||
|
- [IsASCII](#IsASCII)
|
||||||
|
- [IsPrintable](#IsPrintable)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainChinese">ContainChinese</span>
|
### <span id="ContainChinese">ContainChinese</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含中文字符</p>
|
<p>验证字符串是否包含中文字符</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -63,6 +69,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ContainChinese(s string) bool
|
func ContainChinese(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +90,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLetter">ContainLetter</span>
|
### <span id="ContainLetter">ContainLetter</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文字母</p>
|
<p>验证字符串是否包含至少一个英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -93,6 +99,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLetter(str string) bool
|
func ContainLetter(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -113,10 +120,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLower">ContainLower</span>
|
### <span id="ContainLower">ContainLower</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文小写字母</p>
|
<p>验证字符串是否包含至少一个英文小写字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -124,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLower(str string) bool
|
func ContainLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -144,10 +150,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainUpper">ContainUpper</span>
|
### <span id="ContainUpper">ContainUpper</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文大写字母.</p>
|
<p>验证字符串是否包含至少一个英文大写字母.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -155,6 +159,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainUpper(str string) bool
|
func ContainUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -175,10 +180,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAlpha">IsAlpha</span>
|
### <span id="IsAlpha">IsAlpha</span>
|
||||||
|
|
||||||
<p>验证字符串是否只包含英文字母</p>
|
<p>验证字符串是否只包含英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -186,6 +189,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAlpha(s string) bool
|
func IsAlpha(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,6 +211,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsAllUpper">IsAllUpper</span>
|
### <span id="IsAllUpper">IsAllUpper</span>
|
||||||
|
|
||||||
<p>验证字符串是否全是大写英文字母</p>
|
<p>验证字符串是否全是大写英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -214,6 +219,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllUpper(str string) bool
|
func IsAllUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -231,10 +237,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAllLower">IsAllLower</span>
|
### <span id="IsAllLower">IsAllLower</span>
|
||||||
|
|
||||||
<p>验证字符串是否全是小写英文字母</p>
|
<p>验证字符串是否全是小写英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -242,6 +246,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllLower(str string) bool
|
func IsAllLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,10 +264,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsBase64">IsBase64</span>
|
### <span id="IsBase64">IsBase64</span>
|
||||||
|
|
||||||
<p>验证字符串是否是base64编码</p>
|
<p>验证字符串是否是base64编码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -270,6 +273,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsBase64(base64 string) bool
|
func IsBase64(base64 string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -287,10 +291,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseMobile">IsChineseMobile</span>
|
### <span id="IsChineseMobile">IsChineseMobile</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国手机号码</p>
|
<p>验证字符串是否是中国手机号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -298,6 +300,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseMobile(mobileNum string) bool
|
func IsChineseMobile(mobileNum string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -315,9 +318,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国身份证号码</p>
|
<p>验证字符串是否是中国身份证号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -325,6 +327,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseIdNum(id string) bool
|
func IsChineseIdNum(id string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -342,10 +345,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChinesePhone">IsChinesePhone</span>
|
### <span id="IsChinesePhone">IsChinesePhone</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国电话座机号码</p>
|
<p>验证字符串是否是中国电话座机号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -353,6 +354,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChinesePhone(phone string) bool
|
func IsChinesePhone(phone string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -370,10 +372,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsCreditCard">IsCreditCard</span>
|
### <span id="IsCreditCard">IsCreditCard</span>
|
||||||
|
|
||||||
<p>验证字符串是否是信用卡号码</p>
|
<p>验证字符串是否是信用卡号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -381,6 +381,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsCreditCard(creditCart string) bool
|
func IsCreditCard(creditCart string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -398,10 +399,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDns">IsDns</span>
|
### <span id="IsDns">IsDns</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效dns</p>
|
<p>验证字符串是否是有效dns</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -409,6 +408,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDns(dns string) bool
|
func IsDns(dns string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -429,10 +429,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmail">IsEmail</span>
|
### <span id="IsEmail">IsEmail</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效电子邮件地址</p>
|
<p>验证字符串是否是有效电子邮件地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -440,6 +438,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmail(email string) bool
|
func IsEmail(email string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -457,11 +456,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmptyString">IsEmptyString</span>
|
### <span id="IsEmptyString">IsEmptyString</span>
|
||||||
|
|
||||||
<p>验证字符串是否是空字符串</p>
|
<p>验证字符串是否是空字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -469,6 +465,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmptyString(s string) bool
|
func IsEmptyString(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -486,10 +483,156 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsInt">IsInt</span>
|
||||||
|
|
||||||
|
<p>验证参数是否是整数(int, unit)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsInt(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsInt("")
|
||||||
|
result2 := validator.IsInt("3")
|
||||||
|
result3 := validator.IsInt(0.1)
|
||||||
|
result4 := validator.IsInt(0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsFloat">IsFloat</span>
|
||||||
|
|
||||||
|
<p>验证参数是否是浮点数((float32, float34)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsFloat(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsFloat("")
|
||||||
|
result2 := validator.IsFloat("3")
|
||||||
|
result3 := validator.IsFloat(0)
|
||||||
|
result4 := validator.IsFloat(0.1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsNumber">IsNumber</span>
|
||||||
|
|
||||||
|
<p>验证参数是否是数字(integer or float)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsNumber(v interface{}) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsNumber("")
|
||||||
|
result2 := validator.IsNumber("3")
|
||||||
|
result3 := validator.IsNumber(0.1)
|
||||||
|
result4 := validator.IsNumber(0)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsIntStr">IsIntStr</span>
|
||||||
|
|
||||||
|
<p>验证字符串是否是可以转换为整数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsIntStr(s string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsIntStr("+3")
|
||||||
|
result2 := validator.IsIntStr("-3")
|
||||||
|
result3 := validator.IsIntStr("3.")
|
||||||
|
result4 := validator.IsIntStr("abc")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="IsFloatStr">IsFloatStr</span>
|
### <span id="IsFloatStr">IsFloatStr</span>
|
||||||
|
|
||||||
<p>验证字符串是否是可以转换为浮点数</p>
|
<p>验证字符串是否是可以转换为浮点数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -497,6 +640,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsFloatStr(s string) bool
|
func IsFloatStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -515,10 +659,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsNumberStr">IsNumberStr</span>
|
### <span id="IsNumberStr">IsNumberStr</span>
|
||||||
|
|
||||||
<p>验证字符串是否是可以转换为数字</p>
|
<p>验证字符串是否是可以转换为数字</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -526,6 +668,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsNumberStr(s string) bool
|
func IsNumberStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -544,10 +687,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsJSON">IsJSON</span>
|
### <span id="IsJSON">IsJSON</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效json</p>
|
<p>验证字符串是否是有效json</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -555,6 +696,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsJSON(str string) bool
|
func IsJSON(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -573,10 +715,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsRegexMatch">IsRegexMatch</span>
|
### <span id="IsRegexMatch">IsRegexMatch</span>
|
||||||
|
|
||||||
<p>验证字符串是否可以匹配正则表达式</p>
|
<p>验证字符串是否可以匹配正则表达式</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -584,6 +724,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsRegexMatch(s, regex string) bool
|
func IsRegexMatch(s, regex string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -599,37 +740,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIntStr">IsIntStr</span>
|
|
||||||
<p>验证字符串是否是可以转换为整数</p>
|
|
||||||
|
|
||||||
<b>函数签名:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func IsIntStr(s string) bool
|
|
||||||
```
|
|
||||||
<b>例子:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duke-git/lancet/validator"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println(validator.IsIntStr("+3")) //true
|
|
||||||
fmt.Println(validator.IsIntStr("-3")) //true
|
|
||||||
fmt.Println(validator.IsIntStr("3.")) //false
|
|
||||||
fmt.Println(validator.IsIntStr("abc")) //false
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIp">IsIp</span>
|
### <span id="IsIp">IsIp</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ip地址</p>
|
<p>验证字符串是否是ip地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -637,6 +749,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIp(ipstr string) bool
|
func IsIp(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -653,10 +766,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV4">IsIpV4</span>
|
### <span id="IsIpV4">IsIpV4</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ipv4地址</p>
|
<p>验证字符串是否是ipv4地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -664,6 +775,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV4(ipstr string) bool
|
func IsIpV4(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -680,10 +792,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV6">IsIpV6</span>
|
### <span id="IsIpV6">IsIpV6</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ipv6地址</p>
|
<p>验证字符串是否是ipv6地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -691,6 +801,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV6(ipstr string) bool
|
func IsIpV6(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -707,10 +818,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsStrongPassword">IsStrongPassword</span>
|
### <span id="IsStrongPassword">IsStrongPassword</span>
|
||||||
|
|
||||||
<p>验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))</p>
|
<p>验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -718,6 +827,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsStrongPassword(password string, length int) bool
|
func IsStrongPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -735,10 +845,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsUrl">IsUrl</span>
|
### <span id="IsUrl">IsUrl</span>
|
||||||
|
|
||||||
<p>验证字符串是否是url</p>
|
<p>验证字符串是否是url</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -746,6 +854,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsUrl(str string) bool
|
func IsUrl(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -762,10 +871,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWeakPassword">IsWeakPassword</span>
|
### <span id="IsWeakPassword">IsWeakPassword</span>
|
||||||
|
|
||||||
<p>验证字符串是否是弱密码:(only letter or only number or letter + number)
|
<p>验证字符串是否是弱密码:(only letter or only number or letter + number)
|
||||||
.</p>
|
.</p>
|
||||||
|
|
||||||
@@ -774,6 +881,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsWeakPassword(password string, length int) bool
|
func IsWeakPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -790,16 +898,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZeroValue">IsZeroValue</span>
|
### <span id="IsZeroValue">IsZeroValue</span>
|
||||||
|
|
||||||
<p>判断传入的参数值是否为零值</p>
|
<p>判断传入的参数值是否为零值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func IsZeroValue(value any) bool
|
func IsZeroValue(value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -820,8 +928,112 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsGBK">IsGBK</span>
|
||||||
|
|
||||||
|
<p>检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsGBK(data []byte) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := []byte("你好")
|
||||||
|
|
||||||
|
// 先检查utf8编码
|
||||||
|
if utf8.Valid(data) {
|
||||||
|
fmt.Println("data encoding is utf-8")
|
||||||
|
}else if(validator.IsGBK(data)) {
|
||||||
|
fmt.Println("data encoding is GBK")
|
||||||
|
}
|
||||||
|
fmt.Println("data encoding is unknown")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### <span id="IsASCII">IsASCII</span>
|
||||||
|
|
||||||
|
<p>验证字符串全部为ASCII字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsASCII(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsASCII("ABC")
|
||||||
|
result2 := validator.IsASCII("123")
|
||||||
|
result3 := validator.IsASCII("")
|
||||||
|
result4 := validator.IsASCII("😄")
|
||||||
|
result5 := validator.IsASCII("你好")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsPrintable">IsPrintable</span>
|
||||||
|
|
||||||
|
<p>检查字符串是否全部为可打印字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsPrintable(str string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := validator.IsPrintable("ABC")
|
||||||
|
result2 := validator.IsPrintable("{id: 123}")
|
||||||
|
result3 := validator.IsPrintable("")
|
||||||
|
result4 := validator.IsPrintable("😄")
|
||||||
|
result5 := validator.IsPrintable("\u0000")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
119
fileutil/file.go
119
fileutil/file.go
@@ -7,7 +7,13 @@ package fileutil
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/sha512"
|
||||||
|
"encoding/csv"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -15,6 +21,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -220,7 +227,11 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
defer zipReader.Close()
|
defer zipReader.Close()
|
||||||
|
|
||||||
for _, f := range zipReader.File {
|
for _, f := range zipReader.File {
|
||||||
path := filepath.Join(destPath, f.Name)
|
//issue#62: fix ZipSlip bug
|
||||||
|
path, err := safeFilepathJoin(destPath, f.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if f.FileInfo().IsDir() {
|
if f.FileInfo().IsDir() {
|
||||||
os.MkdirAll(path, os.ModePerm)
|
os.MkdirAll(path, os.ModePerm)
|
||||||
} else {
|
} else {
|
||||||
@@ -249,6 +260,17 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func safeFilepathJoin(path1, path2 string) (string, error) {
|
||||||
|
relPath, err := filepath.Rel(".", path2)
|
||||||
|
if err != nil || strings.HasPrefix(relPath, "..") {
|
||||||
|
return "", fmt.Errorf("(zipslip) filepath is unsafe %q: %v", path2, err)
|
||||||
|
}
|
||||||
|
if path1 == "" {
|
||||||
|
path1 = "."
|
||||||
|
}
|
||||||
|
return filepath.Join(path1, filepath.Join("/", relPath)), nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsLink checks if a file is symbol link or not
|
// IsLink checks if a file is symbol link or not
|
||||||
func IsLink(path string) bool {
|
func IsLink(path string) bool {
|
||||||
fi, err := os.Lstat(path)
|
fi, err := os.Lstat(path)
|
||||||
@@ -302,3 +324,98 @@ func MiMeType(file interface{}) string {
|
|||||||
}
|
}
|
||||||
return mediatype
|
return mediatype
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CurrentPath return current absolute path.
|
||||||
|
func CurrentPath() string {
|
||||||
|
var absPath string
|
||||||
|
_, filename, _, ok := runtime.Caller(1)
|
||||||
|
if ok {
|
||||||
|
absPath = path.Dir(filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
return absPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsZipFile checks if file is zip or not.
|
||||||
|
func IsZipFile(filepath string) bool {
|
||||||
|
f, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
buf := make([]byte, 4)
|
||||||
|
if n, err := f.Read(buf); err != nil || n < 4 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes.Equal(buf, []byte("PK\x03\x04"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileSize returns file size in bytes.
|
||||||
|
func FileSize(path string) (int64, error) {
|
||||||
|
f, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return f.Size(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MTime returns file modified time.
|
||||||
|
func MTime(filepath string) (int64, error) {
|
||||||
|
f, err := os.Stat(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return f.ModTime().Unix(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MTime returns file sha value, param `shaType` should be 1, 256 or 512.
|
||||||
|
func Sha(filepath string, shaType ...int) (string, error) {
|
||||||
|
file, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
h := sha1.New()
|
||||||
|
if len(shaType) > 0 {
|
||||||
|
if shaType[0] == 1 {
|
||||||
|
h = sha1.New()
|
||||||
|
} else if shaType[0] == 256 {
|
||||||
|
h = sha256.New()
|
||||||
|
} else if shaType[0] == 512 {
|
||||||
|
h = sha512.New()
|
||||||
|
} else {
|
||||||
|
return "", errors.New("param `shaType` should be 1, 256 or 512.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(h, file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
sha := fmt.Sprintf("%x", h.Sum(nil))
|
||||||
|
|
||||||
|
return sha, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadCsvFile read file content into slice.
|
||||||
|
func ReadCsvFile(filepath string) ([][]string, error) {
|
||||||
|
f, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
csvReader := csv.NewReader(f)
|
||||||
|
records, err := csvReader.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return records, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -221,3 +221,59 @@ func TestMiMeType(t *testing.T) {
|
|||||||
assert.Equal("text/plain; charset=utf-8", MiMeType(f))
|
assert.Equal("text/plain; charset=utf-8", MiMeType(f))
|
||||||
assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go"))
|
assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCurrentPath(t *testing.T) {
|
||||||
|
absPath := CurrentPath()
|
||||||
|
t.Log(absPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsZipFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsZipFile")
|
||||||
|
|
||||||
|
assert.Equal(false, IsZipFile("./file.go"))
|
||||||
|
assert.Equal(true, IsZipFile("./testdata/file.go.zip"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileSize(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFileSize")
|
||||||
|
|
||||||
|
size, err := FileSize("./testdata/test.txt")
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(int64(20), size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMTime(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMTime")
|
||||||
|
|
||||||
|
mtime, err := MTime("./testdata/test.txt")
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(int64(1682478195), mtime)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSha(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSha")
|
||||||
|
|
||||||
|
sha1, err := Sha("./testdata/test.txt", 1)
|
||||||
|
sha256, err := Sha("./testdata/test.txt", 256)
|
||||||
|
sha512, err := Sha("./testdata/test.txt", 512)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal("dda3cf10c5a6ff6c6659a497bf7261b287af2bc7", sha1)
|
||||||
|
assert.Equal("aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35", sha256)
|
||||||
|
assert.Equal("d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870", sha512)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadCsvFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestReadCsvFile")
|
||||||
|
|
||||||
|
content, err := ReadCsvFile("./testdata/test.csv")
|
||||||
|
t.Log(content)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
assert.Equal(3, len(content))
|
||||||
|
assert.Equal(3, len(content[0]))
|
||||||
|
assert.Equal("Bob", content[0][0])
|
||||||
|
}
|
||||||
|
|||||||
BIN
fileutil/testdata/file.go.zip
vendored
Normal file
BIN
fileutil/testdata/file.go.zip
vendored
Normal file
Binary file not shown.
3
fileutil/testdata/test.csv
vendored
Normal file
3
fileutil/testdata/test.csv
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Bob, 12, male
|
||||||
|
Duke, 14, male
|
||||||
|
Lucy, 16, female
|
||||||
|
1
fileutil/testdata/test.txt
vendored
Normal file
1
fileutil/testdata/test.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
this is a test file.
|
||||||
@@ -4,14 +4,176 @@
|
|||||||
// Package formatter implements some functions to format string, struct.
|
// Package formatter implements some functions to format string, struct.
|
||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
// Comma add comma to number by every 3 numbers from right. ahead by symbol char
|
"github.com/duke-git/lancet/convertor"
|
||||||
func Comma(v interface{}, symbol string) string {
|
"github.com/duke-git/lancet/strutil"
|
||||||
s := numString(v)
|
"github.com/duke-git/lancet/validator"
|
||||||
dotIndex := strings.Index(s, ".")
|
)
|
||||||
if dotIndex != -1 {
|
|
||||||
return symbol + commaString(s[:dotIndex]) + s[dotIndex:]
|
// Comma add comma to a number value by every 3 numbers from right. ahead by symbol char.
|
||||||
|
// if value is invalid number string eg "aa", return empty string
|
||||||
|
// Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345"
|
||||||
|
func Comma(value interface{}, symbol string) string {
|
||||||
|
if validator.IsInt(value) {
|
||||||
|
v, err := convertor.ToInt(value)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return symbol + commaString(s)
|
return symbol + commaInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if validator.IsFloat(value) {
|
||||||
|
v, err := convertor.ToFloat(value)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return symbol + commaFloat(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strutil.IsString(value) {
|
||||||
|
v := fmt.Sprintf("%v", value)
|
||||||
|
if validator.IsNumberStr(v) {
|
||||||
|
return symbol + commaStr(v)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pretty data to JSON string.
|
||||||
|
func Pretty(v interface{}) (string, error) {
|
||||||
|
out, err := json.MarshalIndent(v, "", " ")
|
||||||
|
return string(out), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrettyToWriter pretty encode data to writer.
|
||||||
|
func PrettyToWriter(v interface{}, out io.Writer) error {
|
||||||
|
enc := json.NewEncoder(out)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
|
||||||
|
if err := enc.Encode(v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://en.wikipedia.org/wiki/Binary_prefix
|
||||||
|
const (
|
||||||
|
// Decimal
|
||||||
|
UnitB = 1
|
||||||
|
UnitKB = 1000
|
||||||
|
UnitMB = 1000 * UnitKB
|
||||||
|
UnitGB = 1000 * UnitMB
|
||||||
|
UnitTB = 1000 * UnitGB
|
||||||
|
UnitPB = 1000 * UnitTB
|
||||||
|
UnitEB = 1000 * UnitPB
|
||||||
|
|
||||||
|
// Binary
|
||||||
|
UnitBiB = 1
|
||||||
|
UnitKiB = 1024
|
||||||
|
UnitMiB = 1024 * UnitKiB
|
||||||
|
UnitGiB = 1024 * UnitMiB
|
||||||
|
UnitTiB = 1024 * UnitGiB
|
||||||
|
UnitPiB = 1024 * UnitTiB
|
||||||
|
UnitEiB = 1024 * UnitPiB
|
||||||
|
)
|
||||||
|
|
||||||
|
// type byteUnitMap map[byte]int64
|
||||||
|
|
||||||
|
var (
|
||||||
|
decimalByteMap = map[string]uint64{
|
||||||
|
"b": UnitB,
|
||||||
|
"kb": UnitKB,
|
||||||
|
"mb": UnitMB,
|
||||||
|
"gb": UnitGB,
|
||||||
|
"tb": UnitTB,
|
||||||
|
"pb": UnitPB,
|
||||||
|
"eb": UnitEB,
|
||||||
|
|
||||||
|
// Without suffix
|
||||||
|
"": UnitB,
|
||||||
|
"k": UnitKB,
|
||||||
|
"m": UnitMB,
|
||||||
|
"g": UnitGB,
|
||||||
|
"t": UnitTB,
|
||||||
|
"p": UnitPB,
|
||||||
|
"e": UnitEB,
|
||||||
|
}
|
||||||
|
|
||||||
|
binaryByteMap = map[string]uint64{
|
||||||
|
"bi": UnitBiB,
|
||||||
|
"kib": UnitKiB,
|
||||||
|
"mib": UnitMiB,
|
||||||
|
"gib": UnitGiB,
|
||||||
|
"tib": UnitTiB,
|
||||||
|
"pib": UnitPiB,
|
||||||
|
"eib": UnitEiB,
|
||||||
|
|
||||||
|
// Without suffix
|
||||||
|
"": UnitBiB,
|
||||||
|
"ki": UnitKiB,
|
||||||
|
"mi": UnitMiB,
|
||||||
|
"gi": UnitGiB,
|
||||||
|
"ti": UnitTiB,
|
||||||
|
"pi": UnitPiB,
|
||||||
|
"ei": UnitEiB,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
decimalByteUnits = []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||||||
|
binaryByteUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
||||||
|
)
|
||||||
|
|
||||||
|
// DecimalBytes returns a human readable byte size under decimal standard (base 1000)
|
||||||
|
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
|
||||||
|
// Play: https://go.dev/play/p/FPXs1suwRcs
|
||||||
|
func DecimalBytes(size float64, precision ...int) string {
|
||||||
|
p := 5
|
||||||
|
if len(precision) > 0 {
|
||||||
|
p = precision[0] + 1
|
||||||
|
}
|
||||||
|
size, unit := calculateByteSize(size, 1000.0, decimalByteUnits)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%.*g%s", p, size, unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BinaryBytes returns a human-readable byte size under binary standard (base 1024)
|
||||||
|
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
|
||||||
|
func BinaryBytes(size float64, precision ...int) string {
|
||||||
|
p := 5
|
||||||
|
if len(precision) > 0 {
|
||||||
|
p = precision[0] + 1
|
||||||
|
}
|
||||||
|
size, unit := calculateByteSize(size, 1024.0, binaryByteUnits)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%.*g%s", p, size, unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateByteSize(size float64, base float64, byteUnits []string) (float64, string) {
|
||||||
|
i := 0
|
||||||
|
unitsLimit := len(byteUnits) - 1
|
||||||
|
for size >= base && i < unitsLimit {
|
||||||
|
size = size / base
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return size, byteUnits[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDecimalBytes return the human readable bytes size string into the amount it represents(base 1000).
|
||||||
|
// ParseDecimalBytes("42 MB") -> 42000000, nil
|
||||||
|
func ParseDecimalBytes(size string) (uint64, error) {
|
||||||
|
return parseBytes(size, "decimal")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseBinaryBytes return the human readable bytes size string into the amount it represents(base 1024).
|
||||||
|
// ParseBinaryBytes("42 mib") -> 44040192, nil
|
||||||
|
func ParseBinaryBytes(size string) (uint64, error) {
|
||||||
|
return parseBytes(size, "binary")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,134 @@
|
|||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func commaString(s string) string {
|
// see https://github.com/dustin/go-humanize/blob/master/comma.go
|
||||||
|
func commaInt(v int64) string {
|
||||||
|
sign := ""
|
||||||
|
|
||||||
|
// Min int64 can't be negated to a usable value, so it has to be special cased.
|
||||||
|
if v == math.MinInt64 {
|
||||||
|
return "-9,223,372,036,854,775,808"
|
||||||
|
}
|
||||||
|
|
||||||
|
if v < 0 {
|
||||||
|
sign = "-"
|
||||||
|
v = 0 - v
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := []string{"", "", "", "", "", "", ""}
|
||||||
|
j := len(parts) - 1
|
||||||
|
|
||||||
|
for v > 999 {
|
||||||
|
parts[j] = strconv.FormatInt(v%1000, 10)
|
||||||
|
switch len(parts[j]) {
|
||||||
|
case 2:
|
||||||
|
parts[j] = "0" + parts[j]
|
||||||
|
case 1:
|
||||||
|
parts[j] = "00" + parts[j]
|
||||||
|
}
|
||||||
|
v = v / 1000
|
||||||
|
j--
|
||||||
|
}
|
||||||
|
parts[j] = strconv.Itoa(int(v))
|
||||||
|
return sign + strings.Join(parts[j:], ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaFloat(v float64) string {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
if v < 0 {
|
||||||
|
buf.Write([]byte{'-'})
|
||||||
|
v = 0 - v
|
||||||
|
}
|
||||||
|
|
||||||
|
comma := []byte{','}
|
||||||
|
|
||||||
|
parts := strings.Split(strconv.FormatFloat(v, 'f', -1, 64), ".")
|
||||||
|
pos := 0
|
||||||
|
if len(parts[0])%3 != 0 {
|
||||||
|
pos += len(parts[0]) % 3
|
||||||
|
buf.WriteString(parts[0][:pos])
|
||||||
|
buf.Write(comma)
|
||||||
|
}
|
||||||
|
for ; pos < len(parts[0]); pos += 3 {
|
||||||
|
buf.WriteString(parts[0][pos : pos+3])
|
||||||
|
buf.Write(comma)
|
||||||
|
}
|
||||||
|
buf.Truncate(buf.Len() - 1)
|
||||||
|
|
||||||
|
if len(parts) > 1 {
|
||||||
|
buf.Write([]byte{'.'})
|
||||||
|
buf.WriteString(parts[1])
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaStr(s string) string {
|
||||||
|
dotIndex := strings.Index(s, ".")
|
||||||
|
if dotIndex != -1 {
|
||||||
|
return commaStrRecursive(s[:dotIndex]) + s[dotIndex:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return commaStrRecursive(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func commaStrRecursive(s string) string {
|
||||||
if len(s) <= 3 {
|
if len(s) <= 3 {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:])
|
return commaStrRecursive(s[:len(s)-3]) + "," + commaStrRecursive(s[len(s)-3:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func numString(value interface{}) string {
|
// see https://github.com/dustin/go-humanize/blob/master/bytes.go
|
||||||
switch reflect.TypeOf(value).Kind() {
|
func parseBytes(s string, kind string) (uint64, error) {
|
||||||
case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64:
|
lastDigit := 0
|
||||||
return fmt.Sprintf("%v", value)
|
hasComma := false
|
||||||
case reflect.String:
|
for _, r := range s {
|
||||||
{
|
if !(unicode.IsDigit(r) || r == '.' || r == ',') {
|
||||||
sv := fmt.Sprintf("%v", value)
|
break
|
||||||
if strings.Contains(sv, ".") {
|
}
|
||||||
_, err := strconv.ParseFloat(sv, 64)
|
if r == ',' {
|
||||||
if err == nil {
|
hasComma = true
|
||||||
return sv
|
}
|
||||||
|
lastDigit++
|
||||||
|
}
|
||||||
|
|
||||||
|
num := s[:lastDigit]
|
||||||
|
if hasComma {
|
||||||
|
num = strings.Replace(num, ",", "", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := strconv.ParseFloat(num, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
|
||||||
|
|
||||||
|
if kind == "decimal" {
|
||||||
|
if m, ok := decimalByteMap[extra]; ok {
|
||||||
|
f *= float64(m)
|
||||||
|
if f >= math.MaxUint64 {
|
||||||
|
return 0, fmt.Errorf("too large: %v", s)
|
||||||
|
}
|
||||||
|
return uint64(f), nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := strconv.ParseInt(sv, 10, 64)
|
if m, ok := binaryByteMap[extra]; ok {
|
||||||
if err == nil {
|
f *= float64(m)
|
||||||
return sv
|
if f >= math.MaxUint64 {
|
||||||
|
return 0, fmt.Errorf("too large: %v", s)
|
||||||
|
}
|
||||||
|
return uint64(f), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
default:
|
return 0, fmt.Errorf("unhandled size name: %v", extra)
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -12,12 +13,148 @@ func TestComma(t *testing.T) {
|
|||||||
assert.Equal("", Comma("", ""))
|
assert.Equal("", Comma("", ""))
|
||||||
assert.Equal("", Comma("aa", ""))
|
assert.Equal("", Comma("aa", ""))
|
||||||
assert.Equal("", Comma("aa.a", ""))
|
assert.Equal("", Comma("aa.a", ""))
|
||||||
assert.Equal("", Comma([]int{1}, ""))
|
|
||||||
assert.Equal("123", Comma("123", ""))
|
assert.Equal("123", Comma("123", ""))
|
||||||
assert.Equal("12,345", Comma("12345", ""))
|
assert.Equal("12,345", Comma("12345", ""))
|
||||||
|
assert.Equal("12,345.6789", Comma("12345.6789", ""))
|
||||||
|
assert.Equal("123,456,789,000", Comma("123456789000", ""))
|
||||||
|
assert.Equal("12,345,678.9", Comma("12345678.9", ""))
|
||||||
|
|
||||||
assert.Equal("12,345", Comma(12345, ""))
|
assert.Equal("12,345", Comma(12345, ""))
|
||||||
assert.Equal("$12,345", Comma(12345, "$"))
|
assert.Equal("$12,345", Comma(12345, "$"))
|
||||||
assert.Equal("¥12,345", Comma(12345, "¥"))
|
assert.Equal("¥12,345", Comma(12345, "¥"))
|
||||||
assert.Equal("12,345.6789", Comma(12345.6789, ""))
|
assert.Equal("12,345.6789", Comma(12345.6789, ""))
|
||||||
|
assert.Equal("12,345.6789", Comma(+12345.6789, ""))
|
||||||
|
assert.Equal("12,345,678.9", Comma(12345678.9, ""))
|
||||||
|
assert.Equal("123,456,789,000", Comma(123456789000, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPretty(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPretty")
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
"",
|
||||||
|
"abc",
|
||||||
|
123,
|
||||||
|
[]string{"a", "b", "c"},
|
||||||
|
map[string]int{"a": 1},
|
||||||
|
struct {
|
||||||
|
Abc int `json:"abc"`
|
||||||
|
}{Abc: 123},
|
||||||
|
}
|
||||||
|
|
||||||
|
expects := []string{
|
||||||
|
"\"\"",
|
||||||
|
`"abc"`,
|
||||||
|
"123",
|
||||||
|
"[\n \"a\",\n \"b\",\n \"c\"\n]",
|
||||||
|
"{\n \"a\": 1\n}",
|
||||||
|
"{\n \"abc\": 123\n}",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range cases {
|
||||||
|
result, err := Pretty(v)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("result -> ", result)
|
||||||
|
assert.Equal(expects[i], result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrettyToWriter(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPrettyToWriter")
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Aage uint `json:"age"`
|
||||||
|
}
|
||||||
|
user := User{Name: "King", Aage: 10000}
|
||||||
|
|
||||||
|
expects := "{\n \"name\": \"King\",\n \"age\": 10000\n}\n"
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
err := PrettyToWriter(user, buf)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal(expects, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecimalBytes(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestDecimalBytes")
|
||||||
|
|
||||||
|
assert.Equal("1KB", DecimalBytes(1000))
|
||||||
|
assert.Equal("1.024KB", DecimalBytes(1024))
|
||||||
|
assert.Equal("1.2346MB", DecimalBytes(1234567))
|
||||||
|
assert.Equal("1.235MB", DecimalBytes(1234567, 3))
|
||||||
|
assert.Equal("1.123GB", DecimalBytes(float64(1.123*UnitGB)))
|
||||||
|
assert.Equal("2.123TB", DecimalBytes(float64(2.123*UnitTB)))
|
||||||
|
assert.Equal("3.123PB", DecimalBytes(float64(3.123*UnitPB)))
|
||||||
|
assert.Equal("4.123EB", DecimalBytes(float64(4.123*UnitEB)))
|
||||||
|
assert.Equal("1EB", DecimalBytes(float64(1000*UnitPB)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBinaryBytes(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBinaryBytes")
|
||||||
|
|
||||||
|
assert.Equal("1KiB", BinaryBytes(1024))
|
||||||
|
assert.Equal("1MiB", BinaryBytes(1024*1024))
|
||||||
|
assert.Equal("1.1774MiB", BinaryBytes(1234567))
|
||||||
|
assert.Equal("1.18MiB", BinaryBytes(1234567, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseDecimalBytes(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestParseDecimalBytes")
|
||||||
|
|
||||||
|
cases := map[string]uint64{
|
||||||
|
"12": uint64(12),
|
||||||
|
"12 k": uint64(12000),
|
||||||
|
"12 kb": uint64(12000),
|
||||||
|
"12kb": uint64(12000),
|
||||||
|
"12k": uint64(12000),
|
||||||
|
"12K": uint64(12000),
|
||||||
|
"12KB": uint64(12000),
|
||||||
|
"12 K": uint64(12000),
|
||||||
|
"12 KB": uint64(12000),
|
||||||
|
"12 Kb": uint64(12000),
|
||||||
|
"12 kB": uint64(12000),
|
||||||
|
"12.2 KB": uint64(12200),
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
result, err := ParseDecimalBytes(k)
|
||||||
|
assert.Equal(v, result)
|
||||||
|
assert.IsNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := ParseDecimalBytes("12 AB")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseBinaryBytes(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestParseBinaryBytes")
|
||||||
|
|
||||||
|
cases := map[string]uint64{
|
||||||
|
"12": uint64(12),
|
||||||
|
"12 ki": uint64(12288),
|
||||||
|
"12 kib": uint64(12288),
|
||||||
|
"12kib": uint64(12288),
|
||||||
|
"12ki": uint64(12288),
|
||||||
|
"12KI": uint64(12288),
|
||||||
|
"12KIB": uint64(12288),
|
||||||
|
"12KiB": uint64(12288),
|
||||||
|
"12 Ki": uint64(12288),
|
||||||
|
"12 KiB": uint64(12288),
|
||||||
|
"12 Kib": uint64(12288),
|
||||||
|
"12 kiB": uint64(12288),
|
||||||
|
"12.2 KiB": uint64(12492),
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
result, err := ParseBinaryBytes(k)
|
||||||
|
assert.Equal(v, result)
|
||||||
|
assert.IsNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := ParseDecimalBytes("12 AB")
|
||||||
|
assert.IsNotNil(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,3 +113,15 @@ func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
|||||||
|
|
||||||
return quit
|
return quit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pipeline takes a list of functions and returns a function whose param will be passed into
|
||||||
|
// the functions one by one.
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{} {
|
||||||
|
return func(arg interface{}) (result interface{}) {
|
||||||
|
result = arg
|
||||||
|
for _, fn := range funcs {
|
||||||
|
result = fn(result)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -130,3 +130,21 @@ func TestSchedule(t *testing.T) {
|
|||||||
expected := []string{"*", "*", "*", "*", "*"}
|
expected := []string{"*", "*", "*", "*", "*"}
|
||||||
assert.Equal(expected, res)
|
assert.Equal(expected, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPipeline(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPipeline")
|
||||||
|
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
assert.Equal(36, f(2))
|
||||||
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/duke-git/lancet
|
module github.com/duke-git/lancet
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
|
require golang.org/x/text v0.5.0
|
||||||
|
|||||||
25
go.sum
Normal file
25
go.sum
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
|
||||||
|
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
@@ -90,3 +90,39 @@ func TruncRound(x float64, n int) float64 {
|
|||||||
res, _ := strconv.ParseFloat(newFloat, 64)
|
res, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AngleToRadian converts angle value to radian value.
|
||||||
|
func AngleToRadian(angle float64) float64 {
|
||||||
|
radian := angle * (math.Pi / 180)
|
||||||
|
return radian
|
||||||
|
}
|
||||||
|
|
||||||
|
// RadianToAngle converts radian value to angle value.
|
||||||
|
func RadianToAngle(radian float64) float64 {
|
||||||
|
angle := radian * (180 / math.Pi)
|
||||||
|
return angle
|
||||||
|
}
|
||||||
|
|
||||||
|
// PointDistance get two points distance.
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64 {
|
||||||
|
a := x1 - x2
|
||||||
|
b := y1 - y2
|
||||||
|
c := math.Pow(a, 2) + math.Pow(b, 2)
|
||||||
|
|
||||||
|
return math.Sqrt(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsPrimes checks if number is prime number.
|
||||||
|
func IsPrime(n int) bool {
|
||||||
|
if n < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
|
||||||
|
if n%i == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mathutil
|
package mathutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -70,3 +71,46 @@ func TestTruncRound(t *testing.T) {
|
|||||||
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
||||||
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAngleToRadian(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAngleToRadian")
|
||||||
|
|
||||||
|
result1 := AngleToRadian(45)
|
||||||
|
result2 := AngleToRadian(90)
|
||||||
|
result3 := AngleToRadian(180)
|
||||||
|
|
||||||
|
assert.Equal(0.7853981633974483, result1)
|
||||||
|
assert.Equal(1.5707963267948966, result2)
|
||||||
|
assert.Equal(3.141592653589793, result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRadianToAngle(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAngleToRadian")
|
||||||
|
|
||||||
|
result1 := RadianToAngle(math.Pi)
|
||||||
|
result2 := RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
assert.Equal(float64(180), result1)
|
||||||
|
assert.Equal(float64(90), result2)
|
||||||
|
assert.Equal(float64(45), result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPointDistance(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPointDistance")
|
||||||
|
|
||||||
|
result1 := PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
assert.Equal(float64(5), result1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsPrime(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsPrime")
|
||||||
|
|
||||||
|
assert.Equal(false, IsPrime(-1))
|
||||||
|
assert.Equal(false, IsPrime(0))
|
||||||
|
assert.Equal(false, IsPrime(1))
|
||||||
|
assert.Equal(true, IsPrime(2))
|
||||||
|
assert.Equal(true, IsPrime(3))
|
||||||
|
assert.Equal(false, IsPrime(4))
|
||||||
|
}
|
||||||
|
|||||||
221
netutil/http.go
221
netutil/http.go
@@ -13,12 +13,21 @@
|
|||||||
package netutil
|
package netutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HttpGet send get http request
|
// HttpGet send get http request
|
||||||
@@ -77,3 +86,215 @@ func ConvertMapToQueryString(param map[string]interface{}) string {
|
|||||||
}
|
}
|
||||||
return build.String()
|
return build.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HttpRequest struct {
|
||||||
|
RawURL string
|
||||||
|
Method string
|
||||||
|
Headers http.Header
|
||||||
|
QueryParams url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// HttpClientConfig contains some configurations for http client
|
||||||
|
type HttpClientConfig struct {
|
||||||
|
SSLEnabled bool
|
||||||
|
TLSConfig *tls.Config
|
||||||
|
Compressed bool
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
ResponseTimeout time.Duration
|
||||||
|
Verbose bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultHttpClientConfig defalut client config
|
||||||
|
var defaultHttpClientConfig = &HttpClientConfig{
|
||||||
|
Compressed: false,
|
||||||
|
HandshakeTimeout: 20 * time.Second,
|
||||||
|
ResponseTimeout: 40 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
// HttpClient is used for sending http request
|
||||||
|
type HttpClient struct {
|
||||||
|
*http.Client
|
||||||
|
TLS *tls.Config
|
||||||
|
Request *http.Request
|
||||||
|
Config HttpClientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHttpClient make a HttpClient instance
|
||||||
|
func NewHttpClient() *HttpClient {
|
||||||
|
client := &HttpClient{
|
||||||
|
Client: &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
|
||||||
|
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
|
||||||
|
DisableCompression: !defaultHttpClientConfig.Compressed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Config: *defaultHttpClientConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHttpClientWithConfig make a HttpClient instance with pass config
|
||||||
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
||||||
|
if config == nil {
|
||||||
|
config = defaultHttpClientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &HttpClient{
|
||||||
|
Client: &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSHandshakeTimeout: config.HandshakeTimeout,
|
||||||
|
ResponseHeaderTimeout: config.ResponseTimeout,
|
||||||
|
DisableCompression: !config.Compressed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Config: *config,
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.SSLEnabled {
|
||||||
|
client.TLS = config.TLSConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendRequest send http request
|
||||||
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
|
||||||
|
err := validateRequest(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rawUrl := request.RawURL
|
||||||
|
|
||||||
|
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client.setTLS(rawUrl)
|
||||||
|
client.setHeader(req, request.Headers)
|
||||||
|
client.setQueryParam(req, rawUrl, request.QueryParams)
|
||||||
|
|
||||||
|
if request.FormData != nil {
|
||||||
|
client.setFormData(req, request.FormData)
|
||||||
|
}
|
||||||
|
|
||||||
|
client.Request = req
|
||||||
|
|
||||||
|
resp, err := client.Client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeResponse decode response into target object
|
||||||
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error {
|
||||||
|
if resp == nil {
|
||||||
|
return errors.New("invalid target param")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
return json.NewDecoder(resp.Body).Decode(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setTLS set http client transport TLSClientConfig
|
||||||
|
func (client *HttpClient) setTLS(rawUrl string) {
|
||||||
|
if strings.HasPrefix(rawUrl, "https") {
|
||||||
|
if transport, ok := client.Client.Transport.(*http.Transport); ok {
|
||||||
|
transport.TLSClientConfig = client.TLS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// setHeader set http rquest header
|
||||||
|
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
|
||||||
|
if headers == nil {
|
||||||
|
headers = make(http.Header)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := headers["Accept"]; !ok {
|
||||||
|
headers["Accept"] = []string{"*/*"}
|
||||||
|
}
|
||||||
|
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
|
||||||
|
headers["Accept-Encoding"] = []string{"deflate, gzip"}
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header = headers
|
||||||
|
}
|
||||||
|
|
||||||
|
// setQueryParam set http request query string param
|
||||||
|
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
|
||||||
|
if queryParam != nil {
|
||||||
|
if !strings.Contains(reqUrl, "?") {
|
||||||
|
reqUrl = reqUrl + "?" + queryParam.Encode()
|
||||||
|
} else {
|
||||||
|
reqUrl = reqUrl + "&" + queryParam.Encode()
|
||||||
|
}
|
||||||
|
u, err := url.Parse(reqUrl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.URL = u
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
|
||||||
|
formData := []byte(values.Encode())
|
||||||
|
req.Body = ioutil.NopCloser(bytes.NewReader(formData))
|
||||||
|
req.ContentLength = int64(len(formData))
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateRequest check if a request has url, and valid method.
|
||||||
|
func validateRequest(req *HttpRequest) error {
|
||||||
|
if req.RawURL == "" {
|
||||||
|
return errors.New("invalid request url")
|
||||||
|
}
|
||||||
|
|
||||||
|
// common HTTP methods
|
||||||
|
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
|
||||||
|
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
|
||||||
|
|
||||||
|
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
|
||||||
|
return errors.New("invalid request method")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StructToUrlValues convert struct to url valuse,
|
||||||
|
// only convert the field which is exported and has `json` tag
|
||||||
|
func StructToUrlValues(targetStruct interface{}) url.Values {
|
||||||
|
rv := reflect.ValueOf(targetStruct)
|
||||||
|
rt := reflect.TypeOf(targetStruct)
|
||||||
|
|
||||||
|
if rt.Kind() == reflect.Ptr {
|
||||||
|
rt = rt.Elem()
|
||||||
|
}
|
||||||
|
if rt.Kind() != reflect.Struct {
|
||||||
|
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
|
||||||
|
}
|
||||||
|
|
||||||
|
result := url.Values{}
|
||||||
|
|
||||||
|
fieldNum := rt.NumField()
|
||||||
|
pattern := `^[A-Z]`
|
||||||
|
regex := regexp.MustCompile(pattern)
|
||||||
|
for i := 0; i < fieldNum; i++ {
|
||||||
|
name := rt.Field(i).Name
|
||||||
|
tag := rt.Field(i).Tag.Get("json")
|
||||||
|
if regex.MatchString(name) && tag != "" {
|
||||||
|
if strings.Contains(tag, "omitempty") {
|
||||||
|
tag = strings.Split(tag, ",")[0]
|
||||||
|
}
|
||||||
|
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,228 +0,0 @@
|
|||||||
package netutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"reflect"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/duke-git/lancet/slice"
|
|
||||||
)
|
|
||||||
|
|
||||||
// HttpRequest struct is a composed http request
|
|
||||||
type HttpRequest struct {
|
|
||||||
RawURL string
|
|
||||||
Method string
|
|
||||||
Headers http.Header
|
|
||||||
QueryParams url.Values
|
|
||||||
FormData url.Values
|
|
||||||
Body []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// HttpClientConfig contains some configurations for http client
|
|
||||||
type HttpClientConfig struct {
|
|
||||||
SSLEnabled bool
|
|
||||||
TLSConfig *tls.Config
|
|
||||||
Compressed bool
|
|
||||||
HandshakeTimeout time.Duration
|
|
||||||
ResponseTimeout time.Duration
|
|
||||||
Verbose bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// defaultHttpClientConfig defalut client config
|
|
||||||
var defaultHttpClientConfig = &HttpClientConfig{
|
|
||||||
Compressed: false,
|
|
||||||
HandshakeTimeout: 20 * time.Second,
|
|
||||||
ResponseTimeout: 40 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
// HttpClient is used for sending http request
|
|
||||||
type HttpClient struct {
|
|
||||||
*http.Client
|
|
||||||
TLS *tls.Config
|
|
||||||
Request *http.Request
|
|
||||||
Config HttpClientConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHttpClient make a HttpClient instance
|
|
||||||
func NewHttpClient() *HttpClient {
|
|
||||||
client := &HttpClient{
|
|
||||||
Client: &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
|
|
||||||
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
|
|
||||||
DisableCompression: !defaultHttpClientConfig.Compressed,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Config: *defaultHttpClientConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHttpClientWithConfig make a HttpClient instance with pass config
|
|
||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
|
||||||
if config == nil {
|
|
||||||
config = defaultHttpClientConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &HttpClient{
|
|
||||||
Client: &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSHandshakeTimeout: config.HandshakeTimeout,
|
|
||||||
ResponseHeaderTimeout: config.ResponseTimeout,
|
|
||||||
DisableCompression: !config.Compressed,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Config: *config,
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.SSLEnabled {
|
|
||||||
client.TLS = config.TLSConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendRequest send http request
|
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
|
|
||||||
err := validateRequest(request)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rawUrl := request.RawURL
|
|
||||||
|
|
||||||
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
client.setTLS(rawUrl)
|
|
||||||
client.setHeader(req, request.Headers)
|
|
||||||
client.setQueryParam(req, rawUrl, request.QueryParams)
|
|
||||||
|
|
||||||
if request.FormData != nil {
|
|
||||||
client.setFormData(req, request.FormData)
|
|
||||||
}
|
|
||||||
|
|
||||||
client.Request = req
|
|
||||||
|
|
||||||
resp, err := client.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DecodeResponse decode response into target object
|
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error {
|
|
||||||
if resp == nil {
|
|
||||||
return errors.New("invalid target param")
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
return json.NewDecoder(resp.Body).Decode(target)
|
|
||||||
}
|
|
||||||
|
|
||||||
// setTLS set http client transport TLSClientConfig
|
|
||||||
func (client *HttpClient) setTLS(rawUrl string) {
|
|
||||||
if strings.HasPrefix(rawUrl, "https") {
|
|
||||||
if transport, ok := client.Client.Transport.(*http.Transport); ok {
|
|
||||||
transport.TLSClientConfig = client.TLS
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setHeader set http rquest header
|
|
||||||
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
|
|
||||||
if headers == nil {
|
|
||||||
headers = make(http.Header)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := headers["Accept"]; !ok {
|
|
||||||
headers["Accept"] = []string{"*/*"}
|
|
||||||
}
|
|
||||||
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
|
|
||||||
headers["Accept-Encoding"] = []string{"deflate, gzip"}
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header = headers
|
|
||||||
}
|
|
||||||
|
|
||||||
// setQueryParam set http request query string param
|
|
||||||
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
|
|
||||||
if queryParam != nil {
|
|
||||||
if !strings.Contains(reqUrl, "?") {
|
|
||||||
reqUrl = reqUrl + "?" + queryParam.Encode()
|
|
||||||
} else {
|
|
||||||
reqUrl = reqUrl + "&" + queryParam.Encode()
|
|
||||||
}
|
|
||||||
u, err := url.Parse(reqUrl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.URL = u
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
|
|
||||||
formData := []byte(values.Encode())
|
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(formData))
|
|
||||||
req.ContentLength = int64(len(formData))
|
|
||||||
}
|
|
||||||
|
|
||||||
// validateRequest check if a request has url, and valid method.
|
|
||||||
func validateRequest(req *HttpRequest) error {
|
|
||||||
if req.RawURL == "" {
|
|
||||||
return errors.New("invalid request url")
|
|
||||||
}
|
|
||||||
|
|
||||||
// common HTTP methods
|
|
||||||
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
|
|
||||||
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
|
|
||||||
|
|
||||||
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
|
|
||||||
return errors.New("invalid request method")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// StructToUrlValues convert struct to url valuse,
|
|
||||||
// only convert the field which is exported and has `json` tag
|
|
||||||
func StructToUrlValues(targetStruct interface{}) url.Values {
|
|
||||||
rv := reflect.ValueOf(targetStruct)
|
|
||||||
rt := reflect.TypeOf(targetStruct)
|
|
||||||
|
|
||||||
if rt.Kind() == reflect.Ptr {
|
|
||||||
rt = rt.Elem()
|
|
||||||
}
|
|
||||||
if rt.Kind() != reflect.Struct {
|
|
||||||
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
|
|
||||||
}
|
|
||||||
|
|
||||||
result := url.Values{}
|
|
||||||
|
|
||||||
fieldNum := rt.NumField()
|
|
||||||
pattern := `^[A-Z]`
|
|
||||||
regex := regexp.MustCompile(pattern)
|
|
||||||
for i := 0; i < fieldNum; i++ {
|
|
||||||
name := rt.Field(i).Name
|
|
||||||
tag := rt.Field(i).Tag.Get("json")
|
|
||||||
if regex.MatchString(name) && tag != "" {
|
|
||||||
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package netutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHttpClient_Get(t *testing.T) {
|
|
||||||
assert := internal.NewAssert(t, "TestHttpClient_Get")
|
|
||||||
|
|
||||||
request := &HttpRequest{
|
|
||||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
|
||||||
Method: "GET",
|
|
||||||
}
|
|
||||||
|
|
||||||
httpClient := NewHttpClient()
|
|
||||||
resp, err := httpClient.SendRequest(request)
|
|
||||||
if err != nil || resp.StatusCode != 200 {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Completed bool `json:"completed"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var todo Todo
|
|
||||||
httpClient.DecodeResponse(resp, &todo)
|
|
||||||
|
|
||||||
assert.Equal(1, todo.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHttpClent_Post(t *testing.T) {
|
|
||||||
header := http.Header{}
|
|
||||||
header.Add("Content-Type", "multipart/form-data")
|
|
||||||
|
|
||||||
postData := url.Values{}
|
|
||||||
postData.Add("userId", "1")
|
|
||||||
postData.Add("title", "testItem")
|
|
||||||
|
|
||||||
request := &HttpRequest{
|
|
||||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
|
||||||
Method: "POST",
|
|
||||||
Headers: header,
|
|
||||||
FormData: postData,
|
|
||||||
}
|
|
||||||
|
|
||||||
httpClient := NewHttpClient()
|
|
||||||
resp, err := httpClient.SendRequest(request)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStructToUrlValues(t *testing.T) {
|
|
||||||
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
|
||||||
|
|
||||||
type TodoQuery struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
UserId int `json:"userId"`
|
|
||||||
}
|
|
||||||
todoQuery := TodoQuery{
|
|
||||||
Id: 1,
|
|
||||||
UserId: 1,
|
|
||||||
}
|
|
||||||
todoValues := StructToUrlValues(todoQuery)
|
|
||||||
|
|
||||||
assert.Equal("1", todoValues.Get("id"))
|
|
||||||
assert.Equal("1", todoValues.Get("userId"))
|
|
||||||
|
|
||||||
request := &HttpRequest{
|
|
||||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
|
||||||
Method: "GET",
|
|
||||||
QueryParams: todoValues,
|
|
||||||
}
|
|
||||||
|
|
||||||
httpClient := NewHttpClient()
|
|
||||||
resp, err := httpClient.SendRequest(request)
|
|
||||||
if err != nil || resp.StatusCode != 200 {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
t.Log("response: ", string(body))
|
|
||||||
}
|
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -163,3 +164,88 @@ func TestParseResponse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log("response: ", toDoResp)
|
t.Log("response: ", toDoResp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHttpClient_Get(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHttpClient_Get")
|
||||||
|
|
||||||
|
request := &HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||||
|
Method: "GET",
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil || resp.StatusCode != 200 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Id int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var todo Todo
|
||||||
|
httpClient.DecodeResponse(resp, &todo)
|
||||||
|
|
||||||
|
assert.Equal(1, todo.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHttpClent_Post(t *testing.T) {
|
||||||
|
header := http.Header{}
|
||||||
|
header.Add("Content-Type", "multipart/form-data")
|
||||||
|
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "testItem")
|
||||||
|
|
||||||
|
request := &HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||||
|
Method: "POST",
|
||||||
|
Headers: header,
|
||||||
|
FormData: postData,
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructToUrlValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
||||||
|
|
||||||
|
type TodoQuery struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string
|
||||||
|
}
|
||||||
|
item1 := TodoQuery{
|
||||||
|
Id: 1,
|
||||||
|
UserId: 123,
|
||||||
|
Name: "test",
|
||||||
|
Status: "completed",
|
||||||
|
}
|
||||||
|
queryValues1 := StructToUrlValues(item1)
|
||||||
|
|
||||||
|
assert.Equal("1", queryValues1.Get("id"))
|
||||||
|
assert.Equal("123", queryValues1.Get("userId"))
|
||||||
|
assert.Equal("test", queryValues1.Get("name"))
|
||||||
|
assert.Equal("", queryValues1.Get("status"))
|
||||||
|
|
||||||
|
item2 := TodoQuery{
|
||||||
|
Id: 2,
|
||||||
|
UserId: 456,
|
||||||
|
}
|
||||||
|
queryValues2 := StructToUrlValues(item2)
|
||||||
|
|
||||||
|
assert.Equal("2", queryValues2.Get("id"))
|
||||||
|
assert.Equal("456", queryValues2.Get("userId"))
|
||||||
|
assert.Equal("", queryValues2.Get("name"))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
package netutil
|
package netutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetInternalIp return internal ipv4
|
// GetInternalIp return internal ipv4
|
||||||
@@ -170,3 +179,87 @@ func EncodeUrl(urlStr string) (string, error) {
|
|||||||
|
|
||||||
return URL.String(), nil
|
return URL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DownloadFile will upload the file to a server.
|
||||||
|
func UploadFile(filepath string, server string) (bool, error) {
|
||||||
|
if !fileutil.IsExist(filepath) {
|
||||||
|
return false, errors.New("file not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInfo, err := os.Stat(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyBuffer := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(bodyBuffer)
|
||||||
|
|
||||||
|
formFile, err := writer.CreateFormFile("uploadfile", fileInfo.Name())
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
srcFile, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(formFile, srcFile)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := writer.FormDataContentType()
|
||||||
|
writer.Close()
|
||||||
|
|
||||||
|
_, err = http.Post(server, contentType, bodyBuffer)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadFile will download the file exist in url to a local file.
|
||||||
|
func DownloadFile(filepath string, url string) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
out, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsPingConnected checks if can ping specified host or not.
|
||||||
|
func IsPingConnected(host string) bool {
|
||||||
|
cmd := exec.Command("ping", host, "-c", "1", "-W", "6")
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsTelnetConnected checks if can telnet specified host or not.
|
||||||
|
func IsTelnetConnected(host string, port string) bool {
|
||||||
|
adder := host + ":" + port
|
||||||
|
conn, err := net.DialTimeout("tcp", adder, 5*time.Second)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,14 +15,14 @@ func TestGetInternalIp(t *testing.T) {
|
|||||||
assert.IsNotNil(ip)
|
assert.IsNotNil(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPublicIpInfo(t *testing.T) {
|
// func TestGetPublicIpInfo(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestGetPublicIpInfo")
|
// assert := internal.NewAssert(t, "TestGetPublicIpInfo")
|
||||||
|
|
||||||
publicIpInfo, err := GetPublicIpInfo()
|
// publicIpInfo, err := GetPublicIpInfo()
|
||||||
assert.IsNil(err)
|
// assert.IsNil(err)
|
||||||
|
|
||||||
t.Logf("public ip info is: %+v \n", *publicIpInfo)
|
// t.Logf("public ip info is: %+v \n", *publicIpInfo)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func TestIsPublicIP(t *testing.T) {
|
func TestIsPublicIP(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsPublicIP")
|
assert := internal.NewAssert(t, "TestIsPublicIP")
|
||||||
@@ -52,3 +52,30 @@ func TestGetMacAddrs(t *testing.T) {
|
|||||||
macAddrs := GetMacAddrs()
|
macAddrs := GetMacAddrs()
|
||||||
t.Log(macAddrs)
|
t.Log(macAddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsPingConnected(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsPingConnected")
|
||||||
|
|
||||||
|
result1 := IsPingConnected("www.baidu.com")
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
|
||||||
|
result2 := IsPingConnected("www.!@#&&&.com")
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTelnetConnected(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTelnetConnected")
|
||||||
|
|
||||||
|
result1 := IsTelnetConnected("www.baidu.com", "80")
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
|
||||||
|
result2 := IsTelnetConnected("www.baidu.com", "123")
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// func TestDownloadFile(t *testing.T) {
|
||||||
|
// assert := internal.NewAssert(t, "TestDownloadFile")
|
||||||
|
|
||||||
|
// err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b")
|
||||||
|
// assert.IsNil(err)
|
||||||
|
// }
|
||||||
|
|||||||
@@ -19,16 +19,21 @@ const (
|
|||||||
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
// RandInt generate random int between min and max, maybe min, not be max
|
// RandInt generate random int between min and max, maybe min, not be max
|
||||||
func RandInt(min, max int) int {
|
func RandInt(min, max int) int {
|
||||||
if min == max {
|
if min == max {
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|
||||||
if max < min {
|
if max < min {
|
||||||
min, max = max, min
|
min, max = max, min
|
||||||
}
|
}
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
return r.Intn(max-min) + min
|
return rand.Intn(max-min) + min
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandBytes generate random byte slice
|
// RandBytes generate random byte slice
|
||||||
@@ -72,9 +77,12 @@ func RandNumeralOrLetter(length int) string {
|
|||||||
// random generate a random string based on given string range
|
// random generate a random string based on given string range
|
||||||
func random(s string, length int) string {
|
func random(s string, length int) string {
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
// fix: https://github.com/duke-git/lancet/issues/75
|
||||||
|
// r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = s[r.Int63()%int64(len(s))]
|
b[i] = s[rand.Int63()%int64(len(s))]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,53 +5,43 @@
|
|||||||
package strutil
|
package strutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CamelCase covert string to camelCase string.
|
// CamelCase covert string to camelCase string.
|
||||||
|
// non letters and numbers will be ignored
|
||||||
|
// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
|
||||||
func CamelCase(s string) string {
|
func CamelCase(s string) string {
|
||||||
if len(s) == 0 {
|
var builder strings.Builder
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
res := ""
|
strs := splitIntoStrings(s, false)
|
||||||
blankSpace := " "
|
for i, str := range strs {
|
||||||
regex, _ := regexp.Compile("[-_&]+")
|
|
||||||
ss := regex.ReplaceAllString(s, blankSpace)
|
|
||||||
for i, v := range strings.Split(ss, blankSpace) {
|
|
||||||
vv := []rune(v)
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
if vv[i] >= 65 && vv[i] <= 96 {
|
builder.WriteString(strings.ToLower(str))
|
||||||
vv[0] += 32
|
|
||||||
}
|
|
||||||
res += string(vv)
|
|
||||||
} else {
|
} else {
|
||||||
res += Capitalize(v)
|
builder.WriteString(Capitalize(str))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
||||||
func Capitalize(s string) string {
|
func Capitalize(s string) string {
|
||||||
if len(s) == 0 {
|
result := make([]rune, len(s))
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
out := make([]rune, len(s))
|
|
||||||
for i, v := range s {
|
for i, v := range s {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
out[i] = unicode.ToUpper(v)
|
result[i] = unicode.ToUpper(v)
|
||||||
} else {
|
} else {
|
||||||
out[i] = unicode.ToLower(v)
|
result[i] = unicode.ToLower(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(out)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpperFirst converts the first character of string to upper case.
|
// UpperFirst converts the first character of string to upper case.
|
||||||
@@ -78,86 +68,54 @@ func LowerFirst(s string) string {
|
|||||||
return string(r) + s[size:]
|
return string(r) + s[size:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadEnd pads string on the right side if it's shorter than size.
|
// PadStart pads string on the left and right side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
func PadEnd(source string, size int, padStr string) string {
|
func Pad(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 0)
|
||||||
len2 := len(padStr)
|
|
||||||
|
|
||||||
if len1 >= size {
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
|
|
||||||
fill := ""
|
|
||||||
if len2 >= size-len1 {
|
|
||||||
fill = padStr[0 : size-len1]
|
|
||||||
} else {
|
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return source + fill[0:size-len1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadStart pads string on the left side if it's shorter than size.
|
// PadStart pads string on the left side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
func PadStart(source string, size int, padStr string) string {
|
func PadStart(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 1)
|
||||||
len2 := len(padStr)
|
|
||||||
|
|
||||||
if len1 >= size {
|
|
||||||
return source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fill := ""
|
// PadEnd pads string on the right side if it's shorter than size.
|
||||||
if len2 >= size-len1 {
|
// Padding characters are truncated if they exceed size.
|
||||||
fill = padStr[0 : size-len1]
|
func PadEnd(source string, size int, padStr string) string {
|
||||||
} else {
|
return padAtPosition(source, size, padStr, 2)
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return fill[0:size-len1] + source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KebabCase covert string to kebab-case
|
// KebabCase covert string to kebab-case
|
||||||
|
// non letters and numbers will be ignored
|
||||||
|
// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
|
||||||
func KebabCase(s string) string {
|
func KebabCase(s string) string {
|
||||||
if len(s) == 0 {
|
result := splitIntoStrings(s, false)
|
||||||
return ""
|
return strings.Join(result, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
regex := regexp.MustCompile(`[\W|_]+`)
|
// UpperKebabCase covert string to upper KEBAB-CASE
|
||||||
blankSpace := " "
|
// non letters and numbers will be ignored
|
||||||
match := regex.ReplaceAllString(s, blankSpace)
|
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
|
||||||
rs := strings.Split(match, blankSpace)
|
func UpperKebabCase(s string) string {
|
||||||
|
result := splitIntoStrings(s, true)
|
||||||
var res []string
|
return strings.Join(result, "-")
|
||||||
for _, v := range rs {
|
|
||||||
splitWords := splitWordsToLower(v)
|
|
||||||
if len(splitWords) > 0 {
|
|
||||||
res = append(res, splitWords...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(res, "-")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SnakeCase covert string to snake_case
|
// SnakeCase covert string to snake_case
|
||||||
|
// non letters and numbers will be ignored
|
||||||
|
// eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar"
|
||||||
func SnakeCase(s string) string {
|
func SnakeCase(s string) string {
|
||||||
if len(s) == 0 {
|
result := splitIntoStrings(s, false)
|
||||||
return ""
|
return strings.Join(result, "_")
|
||||||
}
|
}
|
||||||
|
|
||||||
regex := regexp.MustCompile(`[\W|_]+`)
|
// UpperSnakeCase covert string to upper SNAKE_CASE
|
||||||
blankSpace := " "
|
// non letters and numbers will be ignored
|
||||||
match := regex.ReplaceAllString(s, blankSpace)
|
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"
|
||||||
rs := strings.Split(match, blankSpace)
|
func UpperSnakeCase(s string) string {
|
||||||
|
result := splitIntoStrings(s, true)
|
||||||
var res []string
|
return strings.Join(result, "_")
|
||||||
for _, v := range rs {
|
|
||||||
splitWords := splitWordsToLower(v)
|
|
||||||
if len(splitWords) > 0 {
|
|
||||||
res = append(res, splitWords...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(res, "_")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before create substring in source string before position when char first appear
|
// Before create substring in source string before position when char first appear
|
||||||
@@ -297,3 +255,151 @@ func SplitEx(s, sep string, removeEmptyString bool) []string {
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitWords splits a string into words, word only contains alphabetic characters.
|
||||||
|
func SplitWords(s string) []string {
|
||||||
|
var word string
|
||||||
|
var words []string
|
||||||
|
var r rune
|
||||||
|
var size, pos int
|
||||||
|
|
||||||
|
isWord := false
|
||||||
|
|
||||||
|
for len(s) > 0 {
|
||||||
|
r, size = utf8.DecodeRuneInString(s)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case isLetter(r):
|
||||||
|
if !isWord {
|
||||||
|
isWord = true
|
||||||
|
word = s
|
||||||
|
pos = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
case isWord && (r == '\'' || r == '-'):
|
||||||
|
// is word
|
||||||
|
|
||||||
|
default:
|
||||||
|
if isWord {
|
||||||
|
isWord = false
|
||||||
|
words = append(words, word[:pos])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += size
|
||||||
|
s = s[size:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if isWord {
|
||||||
|
words = append(words, word[:pos])
|
||||||
|
}
|
||||||
|
|
||||||
|
return words
|
||||||
|
}
|
||||||
|
|
||||||
|
// WordCount return the number of meaningful word, word only contains alphabetic characters.
|
||||||
|
func WordCount(s string) int {
|
||||||
|
var r rune
|
||||||
|
var size, count int
|
||||||
|
|
||||||
|
isWord := false
|
||||||
|
|
||||||
|
for len(s) > 0 {
|
||||||
|
r, size = utf8.DecodeRuneInString(s)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case isLetter(r):
|
||||||
|
if !isWord {
|
||||||
|
isWord = true
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
case isWord && (r == '\'' || r == '-'):
|
||||||
|
// is word
|
||||||
|
|
||||||
|
default:
|
||||||
|
isWord = false
|
||||||
|
}
|
||||||
|
|
||||||
|
s = s[size:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveNonPrintable remove non-printable characters from a string.
|
||||||
|
func RemoveNonPrintable(str string) string {
|
||||||
|
result := strings.Map(func(r rune) rune {
|
||||||
|
if unicode.IsPrint(r) {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}, str)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringToBytes converts a string to byte slice without a memory allocation.
|
||||||
|
func StringToBytes(str string) (b []byte) {
|
||||||
|
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str))
|
||||||
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||||
|
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// BytesToString converts a byte slice to string without a memory allocation.
|
||||||
|
// Play: todo
|
||||||
|
func BytesToString(bytes []byte) string {
|
||||||
|
return *(*string)(unsafe.Pointer(&bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsBlank checks if a string is whitespace, empty.
|
||||||
|
func IsBlank(str string) bool {
|
||||||
|
if len(str) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// memory copies will occur here, but UTF8 will be compatible
|
||||||
|
runes := []rune(str)
|
||||||
|
for _, r := range runes {
|
||||||
|
if !unicode.IsSpace(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasPrefixAny check if a string starts with any of an array of specified strings.
|
||||||
|
func HasPrefixAny(str string, prefixes []string) bool {
|
||||||
|
if len(str) == 0 || len(prefixes) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, prefix := range prefixes {
|
||||||
|
if strings.HasPrefix(str, prefix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasSuffixAny check if a string ends with any of an array of specified strings.
|
||||||
|
func HasSuffixAny(str string, suffixes []string) bool {
|
||||||
|
if len(str) == 0 || len(suffixes) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, suffix := range suffixes {
|
||||||
|
if strings.HasSuffix(str, suffix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func IndexOffset(str string, substr string, idxFrom int) int {
|
||||||
|
if idxFrom > len(str)-1 || idxFrom < 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Index(str[idxFrom:], substr) + idxFrom
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,40 +1,167 @@
|
|||||||
package strutil
|
package strutil
|
||||||
|
|
||||||
import "strings"
|
import "unicode"
|
||||||
|
|
||||||
// splitWordsToLower split a string into worlds by uppercase char
|
func splitIntoStrings(s string, upperCase bool) []string {
|
||||||
func splitWordsToLower(s string) []string {
|
var runes [][]rune
|
||||||
var res []string
|
lastCharType := 0
|
||||||
|
charType := 0
|
||||||
|
|
||||||
upperIndexes := upperIndex(s)
|
// split into fields based on type of unicode character
|
||||||
l := len(upperIndexes)
|
for _, r := range s {
|
||||||
if upperIndexes == nil || l == 0 {
|
switch true {
|
||||||
if s != "" {
|
case isLower(r):
|
||||||
res = append(res, s)
|
charType = 1
|
||||||
|
case isUpper(r):
|
||||||
|
charType = 2
|
||||||
|
case isDigit(r):
|
||||||
|
charType = 3
|
||||||
|
default:
|
||||||
|
charType = 4
|
||||||
}
|
}
|
||||||
return res
|
|
||||||
}
|
if charType == lastCharType {
|
||||||
for i := 0; i < l; i++ {
|
runes[len(runes)-1] = append(runes[len(runes)-1], r)
|
||||||
if i < l-1 {
|
|
||||||
res = append(res, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]]))
|
|
||||||
} else {
|
} else {
|
||||||
res = append(res, strings.ToLower(s[upperIndexes[i]:]))
|
runes = append(runes, []rune{r})
|
||||||
}
|
}
|
||||||
}
|
lastCharType = charType
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// upperIndex get a int slice which elements are all the uppercase char index of a string
|
for i := 0; i < len(runes)-1; i++ {
|
||||||
func upperIndex(s string) []int {
|
if isUpper(runes[i][0]) && isLower(runes[i+1][0]) {
|
||||||
var res []int
|
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
|
||||||
for i := 0; i < len(s); i++ {
|
runes[i] = runes[i][:len(runes[i])-1]
|
||||||
if 64 < s[i] && s[i] < 91 {
|
|
||||||
res = append(res, i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(s) > 0 && res != nil && res[0] != 0 {
|
|
||||||
res = append([]int{0}, res...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
// filter all none letters and none digit
|
||||||
|
var result []string
|
||||||
|
for _, rs := range runes {
|
||||||
|
if len(rs) > 0 && (unicode.IsLetter(rs[0]) || isDigit(rs[0])) {
|
||||||
|
if upperCase {
|
||||||
|
result = append(result, string(toUpperAll(rs)))
|
||||||
|
} else {
|
||||||
|
result = append(result, string(toLowerAll(rs)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// isDigit checks if a character is digit ('0' to '9')
|
||||||
|
func isDigit(r rune) bool {
|
||||||
|
return r >= '0' && r <= '9'
|
||||||
|
}
|
||||||
|
|
||||||
|
// isLower checks if a character is lower case ('a' to 'z')
|
||||||
|
func isLower(r rune) bool {
|
||||||
|
return r >= 'a' && r <= 'z'
|
||||||
|
}
|
||||||
|
|
||||||
|
// isUpper checks if a character is upper case ('A' to 'Z')
|
||||||
|
func isUpper(r rune) bool {
|
||||||
|
return r >= 'A' && r <= 'Z'
|
||||||
|
}
|
||||||
|
|
||||||
|
// toLower converts a character 'A' to 'Z' to its lower case
|
||||||
|
func toLower(r rune) rune {
|
||||||
|
if r >= 'A' && r <= 'Z' {
|
||||||
|
return r + 32
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// toLowerAll converts a character 'A' to 'Z' to its lower case
|
||||||
|
func toLowerAll(rs []rune) []rune {
|
||||||
|
for i := range rs {
|
||||||
|
rs[i] = toLower(rs[i])
|
||||||
|
}
|
||||||
|
return rs
|
||||||
|
}
|
||||||
|
|
||||||
|
// toUpper converts a character 'a' to 'z' to its upper case
|
||||||
|
func toUpper(r rune) rune {
|
||||||
|
if r >= 'a' && r <= 'z' {
|
||||||
|
return r - 32
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// toUpperAll converts a character 'a' to 'z' to its upper case
|
||||||
|
func toUpperAll(rs []rune) []rune {
|
||||||
|
for i := range rs {
|
||||||
|
rs[i] = toUpper(rs[i])
|
||||||
|
}
|
||||||
|
return rs
|
||||||
|
}
|
||||||
|
|
||||||
|
func padAtPosition(str string, length int, padStr string, position int) string {
|
||||||
|
if len(str) >= length {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
if padStr == "" {
|
||||||
|
padStr = " "
|
||||||
|
}
|
||||||
|
|
||||||
|
length = length - len(str)
|
||||||
|
startPadLen := 0
|
||||||
|
if position == 0 {
|
||||||
|
startPadLen = length / 2
|
||||||
|
} else if position == 1 {
|
||||||
|
startPadLen = length
|
||||||
|
}
|
||||||
|
endPadLen := length - startPadLen
|
||||||
|
|
||||||
|
charLen := len(padStr)
|
||||||
|
leftPad := ""
|
||||||
|
cur := 0
|
||||||
|
for cur < startPadLen {
|
||||||
|
leftPad += string(padStr[cur%charLen])
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = 0
|
||||||
|
rightPad := ""
|
||||||
|
for cur < endPadLen {
|
||||||
|
rightPad += string(padStr[cur%charLen])
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
|
||||||
|
return leftPad + str + rightPad
|
||||||
|
}
|
||||||
|
|
||||||
|
// isLetter checks r is a letter but not CJK character.
|
||||||
|
func isLetter(r rune) bool {
|
||||||
|
if !unicode.IsLetter(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// cjk char: /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/
|
||||||
|
|
||||||
|
// hiragana and katakana (Japanese only)
|
||||||
|
case r >= '\u3034' && r < '\u30ff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK unified ideographs extension A (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\u3400' && r < '\u4dbf':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK unified ideographs (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\u4e00' && r < '\u9fff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK compatibility ideographs (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\uf900' && r < '\ufaff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// half-width katakana (Japanese only)
|
||||||
|
case r >= '\uff66' && r < '\uff9f':
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package strutil
|
package strutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -9,72 +10,178 @@ import (
|
|||||||
func TestCamelCase(t *testing.T) {
|
func TestCamelCase(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestCamelCase")
|
assert := internal.NewAssert(t, "TestCamelCase")
|
||||||
|
|
||||||
assert.Equal("fooBar", CamelCase("foo_bar"))
|
cases := map[string]string{
|
||||||
assert.Equal("fooBar", CamelCase("Foo-Bar"))
|
"": "",
|
||||||
assert.Equal("fooBar", CamelCase("Foo&bar"))
|
"foobar": "foobar",
|
||||||
assert.Equal("fooBar", CamelCase("foo bar"))
|
"&FOO:BAR$BAZ": "fooBarBaz",
|
||||||
|
"fooBar": "fooBar",
|
||||||
|
"FOObar": "foObar",
|
||||||
|
"$foo%": "foo",
|
||||||
|
" $#$Foo 22 bar ": "foo22Bar",
|
||||||
|
"Foo-#1😄$_%^&*(1bar": "foo11Bar",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("FooBar", CamelCase("foo_bar"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, CamelCase(k))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCapitalize(t *testing.T) {
|
func TestCapitalize(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestCapitalize")
|
assert := internal.NewAssert(t, "TestCapitalize")
|
||||||
|
|
||||||
assert.Equal("Foo", Capitalize("foo"))
|
cases := map[string]string{
|
||||||
assert.Equal("Foo", Capitalize("Foo"))
|
"": "",
|
||||||
assert.Equal("Foo", Capitalize("Foo"))
|
"Foo": "Foo",
|
||||||
|
"_foo": "_foo",
|
||||||
|
"foobar": "Foobar",
|
||||||
|
"fooBar": "Foobar",
|
||||||
|
"foo Bar": "Foo bar",
|
||||||
|
"foo-bar": "Foo-bar",
|
||||||
|
"$foo%": "$foo%",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("foo", Capitalize("Foo"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, Capitalize(k))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKebabCase(t *testing.T) {
|
func TestKebabCase(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestKebabCase")
|
assert := internal.NewAssert(t, "TestKebabCase")
|
||||||
|
|
||||||
assert.Equal("foo-bar", KebabCase("Foo Bar-"))
|
cases := map[string]string{
|
||||||
assert.Equal("foo-bar", KebabCase("foo_Bar"))
|
"": "",
|
||||||
assert.Equal("foo-bar", KebabCase("fooBar"))
|
"foo-bar": "foo-bar",
|
||||||
assert.Equal("f-o-o-b-a-r", KebabCase("__FOO_BAR__"))
|
"--Foo---Bar-": "foo-bar",
|
||||||
|
"Foo Bar-": "foo-bar",
|
||||||
|
"foo_Bar": "foo-bar",
|
||||||
|
"fooBar": "foo-bar",
|
||||||
|
"FOOBAR": "foobar",
|
||||||
|
"FOO_BAR": "foo-bar",
|
||||||
|
"__FOO_BAR__": "foo-bar",
|
||||||
|
"$foo@Bar": "foo-bar",
|
||||||
|
" $#$Foo 22 bar ": "foo-22-bar",
|
||||||
|
"Foo-#1😄$_%^&*(1bar": "foo-1-1-bar",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("foo_bar", KebabCase("fooBar"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, KebabCase(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpperKebabCase(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUpperKebabCase")
|
||||||
|
|
||||||
|
cases := map[string]string{
|
||||||
|
"": "",
|
||||||
|
"foo-bar": "FOO-BAR",
|
||||||
|
"--Foo---Bar-": "FOO-BAR",
|
||||||
|
"Foo Bar-": "FOO-BAR",
|
||||||
|
"foo_Bar": "FOO-BAR",
|
||||||
|
"fooBar": "FOO-BAR",
|
||||||
|
"FOOBAR": "FOOBAR",
|
||||||
|
"FOO_BAR": "FOO-BAR",
|
||||||
|
"__FOO_BAR__": "FOO-BAR",
|
||||||
|
"$foo@Bar": "FOO-BAR",
|
||||||
|
" $#$Foo 22 bar ": "FOO-22-BAR",
|
||||||
|
"Foo-#1😄$_%^&*(1bar": "FOO-1-1-BAR",
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, UpperKebabCase(k))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnakeCase(t *testing.T) {
|
func TestSnakeCase(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSnakeCase")
|
assert := internal.NewAssert(t, "TestSnakeCase")
|
||||||
|
|
||||||
assert.Equal("foo_bar", SnakeCase("Foo Bar-"))
|
cases := map[string]string{
|
||||||
assert.Equal("foo_bar", SnakeCase("foo_Bar"))
|
"": "",
|
||||||
assert.Equal("foo_bar", SnakeCase("fooBar"))
|
"foo-bar": "foo_bar",
|
||||||
assert.Equal("f_o_o_b_a_r", SnakeCase("__FOO_BAR__"))
|
"--Foo---Bar-": "foo_bar",
|
||||||
assert.Equal("a_bbc_s_a_b_b_c", SnakeCase("aBbc-s$@a&%_B.B^C"))
|
"Foo Bar-": "foo_bar",
|
||||||
|
"foo_Bar": "foo_bar",
|
||||||
|
"fooBar": "foo_bar",
|
||||||
|
"FOOBAR": "foobar",
|
||||||
|
"FOO_BAR": "foo_bar",
|
||||||
|
"__FOO_BAR__": "foo_bar",
|
||||||
|
"$foo@Bar": "foo_bar",
|
||||||
|
" $#$Foo 22 bar ": "foo_22_bar",
|
||||||
|
"Foo-#1😄$_%^&*(1bar": "foo_1_1_bar",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("foo-bar", SnakeCase("foo_Bar"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, SnakeCase(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpperSnakeCase(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUpperSnakeCase")
|
||||||
|
|
||||||
|
cases := map[string]string{
|
||||||
|
"": "",
|
||||||
|
"foo-bar": "FOO_BAR",
|
||||||
|
"--Foo---Bar-": "FOO_BAR",
|
||||||
|
"Foo Bar-": "FOO_BAR",
|
||||||
|
"foo_Bar": "FOO_BAR",
|
||||||
|
"fooBar": "FOO_BAR",
|
||||||
|
"FOOBAR": "FOOBAR",
|
||||||
|
"FOO_BAR": "FOO_BAR",
|
||||||
|
"__FOO_BAR__": "FOO_BAR",
|
||||||
|
"$foo@Bar": "FOO_BAR",
|
||||||
|
" $#$Foo 22 bar ": "FOO_22_BAR",
|
||||||
|
"Foo-#1😄$_%^&*(1bar": "FOO_1_1_BAR",
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, UpperSnakeCase(k))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpperFirst(t *testing.T) {
|
func TestUpperFirst(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestLowerFirst")
|
assert := internal.NewAssert(t, "TestLowerFirst")
|
||||||
|
|
||||||
assert.Equal("Foo", UpperFirst("foo"))
|
cases := map[string]string{
|
||||||
assert.Equal("BAR", UpperFirst("bAR"))
|
"": "",
|
||||||
assert.Equal("FOo", UpperFirst("FOo"))
|
"foo": "Foo",
|
||||||
assert.Equal("FOo大", UpperFirst("fOo大"))
|
"bAR": "BAR",
|
||||||
|
"FOo": "FOo",
|
||||||
|
"fOo大": "FOo大",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("Bar", UpperFirst("BAR"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, UpperFirst(k))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLowerFirst(t *testing.T) {
|
func TestLowerFirst(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestLowerFirst")
|
assert := internal.NewAssert(t, "TestLowerFirst")
|
||||||
|
|
||||||
assert.Equal("foo", LowerFirst("foo"))
|
cases := map[string]string{
|
||||||
assert.Equal("bAR", LowerFirst("BAR"))
|
"": "",
|
||||||
assert.Equal("fOo", LowerFirst("FOo"))
|
"foo": "foo",
|
||||||
assert.Equal("fOo大", LowerFirst("FOo大"))
|
"bAR": "bAR",
|
||||||
|
"FOo": "fOo",
|
||||||
|
"fOo大": "fOo大",
|
||||||
|
}
|
||||||
|
|
||||||
assert.NotEqual("Bar", LowerFirst("BAR"))
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, LowerFirst(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPad(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPad")
|
||||||
|
|
||||||
|
assert.Equal("a ", Pad("a", 2, ""))
|
||||||
|
assert.Equal("a", Pad("a", 1, "b"))
|
||||||
|
assert.Equal("ab", Pad("a", 2, "b"))
|
||||||
|
assert.Equal("mabcdm", Pad("abcd", 6, "m"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPadEnd(t *testing.T) {
|
func TestPadEnd(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestPadEnd")
|
assert := internal.NewAssert(t, "TestPadEnd")
|
||||||
|
|
||||||
|
assert.Equal("a ", PadEnd("a", 2, " "))
|
||||||
assert.Equal("a", PadEnd("a", 1, "b"))
|
assert.Equal("a", PadEnd("a", 1, "b"))
|
||||||
assert.Equal("ab", PadEnd("a", 2, "b"))
|
assert.Equal("ab", PadEnd("a", 2, "b"))
|
||||||
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
||||||
@@ -189,3 +296,93 @@ func TestSplitEx(t *testing.T) {
|
|||||||
assert.Equal([]string{" a", "b", "c", ""}, SplitEx(" a = b = c = ", " = ", false))
|
assert.Equal([]string{" a", "b", "c", ""}, SplitEx(" a = b = c = ", " = ", false))
|
||||||
assert.Equal([]string{" a", "b", "c"}, SplitEx(" a = b = c = ", " = ", true))
|
assert.Equal([]string{" a", "b", "c"}, SplitEx(" a = b = c = ", " = ", true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitWords(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
|
cases := map[string][]string{
|
||||||
|
"a word": {"a", "word"},
|
||||||
|
"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"},
|
||||||
|
"你好,我是一名码农": nil,
|
||||||
|
"こんにちは,私はプログラマーです": nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, SplitWords(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWordCount(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
|
cases := map[string]int{
|
||||||
|
"a word": 2, // {"a", "word"},
|
||||||
|
"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"},
|
||||||
|
"你好,我是一名码农": 0, // nil,
|
||||||
|
"こんにちは,私はプログラマーです": 0, // nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, WordCount(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveNonPrintable(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRemoveNonPrintable")
|
||||||
|
|
||||||
|
assert.Equal("hello world", RemoveNonPrintable("hello\u00a0 \u200bworld\n"))
|
||||||
|
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStringToBytes(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestStringToBytes")
|
||||||
|
str := "abc"
|
||||||
|
bytes := StringToBytes(str)
|
||||||
|
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBytesToString(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBytesToString")
|
||||||
|
bytes := []byte{'a', 'b', 'c'}
|
||||||
|
str := BytesToString(bytes)
|
||||||
|
assert.Equal(str == "abc", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsBlank(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsBlank")
|
||||||
|
assert.Equal(IsBlank(""), true)
|
||||||
|
assert.Equal(IsBlank("\t\v\f\n"), true)
|
||||||
|
assert.Equal(IsBlank(" 中文"), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasPrefixAny(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHasPrefixAny")
|
||||||
|
str := "foo bar"
|
||||||
|
prefixes := []string{"fo", "xyz", "hello"}
|
||||||
|
notMatches := []string{"oom", "world"}
|
||||||
|
assert.Equal(HasPrefixAny(str, prefixes), true)
|
||||||
|
assert.Equal(HasPrefixAny(str, notMatches), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasSuffixAny(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHasSuffixAny")
|
||||||
|
str := "foo bar"
|
||||||
|
suffixes := []string{"bar", "xyz", "hello"}
|
||||||
|
notMatches := []string{"oom", "world"}
|
||||||
|
assert.Equal(HasSuffixAny(str, suffixes), true)
|
||||||
|
assert.Equal(HasSuffixAny(str, notMatches), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexOffset(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIndexOffset")
|
||||||
|
str := "foo bar hello world"
|
||||||
|
assert.Equal(IndexOffset(str, "o", 5), 12)
|
||||||
|
assert.Equal(IndexOffset(str, "o", 0), 1)
|
||||||
|
assert.Equal(IndexOffset(str, "d", len(str)-1), len(str)-1)
|
||||||
|
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
||||||
|
assert.Equal(IndexOffset(str, "f", -1), -1)
|
||||||
|
}
|
||||||
|
|||||||
63
system/os.go
63
system/os.go
@@ -9,6 +9,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/validator"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Option func(*exec.Cmd)
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsWindows check if current os is windows
|
// IsWindows check if current os is windows
|
||||||
@@ -50,27 +58,68 @@ func CompareOsEnv(key, comparedEnv string) bool {
|
|||||||
return env == comparedEnv
|
return env == comparedEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecCommand use shell /bin/bash -c to execute command
|
// ExecCommand execute command, return the stdout and stderr string of command, and error if error occur
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error) {
|
// param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1
|
||||||
|
// in linux, use /bin/bash -c to execute command
|
||||||
|
// in windows, use powershell.exe to execute command
|
||||||
|
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error) {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
var errout bytes.Buffer
|
var errOut bytes.Buffer
|
||||||
|
|
||||||
cmd := exec.Command("/bin/bash", "-c", command)
|
cmd := exec.Command("/bin/bash", "-c", command)
|
||||||
if IsWindows() {
|
if IsWindows() {
|
||||||
cmd = exec.Command("cmd")
|
cmd = exec.Command("powershell.exe", command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if opt != nil {
|
||||||
|
opt(cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
cmd.Stderr = &errout
|
cmd.Stderr = &errOut
|
||||||
|
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stderr = string(errout.Bytes())
|
if utf8.Valid(errOut.Bytes()) {
|
||||||
|
stderr = byteToString(errOut.Bytes(), "UTF8")
|
||||||
|
} else if validator.IsGBK(errOut.Bytes()) {
|
||||||
|
stderr = byteToString(errOut.Bytes(), "GBK")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := out.Bytes()
|
||||||
|
if utf8.Valid(data) {
|
||||||
|
stdout = byteToString(data, "UTF8")
|
||||||
|
} else if validator.IsGBK(data) {
|
||||||
|
stdout = byteToString(data, "GBK")
|
||||||
}
|
}
|
||||||
stdout = string(out.Bytes())
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func byteToString(data []byte, charset string) string {
|
||||||
|
var result string
|
||||||
|
|
||||||
|
switch charset {
|
||||||
|
case "GBK":
|
||||||
|
decodeBytes, _ := simplifiedchinese.GBK.NewDecoder().Bytes(data)
|
||||||
|
result = string(decodeBytes)
|
||||||
|
case "GB18030":
|
||||||
|
decodeBytes, _ := simplifiedchinese.GB18030.NewDecoder().Bytes(data)
|
||||||
|
result = string(decodeBytes)
|
||||||
|
case "UTF8":
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
result = string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// GetOsBits get this system bits 32bit or 64bit
|
// GetOsBits get this system bits 32bit or 64bit
|
||||||
// return bit int (32/64)
|
// return bit int (32/64)
|
||||||
func GetOsBits() int {
|
func GetOsBits() int {
|
||||||
|
|||||||
25
system/os_darwin.go
Normal file
25
system/os_darwin.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//go:build darwin
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithForeground() Option {
|
||||||
|
return func(c *exec.Cmd) {
|
||||||
|
// if c.SysProcAttr == nil {
|
||||||
|
// c.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
// Foreground: true,
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// c.SysProcAttr.Foreground = true
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithWinHide() Option {
|
||||||
|
return func(c *exec.Cmd) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
26
system/os_linux.go
Normal file
26
system/os_linux.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithForeground() Option {
|
||||||
|
return func(c *exec.Cmd) {
|
||||||
|
if c.SysProcAttr == nil {
|
||||||
|
c.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
Foreground: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.SysProcAttr.Foreground = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithWinHide() Option {
|
||||||
|
return func(c *exec.Cmd) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,10 +11,10 @@ func TestOsDetection(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestOsJudgment")
|
assert := internal.NewAssert(t, "TestOsJudgment")
|
||||||
|
|
||||||
osType, _, _ := ExecCommand("echo $OSTYPE")
|
osType, _, _ := ExecCommand("echo $OSTYPE")
|
||||||
if strings.Index(osType, "linux") != -1 {
|
if strings.Contains(osType, "linux") {
|
||||||
assert.Equal(true, IsLinux())
|
assert.Equal(true, IsLinux())
|
||||||
}
|
}
|
||||||
if strings.Index(osType, "darwin") != -1 {
|
if strings.Contains(osType, "darwin") {
|
||||||
assert.Equal(true, IsMac())
|
assert.Equal(true, IsMac())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,22 +44,27 @@ func TestOsEnvOperation(t *testing.T) {
|
|||||||
func TestExecCommand(t *testing.T) {
|
func TestExecCommand(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestExecCommand")
|
assert := internal.NewAssert(t, "TestExecCommand")
|
||||||
|
|
||||||
out, errout, err := ExecCommand("ls")
|
// linux or mac
|
||||||
t.Log("std out: ", out)
|
stdout, stderr, err := ExecCommand("ls", WithForeground())
|
||||||
t.Log("std err: ", errout)
|
t.Log("std out: ", stdout)
|
||||||
|
t.Log("std err: ", stderr)
|
||||||
|
assert.Equal("", stderr)
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
out, errout, err = ExecCommand("abc")
|
// windows
|
||||||
t.Log("std out: ", out)
|
stdout, stderr, err = ExecCommand("dir")
|
||||||
t.Log("std err: ", errout)
|
t.Log("std out: ", stdout)
|
||||||
if err != nil {
|
t.Log("std err: ", stderr)
|
||||||
t.Logf("error: %v\n", err)
|
if IsWindows() {
|
||||||
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !IsWindows() {
|
// error command
|
||||||
|
stdout, stderr, err = ExecCommand("abc")
|
||||||
|
t.Log("std out: ", stdout)
|
||||||
|
t.Log("std err: ", stderr)
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetOsBits(t *testing.T) {
|
func TestGetOsBits(t *testing.T) {
|
||||||
osBits := GetOsBits()
|
osBits := GetOsBits()
|
||||||
|
|||||||
20
system/os_windows.go
Normal file
20
system/os_windows.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithWinHide() Option {
|
||||||
|
return func(c *exec.Cmd) {
|
||||||
|
if c.SysProcAttr == nil {
|
||||||
|
c.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
HideWindow: true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.SysProcAttr.HideWindow = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,24 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
var (
|
||||||
|
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
|
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||||
|
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
|
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||||
|
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
||||||
|
emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
||||||
|
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
|
||||||
|
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
||||||
|
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||||
|
chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
||||||
|
creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
||||||
|
base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
||||||
|
)
|
||||||
|
|
||||||
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
||||||
func IsAlpha(str string) bool {
|
func IsAlpha(str string) bool {
|
||||||
return isAlphaRegexMatcher.MatchString(str)
|
return alphaMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAllUpper check if the string is all upper case letters A-Z
|
// IsAllUpper check if the string is all upper case letters A-Z
|
||||||
@@ -62,11 +75,9 @@ func ContainLower(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var containLetterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
||||||
|
|
||||||
// ContainLetter check if the string contain at least one letter
|
// ContainLetter check if the string contain at least one letter
|
||||||
func ContainLetter(str string) bool {
|
func ContainLetter(str string) bool {
|
||||||
return containLetterRegexMatcher.MatchString(str)
|
return letterRegexMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsJSON checks if the string is valid JSON
|
// IsJSON checks if the string is valid JSON
|
||||||
@@ -86,11 +97,9 @@ func IsFloatStr(str string) bool {
|
|||||||
return e == nil
|
return e == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
|
||||||
|
|
||||||
// IsIntStr check if the string can convert to a integer.
|
// IsIntStr check if the string can convert to a integer.
|
||||||
func IsIntStr(str string) bool {
|
func IsIntStr(str string) bool {
|
||||||
return isIntStrRegexMatcher.MatchString(str)
|
return intStrMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIp check if the string is a ip address.
|
// IsIp check if the string is a ip address.
|
||||||
@@ -125,8 +134,6 @@ func IsPort(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var isUrlRegexMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
|
||||||
|
|
||||||
// IsUrl check if the string is url.
|
// IsUrl check if the string is url.
|
||||||
func IsUrl(str string) bool {
|
func IsUrl(str string) bool {
|
||||||
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
|
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
|
||||||
@@ -143,64 +150,48 @@ func IsUrl(str string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return isUrlRegexMatcher.MatchString(str)
|
return urlMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isDnsRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
|
|
||||||
|
|
||||||
// IsDns check if the string is dns.
|
// IsDns check if the string is dns.
|
||||||
func IsDns(dns string) bool {
|
func IsDns(dns string) bool {
|
||||||
return isDnsRegexMatcher.MatchString(dns)
|
return dnsMatcher.MatchString(dns)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isEmailRegexMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
|
|
||||||
|
|
||||||
// IsEmail check if the string is a email address.
|
// IsEmail check if the string is a email address.
|
||||||
func IsEmail(email string) bool {
|
func IsEmail(email string) bool {
|
||||||
return isEmailRegexMatcher.MatchString(email)
|
return emailMatcher.MatchString(email)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChineseMobileRegexMatcher *regexp.Regexp = regexp.MustCompile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")
|
|
||||||
|
|
||||||
// IsChineseMobile check if the string is chinese mobile number.
|
// IsChineseMobile check if the string is chinese mobile number.
|
||||||
func IsChineseMobile(mobileNum string) bool {
|
func IsChineseMobile(mobileNum string) bool {
|
||||||
return isChineseMobileRegexMatcher.MatchString(mobileNum)
|
return chineseMobileMatcher.MatchString(mobileNum)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChineseIdNumRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
|
|
||||||
|
|
||||||
// IsChineseIdNum check if the string is chinese id number.
|
// IsChineseIdNum check if the string is chinese id number.
|
||||||
func IsChineseIdNum(id string) bool {
|
func IsChineseIdNum(id string) bool {
|
||||||
return isChineseIdNumRegexMatcher.MatchString(id)
|
return chineseIdMatcher.MatchString(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var containChineseRegexMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
|
||||||
|
|
||||||
// ContainChinese check if the string contain mandarin chinese.
|
// ContainChinese check if the string contain mandarin chinese.
|
||||||
func ContainChinese(s string) bool {
|
func ContainChinese(s string) bool {
|
||||||
return containChineseRegexMatcher.MatchString(s)
|
return chineseMatcher.MatchString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isChinesePhoneRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`)
|
|
||||||
|
|
||||||
// IsChinesePhone check if the string is chinese phone number.
|
// IsChinesePhone check if the string is chinese phone number.
|
||||||
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
|
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
|
||||||
func IsChinesePhone(phone string) bool {
|
func IsChinesePhone(phone string) bool {
|
||||||
return isChinesePhoneRegexMatcher.MatchString(phone)
|
return chinesePhoneMatcher.MatchString(phone)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isCreditCardRegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`)
|
|
||||||
|
|
||||||
// IsCreditCard check if the string is credit card.
|
// IsCreditCard check if the string is credit card.
|
||||||
func IsCreditCard(creditCart string) bool {
|
func IsCreditCard(creditCart string) bool {
|
||||||
return isCreditCardRegexMatcher.MatchString(creditCart)
|
return creditCardMatcher.MatchString(creditCart)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isBase64RegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`)
|
|
||||||
|
|
||||||
// IsBase64 check if the string is base64 string.
|
// IsBase64 check if the string is base64 string.
|
||||||
func IsBase64(base64 string) bool {
|
func IsBase64(base64 string) bool {
|
||||||
return isBase64RegexMatcher.MatchString(base64)
|
return base64Matcher.MatchString(base64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmptyString check if the string is empty.
|
// IsEmptyString check if the string is empty.
|
||||||
@@ -288,3 +279,86 @@ func IsZeroValue(value interface{}) bool {
|
|||||||
|
|
||||||
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
|
return reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsGBK check if data encoding is gbk
|
||||||
|
// Note: this function is implemented by whether double bytes fall within the encoding range of gbk,
|
||||||
|
// while each byte of utf-8 encoding format falls within the encoding range of gbk.
|
||||||
|
// Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding,
|
||||||
|
// and then call IsGBK() to check gbk encoding. like below
|
||||||
|
/**
|
||||||
|
data := []byte("你好")
|
||||||
|
if utf8.Valid(data) {
|
||||||
|
fmt.Println("data encoding is utf-8")
|
||||||
|
}else if(IsGBK(data)) {
|
||||||
|
fmt.Println("data encoding is GBK")
|
||||||
|
}
|
||||||
|
fmt.Println("data encoding is unknown")
|
||||||
|
**/
|
||||||
|
func IsGBK(data []byte) bool {
|
||||||
|
i := 0
|
||||||
|
for i < len(data) {
|
||||||
|
if data[i] <= 0xff {
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
if data[i] >= 0x81 &&
|
||||||
|
data[i] <= 0xfe &&
|
||||||
|
data[i+1] >= 0x40 &&
|
||||||
|
data[i+1] <= 0xfe &&
|
||||||
|
data[i+1] != 0xf7 {
|
||||||
|
i += 2
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsNumberStr check if the value is number(integer, float) or not.
|
||||||
|
func IsNumber(v interface{}) bool {
|
||||||
|
return IsInt(v) || IsFloat(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFloat check if the value is float(float32, float34) or not.
|
||||||
|
func IsFloat(v interface{}) bool {
|
||||||
|
switch v.(type) {
|
||||||
|
case float32, float64:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsInt check if the value is integer(int, unit) or not.
|
||||||
|
func IsInt(v interface{}) bool {
|
||||||
|
switch v.(type) {
|
||||||
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsASCII checks if string is all ASCII char.
|
||||||
|
func IsASCII(str string) bool {
|
||||||
|
for i := 0; i < len(str); i++ {
|
||||||
|
if str[i] > unicode.MaxASCII {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsPrintable checks if string is all printable chars.
|
||||||
|
func IsPrintable(str string) bool {
|
||||||
|
for _, r := range str {
|
||||||
|
if !unicode.IsPrint(r) {
|
||||||
|
if r == '\n' || r == '\r' || r == '\t' || r == '`' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsAllUpper(t *testing.T) {
|
func TestIsAllUpper(t *testing.T) {
|
||||||
@@ -388,3 +390,61 @@ func TestIsZeroValue(t *testing.T) {
|
|||||||
assert.Equal(false, IsZeroValue(value))
|
assert.Equal(false, IsZeroValue(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsGBK(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsGBK")
|
||||||
|
|
||||||
|
str := "你好"
|
||||||
|
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
|
||||||
|
|
||||||
|
assert.Equal(true, IsGBK(gbkData))
|
||||||
|
assert.Equal(false, utf8.Valid(gbkData))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsNumber(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsNumber")
|
||||||
|
|
||||||
|
assert.Equal(false, IsNumber(""))
|
||||||
|
assert.Equal(false, IsNumber("3"))
|
||||||
|
assert.Equal(true, IsNumber(0))
|
||||||
|
assert.Equal(true, IsNumber(0.1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsFloat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsFloat")
|
||||||
|
|
||||||
|
assert.Equal(false, IsFloat(""))
|
||||||
|
assert.Equal(false, IsFloat("3"))
|
||||||
|
assert.Equal(false, IsFloat(0))
|
||||||
|
assert.Equal(true, IsFloat(0.1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsInt(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsInt")
|
||||||
|
|
||||||
|
assert.Equal(false, IsInt(""))
|
||||||
|
assert.Equal(false, IsInt("3"))
|
||||||
|
assert.Equal(false, IsInt(0.1))
|
||||||
|
assert.Equal(true, IsInt(0))
|
||||||
|
assert.Equal(true, IsInt(-1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsASCII(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsASCII")
|
||||||
|
|
||||||
|
assert.Equal(true, IsASCII("ABC"))
|
||||||
|
assert.Equal(true, IsASCII("123"))
|
||||||
|
assert.Equal(true, IsASCII(""))
|
||||||
|
assert.Equal(false, IsASCII("😄"))
|
||||||
|
assert.Equal(false, IsASCII("你好"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsPrintable(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsPrintable")
|
||||||
|
|
||||||
|
assert.Equal(true, IsPrintable("ABC"))
|
||||||
|
assert.Equal(true, IsPrintable("{id: 123}"))
|
||||||
|
assert.Equal(true, IsPrintable(""))
|
||||||
|
assert.Equal(true, IsPrintable("😄"))
|
||||||
|
assert.Equal(false, IsPrintable("\u0000"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user