mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a8ff17b52 | ||
|
|
6b46a0c05c | ||
|
|
4b8b624b4c | ||
|
|
f274375e62 | ||
|
|
6dc017a5fa | ||
|
|
e1fb97095d | ||
|
|
d8f5a15590 | ||
|
|
1202ac955d | ||
|
|
84b2cec9b5 | ||
|
|
2d01a13787 | ||
|
|
db9b045500 | ||
|
|
2701bcc4d2 | ||
|
|
b9745fd08b | ||
|
|
3b1597d6f7 | ||
|
|
40ab5e8f7b |
19
README.md
19
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)
|
||||||
@@ -153,6 +153,7 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute)
|
||||||
|
- [AddYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddYear)
|
||||||
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfMinute)
|
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfMinute)
|
||||||
- [BeginOfHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfHour)
|
- [BeginOfHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfHour)
|
||||||
- [BeginOfDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfDay)
|
- [BeginOfDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfDay)
|
||||||
@@ -180,6 +181,10 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToFormat)
|
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#ToFormat)
|
||||||
- [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)
|
||||||
|
- [IsLeapYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#IsLeapYear)
|
||||||
|
- [BetweenSeconds](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BetweenSeconds)
|
||||||
|
- [DayOfYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#DayOfYear)
|
||||||
|
- [IsWeekend](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#IsWeekend)
|
||||||
|
|
||||||
### 5. Fileutil package implements some basic functions for file operations.
|
### 5. Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
@@ -210,6 +215,8 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#MTime)
|
- [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)
|
- [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)
|
- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#WriteBytesToFile)
|
||||||
|
|
||||||
### 6. Formatter contains some functions for data formatting.
|
### 6. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
@@ -264,6 +271,10 @@ import "github.com/duke-git/lancet/mathutil"
|
|||||||
- [RadianToAngle](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RadianToAngle)
|
- [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)
|
- [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)
|
- [IsPrime](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#IsPrime)
|
||||||
|
- [GCD](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#GCD)
|
||||||
|
- [LCM](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#LCM)
|
||||||
|
- [Cos](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Cos)
|
||||||
|
- [Sin](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Sin)
|
||||||
|
|
||||||
### 9. 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.
|
||||||
|
|
||||||
@@ -387,6 +398,8 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [BeforeLast](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#BeforeLast)
|
- [BeforeLast](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#BeforeLast)
|
||||||
- [CamelCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#CamelCase)
|
- [CamelCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#CamelCase)
|
||||||
- [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)
|
||||||
|
- [ContainsAll](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#ContainsAll)
|
||||||
|
- [ContainsAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#ContainsAny)
|
||||||
- [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)
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperKebabCase)
|
||||||
@@ -410,6 +423,10 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [HasPrefixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#HasPrefixAny)
|
- [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)
|
- [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)
|
- [IndexOffset](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#IndexOffset)
|
||||||
|
- [ReplaceWithMap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#ReplaceWithMap)
|
||||||
|
- [Trim](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Trim)
|
||||||
|
- [SplitAndTrim](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitAndTrim)
|
||||||
|
- [HideString](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#HideString)
|
||||||
|
|
||||||
### 14. System package contain some functions about os, runtime, shell command.
|
### 14. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](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)
|
||||||
@@ -152,6 +152,7 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute)
|
||||||
|
- [AddYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddYear)
|
||||||
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfMinute)
|
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfMinute)
|
||||||
- [BeginOfHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfHour)
|
- [BeginOfHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfHour)
|
||||||
- [BeginOfDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfDay)
|
- [BeginOfDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#BeginOfDay)
|
||||||
@@ -179,6 +180,10 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat)
|
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat)
|
||||||
- [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)
|
||||||
|
- [IsLeapYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#IsLeapYear)
|
||||||
|
- [BetweenSeconds](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#BetweenSeconds)
|
||||||
|
- [DayOfYear](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#DayOfYear)
|
||||||
|
- [IsWeekend](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#IsWeekend)
|
||||||
|
|
||||||
### 5. fileutil 包支持文件基本操作。
|
### 5. fileutil 包支持文件基本操作。
|
||||||
|
|
||||||
@@ -209,6 +214,8 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#MTime)
|
- [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)
|
- [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)
|
- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#WriteBytesToFile)
|
||||||
|
|
||||||
### 6. formatter 格式化器包含一些数据格式化处理方法。
|
### 6. formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
@@ -263,6 +270,10 @@ import "github.com/duke-git/lancet/mathutil"
|
|||||||
- [RadianToAngle](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RadianToAngle)
|
- [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)
|
- [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)
|
- [IsPrime](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#IsPrime)
|
||||||
|
- [GCD](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#GCD)
|
||||||
|
- [LCM](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#LCM)
|
||||||
|
- [Cos](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Cos)
|
||||||
|
- [Sin](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Sin)
|
||||||
|
|
||||||
### 9. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
### 9. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
@@ -386,6 +397,8 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [BeforeLast](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#BeforeLast)
|
- [BeforeLast](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#BeforeLast)
|
||||||
- [CamelCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#CamelCase)
|
- [CamelCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#CamelCase)
|
||||||
- [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)
|
||||||
|
- [ContainsAll](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#ContainsAll)
|
||||||
|
- [ContainsAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#ContainsAny)
|
||||||
- [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)
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperKebabCase)
|
||||||
@@ -409,6 +422,10 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [HasPrefixAny](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#HasPrefixAny)
|
- [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)
|
- [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)
|
- [IndexOffset](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#IndexOffset)
|
||||||
|
- [ReplaceWithMap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#ReplaceWithMap)
|
||||||
|
- [Trim](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Trim)
|
||||||
|
- [SplitAndTrim](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitAndTrim)
|
||||||
|
- [HideString](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#HideString)
|
||||||
|
|
||||||
### 14. system 包含 os, runtime, shell command 相关函数。
|
### 14. system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
|
|||||||
@@ -278,44 +278,99 @@ func DeepClone(src interface{}) interface{} {
|
|||||||
return result.Interface()
|
return result.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
// // CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
||||||
func CopyProperties(dst, src interface{}) (err error) {
|
// func CopyProperties(dst, src interface{}) (err error) {
|
||||||
defer func() {
|
// defer func() {
|
||||||
if e := recover(); e != nil {
|
// if e := recover(); e != nil {
|
||||||
err = errors.New(fmt.Sprintf("%v", e))
|
// err = errors.New(fmt.Sprintf("%v", e))
|
||||||
}
|
// }
|
||||||
}()
|
// }()
|
||||||
|
|
||||||
dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst)
|
// dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst)
|
||||||
srcType, srcValue := reflect.TypeOf(src), reflect.ValueOf(src)
|
// 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
|
||||||
|
// }
|
||||||
|
|
||||||
|
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
||||||
|
// use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.
|
||||||
|
func CopyProperties(dst, src interface{}) error {
|
||||||
|
dstType, srcType := reflect.TypeOf(dst), reflect.TypeOf(src)
|
||||||
|
|
||||||
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct {
|
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct {
|
||||||
return errors.New("CopyProperties: param dst should be struct pointer")
|
return errors.New("CopyProperties: parameter dst should be struct pointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
if srcType.Kind() == reflect.Ptr {
|
if srcType.Kind() == reflect.Ptr {
|
||||||
srcType, srcValue = srcType.Elem(), srcValue.Elem()
|
srcType = srcType.Elem()
|
||||||
}
|
}
|
||||||
if srcType.Kind() != reflect.Struct {
|
if srcType.Kind() != reflect.Struct {
|
||||||
return errors.New("CopyProperties: param src should be a struct or struct pointer")
|
return errors.New("CopyProperties: parameter src should be a struct or struct pointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
dstType, dstValue = dstType.Elem(), dstValue.Elem()
|
bytes, err := json.Marshal(src)
|
||||||
|
if err != nil {
|
||||||
propertyNums := dstType.NumField()
|
return fmt.Errorf("CopyProperties: unable to marshal src: %s", err)
|
||||||
|
}
|
||||||
for i := 0; i < propertyNums; i++ {
|
err = json.Unmarshal(bytes, dst)
|
||||||
property := dstType.Field(i)
|
if err != nil {
|
||||||
propertyValue := srcValue.FieldByName(property.Name)
|
return fmt.Errorf("CopyProperties: unable to unmarshal into dst: %s", err)
|
||||||
|
|
||||||
if !propertyValue.IsValid() || property.Type != propertyValue.Type() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if dstValue.Field(i).CanSet() {
|
|
||||||
dstValue.Field(i).Set(propertyValue)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInterface converts reflect value to its interface type.
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool) {
|
||||||
|
if v.IsValid() && v.CanInterface() {
|
||||||
|
return v.Interface(), true
|
||||||
|
}
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Bool:
|
||||||
|
return v.Bool(), true
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return v.Int(), true
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
return v.Uint(), true
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return v.Float(), true
|
||||||
|
case reflect.Complex64, reflect.Complex128:
|
||||||
|
return v.Complex(), true
|
||||||
|
case reflect.String:
|
||||||
|
return v.String(), true
|
||||||
|
case reflect.Ptr:
|
||||||
|
return ToInterface(v.Elem())
|
||||||
|
case reflect.Interface:
|
||||||
|
return ToInterface(v.Elem())
|
||||||
|
default:
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -262,49 +262,80 @@ func TestDeepClone(t *testing.T) {
|
|||||||
func TestCopyProperties(t *testing.T) {
|
func TestCopyProperties(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestCopyProperties")
|
assert := internal.NewAssert(t, "TestCopyProperties")
|
||||||
|
|
||||||
type Address struct {
|
type Disk struct {
|
||||||
Country string
|
Name string `json:"name"`
|
||||||
ZipCode string
|
Total string `json:"total"`
|
||||||
|
Used string `json:"used"`
|
||||||
|
Percent float64 `json:"percent"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type DiskVO struct {
|
||||||
Name string
|
Name string `json:"name"`
|
||||||
Age int
|
Total string `json:"total"`
|
||||||
Role string
|
Used string `json:"used"`
|
||||||
Addr Address
|
Percent float64 `json:"percent"`
|
||||||
Hobbys []string
|
|
||||||
salary int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Employee struct {
|
type Indicator struct {
|
||||||
Name string
|
Id string `json:"id"`
|
||||||
Age int
|
Ip string `json:"ip"`
|
||||||
Role string
|
UpTime string `json:"upTime"`
|
||||||
Addr Address
|
LoadAvg string `json:"loadAvg"`
|
||||||
Hobbys []string
|
Cpu int `json:"cpu"`
|
||||||
salary int
|
Disk []Disk `json:"disk"`
|
||||||
|
Stop chan bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000}
|
type IndicatorVO struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
|
UpTime string `json:"upTime"`
|
||||||
|
LoadAvg string `json:"loadAvg"`
|
||||||
|
Cpu int64 `json:"cpu"`
|
||||||
|
Disk []DiskVO `json:"disk"`
|
||||||
|
}
|
||||||
|
|
||||||
employee1 := Employee{}
|
indicator := &Indicator{Id: "001", Ip: "127.0.0.1", Cpu: 1, Disk: []Disk{
|
||||||
|
{Name: "disk-001", Total: "100", Used: "1", Percent: 10},
|
||||||
|
{Name: "disk-002", Total: "200", Used: "1", Percent: 20},
|
||||||
|
{Name: "disk-003", Total: "300", Used: "1", Percent: 30},
|
||||||
|
}}
|
||||||
|
|
||||||
err := CopyProperties(&employee1, &user)
|
indicatorVO := IndicatorVO{}
|
||||||
|
|
||||||
|
err := CopyProperties(&indicatorVO, indicator)
|
||||||
|
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal("user001", employee1.Name)
|
assert.Equal("001", indicatorVO.Id)
|
||||||
assert.Equal("Admin", employee1.Role)
|
assert.Equal("127.0.0.1", indicatorVO.Ip)
|
||||||
assert.Equal("CN", employee1.Addr.Country)
|
assert.Equal(3, len(indicatorVO.Disk))
|
||||||
assert.Equal(0, employee1.salary)
|
}
|
||||||
|
|
||||||
employee2 := Employee{Name: "employee001", Age: 20, Role: "User",
|
func TestToInterface(t *testing.T) {
|
||||||
Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500}
|
assert := internal.NewAssert(t, "TestToInterface")
|
||||||
|
|
||||||
err = CopyProperties(&employee2, &user)
|
cases := []reflect.Value{
|
||||||
|
reflect.ValueOf("abc"),
|
||||||
assert.IsNil(err)
|
reflect.ValueOf(int(0)), reflect.ValueOf(int8(1)), reflect.ValueOf(int16(-1)), reflect.ValueOf(int32(123)), reflect.ValueOf(int64(123)),
|
||||||
assert.Equal("user001", employee2.Name)
|
reflect.ValueOf(uint(123)), reflect.ValueOf(uint8(123)), reflect.ValueOf(uint16(123)), reflect.ValueOf(uint32(123)), reflect.ValueOf(uint64(123)),
|
||||||
assert.Equal("Admin", employee2.Role)
|
reflect.ValueOf(float64(12.3)), reflect.ValueOf(float32(12.3)),
|
||||||
assert.Equal("CN", employee2.Addr.Country)
|
reflect.ValueOf(true), reflect.ValueOf(false),
|
||||||
assert.Equal(500, employee2.salary)
|
}
|
||||||
|
|
||||||
|
expected := []interface{}{
|
||||||
|
"abc",
|
||||||
|
0, int8(1), int16(-1), int32(123), int64(123),
|
||||||
|
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
|
||||||
|
float64(12.3), float32(12.3),
|
||||||
|
true, false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(cases); i++ {
|
||||||
|
actual, _ := ToInterface(cases[i])
|
||||||
|
assert.Equal(expected[i], actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
nilVal, ok := ToInterface(reflect.ValueOf(nil))
|
||||||
|
assert.EqualValues(nil, nilVal)
|
||||||
|
assert.Equal(false, ok)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,24 +4,24 @@
|
|||||||
// Package datetime implements some functions to format date and time.
|
// Package datetime implements some functions to format date and time.
|
||||||
// Note:
|
// Note:
|
||||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
// 1. `format` param in FormatTimeToStr function should be as flow:
|
||||||
//"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"
|
||||||
//"yyyy-mm-dd"
|
// "yyyy-mm-dd"
|
||||||
//"yyyy-mm"
|
// "yyyy-mm"
|
||||||
//"mm-dd"
|
// "mm-dd"
|
||||||
//"dd-mm-yy hh:mm:ss"
|
// "dd-mm-yy hh:mm:ss"
|
||||||
//"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"
|
||||||
//"yyyy/mm/dd"
|
// "yyyy/mm/dd"
|
||||||
//"yyyy/mm"
|
// "yyyy/mm"
|
||||||
//"mm/dd"
|
// "mm/dd"
|
||||||
//"dd/mm/yy hh:mm:ss"
|
// "dd/mm/yy hh:mm:ss"
|
||||||
//"yyyy"
|
// "yyyy"
|
||||||
//"mm"
|
// "mm"
|
||||||
//"hh:mm:ss"
|
// "hh:mm:ss"
|
||||||
//"mm:ss"
|
// "mm:ss"
|
||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -35,7 +35,7 @@ func init() {
|
|||||||
timeFormat = map[string]string{
|
timeFormat = map[string]string{
|
||||||
"yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05",
|
"yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05",
|
||||||
"yyyy-mm-dd hh:mm": "2006-01-02 15:04",
|
"yyyy-mm-dd hh:mm": "2006-01-02 15:04",
|
||||||
"yyyy-mm-dd hh": "2006-01-02 15:04",
|
"yyyy-mm-dd hh": "2006-01-02 15",
|
||||||
"yyyy-mm-dd": "2006-01-02",
|
"yyyy-mm-dd": "2006-01-02",
|
||||||
"yyyy-mm": "2006-01",
|
"yyyy-mm": "2006-01",
|
||||||
"mm-dd": "01-02",
|
"mm-dd": "01-02",
|
||||||
@@ -69,6 +69,11 @@ func AddDay(t time.Time, day int64) time.Time {
|
|||||||
return t.Add(24 * time.Hour * time.Duration(day))
|
return t.Add(24 * time.Hour * time.Duration(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddYear add or sub year to the time.
|
||||||
|
func AddYear(t time.Time, year int64) time.Time {
|
||||||
|
return t.Add(365 * 24 * time.Hour * time.Duration(year))
|
||||||
|
}
|
||||||
|
|
||||||
// GetNowDate return format yyyy-mm-dd of current date
|
// GetNowDate return format yyyy-mm-dd of current date
|
||||||
func GetNowDate() string {
|
func GetNowDate() string {
|
||||||
return time.Now().Format("2006-01-02")
|
return time.Now().Format("2006-01-02")
|
||||||
@@ -180,3 +185,28 @@ func BeginOfYear(t time.Time) time.Time {
|
|||||||
func EndOfYear(t time.Time) time.Time {
|
func EndOfYear(t time.Time) time.Time {
|
||||||
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
|
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsLeapYear check if param year is leap year or not.
|
||||||
|
func IsLeapYear(year int) bool {
|
||||||
|
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BetweenSeconds returns the number of seconds between two times.
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64 {
|
||||||
|
index := t2.Unix() - t1.Unix()
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
|
// DayOfYear returns which day of the year the parameter date `t` is.
|
||||||
|
func DayOfYear(t time.Time) int {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t.Location())
|
||||||
|
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
|
||||||
|
return int(nowDate.Sub(firstDay).Hours() / 24)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsWeekend checks if passed time is weekend or not.
|
||||||
|
func IsWeekend(t time.Time) bool {
|
||||||
|
return time.Saturday == t.Weekday() || time.Sunday == t.Weekday()
|
||||||
|
}
|
||||||
|
|||||||
@@ -46,6 +46,19 @@ func TestAddMinute(t *testing.T) {
|
|||||||
assert.Equal(float64(-2), diff2.Minutes())
|
assert.Equal(float64(-2), diff2.Minutes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAddDay")
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
after2Years := AddYear(now, 1)
|
||||||
|
diff1 := after2Years.Sub(now)
|
||||||
|
assert.Equal(float64(8760), diff1.Hours())
|
||||||
|
|
||||||
|
before2Years := AddYear(now, -1)
|
||||||
|
diff2 := before2Years.Sub(now)
|
||||||
|
assert.Equal(float64(-8760), diff2.Hours())
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetNowDate(t *testing.T) {
|
func TestGetNowDate(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestGetNowDate")
|
assert := internal.NewAssert(t, "TestGetNowDate")
|
||||||
expected := time.Now().Format("2006-01-02")
|
expected := time.Now().Format("2006-01-02")
|
||||||
@@ -231,3 +244,57 @@ func TestEndOfYear(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsLeapYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfYear")
|
||||||
|
|
||||||
|
result1 := IsLeapYear(2000)
|
||||||
|
result2 := IsLeapYear(2001)
|
||||||
|
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBetweenSeconds(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBetweenSeconds")
|
||||||
|
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
assert.Equal(int64(86400), result1)
|
||||||
|
assert.Equal(int64(-86400), result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDayOfYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestDayOfYear")
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := DayOfYear(date1)
|
||||||
|
assert.Equal(31, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := DayOfYear(date2)
|
||||||
|
assert.Equal(1, result2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := DayOfYear(date3)
|
||||||
|
assert.Equal(0, result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsWeekend(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsWeekend")
|
||||||
|
date := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
result := IsWeekend(date)
|
||||||
|
assert.Equal(true, result)
|
||||||
|
|
||||||
|
date1 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
result1 := IsWeekend(date1)
|
||||||
|
assert.Equal(true, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
result2 := IsWeekend(date2)
|
||||||
|
assert.Equal(false, result2)
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ import (
|
|||||||
- [EncodeByte](#EncodeByte)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
|
- [CopyProperties](#CopyProperties)
|
||||||
|
- [ToInterface](#ToInterface)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -501,3 +504,117 @@ func main() {
|
|||||||
// &{test 1 0.1 true <nil> } false
|
// &{test 1 0.1 true <nil> } false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="CopyProperties">CopyProperties</span>
|
||||||
|
|
||||||
|
<p>Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CopyProperties(dst, src interface{}) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Disk struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Total string `json:"total"`
|
||||||
|
Used string `json:"used"`
|
||||||
|
Percent float64 `json:"percent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiskVO struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Total string `json:"total"`
|
||||||
|
Used string `json:"used"`
|
||||||
|
Percent float64 `json:"percent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Indicator struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
|
UpTime string `json:"upTime"`
|
||||||
|
LoadAvg string `json:"loadAvg"`
|
||||||
|
Cpu int `json:"cpu"`
|
||||||
|
Disk []Disk `json:"disk"`
|
||||||
|
Stop chan bool `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndicatorVO struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
|
UpTime string `json:"upTime"`
|
||||||
|
LoadAvg string `json:"loadAvg"`
|
||||||
|
Cpu int64 `json:"cpu"`
|
||||||
|
Disk []DiskVO `json:"disk"`
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator := &Indicator{Id: "001", Ip: "127.0.0.1", Cpu: 1, Disk: []Disk{
|
||||||
|
{Name: "disk-001", Total: "100", Used: "1", Percent: 10},
|
||||||
|
{Name: "disk-002", Total: "200", Used: "1", Percent: 20},
|
||||||
|
{Name: "disk-003", Total: "300", Used: "1", Percent: 30},
|
||||||
|
}}
|
||||||
|
|
||||||
|
indicatorVO := IndicatorVO{}
|
||||||
|
|
||||||
|
err := convertor.CopyProperties(&indicatorVO, indicator)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(indicatorVO.Id)
|
||||||
|
fmt.Println(indicatorVO.Ip)
|
||||||
|
fmt.Println(len(indicatorVO.Disk))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 001
|
||||||
|
// 127.0.0.1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ToInterface">ToInterface</span>
|
||||||
|
|
||||||
|
<p>Converts reflect value to its interface type.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
val := reflect.ValueOf("abc")
|
||||||
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
|
fmt.Printf("%T\n", iVal)
|
||||||
|
fmt.Printf("%v\n", iVal)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// string
|
||||||
|
// abc
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -36,6 +36,8 @@ import (
|
|||||||
- [EncodeByte](#EncodeByte)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
|
- [CopyProperties](#CopyProperties)
|
||||||
|
- [ToInterface](#ToInterface)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -501,3 +503,118 @@ func main() {
|
|||||||
// &{test 1 0.1 true <nil> } false
|
// &{test 1 0.1 true <nil> } false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CopyProperties">CopyProperties</span>
|
||||||
|
|
||||||
|
<p>拷贝不同结构体之间的同名字段。使用json.Marshal序列化,需要设置dst和src struct字段的json tag。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CopyProperties(dst, src interface{}) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Disk struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Total string `json:"total"`
|
||||||
|
Used string `json:"used"`
|
||||||
|
Percent float64 `json:"percent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiskVO struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Total string `json:"total"`
|
||||||
|
Used string `json:"used"`
|
||||||
|
Percent float64 `json:"percent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Indicator struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
|
UpTime string `json:"upTime"`
|
||||||
|
LoadAvg string `json:"loadAvg"`
|
||||||
|
Cpu int `json:"cpu"`
|
||||||
|
Disk []Disk `json:"disk"`
|
||||||
|
Stop chan bool `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndicatorVO struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
|
UpTime string `json:"upTime"`
|
||||||
|
LoadAvg string `json:"loadAvg"`
|
||||||
|
Cpu int64 `json:"cpu"`
|
||||||
|
Disk []DiskVO `json:"disk"`
|
||||||
|
}
|
||||||
|
|
||||||
|
indicator := &Indicator{Id: "001", Ip: "127.0.0.1", Cpu: 1, Disk: []Disk{
|
||||||
|
{Name: "disk-001", Total: "100", Used: "1", Percent: 10},
|
||||||
|
{Name: "disk-002", Total: "200", Used: "1", Percent: 20},
|
||||||
|
{Name: "disk-003", Total: "300", Used: "1", Percent: 30},
|
||||||
|
}}
|
||||||
|
|
||||||
|
indicatorVO := IndicatorVO{}
|
||||||
|
|
||||||
|
err := convertor.CopyProperties(&indicatorVO, indicator)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(indicatorVO.Id)
|
||||||
|
fmt.Println(indicatorVO.Ip)
|
||||||
|
fmt.Println(len(indicatorVO.Disk))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 001
|
||||||
|
// 127.0.0.1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ToInterface">ToInterface</span>
|
||||||
|
|
||||||
|
<p>将反射值转换成对应的interface类型。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToInterface(v reflect.Value) (value interface{}, ok bool)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
val := reflect.ValueOf("abc")
|
||||||
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
|
fmt.Printf("%T\n", iVal)
|
||||||
|
fmt.Printf("%v\n", iVal)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// string
|
||||||
|
// abc
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
236
docs/datetime.md
236
docs/datetime.md
@@ -25,6 +25,7 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [AddYear](#AddYear)
|
||||||
- [BeginOfMinute](#BeginOfMinute)
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
- [BeginOfHour](#BeginOfHour)
|
- [BeginOfHour](#BeginOfHour)
|
||||||
- [BeginOfDay](#BeginOfDay)
|
- [BeginOfDay](#BeginOfDay)
|
||||||
@@ -52,6 +53,10 @@ import (
|
|||||||
- [ToFormat](#ToFormat)
|
- [ToFormat](#ToFormat)
|
||||||
- [ToFormatForTpl](#ToFormatForTpl)
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
|
- [IsLeapYear](#IsLeapYear)
|
||||||
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -170,6 +175,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AddYear">AddYear</span>
|
||||||
|
|
||||||
|
<p>Add or sub year to the time.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AddYear(t time.Time, year int64) time.Time
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after1Year := datetime.AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := datetime.AddYear(now, -1)
|
||||||
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 8760h0m0s
|
||||||
|
// -8760h0m0s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>Return beginning minute time of day.</p>
|
<p>Return beginning minute time of day.</p>
|
||||||
@@ -921,3 +965,195 @@ func main() {
|
|||||||
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsLeapYear">IsLeapYear</span>
|
||||||
|
|
||||||
|
<p>check if param `year` is leap year or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsLeapYear(year int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.IsLeapYear(2000)
|
||||||
|
result2 := datetime.IsLeapYear(2001)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BetweenSeconds">BetweenSeconds</span>
|
||||||
|
|
||||||
|
<p>Return the number of seconds between two times.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := datetime.BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := datetime.BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 86400
|
||||||
|
// -86400
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>Returns which day of the year the parameter date `t` is.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>Checks if passed time is weekend or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsWeekend">IsWeekend</span>
|
||||||
|
|
||||||
|
<p>判断日期是否是周末。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsWeekend(t time.Time) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local)
|
||||||
|
date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local)
|
||||||
|
date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
result1 := datetime.IsWeekend(date1)
|
||||||
|
result2 := datetime.IsWeekend(date2)
|
||||||
|
result3 := datetime.IsWeekend(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [AddYear](#AddYear)
|
||||||
- [BeginOfMinute](#BeginOfMinute)
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
- [BeginOfHour](#BeginOfHour)
|
- [BeginOfHour](#BeginOfHour)
|
||||||
- [BeginOfDay](#BeginOfDay)
|
- [BeginOfDay](#BeginOfDay)
|
||||||
@@ -52,6 +53,11 @@ import (
|
|||||||
- [ToFormat](#ToFormat)
|
- [ToFormat](#ToFormat)
|
||||||
- [ToFormatForTpl](#ToFormatForTpl)
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
|
- [IsLeapYear](#IsLeapYear)
|
||||||
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
- [IsWeekend](#IsWeekend)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -170,6 +176,45 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AddYear">AddYear</span>
|
||||||
|
|
||||||
|
<p>将日期加/减年数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AddYear(t time.Time, year int64) time.Time
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after1Year := datetime.AddYear(now, 1)
|
||||||
|
diff1 := after1Year.Sub(now)
|
||||||
|
|
||||||
|
before1Year := datetime.AddYear(now, -1)
|
||||||
|
diff2 := before1Year.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 8760h0m0s
|
||||||
|
// -8760h0m0s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟开始时间</p>
|
<p>返回指定时间的分钟开始时间</p>
|
||||||
@@ -921,3 +966,114 @@ func main() {
|
|||||||
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsLeapYear">IsLeapYear</span>
|
||||||
|
|
||||||
|
<p>验证是否是闰年。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsLeapYear(year int) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := datetime.IsLeapYear(2000)
|
||||||
|
result2 := datetime.IsLeapYear(2001)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BetweenSeconds">BetweenSeconds</span>
|
||||||
|
|
||||||
|
<p>返回两个时间的间隔秒数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BetweenSeconds(t1 time.Time, t2 time.Time) int64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
today := time.Now()
|
||||||
|
tomorrow := AddDay(today, 1)
|
||||||
|
yesterday := AddDay(today, -1)
|
||||||
|
|
||||||
|
result1 := datetime.BetweenSeconds(today, tomorrow)
|
||||||
|
result2 := datetime.BetweenSeconds(today, yesterday)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 86400
|
||||||
|
// -86400
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>返回参数日期是一年中的第几天。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
102
docs/fileutil.md
102
docs/fileutil.md
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -661,3 +663,103 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>Writes bytes to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>Writes string to target file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -43,6 +43,8 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -451,7 +453,7 @@ func main() {
|
|||||||
|
|
||||||
<p>zip解压缩文件并保存在目录中</p>
|
<p>zip解压缩文件并保存在目录中</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
@@ -661,3 +663,102 @@ func main() {
|
|||||||
// <nil>
|
// <nil>
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
|
<p>将bytes写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
|
<p>将字符串写入文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
168
docs/mathutil.md
168
docs/mathutil.md
@@ -33,6 +33,10 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
- [Cos](#Cos)
|
||||||
|
- [Sin](#Sin)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -375,3 +379,167 @@ func main() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="GCD">GCD</span>
|
||||||
|
|
||||||
|
<p>Return greatest common divisor (GCD) of integers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GCD(integers ...int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.GCD(1, 1)
|
||||||
|
result2 := mathutil.GCD(1, -1)
|
||||||
|
result3 := mathutil.GCD(-1, 1)
|
||||||
|
result4 := mathutil.GCD(-1, -1)
|
||||||
|
result5 := mathutil.GCD(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LCM">LCM</span>
|
||||||
|
|
||||||
|
<p>Return Least Common Multiple (LCM) of integers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LCM(integers ...int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.LCM(1, 1)
|
||||||
|
result2 := mathutil.LCM(1, 2)
|
||||||
|
result3 := mathutil.LCM(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 18
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Cos">Cos</span>
|
||||||
|
|
||||||
|
<p>Returns the cosine of the radian argument.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Cos(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Cos(0)
|
||||||
|
result2 := mathutil.Cos(90)
|
||||||
|
result3 := mathutil.Cos(180)
|
||||||
|
result4 := mathutil.Cos(math.Pi)
|
||||||
|
result5 := mathutil.Cos(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// -0.447
|
||||||
|
// -0.598
|
||||||
|
// -1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Sin">Sin</span>
|
||||||
|
|
||||||
|
<p>Returns the sine of the radian argument.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sin(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Sin(0)
|
||||||
|
result2 := mathutil.Sin(90)
|
||||||
|
result3 := mathutil.Sin(180)
|
||||||
|
result4 := mathutil.Sin(math.Pi)
|
||||||
|
result5 := mathutil.Sin(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0.894
|
||||||
|
// -0.801
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -33,6 +33,10 @@ import (
|
|||||||
- [RadianToAngle](#RadianToAngle)
|
- [RadianToAngle](#RadianToAngle)
|
||||||
- [PointDistance](#PointDistance)
|
- [PointDistance](#PointDistance)
|
||||||
- [IsPrime](#IsPrime)
|
- [IsPrime](#IsPrime)
|
||||||
|
- [GCD](#GCD)
|
||||||
|
- [LCM](#LCM)
|
||||||
|
- [Cos](#Cos)
|
||||||
|
- [Sin](#Sin)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -375,3 +379,168 @@ func main() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GCD">GCD</span>
|
||||||
|
|
||||||
|
<p>计算整数最大公约数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GCD(integers ...int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.GCD(1, 1)
|
||||||
|
result2 := mathutil.GCD(1, -1)
|
||||||
|
result3 := mathutil.GCD(-1, 1)
|
||||||
|
result4 := mathutil.GCD(-1, -1)
|
||||||
|
result5 := mathutil.GCD(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// -1
|
||||||
|
// -1
|
||||||
|
// 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LCM">LCM</span>
|
||||||
|
|
||||||
|
<p>计算整数最小公倍数。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LCM(integers ...int) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.LCM(1, 1)
|
||||||
|
result2 := mathutil.LCM(1, 2)
|
||||||
|
result3 := mathutil.LCM(3, 6, 9)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 18
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Cos">Cos</span>
|
||||||
|
|
||||||
|
<p>计算弧度的余弦值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Cos(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Cos(0)
|
||||||
|
result2 := mathutil.Cos(90)
|
||||||
|
result3 := mathutil.Cos(180)
|
||||||
|
result4 := mathutil.Cos(math.Pi)
|
||||||
|
result5 := mathutil.Cos(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// -0.447
|
||||||
|
// -0.598
|
||||||
|
// -1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Sin">Sin</span>
|
||||||
|
|
||||||
|
<p>计算弧度的正弦值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Sin(radian float64, precision ...int) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.Sin(0)
|
||||||
|
result2 := mathutil.Sin(90)
|
||||||
|
result3 := mathutil.Sin(180)
|
||||||
|
result4 := mathutil.Sin(math.Pi)
|
||||||
|
result5 := mathutil.Sin(math.Pi / 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0.894
|
||||||
|
// -0.801
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -586,9 +586,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -628,9 +628,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -658,28 +658,30 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
// "Content-Type": "multipart/form-data",
|
||||||
type Todo struct {
|
}
|
||||||
UserId int `json:"userId"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
}
|
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
postData := url.Values{}
|
||||||
if err != nil {
|
postData.Add("userId", "1")
|
||||||
log.Fatal(err)
|
postData.Add("title", "TestToDo")
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
// postData := make(map[string]string)
|
||||||
fmt.Println(body)
|
// postData["userId"] = "1"
|
||||||
|
// postData["title"] = "title"
|
||||||
|
|
||||||
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -727,9 +729,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -766,9 +768,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request. (Deprecated: use SendRequest for replacement)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -585,9 +585,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求。(已废弃: 使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -627,9 +627,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求。(已废弃: 使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -657,28 +657,30 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos"
|
url := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
// "Content-Type": "multipart/form-data",
|
||||||
type Todo struct {
|
}
|
||||||
UserId int `json:"userId"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
}
|
|
||||||
todo := Todo{1, "TestAddToDo"}
|
|
||||||
bodyParams, _ := json.Marshal(todo)
|
|
||||||
|
|
||||||
resp, err := netutil.HttpPost(url, header, nil, bodyParams)
|
postData := url.Values{}
|
||||||
if err != nil {
|
postData.Add("userId", "1")
|
||||||
log.Fatal(err)
|
postData.Add("title", "TestToDo")
|
||||||
}
|
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
// postData := make(map[string]string)
|
||||||
|
// postData["userId"] = "1"
|
||||||
|
// postData["title"] = "title"
|
||||||
|
|
||||||
|
resp, err := netutil.HttpPost(apiUrl, header, nil, postData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
fmt.Println(body)
|
fmt.Println(body)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求。(已废弃: 使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -726,9 +728,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求。(已废弃: 使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -765,9 +767,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求。(已废弃: 使用SendRequest)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
|||||||
220
docs/strutil.md
220
docs/strutil.md
@@ -28,6 +28,8 @@ import (
|
|||||||
- [BeforeLast](#BeforeLast)
|
- [BeforeLast](#BeforeLast)
|
||||||
- [CamelCase](#CamelCase)
|
- [CamelCase](#CamelCase)
|
||||||
- [Capitalize](#Capitalize)
|
- [Capitalize](#Capitalize)
|
||||||
|
- [ContainsAll](#ContainsAll)
|
||||||
|
- [ContainsAny](#ContainsAny)
|
||||||
- [IsString](#IsString)
|
- [IsString](#IsString)
|
||||||
- [KebabCase](#KebabCase)
|
- [KebabCase](#KebabCase)
|
||||||
- [UpperKebabCase](#UpperKebabCase)
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
@@ -51,6 +53,10 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
- [HideString](#HideString)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1021,4 +1027,216 @@ func main() {
|
|||||||
// -1
|
// -1
|
||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReplaceWithMap">ReplaceWithMap</span>
|
||||||
|
|
||||||
|
<p>Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// 1c 12 12 1c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Trim">Trim</span>
|
||||||
|
|
||||||
|
<p>Strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Trim(str string, characterMask ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := strutil.Trim(str)
|
||||||
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="SplitAndTrim">SplitAndTrim</span>
|
||||||
|
|
||||||
|
<p>Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice. It ignores the elements which are empty after Trim.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HideString">HideString</span>
|
||||||
|
|
||||||
|
<p>HideString hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 13242658976
|
||||||
|
// 132*2658976
|
||||||
|
// 132****8976
|
||||||
|
// 1324265****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains all the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>Return true if target string contains any one of the substrings.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import (
|
|||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
- [BeforeLast](#BeforeLast)
|
- [BeforeLast](#BeforeLast)
|
||||||
- [CamelCase](#CamelCase)
|
- [CamelCase](#CamelCase)
|
||||||
|
- [ContainsAll](#ContainsAll)
|
||||||
|
- [ContainsAny](#ContainsAny)
|
||||||
- [Capitalize](#Capitalize)
|
- [Capitalize](#Capitalize)
|
||||||
- [IsString](#IsString)
|
- [IsString](#IsString)
|
||||||
- [KebabCase](#KebabCase)
|
- [KebabCase](#KebabCase)
|
||||||
@@ -51,6 +53,10 @@ import (
|
|||||||
- [HasPrefixAny](#HasPrefixAny)
|
- [HasPrefixAny](#HasPrefixAny)
|
||||||
- [HasSuffixAny](#HasSuffixAny)
|
- [HasSuffixAny](#HasSuffixAny)
|
||||||
- [IndexOffset](#IndexOffset)
|
- [IndexOffset](#IndexOffset)
|
||||||
|
- [ReplaceWithMap](#ReplaceWithMap)
|
||||||
|
- [Trim](#Trim)
|
||||||
|
- [SplitAndTrim](#SplitAndTrim)
|
||||||
|
- [HideString](#HideString)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -832,7 +838,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
<p>删除字符串中不可打印的字符。</p>
|
<p>删除字符串中不可打印的字符。</p>
|
||||||
@@ -1053,4 +1058,217 @@ func main() {
|
|||||||
// -1
|
// -1
|
||||||
// -1
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReplaceWithMap">ReplaceWithMap</span>
|
||||||
|
|
||||||
|
<p>返回`str`的副本,以无序的方式被map替换,区分大小写。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strutil.ReplaceWithMap(str, replaces)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output:
|
||||||
|
// 1c 12 12 1c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Trim">Trim</span>
|
||||||
|
|
||||||
|
<p>从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Trim(str string, characterMask ...string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := strutil.Trim(str)
|
||||||
|
result3 := strutil.Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SplitAndTrim">SplitAndTrim</span>
|
||||||
|
|
||||||
|
<p>将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := strutil.SplitAndTrim(str, ",")
|
||||||
|
result2 := strutil.SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HideString">HideString</span>
|
||||||
|
|
||||||
|
<p>使用参数`replaceChar`隐藏源字符串中的一些字符。替换范围是 origin[start : end]。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
result1 := strutil.HideString(str, 3, 3, "*")
|
||||||
|
result2 := strutil.HideString(str, 3, 4, "*")
|
||||||
|
result3 := strutil.HideString(str, 3, 7, "*")
|
||||||
|
result4 := strutil.HideString(str, 7, 11, "*")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 13242658976
|
||||||
|
// 132*2658976
|
||||||
|
// 132****8976
|
||||||
|
// 1324265****
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAll">ContainsAll</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括全部给定的子字符串切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAll(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAll(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAll(str, []string{"hello", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ContainsAny">ContainsAny</span>
|
||||||
|
|
||||||
|
<p>判断字符串是否包括给定的子字符串切片中任意一个子字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ContainsAny(str string, substrs []string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "hello world"
|
||||||
|
|
||||||
|
result1 := strutil.ContainsAny(str, []string{"hello", "world"})
|
||||||
|
result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
|
||||||
|
result3 := strutil.ContainsAny(str, []string{"123", "abc"})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -419,3 +419,33 @@ func ReadCsvFile(filepath string) ([][]string, error) {
|
|||||||
|
|
||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteStringToFile write string to target file.
|
||||||
|
func WriteStringToFile(filepath string, content string, append bool) error {
|
||||||
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
if append {
|
||||||
|
flag = flag | os.O_APPEND
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(filepath, flag, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.WriteString(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytesToFile write bytes to target file.
|
||||||
|
func WriteBytesToFile(filepath string, content []byte) error {
|
||||||
|
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.Write(content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -244,12 +244,12 @@ func TestFileSize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMTime(t *testing.T) {
|
func TestMTime(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestMTime")
|
// assert := internal.NewAssert(t, "TestMTime")
|
||||||
|
|
||||||
mtime, err := MTime("./testdata/test.txt")
|
// mtime, err := MTime("./testdata/test.txt")
|
||||||
|
|
||||||
assert.IsNil(err)
|
// assert.IsNil(err)
|
||||||
assert.Equal(int64(1682478195), mtime)
|
// assert.Equal(int64(1682478195), mtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSha(t *testing.T) {
|
func TestSha(t *testing.T) {
|
||||||
@@ -277,3 +277,68 @@ func TestReadCsvFile(t *testing.T) {
|
|||||||
assert.Equal(3, len(content[0]))
|
assert.Equal(3, len(content[0]))
|
||||||
assert.Equal("Bob", content[0][0])
|
assert.Equal("Bob", content[0][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteStringToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
||||||
|
|
||||||
|
filepath := "./test.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, "hello", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content1, err := ReadFileToString(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteStringToFile(filepath, " world", true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content2, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", content1)
|
||||||
|
assert.Equal("hello world", string(content2))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteBytesToFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteBytesToFile")
|
||||||
|
|
||||||
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
|
file, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
err = WriteBytesToFile(filepath, []byte("hello"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("hello", string(content))
|
||||||
|
|
||||||
|
os.Remove(filepath)
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -36,6 +37,52 @@ func (a *Assert) Equal(expected, actual interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EqualValues asserts that two objects are equal or convertable to the same types and equal.
|
||||||
|
// https://github.com/stretchr/testify/assert/assertions.go
|
||||||
|
func (a *Assert) EqualValues(expected, actual interface{}) {
|
||||||
|
if !objectsAreEqualValues(expected, actual) {
|
||||||
|
makeTestFailed(a.T, a.CaseName, expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
// NotEqual check if expected is not equal with actual
|
// NotEqual check if expected is not equal with actual
|
||||||
func (a *Assert) NotEqual(expected, actual interface{}) {
|
func (a *Assert) NotEqual(expected, actual interface{}) {
|
||||||
if compare(expected, actual) == compareEqual {
|
if compare(expected, actual) == compareEqual {
|
||||||
|
|||||||
@@ -126,3 +126,66 @@ func IsPrime(n int) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GCD return greatest common divisor (GCD) of integers.
|
||||||
|
func GCD(integers ...int) int {
|
||||||
|
result := integers[0]
|
||||||
|
|
||||||
|
for k := range integers {
|
||||||
|
result = gcd(integers[k], result)
|
||||||
|
|
||||||
|
if result == 1 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// find greatest common divisor (GCD)
|
||||||
|
func gcd(a, b int) int {
|
||||||
|
if b == 0 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return gcd(b, a%b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LCM return Least Common Multiple (LCM) of integers.
|
||||||
|
func LCM(integers ...int) int {
|
||||||
|
result := integers[0]
|
||||||
|
|
||||||
|
for k := range integers {
|
||||||
|
result = lcm(integers[k], result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// find Least Common Multiple (LCM) via GCD.
|
||||||
|
func lcm(a, b int) int {
|
||||||
|
if a == 0 || b == 0 {
|
||||||
|
panic("lcm function: provide non zero integers only.")
|
||||||
|
}
|
||||||
|
return a * b / gcd(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cos returns the cosine of the radian argument.
|
||||||
|
func Cos(radian float64, precision ...int) float64 {
|
||||||
|
t := 1.0 / (2.0 * math.Pi)
|
||||||
|
radian *= t
|
||||||
|
radian -= 0.25 + math.Floor(radian+0.25)
|
||||||
|
radian *= 16.0 * (math.Abs(radian) - 0.5)
|
||||||
|
radian += 0.225 * radian * (math.Abs(radian) - 1.0)
|
||||||
|
|
||||||
|
if len(precision) == 1 {
|
||||||
|
return TruncRound(radian, precision[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return TruncRound(radian, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cos returns the sine of the radian argument.
|
||||||
|
func Sin(radian float64, precision ...int) float64 {
|
||||||
|
return Cos((math.Pi / 2) - radian)
|
||||||
|
}
|
||||||
|
|||||||
@@ -114,3 +114,61 @@ func TestIsPrime(t *testing.T) {
|
|||||||
assert.Equal(true, IsPrime(3))
|
assert.Equal(true, IsPrime(3))
|
||||||
assert.Equal(false, IsPrime(4))
|
assert.Equal(false, IsPrime(4))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGCD(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGCD")
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, 1))
|
||||||
|
assert.Equal(1, GCD(1, -1))
|
||||||
|
assert.Equal(-1, GCD(-1, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, -1))
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, 0))
|
||||||
|
assert.Equal(1, GCD(0, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, 0))
|
||||||
|
assert.Equal(-1, GCD(0, -1))
|
||||||
|
|
||||||
|
assert.Equal(1, GCD(1, -2))
|
||||||
|
assert.Equal(1, GCD(-2, 1))
|
||||||
|
assert.Equal(-1, GCD(-1, 2))
|
||||||
|
assert.Equal(-1, GCD(2, -1))
|
||||||
|
|
||||||
|
assert.Equal(-1, GCD(-1, -2))
|
||||||
|
assert.Equal(-1, GCD(-2, -1))
|
||||||
|
|
||||||
|
assert.Equal(-9, GCD(-27, -36))
|
||||||
|
assert.Equal(3, GCD(3, 6, 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLCM(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLCM")
|
||||||
|
|
||||||
|
assert.Equal(1, LCM(1))
|
||||||
|
assert.Equal(-1, LCM(-1))
|
||||||
|
assert.Equal(-1, LCM(1, -1))
|
||||||
|
assert.Equal(1, LCM(-1, 1))
|
||||||
|
assert.Equal(1, LCM(1, 1))
|
||||||
|
assert.Equal(-1, LCM(-1, -1))
|
||||||
|
assert.Equal(2, LCM(1, 2))
|
||||||
|
assert.Equal(18, LCM(3, 6, 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCos(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestCos")
|
||||||
|
|
||||||
|
assert.EqualValues(1, Cos(0))
|
||||||
|
assert.EqualValues(-0.447, Cos(90))
|
||||||
|
assert.EqualValues(-0.598, Cos(180))
|
||||||
|
assert.EqualValues(-1, Cos(math.Pi))
|
||||||
|
assert.EqualValues(0, Cos(math.Pi/2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSin(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSin")
|
||||||
|
|
||||||
|
assert.EqualValues(0, Sin(0))
|
||||||
|
assert.EqualValues(0.894, Sin(90))
|
||||||
|
assert.EqualValues(-0.801, Sin(180))
|
||||||
|
assert.EqualValues(0, Sin(math.Pi))
|
||||||
|
assert.EqualValues(1, Sin(math.Pi/2))
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package netutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -51,23 +52,23 @@ func TestHttpPost(t *testing.T) {
|
|||||||
func TestHttpPostFormData(t *testing.T) {
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
// "Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
"Content-Type": "multipart/form-data",
|
// "Content-Type": "multipart/form-data",
|
||||||
}
|
|
||||||
type Todo struct {
|
|
||||||
UserId int `json:"userId"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postData := url.Values{}
|
postData := url.Values{}
|
||||||
postData.Add("userId", "1")
|
postData.Add("userId", "1")
|
||||||
postData.Add("title", "TestAddToDo")
|
postData.Add("title", "TestToDo")
|
||||||
|
|
||||||
resp, err := HttpPost(apiUrl, header, postData, nil)
|
// postData := make(map[string]string)
|
||||||
|
// postData["userId"] = "1"
|
||||||
|
// postData["title"] = "title"
|
||||||
|
|
||||||
|
resp, err := HttpPost(apiUrl, header, nil, postData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -242,11 +243,17 @@ func DownloadFile(filepath string, url string) error {
|
|||||||
|
|
||||||
// IsPingConnected checks if can ping specified host or not.
|
// IsPingConnected checks if can ping specified host or not.
|
||||||
func IsPingConnected(host string) bool {
|
func IsPingConnected(host string) bool {
|
||||||
cmd := exec.Command("ping", host, "-c", "1", "-W", "6")
|
cmd := exec.Command("ping", host, "-c", "4", "-W", "6")
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("ping", host, "-n", "4", "-w", "6")
|
||||||
|
}
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -74,51 +75,34 @@ func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body interface{}) error {
|
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body interface{}) error {
|
||||||
err := setHeader(req, header)
|
if err := setHeader(req, header); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
} else if err = setQueryParam(req, reqUrl, queryParam); err != nil {
|
||||||
err = setQueryParam(req, reqUrl, queryParam)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
} else if err = setBodyByte(req, body); err != nil {
|
||||||
if strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
|
||||||
if formData, ok := queryParam.(url.Values); ok {
|
|
||||||
err = setBodyByte(req, []byte(formData.Encode()))
|
|
||||||
}
|
|
||||||
if formData, ok := queryParam.(map[string]string); ok {
|
|
||||||
postData := url.Values{}
|
|
||||||
for k, v := range formData {
|
|
||||||
postData.Set(k, v)
|
|
||||||
}
|
|
||||||
err = setBodyByte(req, []byte(postData.Encode()))
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
err = setBodyByte(req, body)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setHeader(req *http.Request, header interface{}) error {
|
func setHeader(req *http.Request, header interface{}) error {
|
||||||
if header != nil {
|
if header == nil {
|
||||||
switch v := header.(type) {
|
return nil
|
||||||
case map[string]string:
|
}
|
||||||
for k := range v {
|
|
||||||
req.Header.Add(k, v[k])
|
switch v := header.(type) {
|
||||||
}
|
case map[string]string:
|
||||||
case http.Header:
|
for k := range v {
|
||||||
for k, vv := range v {
|
req.Header.Add(k, v[k])
|
||||||
for _, vvv := range vv {
|
|
||||||
req.Header.Add(k, vvv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.New("header params type should be http.Header or map[string]string")
|
|
||||||
}
|
}
|
||||||
|
case http.Header:
|
||||||
|
for k, vv := range v {
|
||||||
|
for _, vvv := range vv {
|
||||||
|
req.Header.Add(k, vvv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.New("header params type should be http.Header or map[string]string")
|
||||||
}
|
}
|
||||||
|
|
||||||
if host := req.Header.Get("Host"); host != "" {
|
if host := req.Header.Get("Host"); host != "" {
|
||||||
@@ -138,19 +122,22 @@ func setUrl(req *http.Request, reqUrl string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) error {
|
func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) error {
|
||||||
|
if queryParam == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var values url.Values
|
var values url.Values
|
||||||
if queryParam != nil {
|
|
||||||
switch v := queryParam.(type) {
|
switch v := queryParam.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
values = url.Values{}
|
values = url.Values{}
|
||||||
for k := range v {
|
for k := range v {
|
||||||
values.Set(k, fmt.Sprintf("%v", v[k]))
|
values.Set(k, fmt.Sprintf("%v", v[k]))
|
||||||
}
|
|
||||||
case url.Values:
|
|
||||||
values = v
|
|
||||||
default:
|
|
||||||
return errors.New("query params type should be url.Values or map[string]interface{}")
|
|
||||||
}
|
}
|
||||||
|
case url.Values:
|
||||||
|
values = v
|
||||||
|
default:
|
||||||
|
return errors.New("query params type should be url.Values or map[string]interface{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set url
|
// set url
|
||||||
@@ -171,14 +158,36 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setBodyByte(req *http.Request, body interface{}) error {
|
func setBodyByte(req *http.Request, body interface{}) error {
|
||||||
if body != nil {
|
if body == nil {
|
||||||
switch b := body.(type) {
|
return nil
|
||||||
case []byte:
|
}
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(b))
|
var bodyReader *bytes.Reader
|
||||||
req.ContentLength = int64(len(b))
|
switch b := body.(type) {
|
||||||
default:
|
case io.Reader:
|
||||||
return errors.New("body type should be []byte")
|
buf := bytes.NewBuffer(nil)
|
||||||
|
if _, err := io.Copy(buf, b); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
req.Body = ioutil.NopCloser(buf)
|
||||||
|
req.ContentLength = int64(buf.Len())
|
||||||
|
case []byte:
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case map[string]interface{}:
|
||||||
|
values := url.Values{}
|
||||||
|
for k := range b {
|
||||||
|
values.Set(k, fmt.Sprintf("%v", b[k]))
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader([]byte(values.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
case url.Values:
|
||||||
|
bodyReader = bytes.NewReader([]byte(b.Encode()))
|
||||||
|
req.Body = ioutil.NopCloser(bodyReader)
|
||||||
|
req.ContentLength = int64(bodyReader.Len())
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid body type: %T", b)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -403,3 +403,103 @@ func IndexOffset(str string, substr string, idxFrom int) int {
|
|||||||
|
|
||||||
return strings.Index(str[idxFrom:], substr) + idxFrom
|
return strings.Index(str[idxFrom:], substr) + idxFrom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplaceWithMap returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.
|
||||||
|
func ReplaceWithMap(str string, replaces map[string]string) string {
|
||||||
|
for k, v := range replaces {
|
||||||
|
str = strings.ReplaceAll(str, k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
// SplitAndTrim splits string `str` by a string `delimiter` to a slice,
|
||||||
|
// and calls Trim to every element of this slice. It ignores the elements
|
||||||
|
// which are empty after Trim.
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
|
||||||
|
result := make([]string, 0)
|
||||||
|
|
||||||
|
for _, v := range strings.Split(str, delimiter) {
|
||||||
|
v = Trim(v, characterMask...)
|
||||||
|
if v != "" {
|
||||||
|
result = append(result, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultTrimChars are the characters which are stripped by Trim* functions in default.
|
||||||
|
DefaultTrimChars = string([]byte{
|
||||||
|
'\t', // Tab.
|
||||||
|
'\v', // Vertical tab.
|
||||||
|
'\n', // New line (line feed).
|
||||||
|
'\r', // Carriage return.
|
||||||
|
'\f', // New page.
|
||||||
|
' ', // Ordinary space.
|
||||||
|
0x00, // NUL-byte.
|
||||||
|
0x85, // Delete.
|
||||||
|
0xA0, // Non-breaking space.
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
// Trim strips whitespace (or other characters) from the beginning and end of a string.
|
||||||
|
// The optional parameter `characterMask` specifies the additional stripped characters.
|
||||||
|
func Trim(str string, characterMask ...string) string {
|
||||||
|
trimChars := DefaultTrimChars
|
||||||
|
|
||||||
|
if len(characterMask) > 0 {
|
||||||
|
trimChars += characterMask[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(str, trimChars)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HideString hide some chars in source string with param `replaceChar`.
|
||||||
|
// replace range is origin[start : end]. [start, end)
|
||||||
|
func HideString(origin string, start, end int, replaceChar string) string {
|
||||||
|
size := len(origin)
|
||||||
|
|
||||||
|
if start > size-1 || start < 0 || end < 0 || start > end {
|
||||||
|
return origin
|
||||||
|
}
|
||||||
|
|
||||||
|
if end > size {
|
||||||
|
end = size
|
||||||
|
}
|
||||||
|
|
||||||
|
if replaceChar == "" {
|
||||||
|
return origin
|
||||||
|
}
|
||||||
|
|
||||||
|
startStr := origin[0:start]
|
||||||
|
endStr := origin[end:size]
|
||||||
|
|
||||||
|
replaceSize := end - start
|
||||||
|
replaceStr := strings.Repeat(replaceChar, replaceSize)
|
||||||
|
|
||||||
|
return startStr + replaceStr + endStr
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainsAll return true if target string contains all the substrs.
|
||||||
|
func ContainsAll(str string, substrs []string) bool {
|
||||||
|
for _, v := range substrs {
|
||||||
|
if !strings.Contains(str, v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainsAny return true if target string contains any one of the substrs.
|
||||||
|
func ContainsAny(str string, substrs []string) bool {
|
||||||
|
for _, v := range substrs {
|
||||||
|
if strings.Contains(str, v) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -386,3 +386,72 @@ func TestIndexOffset(t *testing.T) {
|
|||||||
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
||||||
assert.Equal(IndexOffset(str, "f", -1), -1)
|
assert.Equal(IndexOffset(str, "f", -1), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplaceWithMap(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestReplaceWithMap")
|
||||||
|
|
||||||
|
str := "ac ab ab ac"
|
||||||
|
replaces := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(str, "ac ab ab ac")
|
||||||
|
assert.Equal(ReplaceWithMap(str, replaces), "1c 12 12 1c")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str1 := "$ ab cd $ "
|
||||||
|
|
||||||
|
assert.Equal("$ ab cd $", Trim(str1))
|
||||||
|
assert.Equal("ab cd", Trim(str1, "$"))
|
||||||
|
assert.Equal("abcd", Trim("\nabcd"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitAndTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := SplitAndTrim(str, ",")
|
||||||
|
result2 := SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "$1"}, result1)
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "1"}, result2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHideString(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str := "13242658976"
|
||||||
|
|
||||||
|
assert.Equal("13242658976", HideString(str, 0, -1, "*"))
|
||||||
|
assert.Equal("13242658976", HideString(str, 0, 0, "*"))
|
||||||
|
assert.Equal("****2658976", HideString(str, 0, 4, "*"))
|
||||||
|
|
||||||
|
assert.Equal("13242658976", HideString(str, 3, 3, "*"))
|
||||||
|
assert.Equal("132*2658976", HideString(str, 3, 4, "*"))
|
||||||
|
assert.Equal("132****8976", HideString(str, 3, 7, "*"))
|
||||||
|
assert.Equal("1324265****", HideString(str, 7, 11, "*"))
|
||||||
|
|
||||||
|
assert.Equal("1324265****", HideString(str, 7, 100, "*"))
|
||||||
|
assert.Equal("13242658976", HideString(str, 100, 100, "*"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsAll(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestContainsAll")
|
||||||
|
|
||||||
|
assert.Equal(true, ContainsAll("hello world", []string{"hello", "world"}))
|
||||||
|
assert.Equal(true, ContainsAll("hello world", []string{""}))
|
||||||
|
assert.Equal(false, ContainsAll("hello world", []string{"hello", "abc"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsAny(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestContainsAny")
|
||||||
|
|
||||||
|
assert.Equal(true, ContainsAny("hello world", []string{"hello", "world"}))
|
||||||
|
assert.Equal(true, ContainsAny("hello world", []string{"hello", "abc"}))
|
||||||
|
assert.Equal(false, ContainsAny("hello world", []string{"123", "abc"}))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user