mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 14:12:28 +08:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac0fb5ef25 | ||
|
|
9bd1c205fe | ||
|
|
6ccf9fd3cf | ||
|
|
c02ef2c62d | ||
|
|
6414031754 | ||
|
|
5336130570 | ||
|
|
bfa46d46a2 | ||
|
|
aab28b914c | ||
|
|
336e454ce7 | ||
|
|
35a50bd792 | ||
|
|
2fd23f02f6 | ||
|
|
aad5b447c9 | ||
|
|
cece13e929 | ||
|
|
ecf325a06c | ||
|
|
44370aef5e | ||
|
|
1782b11ec4 | ||
|
|
fe6495123c | ||
|
|
d442f564ce | ||
|
|
f35446cc13 | ||
|
|
506a8b4776 | ||
|
|
47ecfbfd5f | ||
|
|
82f7401368 |
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -292,6 +292,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
|
||||||
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
|
||||||
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
||||||
|
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)
|
||||||
|
|
||||||
|
|
||||||
### 11. Mathutil package implements some functions for math calculation.
|
### 11. Mathutil package implements some functions for math calculation.
|
||||||
@@ -323,6 +324,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
||||||
|
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
||||||
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
||||||
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
||||||
@@ -386,7 +388,8 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter)
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter)
|
||||||
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find)
|
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find)
|
||||||
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)
|
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)
|
||||||
|
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
||||||
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
||||||
@@ -451,6 +454,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv)
|
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv)
|
||||||
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)
|
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)
|
||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)
|
||||||
|
- [GetOsBits](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)
|
||||||
|
|
||||||
### 18. Validator package contains some functions for data validation.
|
### 18. Validator package contains some functions for data validation.
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -290,6 +290,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Merge)
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Merge)
|
||||||
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus)
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus)
|
||||||
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
|
||||||
|
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)
|
||||||
|
|
||||||
### 11. mathutil包实现了一些数学计算的函数。
|
### 11. mathutil包实现了一些数学计算的函数。
|
||||||
|
|
||||||
@@ -320,6 +321,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
||||||
|
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)
|
||||||
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
||||||
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)
|
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)
|
||||||
@@ -380,7 +382,8 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter)
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter)
|
||||||
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find)
|
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find)
|
||||||
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)
|
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)
|
||||||
|
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
||||||
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
||||||
@@ -449,6 +452,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#RemoveOsEnv)
|
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#RemoveOsEnv)
|
||||||
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv)
|
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv)
|
||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand)
|
||||||
|
- [GetOsBits](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#GetOsBits)
|
||||||
|
|
||||||
### 18. validator验证器包,包含常用字符串格式验证函数。
|
### 18. validator验证器包,包含常用字符串格式验证函数。
|
||||||
|
|
||||||
|
|||||||
@@ -90,70 +90,70 @@ func ToChannel[T any](array []T) <-chan T {
|
|||||||
|
|
||||||
// ToString convert value to string
|
// ToString convert value to string
|
||||||
func ToString(value any) string {
|
func ToString(value any) string {
|
||||||
res := ""
|
result := ""
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
||||||
return res
|
return result
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = strconv.FormatInt(v.Int(), 10)
|
result = strconv.FormatInt(v.Int(), 10)
|
||||||
return res
|
return result
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = strconv.FormatUint(v.Uint(), 10)
|
result = strconv.FormatUint(v.Uint(), 10)
|
||||||
return res
|
return result
|
||||||
case string:
|
case string:
|
||||||
res = v.String()
|
result = v.String()
|
||||||
return res
|
return result
|
||||||
case []byte:
|
case []byte:
|
||||||
res = string(v.Bytes())
|
result = string(v.Bytes())
|
||||||
return res
|
return result
|
||||||
default:
|
default:
|
||||||
newValue, _ := json.Marshal(value)
|
newValue, _ := json.Marshal(value)
|
||||||
res = string(newValue)
|
result = string(newValue)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToJson convert value to a valid json string
|
// ToJson convert value to a valid json string
|
||||||
func ToJson(value any) (string, error) {
|
func ToJson(value any) (string, error) {
|
||||||
res, err := json.Marshal(value)
|
result, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(res), nil
|
return string(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFloat convert value to a float64, if input is not a float return 0.0 and error
|
// ToFloat convert value to a float64, if input is not a float return 0.0 and error
|
||||||
func ToFloat(value any) (float64, error) {
|
func ToFloat(value any) (float64, error) {
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
res := 0.0
|
result := 0.0
|
||||||
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
|
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = float64(v.Int())
|
result = float64(v.Int())
|
||||||
return res, nil
|
return result, nil
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = float64(v.Uint())
|
result = float64(v.Uint())
|
||||||
return res, nil
|
return result, nil
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = v.Float()
|
result = v.Float()
|
||||||
return res, nil
|
return result, nil
|
||||||
case string:
|
case string:
|
||||||
res, err = strconv.ParseFloat(v.String(), 64)
|
result, err = strconv.ParseFloat(v.String(), 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res = 0.0
|
result = 0.0
|
||||||
}
|
}
|
||||||
return res, err
|
return result, err
|
||||||
default:
|
default:
|
||||||
return res, err
|
return result, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,26 +161,26 @@ func ToFloat(value any) (float64, error) {
|
|||||||
func ToInt(value any) (int64, error) {
|
func ToInt(value any) (int64, error) {
|
||||||
v := reflect.ValueOf(value)
|
v := reflect.ValueOf(value)
|
||||||
|
|
||||||
var res int64
|
var result int64
|
||||||
err := fmt.Errorf("ToInt: invalid interface type %T", value)
|
err := fmt.Errorf("ToInt: invalid interface type %T", value)
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case int, int8, int16, int32, int64:
|
case int, int8, int16, int32, int64:
|
||||||
res = v.Int()
|
result = v.Int()
|
||||||
return res, nil
|
return result, nil
|
||||||
case uint, uint8, uint16, uint32, uint64:
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
res = int64(v.Uint())
|
result = int64(v.Uint())
|
||||||
return res, nil
|
return result, nil
|
||||||
case float32, float64:
|
case float32, float64:
|
||||||
res = int64(v.Float())
|
result = int64(v.Float())
|
||||||
return res, nil
|
return result, nil
|
||||||
case string:
|
case string:
|
||||||
res, err = strconv.ParseInt(v.String(), 0, 64)
|
result, err = strconv.ParseInt(v.String(), 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res = 0
|
result = 0
|
||||||
}
|
}
|
||||||
return res, err
|
return result, err
|
||||||
default:
|
default:
|
||||||
return res, err
|
return result, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,13 +191,13 @@ func ToPointer[T any](value T) *T {
|
|||||||
|
|
||||||
// ToMap convert a slice or an array of structs to a map based on iteratee function
|
// ToMap convert a slice or an array of structs to a map based on iteratee function
|
||||||
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V {
|
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V {
|
||||||
res := make(map[K]V, len(array))
|
result := make(map[K]V, len(array))
|
||||||
for _, item := range array {
|
for _, item := range array {
|
||||||
k, v := iteratee(item)
|
k, v := iteratee(item)
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructToMap convert struct to map, only convert exported struct field
|
// StructToMap convert struct to map, only convert exported struct field
|
||||||
@@ -213,7 +213,7 @@ func StructToMap(value any) (map[string]any, error) {
|
|||||||
return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value)
|
return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make(map[string]any)
|
result := make(map[string]any)
|
||||||
|
|
||||||
fieldNum := t.NumField()
|
fieldNum := t.NumField()
|
||||||
pattern := `^[A-Z]`
|
pattern := `^[A-Z]`
|
||||||
@@ -222,23 +222,23 @@ func StructToMap(value any) (map[string]any, error) {
|
|||||||
name := t.Field(i).Name
|
name := t.Field(i).Name
|
||||||
tag := t.Field(i).Tag.Get("json")
|
tag := t.Field(i).Tag.Get("json")
|
||||||
if regex.MatchString(name) && tag != "" {
|
if regex.MatchString(name) && tag != "" {
|
||||||
//res[name] = v.Field(i).Interface()
|
//result[name] = v.Field(i).Interface()
|
||||||
res[tag] = v.Field(i).Interface()
|
result[tag] = v.Field(i).Interface()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapToSlice convert a map to a slice based on iteratee function
|
// MapToSlice convert a map to a slice based on iteratee function
|
||||||
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T {
|
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T {
|
||||||
res := make([]T, 0, len(aMap))
|
result := make([]T, 0, len(aMap))
|
||||||
|
|
||||||
for k, v := range aMap {
|
for k, v := range aMap {
|
||||||
res = append(res, iteratee(k, v))
|
result = append(result, iteratee(k, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// ColorHexToRGB convert hex color to rgb color
|
// ColorHexToRGB convert hex color to rgb color
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func AesEcbEncrypt(data, key []byte) []byte {
|
|||||||
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
cipher, _ := aes.NewCipher(generateAesKey(key))
|
||||||
decrypted := make([]byte, len(encrypted))
|
decrypted := make([]byte, len(encrypted))
|
||||||
//
|
|
||||||
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
||||||
}
|
}
|
||||||
@@ -54,14 +54,18 @@ func AesEcbDecrypt(encrypted, key []byte) []byte {
|
|||||||
// AesCbcEncrypt encrypt data with key use AES CBC algorithm
|
// AesCbcEncrypt encrypt data with key use AES CBC algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesCbcEncrypt(data, key []byte) []byte {
|
func AesCbcEncrypt(data, key []byte) []byte {
|
||||||
// len(key) should be 16, 24 or 32
|
|
||||||
block, _ := aes.NewCipher(key)
|
block, _ := aes.NewCipher(key)
|
||||||
blockSize := block.BlockSize()
|
data = pkcs7Padding(data, block.BlockSize())
|
||||||
data = pkcs7Padding(data, blockSize)
|
|
||||||
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
|
encrypted := make([]byte, aes.BlockSize+len(data))
|
||||||
|
iv := encrypted[:aes.BlockSize]
|
||||||
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := cipher.NewCBCEncrypter(block, iv)
|
||||||
|
mode.CryptBlocks(encrypted[aes.BlockSize:], data)
|
||||||
|
|
||||||
encrypted := make([]byte, len(data))
|
|
||||||
blockMode.CryptBlocks(encrypted, data)
|
|
||||||
return encrypted
|
return encrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,12 +73,14 @@ func AesCbcEncrypt(data, key []byte) []byte {
|
|||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesCbcDecrypt(encrypted, key []byte) []byte {
|
func AesCbcDecrypt(encrypted, key []byte) []byte {
|
||||||
block, _ := aes.NewCipher(key)
|
block, _ := aes.NewCipher(key)
|
||||||
blockSize := block.BlockSize()
|
|
||||||
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
|
|
||||||
|
|
||||||
decrypted := make([]byte, len(encrypted))
|
iv := encrypted[:aes.BlockSize]
|
||||||
blockMode.CryptBlocks(decrypted, encrypted)
|
encrypted = encrypted[aes.BlockSize:]
|
||||||
decrypted = pkcs7UnPadding(decrypted)
|
|
||||||
|
mode := cipher.NewCBCDecrypter(block, iv)
|
||||||
|
mode.CryptBlocks(encrypted, encrypted)
|
||||||
|
|
||||||
|
decrypted := pkcs7UnPadding(encrypted)
|
||||||
return decrypted
|
return decrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,12 +55,16 @@ func DesEcbDecrypt(encrypted, key []byte) []byte {
|
|||||||
// len(key) should be 8
|
// len(key) should be 8
|
||||||
func DesCbcEncrypt(data, key []byte) []byte {
|
func DesCbcEncrypt(data, key []byte) []byte {
|
||||||
block, _ := des.NewCipher(key)
|
block, _ := des.NewCipher(key)
|
||||||
blockSize := block.BlockSize()
|
data = pkcs7Padding(data, block.BlockSize())
|
||||||
data = pkcs7Padding(data, blockSize)
|
|
||||||
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
|
|
||||||
|
|
||||||
encrypted := make([]byte, len(data))
|
encrypted := make([]byte, des.BlockSize+len(data))
|
||||||
blockMode.CryptBlocks(encrypted, data)
|
iv := encrypted[:des.BlockSize]
|
||||||
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := cipher.NewCBCEncrypter(block, iv)
|
||||||
|
mode.CryptBlocks(encrypted[des.BlockSize:], data)
|
||||||
|
|
||||||
return encrypted
|
return encrypted
|
||||||
}
|
}
|
||||||
@@ -69,13 +73,14 @@ func DesCbcEncrypt(data, key []byte) []byte {
|
|||||||
// len(key) should be 8
|
// len(key) should be 8
|
||||||
func DesCbcDecrypt(encrypted, key []byte) []byte {
|
func DesCbcDecrypt(encrypted, key []byte) []byte {
|
||||||
block, _ := des.NewCipher(key)
|
block, _ := des.NewCipher(key)
|
||||||
blockSize := block.BlockSize()
|
|
||||||
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
|
|
||||||
|
|
||||||
decrypted := make([]byte, len(encrypted))
|
iv := encrypted[:des.BlockSize]
|
||||||
blockMode.CryptBlocks(decrypted, encrypted)
|
encrypted = encrypted[des.BlockSize:]
|
||||||
decrypted = pkcs7UnPadding(decrypted)
|
|
||||||
|
|
||||||
|
mode := cipher.NewCBCDecrypter(block, iv)
|
||||||
|
mode.CryptBlocks(encrypted, encrypted)
|
||||||
|
|
||||||
|
decrypted := pkcs7UnPadding(encrypted)
|
||||||
return decrypted
|
return decrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -208,13 +208,13 @@ func (link *DoublyLink[T]) Size() int {
|
|||||||
|
|
||||||
// Values return slice of all doubly linklist node value
|
// Values return slice of all doubly linklist node value
|
||||||
func (link *DoublyLink[T]) Values() []T {
|
func (link *DoublyLink[T]) Values() []T {
|
||||||
res := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := link.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
res = append(res, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print all nodes info of a linked list
|
// Print all nodes info of a linked list
|
||||||
|
|||||||
@@ -213,13 +213,13 @@ func (link *SinglyLink[T]) Size() int {
|
|||||||
|
|
||||||
// Values return slice of all singly linklist node value
|
// Values return slice of all singly linklist node value
|
||||||
func (link *SinglyLink[T]) Values() []T {
|
func (link *SinglyLink[T]) Values() []T {
|
||||||
res := []T{}
|
result := []T{}
|
||||||
current := link.Head
|
current := link.Head
|
||||||
for current != nil {
|
for current != nil {
|
||||||
res = append(res, current.Value)
|
result = append(result, current.Value)
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if link is empty or not
|
// IsEmpty checks if link is empty or not
|
||||||
|
|||||||
@@ -222,24 +222,24 @@ func (l *List[T]) Unique() {
|
|||||||
|
|
||||||
// Union creates a new list contain all element in list l and other, remove duplicate element.
|
// Union creates a new list contain all element in list l and other, remove duplicate element.
|
||||||
func (l *List[T]) Union(other *List[T]) *List[T] {
|
func (l *List[T]) Union(other *List[T]) *List[T] {
|
||||||
res := NewList([]T{})
|
result := NewList([]T{})
|
||||||
|
|
||||||
res.data = append(res.data, l.data...)
|
result.data = append(result.data, l.data...)
|
||||||
res.data = append(res.data, other.data...)
|
result.data = append(result.data, other.data...)
|
||||||
res.Unique()
|
result.Unique()
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intersection creates a new list whose element both be contained in list l and other
|
// Intersection creates a new list whose element both be contained in list l and other
|
||||||
func (l *List[T]) Intersection(other *List[T]) *List[T] {
|
func (l *List[T]) Intersection(other *List[T]) *List[T] {
|
||||||
res := NewList(make([]T, 0, 0))
|
result := NewList(make([]T, 0, 0))
|
||||||
|
|
||||||
for _, v := range l.data {
|
for _, v := range l.data {
|
||||||
if other.Contain(v) {
|
if other.Contain(v) {
|
||||||
res.data = append(res.data, v)
|
result.data = append(result.data, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,20 +84,20 @@ func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
|||||||
|
|
||||||
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
||||||
comparator lancetconstraints.Comparator) bool {
|
comparator lancetconstraints.Comparator) bool {
|
||||||
res := false
|
result := false
|
||||||
|
|
||||||
if superTreeRoot != nil && subTreeRoot != nil {
|
if superTreeRoot != nil && subTreeRoot != nil {
|
||||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||||
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
result = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
if !res {
|
if !result {
|
||||||
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
result = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
if !res {
|
if !result {
|
||||||
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
result = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the bstree structure
|
// Print the bstree structure
|
||||||
|
|||||||
@@ -226,8 +226,8 @@ func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], com
|
|||||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
result := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
|
|||||||
@@ -584,8 +584,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
cache := algorithm.NewLRUCache[int, int](2)
|
cache := algorithm.NewLRUCache[int, int](2)
|
||||||
|
|
||||||
cache.Put(1, 1)
|
cache.Put(1, 1)
|
||||||
cache.Put(2, 2)
|
cache.Put(2, 2)
|
||||||
|
|
||||||
_, ok := cache.Get(0) // ok -> false
|
_, ok := cache.Get(0) // ok -> false
|
||||||
|
|
||||||
|
|||||||
@@ -585,8 +585,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
cache := algorithm.NewLRUCache[int, int](2)
|
cache := algorithm.NewLRUCache[int, int](2)
|
||||||
|
|
||||||
cache.Put(1, 1)
|
cache.Put(1, 1)
|
||||||
cache.Put(2, 2)
|
cache.Put(2, 2)
|
||||||
|
|
||||||
_, ok := cache.Get(0) // ok -> false
|
_, ok := cache.Get(0) // ok -> false
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -133,7 +133,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -173,14 +173,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
||||||
|
|
||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v) //1, 2, 1, 2, 1
|
fmt.Println(v) //1, 2, 1, 2, 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -209,7 +209,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fn := func() any {
|
fn := func() any {
|
||||||
@@ -220,7 +220,7 @@ func main() {
|
|||||||
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
||||||
|
|
||||||
for v := range dataStream {
|
for v := range dataStream {
|
||||||
fmt.Println(v) //a, a, a
|
fmt.Println(v) //a, a, a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -248,7 +248,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
sig := func(after time.Duration) <-chan any {
|
sig := func(after time.Duration) <-chan any {
|
||||||
c := make(chan interface{})
|
c := make(chan interface{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(c)
|
defer close(c)
|
||||||
@@ -296,7 +296,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -332,7 +332,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
numbers := make(chan any, 5)
|
numbers := make(chan any, 5)
|
||||||
@@ -347,7 +347,7 @@ func main() {
|
|||||||
intStream := c.Take(ctx, numbers, 3)
|
intStream := c.Take(ctx, numbers, 3)
|
||||||
|
|
||||||
for val := range intStream {
|
for val := range intStream {
|
||||||
fmt.Println(val) //1, 2, 3
|
fmt.Println(val) //1, 2, 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -375,7 +375,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -383,8 +383,8 @@ func main() {
|
|||||||
|
|
||||||
out1, out2 := c.Tee(ctx, inStream)
|
out1, out2 := c.Tee(ctx, inStream)
|
||||||
for val := range out1 {
|
for val := range out1 {
|
||||||
fmt.Println(val) //1
|
fmt.Println(val) //1
|
||||||
fmt.Println(<-out2) //1
|
fmt.Println(<-out2) //1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -180,7 +180,7 @@ func main() {
|
|||||||
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
||||||
|
|
||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v) //1, 2, 1, 2, 1
|
fmt.Println(v) //1, 2, 1, 2, 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -220,7 +220,7 @@ func main() {
|
|||||||
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
||||||
|
|
||||||
for v := range dataStream {
|
for v := range dataStream {
|
||||||
fmt.Println(v) //a, a, a
|
fmt.Println(v) //a, a, a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -248,7 +248,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
sig := func(after time.Duration) <-chan any {
|
sig := func(after time.Duration) <-chan any {
|
||||||
c := make(chan interface{})
|
c := make(chan interface{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(c)
|
defer close(c)
|
||||||
@@ -296,7 +296,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -332,7 +332,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
numbers := make(chan any, 5)
|
numbers := make(chan any, 5)
|
||||||
@@ -347,7 +347,7 @@ func main() {
|
|||||||
intStream := c.Take(ctx, numbers, 3)
|
intStream := c.Take(ctx, numbers, 3)
|
||||||
|
|
||||||
for val := range intStream {
|
for val := range intStream {
|
||||||
fmt.Println(val) //1, 2, 3
|
fmt.Println(val) //1, 2, 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -375,7 +375,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c := concurrency.NewChannel()
|
c := concurrency.NewChannel()
|
||||||
@@ -383,8 +383,8 @@ func main() {
|
|||||||
|
|
||||||
out1, out2 := c.Tee(ctx, inStream)
|
out1, out2 := c.Tee(ctx, inStream)
|
||||||
for val := range out1 {
|
for val := range out1 {
|
||||||
fmt.Println(val) //1
|
fmt.Println(val) //1
|
||||||
fmt.Println(<-out2) //1
|
fmt.Println(<-out2) //1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -219,16 +219,16 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ch := convertor.ToChannel([]int{1, 2, 3})
|
ch := convertor.ToChannel([]int{1, 2, 3})
|
||||||
|
|
||||||
val1, _ := <-ch
|
val1, _ := <-ch
|
||||||
fmt.Println(val1) //1
|
fmt.Println(val1) //1
|
||||||
|
|
||||||
val2, _ := <-ch
|
val2, _ := <-ch
|
||||||
fmt.Println(val2) //2
|
fmt.Println(val2) //2
|
||||||
|
|
||||||
val3, _ := <-ch
|
val3, _ := <-ch
|
||||||
fmt.Println(val3) //3
|
fmt.Println(val3) //3
|
||||||
|
|
||||||
_, ok := <-ch
|
_, ok := <-ch
|
||||||
fmt.Println(ok) //false
|
fmt.Println(ok) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -350,16 +350,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type Message struct {
|
type Message struct {
|
||||||
name string
|
name string
|
||||||
code int
|
code int
|
||||||
}
|
}
|
||||||
messages := []Message{
|
messages := []Message{
|
||||||
{name: "Hello", code: 100},
|
{name: "Hello", code: 100},
|
||||||
{name: "Hi", code: 101},
|
{name: "Hi", code: 101},
|
||||||
}
|
}
|
||||||
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
||||||
return msg.code, msg.name
|
return msg.code, msg.name
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
||||||
}
|
}
|
||||||
@@ -451,9 +451,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
result := MapToSlice(aMap, func(key string, value int) string {
|
result := MapToSlice(aMap, func(key string, value int) string {
|
||||||
return key + ":" + strconv.Itoa(value)
|
return key + ":" + strconv.Itoa(value)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
|
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,16 +222,16 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ch := convertor.ToChannel([]int{1, 2, 3})
|
ch := convertor.ToChannel([]int{1, 2, 3})
|
||||||
|
|
||||||
val1, _ := <-ch
|
val1, _ := <-ch
|
||||||
fmt.Println(val1) //1
|
fmt.Println(val1) //1
|
||||||
|
|
||||||
val2, _ := <-ch
|
val2, _ := <-ch
|
||||||
fmt.Println(val2) //2
|
fmt.Println(val2) //2
|
||||||
|
|
||||||
val3, _ := <-ch
|
val3, _ := <-ch
|
||||||
fmt.Println(val3) //3
|
fmt.Println(val3) //3
|
||||||
|
|
||||||
_, ok := <-ch
|
_, ok := <-ch
|
||||||
fmt.Println(ok) //false
|
fmt.Println(ok) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -273,7 +273,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>将interface转成intt64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成int64类型,如果参数无法转换,会返回0和error</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -353,16 +353,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type Message struct {
|
type Message struct {
|
||||||
name string
|
name string
|
||||||
code int
|
code int
|
||||||
}
|
}
|
||||||
messages := []Message{
|
messages := []Message{
|
||||||
{name: "Hello", code: 100},
|
{name: "Hello", code: 100},
|
||||||
{name: "Hi", code: 101},
|
{name: "Hi", code: 101},
|
||||||
}
|
}
|
||||||
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
||||||
return msg.code, msg.name
|
return msg.code, msg.name
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
||||||
}
|
}
|
||||||
@@ -482,10 +482,10 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
result := MapToSlice(aMap, func(key string, value int) string {
|
result := MapToSlice(aMap, func(key string, value int) string {
|
||||||
return key + ":" + strconv.Itoa(value)
|
return key + ":" + strconv.Itoa(value)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
|
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -115,9 +115,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -145,7 +145,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -176,9 +176,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -207,7 +207,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -239,7 +239,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ func main() {
|
|||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -300,7 +300,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -330,9 +330,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
@@ -360,7 +360,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
base64Str := cryptor.Base64StdEncode("hello world")
|
base64Str := cryptor.Base64StdEncode("hello world")
|
||||||
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
|
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -417,7 +417,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -448,9 +448,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
|
||||||
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
@@ -480,7 +480,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -511,7 +511,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
||||||
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -543,7 +543,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -575,7 +575,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -605,9 +605,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
||||||
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -636,7 +636,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -666,9 +666,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
@@ -725,8 +725,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha1("hello world", "12345"))
|
s := cryptor.HmacSha1("hello world", "12345"))
|
||||||
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -753,8 +753,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha256("hello world", "12345"))
|
s := cryptor.HmacSha256("hello world", "12345"))
|
||||||
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
|
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -781,8 +781,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha512("hello world", "12345"))
|
s := cryptor.HmacSha512("hello world", "12345"))
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -810,8 +810,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Md5String("hello"))
|
s := cryptor.Md5String("hello"))
|
||||||
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
|
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -838,8 +838,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Md5File("./main.go"))
|
s := cryptor.Md5File("./main.go"))
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -866,8 +866,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha1("hello world"))
|
s := cryptor.Sha1("hello world"))
|
||||||
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -894,8 +894,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha256("hello world"))
|
s := cryptor.Sha256("hello world"))
|
||||||
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -922,7 +922,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha512("hello world"))
|
s := cryptor.Sha512("hello world"))
|
||||||
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
|
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -950,10 +950,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -980,14 +980,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
@@ -1016,14 +1016,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -115,9 +115,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -145,7 +145,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -176,9 +176,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
@@ -208,7 +208,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -240,7 +240,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -270,9 +270,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -301,7 +301,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -331,9 +331,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -360,7 +360,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
base64Str := cryptor.Base64StdEncode("hello world")
|
base64Str := cryptor.Base64StdEncode("hello world")
|
||||||
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
|
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -388,7 +388,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
|
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
|
||||||
fmt.Println(str) //hello world
|
fmt.Println(str) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -417,7 +417,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -448,7 +448,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -480,7 +480,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
@@ -511,7 +511,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -543,7 +543,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
@@ -575,7 +575,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -605,7 +605,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
@@ -636,7 +636,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(encrypted))
|
fmt.Println(string(encrypted))
|
||||||
}
|
}
|
||||||
@@ -666,7 +666,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello world"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
@@ -696,8 +696,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacMd5("hello world", "12345"))
|
s := cryptor.HmacMd5("hello world", "12345"))
|
||||||
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
|
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -724,8 +724,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha1("hello world", "12345"))
|
s := cryptor.HmacSha1("hello world", "12345"))
|
||||||
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -752,8 +752,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha256("hello world", "12345"))
|
s := cryptor.HmacSha256("hello world", "12345"))
|
||||||
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
|
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -780,8 +780,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha512("hello world", "12345"))
|
s := cryptor.HmacSha512("hello world", "12345"))
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -809,8 +809,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Md5String("hello"))
|
s := cryptor.Md5String("hello"))
|
||||||
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
|
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -837,8 +837,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Md5File("./main.go"))
|
s := cryptor.Md5File("./main.go"))
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -865,8 +865,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha1("hello world"))
|
s := cryptor.Sha1("hello world"))
|
||||||
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -893,8 +893,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha256("hello world"))
|
s := cryptor.Sha256("hello world"))
|
||||||
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -921,8 +921,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha512("hello world"))
|
s := cryptor.Sha512("hello world"))
|
||||||
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
|
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -949,10 +949,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -979,11 +979,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
@@ -1013,11 +1013,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
|
|||||||
@@ -712,7 +712,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
@@ -741,7 +741,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
@@ -771,7 +771,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -680,7 +680,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
@@ -708,7 +708,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
@@ -737,7 +737,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
@@ -767,7 +767,7 @@ func main() {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
type theTime struct {
|
type theTime struct {
|
||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -140,9 +140,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
|
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -168,9 +168,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mode, err := fileutil.FileMode("./test.txt")
|
mode, err := fileutil.FileMode("./test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(mode)
|
fmt.Println(mode)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -338,8 +338,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
err := fileutil.RemoveFile("./test.txt")
|
err := fileutil.RemoveFile("./test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -365,12 +365,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
path := "./test.txt"
|
path := "./test.txt"
|
||||||
fileutil.CreateFile(path)
|
fileutil.CreateFile(path)
|
||||||
|
|
||||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
f.WriteString("hello world")
|
f.WriteString("hello world")
|
||||||
|
|
||||||
content, _ := fileutil.ReadFileToString(path)
|
content, _ := fileutil.ReadFileToString(path)
|
||||||
fmt.Println(content) //hello world
|
fmt.Println(content) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -398,13 +398,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
path := "./text.txt"
|
path := "./text.txt"
|
||||||
fileutil.CreateFile(path)
|
fileutil.CreateFile(path)
|
||||||
|
|
||||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString("hello\nworld")
|
f.WriteString("hello\nworld")
|
||||||
|
|
||||||
contents, _ := fileutil.ReadFileByLine(path)
|
contents, _ := fileutil.ReadFileByLine(path)
|
||||||
fmt.Println(contents) //[]string{"hello", "world"}
|
fmt.Println(contents) //[]string{"hello", "world"}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -430,7 +430,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.Zip("./test.txt", "./test.zip")
|
err := fileutil.Zip("./test.txt", "./test.zip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@@ -459,7 +459,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
|
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,9 +139,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
|
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -167,9 +167,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mode, err := fileutil.FileMode("./test.txt")
|
mode, err := fileutil.FileMode("./test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(mode)
|
fmt.Println(mode)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -337,8 +337,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
err := fileutil.RemoveFile("./test.txt")
|
err := fileutil.RemoveFile("./test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -364,12 +364,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
path := "./test.txt"
|
path := "./test.txt"
|
||||||
fileutil.CreateFile(path)
|
fileutil.CreateFile(path)
|
||||||
|
|
||||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
f.WriteString("hello world")
|
f.WriteString("hello world")
|
||||||
|
|
||||||
content, _ := fileutil.ReadFileToString(path)
|
content, _ := fileutil.ReadFileToString(path)
|
||||||
fmt.Println(content) //hello world
|
fmt.Println(content) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -397,13 +397,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
path := "./text.txt"
|
path := "./text.txt"
|
||||||
fileutil.CreateFile(path)
|
fileutil.CreateFile(path)
|
||||||
|
|
||||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString("hello\nworld")
|
f.WriteString("hello\nworld")
|
||||||
|
|
||||||
contents, _ := fileutil.ReadFileByLine(path)
|
contents, _ := fileutil.ReadFileByLine(path)
|
||||||
fmt.Println(contents) //[]string{"hello", "world"}
|
fmt.Println(contents) //[]string{"hello", "world"}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -429,7 +429,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.Zip("./test.txt", "./test.zip")
|
err := fileutil.Zip("./test.txt", "./test.zip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@@ -458,7 +458,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
|
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,8 +101,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
assert := internal.NewAssert(t, "TestBefore")
|
|
||||||
|
|
||||||
arr := []string{"a", "b", "c", "d", "e"}
|
arr := []string{"a", "b", "c", "d", "e"}
|
||||||
f := function.Before(3, func(i int) int {
|
f := function.Before(3, func(i int) int {
|
||||||
return i
|
return i
|
||||||
@@ -120,7 +118,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected := []int64{0, 1, 2, 2, 2}
|
expected := []int64{0, 1, 2, 2, 2}
|
||||||
assert.Equal(expected, res)
|
fmt.Println(res) // 0, 1, 2, 2, 2
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -101,8 +101,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
assert := internal.NewAssert(t, "TestBefore")
|
|
||||||
|
|
||||||
arr := []string{"a", "b", "c", "d", "e"}
|
arr := []string{"a", "b", "c", "d", "e"}
|
||||||
f := function.Before(3, func(i int) int {
|
f := function.Before(3, func(i int) int {
|
||||||
return i
|
return i
|
||||||
@@ -119,8 +117,7 @@ func main() {
|
|||||||
appendStr(i, arr[i], f)
|
appendStr(i, arr[i], f)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []int64{0, 1, 2, 2, 2}
|
fmt.Println(res) // 0, 1, 2, 2, 2
|
||||||
assert.Equal(expected, res)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -297,7 +294,7 @@ func main() {
|
|||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
close(stop)
|
close(stop)
|
||||||
|
|
||||||
fmt.Println(res) //[* * * * *]
|
fmt.Println(res) //[* * * * *]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package maputil includes some functions to manipulate map.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -301,4 +302,51 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsDisjoint">IsDisjoint</span>
|
||||||
|
<p>Checks two are disjoint if they have no keys in common</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[int]string{
|
||||||
|
6: "a",
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := maputil.IsDisjoint(m2, m1)
|
||||||
|
fmt.Println(ok) // false
|
||||||
|
|
||||||
|
ok = maputil.IsDisjoint(m2, m3)
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -5,7 +5,7 @@ maputil包包括一些操作map的函数。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Minus](#Minus)
|
- [Minus](#Minus)
|
||||||
- [Values](#Values)
|
- [Values](#Values)
|
||||||
|
- [IsDisjoint](#IsDisjoint)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -231,7 +232,6 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Minus">Minus</span>
|
### <span id="Minus">Minus</span>
|
||||||
<p>返回一个map,其中的key存在于mapA,不存在于mapB.</p>
|
<p>返回一个map,其中的key存在于mapA,不存在于mapB.</p>
|
||||||
|
|
||||||
@@ -301,4 +301,52 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsDisjoint">IsDisjoint</span>
|
||||||
|
<p>验证两个map是否具有不同的key</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[int]string{
|
||||||
|
6: "a",
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := maputil.IsDisjoint(m2, m1)
|
||||||
|
fmt.Println(ok) // false
|
||||||
|
|
||||||
|
ok = maputil.IsDisjoint(m2, m3)
|
||||||
|
fmt.Println(ok) // true
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -22,6 +22,8 @@ import (
|
|||||||
|
|
||||||
## Index
|
## Index
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
|
- [EncodeUrl](#EncodeUrl)
|
||||||
|
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
- [GetIps](#GetIps)
|
- [GetIps](#GetIps)
|
||||||
- [GetMacAddrs](#GetMacAddrs)
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
@@ -33,7 +35,6 @@ import (
|
|||||||
- [HttpDelete](#HttpDelete)
|
- [HttpDelete](#HttpDelete)
|
||||||
- [HttpPost](#HttpPost)
|
- [HttpPost](#HttpPost)
|
||||||
- [HttpPut](#HttpPut)
|
- [HttpPut](#HttpPut)
|
||||||
|
|
||||||
- [HttpPatch](#HttpPatch)
|
- [HttpPatch](#HttpPatch)
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
|
||||||
@@ -74,6 +75,36 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
<p>Encode url query string values.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||||
|
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
<p>Get internal ip information.</p>
|
<p>Get internal ip information.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
- [GetIps](#GetIps)
|
- [GetIps](#GetIps)
|
||||||
- [GetMacAddrs](#GetMacAddrs)
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
@@ -73,6 +74,37 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
<p>编码url query string的值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||||
|
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
<p>获取内部ip</p>
|
<p>获取内部ip</p>
|
||||||
|
|
||||||
|
|||||||
@@ -97,8 +97,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
var number int
|
||||||
var increaseNumber retry.RetryFunc
|
|
||||||
increaseNumber = func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
return nil
|
return nil
|
||||||
@@ -139,6 +139,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
var number int
|
||||||
|
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
|
|||||||
@@ -99,8 +99,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
var number int
|
||||||
var increaseNumber retry.RetryFunc
|
increaseNumber := func() error {
|
||||||
increaseNumber = func() error {
|
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [Filter](#Filter)
|
- [Filter](#Filter)
|
||||||
- [Find](#Find)
|
- [Find](#Find)
|
||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
|
- [Flatten](#Flatten)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
@@ -56,6 +57,8 @@ import (
|
|||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
- [SymmetricDifference](#SymmetricDifference)
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
|
- [ToSlice](#ToSlice)
|
||||||
|
- [ToSlicePointer](#ToSlicePointer)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [UniqueBy](#UniqueBy)
|
- [UniqueBy](#UniqueBy)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
@@ -546,6 +549,31 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Flatten">Flatten</span>
|
||||||
|
<p>Flatten slice with one level.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Flatten(slice any) any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
|
res := slice.Flatten(arr)
|
||||||
|
fmt.Println(res) //{{"a", "b"}, {"c", "d"}}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
|
|
||||||
@@ -565,7 +593,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
res := slice.FlattenDeep(arr)
|
res := slice.FlattenDeep(arr)
|
||||||
fmt.Println(res) //[]string{"a", "b", "c", "d"}
|
fmt.Println(res) //{"a", "b", "c", "d"}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1087,7 +1115,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlice</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [Filter](#Filter)
|
- [Filter](#Filter)
|
||||||
- [Find](#Find)
|
- [Find](#Find)
|
||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
|
- [Flatten](#Flatten)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
@@ -550,6 +551,30 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Flatten">Flatten</span>
|
||||||
|
<p>将切片压平一层</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Flatten(slice any) any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
|
res := slice.Flatten(arr)
|
||||||
|
fmt.Println(res) //{{"a", "b"}, {"c", "d"}}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
@@ -1088,7 +1113,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlice</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
<p>将可变参数转为指针切片</p>
|
<p>将可变参数转为指针切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
- [RemoveOsEnv](#RemoveOsEnv)
|
- [RemoveOsEnv](#RemoveOsEnv)
|
||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -210,7 +211,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
### <span id="ExecCommand">CompareOsEnv</span>
|
||||||
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
<p>Use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -236,7 +237,27 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetOsBits() int
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
osBit := system.GetOsBits()
|
||||||
|
fmt.Println(osBit)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,10 @@ import (
|
|||||||
- [GetOsEnv](#GetOsEnv)
|
- [GetOsEnv](#GetOsEnv)
|
||||||
- [SetOsEnv](#SetOsEnv)
|
- [SetOsEnv](#SetOsEnv)
|
||||||
- [RemoveOsEnv](#RemoveOsEnv)
|
- [RemoveOsEnv](#RemoveOsEnv)
|
||||||
|
|
||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -236,6 +238,30 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
<p>获取当前操作系统位数,返回32或64</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetOsBits() int
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
osBit := system.GetOsBits()
|
||||||
|
fmt.Println(osBit)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func ReadFileByLine(path string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
res := make([]string, 0)
|
result := make([]string, 0)
|
||||||
buf := bufio.NewReader(f)
|
buf := bufio.NewReader(f)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -128,10 +128,10 @@ func ReadFileByLine(path string) ([]string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res = append(res, l)
|
result = append(result, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListFileNames return all file names in the path
|
// ListFileNames return all file names in the path
|
||||||
@@ -150,14 +150,14 @@ func ListFileNames(path string) ([]string, error) {
|
|||||||
return []string{}, nil
|
return []string{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res := []string{}
|
result := []string{}
|
||||||
for i := 0; i < sz; i++ {
|
for i := 0; i < sz; i++ {
|
||||||
if !fs[i].IsDir() {
|
if !fs[i].IsDir() {
|
||||||
res = append(res, fs[i].Name())
|
result = append(result, fs[i].Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zip create zip file, fpath could be a single file or a directory
|
// Zip create zip file, fpath could be a single file or a directory
|
||||||
|
|||||||
@@ -27,16 +27,16 @@ func After(n int, fn any) func(args ...any) []reflect.Value {
|
|||||||
func Before(n int, fn any) func(args ...any) []reflect.Value {
|
func Before(n int, fn any) func(args ...any) []reflect.Value {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
var res []reflect.Value
|
var result []reflect.Value
|
||||||
return func(args ...any) []reflect.Value {
|
return func(args ...any) []reflect.Value {
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
res = unsafeInvokeFunc(fn, args...)
|
result = unsafeInvokeFunc(fn, args...)
|
||||||
}
|
}
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
fn = nil
|
fn = nil
|
||||||
}
|
}
|
||||||
n--
|
n--
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ import "reflect"
|
|||||||
|
|
||||||
// Keys returns a slice of the map's keys
|
// Keys returns a slice of the map's keys
|
||||||
func Keys[K comparable, V any](m map[K]V) []K {
|
func Keys[K comparable, V any](m map[K]V) []K {
|
||||||
keys := make([]K, 0, len(m))
|
keys := make([]K, len(m))
|
||||||
|
|
||||||
|
var i int
|
||||||
for k := range m {
|
for k := range m {
|
||||||
keys = append(keys, k)
|
keys[i] = k
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys
|
return keys
|
||||||
@@ -19,10 +21,12 @@ func Keys[K comparable, V any](m map[K]V) []K {
|
|||||||
|
|
||||||
// Values returns a slice of the map's values
|
// Values returns a slice of the map's values
|
||||||
func Values[K comparable, V any](m map[K]V) []V {
|
func Values[K comparable, V any](m map[K]V) []V {
|
||||||
values := make([]V, 0, len(m))
|
values := make([]V, len(m))
|
||||||
|
|
||||||
|
var i int
|
||||||
for _, v := range m {
|
for _, v := range m {
|
||||||
values = append(values, v)
|
values[i] = v
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return values
|
return values
|
||||||
@@ -30,15 +34,15 @@ func Values[K comparable, V any](m map[K]V) []V {
|
|||||||
|
|
||||||
// Merge maps, next key will overwrite previous key
|
// Merge maps, next key will overwrite previous key
|
||||||
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
res := make(map[K]V, 0)
|
result := make(map[K]V, 0)
|
||||||
|
|
||||||
for _, m := range maps {
|
for _, m := range maps {
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEach executes iteratee funcation for every key and value pair in map
|
// ForEach executes iteratee funcation for every key and value pair in map
|
||||||
@@ -50,14 +54,14 @@ func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
|
|||||||
|
|
||||||
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
|
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
|
||||||
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
|
||||||
res := make(map[K]V)
|
result := make(map[K]V)
|
||||||
|
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
if predicate(k, v) {
|
if predicate(k, v) {
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
|
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
|
||||||
@@ -69,7 +73,7 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
|||||||
return maps[0]
|
return maps[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
var res map[K]V
|
var result map[K]V
|
||||||
|
|
||||||
reducer := func(m1, m2 map[K]V) map[K]V {
|
reducer := func(m1, m2 map[K]V) map[K]V {
|
||||||
m := make(map[K]V)
|
m := make(map[K]V)
|
||||||
@@ -82,25 +86,35 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reduceMaps := make([]map[K]V, 2, 2)
|
reduceMaps := make([]map[K]V, 2, 2)
|
||||||
res = reducer(maps[0], maps[1])
|
result = reducer(maps[0], maps[1])
|
||||||
|
|
||||||
for i := 2; i < len(maps); i++ {
|
for i := 2; i < len(maps); i++ {
|
||||||
reduceMaps[0] = res
|
reduceMaps[0] = result
|
||||||
reduceMaps[1] = maps[i]
|
reduceMaps[1] = maps[i]
|
||||||
res = reducer(reduceMaps[0], reduceMaps[1])
|
result = reducer(reduceMaps[0], reduceMaps[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Minus creates an map of whose key in mapA but not in mapB
|
// Minus creates an map of whose key in mapA but not in mapB
|
||||||
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
|
||||||
res := make(map[K]V)
|
result := make(map[K]V)
|
||||||
|
|
||||||
for k, v := range mapA {
|
for k, v := range mapA {
|
||||||
if _, ok := mapB[k]; !ok {
|
if _, ok := mapB[k]; !ok {
|
||||||
res[k] = v
|
result[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsDisjoint two map are disjoint if they have no keys in common
|
||||||
|
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool {
|
||||||
|
for k := range mapA {
|
||||||
|
if _, ok := mapB[k]; ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,3 +148,25 @@ func TestMinus(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
|
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsDisjoint(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMinus")
|
||||||
|
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"d": 22,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(true, IsDisjoint(m1, m2))
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 22,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(false, IsDisjoint(m1, m3))
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ func Percent(val, total float64, n int) float64 {
|
|||||||
return float64(0)
|
return float64(0)
|
||||||
}
|
}
|
||||||
tmp := val / total * 100
|
tmp := val / total * 100
|
||||||
res := RoundToFloat(tmp, n)
|
result := RoundToFloat(tmp, n)
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoundToString round up to n decimal places
|
// RoundToString round up to n decimal places
|
||||||
@@ -67,8 +67,8 @@ func RoundToString(x float64, n int) string {
|
|||||||
tmp := math.Pow(10.0, float64(n))
|
tmp := math.Pow(10.0, float64(n))
|
||||||
x *= tmp
|
x *= tmp
|
||||||
x = math.Round(x)
|
x = math.Round(x)
|
||||||
res := strconv.FormatFloat(x/tmp, 'f', n, 64)
|
result := strconv.FormatFloat(x/tmp, 'f', n, 64)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoundToFloat round up to n decimal places
|
// RoundToFloat round up to n decimal places
|
||||||
@@ -89,8 +89,8 @@ func TruncRound(x float64, n int) float64 {
|
|||||||
} else {
|
} else {
|
||||||
newFloat = temp[0] + "." + temp[1][:n]
|
newFloat = temp[0] + "." + temp[1][:n]
|
||||||
}
|
}
|
||||||
res, _ := strconv.ParseFloat(newFloat, 64)
|
result, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Max return max value of params
|
// Max return max value of params
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -157,3 +158,15 @@ func IsInternalIP(IP net.IP) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EncodeUrl encode url
|
||||||
|
func EncodeUrl(urlStr string) (string, error) {
|
||||||
|
URL, err := url.Parse(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
URL.RawQuery = URL.Query().Encode()
|
||||||
|
|
||||||
|
return URL.String(), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,3 +96,16 @@ func TestGetMacAddrs(t *testing.T) {
|
|||||||
macAddrs := GetMacAddrs()
|
macAddrs := GetMacAddrs()
|
||||||
t.Log(macAddrs)
|
t.Log(macAddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEncodeUrl(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsInternalIP")
|
||||||
|
|
||||||
|
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||||
|
encodedUrl, err := EncodeUrl(urlAddr)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := "http://www.lancet.com?a=1&b=%5B2%5D"
|
||||||
|
assert.Equal(expected, encodedUrl)
|
||||||
|
}
|
||||||
|
|||||||
174
slice/slice.go
174
slice/slice.go
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
// Copyright 2021 dudaodong@gmail.com. All rights resulterved.
|
||||||
// Use of this source code is governed by MIT license
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
// Package slice implements some functions to manipulate slice.
|
// Package slice implements some functions to manipulate slice.
|
||||||
@@ -36,10 +36,10 @@ func ContainSubSlice[T comparable](slice, subslice []T) bool {
|
|||||||
|
|
||||||
// Chunk creates an slice of elements split into groups the length of size.
|
// Chunk creates an slice of elements split into groups the length of size.
|
||||||
func Chunk[T any](slice []T, size int) [][]T {
|
func Chunk[T any](slice []T, size int) [][]T {
|
||||||
var res [][]T
|
var result [][]T
|
||||||
|
|
||||||
if len(slice) == 0 || size <= 0 {
|
if len(slice) == 0 || size <= 0 {
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(slice)
|
length := len(slice)
|
||||||
@@ -47,9 +47,9 @@ func Chunk[T any](slice []T, size int) [][]T {
|
|||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
var tmp []T
|
var tmp []T
|
||||||
tmp = append(tmp, v)
|
tmp = append(tmp, v)
|
||||||
res = append(res, tmp)
|
result = append(result, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// divide slice equally
|
// divide slice equally
|
||||||
@@ -57,52 +57,52 @@ func Chunk[T any](slice []T, size int) [][]T {
|
|||||||
for i := 0; i < divideNum; i++ {
|
for i := 0; i < divideNum; i++ {
|
||||||
if i == divideNum-1 {
|
if i == divideNum-1 {
|
||||||
if len(slice[i*size:]) > 0 {
|
if len(slice[i*size:]) > 0 {
|
||||||
res = append(res, slice[i*size:])
|
result = append(result, slice[i*size:])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res = append(res, slice[i*size:(i+1)*size])
|
result = append(result, slice[i*size:(i+1)*size])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
||||||
func Compact[T any](slice []T) []T {
|
func Compact[T any](slice []T) []T {
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !reflect.DeepEqual(v, nil) &&
|
if !reflect.DeepEqual(v, nil) &&
|
||||||
!reflect.DeepEqual(v, false) &&
|
!reflect.DeepEqual(v, false) &&
|
||||||
!reflect.DeepEqual(v, "") &&
|
!reflect.DeepEqual(v, "") &&
|
||||||
!reflect.DeepEqual(v, 0) {
|
!reflect.DeepEqual(v, 0) {
|
||||||
res = append(res, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concat creates a new slice concatenating slice with any additional slices and/or values.
|
// Concat creates a new slice concatenating slice with any additional slices and/or values.
|
||||||
func Concat[T any](slice []T, values ...[]T) []T {
|
func Concat[T any](slice []T, values ...[]T) []T {
|
||||||
res := append([]T{}, slice...)
|
result := append([]T{}, slice...)
|
||||||
|
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
res = append(res, v...)
|
result = append(result, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Difference creates an slice of whose element in slice but not in comparedSlice
|
// Difference creates an slice of whose element in slice but not in comparedSlice
|
||||||
func Difference[T comparable](slice, comparedSlice []T) []T {
|
func Difference[T comparable](slice, comparedSlice []T) []T {
|
||||||
var res []T
|
var result []T
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(comparedSlice, v) {
|
if !Contain(comparedSlice, v) {
|
||||||
res = append(res, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// DifferenceBy it accepts iteratee which is invoked for each element of slice
|
// DifferenceBy it accepts iteratee which is invoked for each element of slice
|
||||||
@@ -112,19 +112,19 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
|
|||||||
orginSliceAfterMap := Map(slice, iteratee)
|
orginSliceAfterMap := Map(slice, iteratee)
|
||||||
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
||||||
|
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for i, v := range orginSliceAfterMap {
|
for i, v := range orginSliceAfterMap {
|
||||||
if !Contain(comparedSliceAfterMap, v) {
|
if !Contain(comparedSliceAfterMap, v) {
|
||||||
res = append(res, slice[i])
|
result = append(result, slice[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
//DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
|
//DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
|
||||||
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T {
|
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T {
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
|
|
||||||
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
||||||
index := -1
|
index := -1
|
||||||
@@ -140,11 +140,11 @@ func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value,
|
|||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
index := getIndex(comparedSlice, v, comparator)
|
index := getIndex(comparedSlice, v, comparator)
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
res = append(res, slice[i])
|
result = append(result, slice[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if two slices are equal: the same length and all elements' order and value are equal
|
// Equal checks if two slices are equal: the same length and all elements' order and value are equal
|
||||||
@@ -230,13 +230,13 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
|
|||||||
panic("predicate func is missing")
|
panic("predicate func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]T, 0, 0)
|
result := make([]T, 0, 0)
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
res = append(res, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count iterates over elements of slice, returns a count of all matched elements
|
// Count iterates over elements of slice, returns a count of all matched elements
|
||||||
@@ -284,23 +284,23 @@ func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
|
|||||||
return groupA, groupB
|
return groupA, groupB
|
||||||
}
|
}
|
||||||
|
|
||||||
// GroupWith return a map composed of keys generated from the results of running each element of slice thru iteratee.
|
// GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
|
||||||
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
||||||
if iteratee == nil {
|
if iteratee == nil {
|
||||||
panic("iteratee func is missing")
|
panic("iteratee func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make(map[U][]T)
|
result := make(map[U][]T)
|
||||||
|
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
key := iteratee(v)
|
key := iteratee(v)
|
||||||
if _, ok := res[key]; !ok {
|
if _, ok := result[key]; !ok {
|
||||||
res[key] = []T{}
|
result[key] = []T{}
|
||||||
}
|
}
|
||||||
res[key] = append(res[key], v)
|
result[key] = append(result[key], v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
@@ -355,13 +355,40 @@ func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, boo
|
|||||||
return &slice[index], true
|
return &slice[index], true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flatten flattens slice with one level
|
||||||
|
func Flatten(slice any) any {
|
||||||
|
sv := sliceValue(slice)
|
||||||
|
|
||||||
|
var result reflect.Value
|
||||||
|
if sv.Type().Elem().Kind() == reflect.Interface {
|
||||||
|
result = reflect.MakeSlice(reflect.TypeOf([]interface{}{}), 0, sv.Len())
|
||||||
|
} else if sv.Type().Elem().Kind() == reflect.Slice {
|
||||||
|
result = reflect.MakeSlice(sv.Type().Elem(), 0, sv.Len())
|
||||||
|
} else {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < sv.Len(); i++ {
|
||||||
|
item := reflect.ValueOf(sv.Index(i).Interface())
|
||||||
|
if item.Kind() == reflect.Slice {
|
||||||
|
for j := 0; j < item.Len(); j++ {
|
||||||
|
result = reflect.Append(result, item.Index(j))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = reflect.Append(result, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
// FlattenDeep flattens slice recursive
|
// FlattenDeep flattens slice recursive
|
||||||
func FlattenDeep(slice any) any {
|
func FlattenDeep(slice any) any {
|
||||||
sv := sliceValue(slice)
|
sv := sliceValue(slice)
|
||||||
st := sliceElemType(sv.Type())
|
st := sliceElemType(sv.Type())
|
||||||
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
|
||||||
res := flattenRecursive(sv, tmp)
|
result := flattenRecursive(sv, tmp)
|
||||||
return res.Interface()
|
return result.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
||||||
@@ -396,12 +423,12 @@ func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
|
|||||||
panic("iteratee func is missing")
|
panic("iteratee func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]U, len(slice), cap(slice))
|
result := make([]U, len(slice), cap(slice))
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
res[i] = iteratee(i, v)
|
result[i] = iteratee(i, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce creates an slice of values by running each element of slice thru iteratee function.
|
// Reduce creates an slice of values by running each element of slice thru iteratee function.
|
||||||
@@ -414,16 +441,16 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
|
|||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
|
|
||||||
res := iteratee(0, initial, slice[0])
|
result := iteratee(0, initial, slice[0])
|
||||||
|
|
||||||
tmp := make([]T, 2, 2)
|
tmp := make([]T, 2, 2)
|
||||||
for i := 1; i < len(slice); i++ {
|
for i := 1; i < len(slice); i++ {
|
||||||
tmp[0] = res
|
tmp[0] = result
|
||||||
tmp[1] = slice[i]
|
tmp[1] = slice[i]
|
||||||
res = iteratee(i, tmp[0], tmp[1])
|
result = iteratee(i, tmp[0], tmp[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// InterfaceSlice convert param to slice of interface.
|
// InterfaceSlice convert param to slice of interface.
|
||||||
@@ -433,12 +460,12 @@ func InterfaceSlice(slice any) []any {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]any, sv.Len())
|
result := make([]any, sv.Len())
|
||||||
for i := 0; i < sv.Len(); i++ {
|
for i := 0; i < sv.Len(); i++ {
|
||||||
res[i] = sv.Index(i).Interface()
|
result[i] = sv.Index(i).Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringSlice convert param to slice of string.
|
// StringSlice convert param to slice of string.
|
||||||
@@ -530,13 +557,11 @@ func InsertAt[T any](slice []T, index int, value any) []T {
|
|||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is T
|
|
||||||
if v, ok := value.(T); ok {
|
if v, ok := value.(T); ok {
|
||||||
slice = append(slice[:index], append([]T{v}, slice[index:]...)...)
|
slice = append(slice[:index], append([]T{v}, slice[index:]...)...)
|
||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// value is []T
|
|
||||||
if v, ok := value.([]T); ok {
|
if v, ok := value.([]T); ok {
|
||||||
slice = append(slice[:index], append(v, slice[index:]...)...)
|
slice = append(slice[:index], append(v, slice[index:]...)...)
|
||||||
return slice
|
return slice
|
||||||
@@ -564,22 +589,22 @@ func Unique[T comparable](slice []T) []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// here no use map filter. if use it, the result slice element order is random, not same as origin slice
|
// here no use map filter. if use it, the result slice element order is random, not same as origin slice
|
||||||
var res []T
|
var result []T
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
v := slice[i]
|
v := slice[i]
|
||||||
skip := true
|
skip := true
|
||||||
for j := range res {
|
for j := range result {
|
||||||
if v == res[j] {
|
if v == result[j] {
|
||||||
skip = false
|
skip = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if skip {
|
if skip {
|
||||||
res = append(res, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||||
@@ -588,13 +613,13 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
|||||||
return []T{}
|
return []T{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var res []T
|
var result []T
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
val := iteratee(v)
|
val := iteratee(v)
|
||||||
res = append(res, val)
|
result = append(result, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(res)
|
return Unique(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||||
@@ -624,28 +649,34 @@ func Intersection[T comparable](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
var res []T
|
var result []T
|
||||||
|
|
||||||
reducer := func(s1, s2 []T) []T {
|
reducer := func(sliceA, sliceB []T) []T {
|
||||||
s := make([]T, 0, 0)
|
hashMap := make(map[T]int)
|
||||||
for _, v := range s1 {
|
for _, val := range sliceA {
|
||||||
if Contain(s2, v) {
|
hashMap[val] = 1
|
||||||
s = append(s, v)
|
}
|
||||||
|
|
||||||
|
out := make([]T, 0)
|
||||||
|
for _, val := range sliceB {
|
||||||
|
if v, ok := hashMap[val]; v == 1 && ok {
|
||||||
|
out = append(out, val)
|
||||||
|
hashMap[val]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
res = reducer(slices[0], slices[1])
|
result = reducer(slices[0], slices[1])
|
||||||
|
|
||||||
reduceSlice := make([][]T, 2, 2)
|
reduceSlice := make([][]T, 2, 2)
|
||||||
for i := 2; i < len(slices); i++ {
|
for i := 2; i < len(slices); i++ {
|
||||||
reduceSlice[0] = res
|
reduceSlice[0] = result
|
||||||
reduceSlice[1] = slices[i]
|
reduceSlice[1] = slices[i]
|
||||||
res = reducer(reduceSlice[0], reduceSlice[1])
|
result = reducer(reduceSlice[0], reduceSlice[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(res)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// SymmetricDifference oppoiste operation of intersection function
|
// SymmetricDifference oppoiste operation of intersection function
|
||||||
@@ -657,7 +688,7 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]T, 0)
|
result := make([]T, 0)
|
||||||
|
|
||||||
intersectSlice := Intersection(slices...)
|
intersectSlice := Intersection(slices...)
|
||||||
|
|
||||||
@@ -665,13 +696,13 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
|
|||||||
slice := slices[i]
|
slice := slices[i]
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !Contain(intersectSlice, v) {
|
if !Contain(intersectSlice, v) {
|
||||||
res = append(res, v)
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(res)
|
return Unique(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse return slice of element order is reversed to the given slice
|
// Reverse return slice of element order is reversed to the given slice
|
||||||
@@ -683,13 +714,12 @@ func Reverse[T any](slice []T) {
|
|||||||
|
|
||||||
// Shuffle creates an slice of shuffled values
|
// Shuffle creates an slice of shuffled values
|
||||||
func Shuffle[T any](slice []T) []T {
|
func Shuffle[T any](slice []T) []T {
|
||||||
|
result := make([]T, len(slice))
|
||||||
res := make([]T, len(slice))
|
|
||||||
for i, v := range rand.Perm(len(slice)) {
|
for i, v := range rand.Perm(len(slice)) {
|
||||||
res[i] = slice[v]
|
result[i] = slice[v]
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// SortByField return sorted slice by field
|
// SortByField return sorted slice by field
|
||||||
|
|||||||
@@ -236,6 +236,14 @@ func TestFindFoundNothing(t *testing.T) {
|
|||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFlatten(t *testing.T) {
|
||||||
|
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
|
expected := [][]string{{"a", "b"}, {"c", "d"}}
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestFlattenDeep")
|
||||||
|
assert.Equal(expected, Flatten(input))
|
||||||
|
}
|
||||||
|
|
||||||
func TestFlattenDeep(t *testing.T) {
|
func TestFlattenDeep(t *testing.T) {
|
||||||
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
expected := []string{"a", "b", "c", "d"}
|
expected := []string{"a", "b", "c", "d"}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func CamelCase(s string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
res := ""
|
result := ""
|
||||||
blankSpace := " "
|
blankSpace := " "
|
||||||
regex, _ := regexp.Compile("[-_&]+")
|
regex, _ := regexp.Compile("[-_&]+")
|
||||||
ss := regex.ReplaceAllString(s, blankSpace)
|
ss := regex.ReplaceAllString(s, blankSpace)
|
||||||
@@ -26,13 +26,13 @@ func CamelCase(s string) string {
|
|||||||
if vv[i] >= 65 && vv[i] <= 96 {
|
if vv[i] >= 65 && vv[i] <= 96 {
|
||||||
vv[0] += 32
|
vv[0] += 32
|
||||||
}
|
}
|
||||||
res += string(vv)
|
result += string(vv)
|
||||||
} else {
|
} else {
|
||||||
res += Capitalize(v)
|
result += Capitalize(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
// Capitalize converts the first character of a string to upper case and the remaining to lower case.
|
||||||
@@ -126,15 +126,15 @@ func KebabCase(s string) string {
|
|||||||
match := regex.ReplaceAllString(s, blankSpace)
|
match := regex.ReplaceAllString(s, blankSpace)
|
||||||
rs := strings.Split(match, blankSpace)
|
rs := strings.Split(match, blankSpace)
|
||||||
|
|
||||||
var res []string
|
var result []string
|
||||||
for _, v := range rs {
|
for _, v := range rs {
|
||||||
splitWords := splitWordsToLower(v)
|
splitWords := splitWordsToLower(v)
|
||||||
if len(splitWords) > 0 {
|
if len(splitWords) > 0 {
|
||||||
res = append(res, splitWords...)
|
result = append(result, splitWords...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(res, "-")
|
return strings.Join(result, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SnakeCase covert string to snake_case
|
// SnakeCase covert string to snake_case
|
||||||
@@ -148,15 +148,15 @@ func SnakeCase(s string) string {
|
|||||||
match := regex.ReplaceAllString(s, blankSpace)
|
match := regex.ReplaceAllString(s, blankSpace)
|
||||||
rs := strings.Split(match, blankSpace)
|
rs := strings.Split(match, blankSpace)
|
||||||
|
|
||||||
var res []string
|
var result []string
|
||||||
for _, v := range rs {
|
for _, v := range rs {
|
||||||
splitWords := splitWordsToLower(v)
|
splitWords := splitWordsToLower(v)
|
||||||
if len(splitWords) > 0 {
|
if len(splitWords) > 0 {
|
||||||
res = append(res, splitWords...)
|
result = append(result, splitWords...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(res, "_")
|
return strings.Join(result, "_")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before create substring in source string before position when char first appear
|
// Before create substring in source string before position when char first appear
|
||||||
|
|||||||
@@ -4,37 +4,37 @@ import "strings"
|
|||||||
|
|
||||||
// splitWordsToLower split a string into worlds by uppercase char
|
// splitWordsToLower split a string into worlds by uppercase char
|
||||||
func splitWordsToLower(s string) []string {
|
func splitWordsToLower(s string) []string {
|
||||||
var res []string
|
var result []string
|
||||||
|
|
||||||
upperIndexes := upperIndex(s)
|
upperIndexes := upperIndex(s)
|
||||||
l := len(upperIndexes)
|
l := len(upperIndexes)
|
||||||
if upperIndexes == nil || l == 0 {
|
if upperIndexes == nil || l == 0 {
|
||||||
if s != "" {
|
if s != "" {
|
||||||
res = append(res, s)
|
result = append(result, s)
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
for i := 0; i < l; i++ {
|
for i := 0; i < l; i++ {
|
||||||
if i < l-1 {
|
if i < l-1 {
|
||||||
res = append(res, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]]))
|
result = append(result, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]]))
|
||||||
} else {
|
} else {
|
||||||
res = append(res, strings.ToLower(s[upperIndexes[i]:]))
|
result = append(result, strings.ToLower(s[upperIndexes[i]:]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// upperIndex get a int slice which elements are all the uppercase char index of a string
|
// upperIndex get a int slice which elements are all the uppercase char index of a string
|
||||||
func upperIndex(s string) []int {
|
func upperIndex(s string) []int {
|
||||||
var res []int
|
var result []int
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
if 64 < s[i] && s[i] < 91 {
|
if 64 < s[i] && s[i] < 91 {
|
||||||
res = append(res, i)
|
result = append(result, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(s) > 0 && res != nil && res[0] != 0 {
|
if len(s) > 0 && result != nil && result[0] != 0 {
|
||||||
res = append([]int{0}, res...)
|
result = append([]int{0}, result...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,3 +70,9 @@ func ExecCommand(command string) (stdout, stderr string, err error) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOsBits get this system bits 32bit or 64bit
|
||||||
|
// return bit int (32/64)
|
||||||
|
func GetOsBits() int {
|
||||||
|
return 32 << (^uint(0) >> 63)
|
||||||
|
}
|
||||||
|
|||||||
@@ -60,3 +60,13 @@ func TestExecCommand(t *testing.T) {
|
|||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetOsBits(t *testing.T) {
|
||||||
|
osBits := GetOsBits()
|
||||||
|
switch osBits {
|
||||||
|
case 32, 64:
|
||||||
|
t.Logf("os is %d", osBits)
|
||||||
|
default:
|
||||||
|
t.Error("os is not 32 or 64 bits")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user