mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-24 16:40:27 +08:00
v1.7.0
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// ArrayFilter - 数组过滤
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type AsyncClass[T any] struct {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// ForMap - 遍历数组,返回新数组
|
||||
|
||||
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -11,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type FileCacheClientItem struct {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Cases *CasesClass
|
||||
|
||||
@@ -13,10 +13,11 @@ import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"hash/fnv"
|
||||
rand2 "math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type HashClass struct{}
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// CurlRequest - 发起请求的结构体
|
||||
|
||||
@@ -2,8 +2,9 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var Date *DateClass
|
||||
|
||||
@@ -2,10 +2,11 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/viper"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// EnvToml - 读取配置文件
|
||||
|
||||
@@ -4,13 +4,14 @@ import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/spf13/cast"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// FileStruct - File 结构体
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var Format *FormatClass
|
||||
|
||||
64
utils/gen.go
Normal file
64
utils/gen.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Gen - 生成
|
||||
var Gen *GenClass
|
||||
|
||||
type GenClass struct{}
|
||||
|
||||
// SerialNo 生成指定前缀和长度的序列号
|
||||
// prefix: 前缀字符串
|
||||
// len: 序列号总长度
|
||||
// 格式: 前缀 + 日期(8位) + 时间(6位) + 随机数(动态长度)
|
||||
// 当指定长度小于前缀+日期+时间的长度时,会截断到指定长度
|
||||
func (this *GenClass) SerialNo(prefix string, length int) string {
|
||||
|
||||
// 种子
|
||||
seed := Hash.Sum32(fmt.Sprintf("%s-%d-%d", Get.Mac(), Get.Pid(), time.Now().UnixNano()))
|
||||
// 使用当前时间戳创建随机数生成器
|
||||
source := rand.New(rand.NewSource(cast.ToInt64(seed)))
|
||||
|
||||
// 获取当前日期时间
|
||||
now := time.Now()
|
||||
datePart := now.Format("20060102") // 8位日期
|
||||
timePart := now.Format("150405") // 6位时间
|
||||
|
||||
// 计算固定部分的总长度
|
||||
fixedPart := prefix + datePart + timePart
|
||||
fixedLength := len(fixedPart)
|
||||
|
||||
var serialNo string
|
||||
|
||||
// 如果指定长度小于等于固定部分长度,直接截断到指定长度
|
||||
if length <= fixedLength {
|
||||
|
||||
serialNo = fixedPart[:length]
|
||||
|
||||
} else {
|
||||
|
||||
// 计算需要的随机数长度
|
||||
randomLength := length - fixedLength
|
||||
|
||||
// 生成指定长度的随机数
|
||||
// 计算10的randomLength次方,作为随机数的上限
|
||||
MAX := 1
|
||||
for i := 0; i < randomLength; i++ {
|
||||
MAX *= 10
|
||||
}
|
||||
|
||||
// 生成随机数并格式化到指定长度
|
||||
randomPart := fmt.Sprintf("%0" + fmt.Sprintf("%dd", randomLength), source.Intn(MAX))
|
||||
|
||||
// 组合所有部分
|
||||
serialNo = fixedPart + randomPart
|
||||
}
|
||||
|
||||
return serialNo
|
||||
}
|
||||
23
utils/get.go
23
utils/get.go
@@ -2,11 +2,13 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Get - 获取
|
||||
@@ -115,3 +117,22 @@ func (this *GetClass) Pwd() (result string) {
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
// Env - 获取环境变量
|
||||
func (this *GetClass) Env() (config *any) {
|
||||
|
||||
// 初始化 Viper
|
||||
viper.SetConfigFile(".env")
|
||||
viper.SetConfigType("env")
|
||||
|
||||
// 读取配置文件
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return
|
||||
}
|
||||
// 反序列化配置
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"reflect"
|
||||
"regexp"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Is - 是否为
|
||||
|
||||
@@ -3,10 +3,11 @@ package utils
|
||||
import (
|
||||
JSON "encoding/json"
|
||||
"fmt"
|
||||
json "github.com/json-iterator/go"
|
||||
"github.com/spf13/cast"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
json "github.com/json-iterator/go"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Json - JSON 处理
|
||||
|
||||
@@ -2,9 +2,10 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var Lang *LangClass
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var Mime *MimeClass
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Net - 网络
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// 读写锁 - 防止并发写入
|
||||
|
||||
@@ -2,9 +2,10 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Rand - 随机数
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"reflect"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Struct - 操作结构体
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Unity - 统一规范化
|
||||
|
||||
@@ -4,9 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type validator struct {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/spf13/cast"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Version - 版本
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ViperClass struct {
|
||||
|
||||
Reference in New Issue
Block a user