update go mod
This commit is contained in:
commit
445c6407db
78
array.go
78
array.go
@ -1,78 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-03-30 14:35
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type array struct {
|
||||
}
|
||||
|
||||
// In 判断指定数据是否在目标数据集中, 不存在返回 -1 , 存在时返回数据对应的索引
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:38 2023/3/30
|
||||
func (a *array) In(search interface{}, source interface{}) int {
|
||||
if nil == source {
|
||||
return -1
|
||||
}
|
||||
sourceType := reflect.TypeOf(source).Kind()
|
||||
if sourceType != reflect.Array && sourceType != reflect.Slice {
|
||||
return -1
|
||||
}
|
||||
|
||||
byteData, _ := json.Marshal(source)
|
||||
var (
|
||||
formatSourceData []interface{}
|
||||
)
|
||||
|
||||
_ = JSON.UnmarshalWithNumber(byteData, &formatSourceData)
|
||||
|
||||
if nil == search {
|
||||
for idx, item := range formatSourceData {
|
||||
if item == nil {
|
||||
return idx
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
searchType := reflect.TypeOf(search).Kind()
|
||||
for idx, item := range formatSourceData {
|
||||
itemKind := reflect.TypeOf(item).Kind()
|
||||
if searchType != itemKind {
|
||||
// 类型不同, 检测是否为数字
|
||||
if strings.Contains(searchType.String(), "int") || strings.Contains(searchType.String(), "float") {
|
||||
// 查询的是数字
|
||||
if _, ok := item.(json.Number); ok {
|
||||
if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {
|
||||
return idx
|
||||
}
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {
|
||||
return idx
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// InAny In 函数的简化写法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:25 2023/3/30
|
||||
func (a *array) InAny(search interface{}, source ...interface{}) int {
|
||||
return a.In(search, source)
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-03-30 14:43
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_array_In(t *testing.T) {
|
||||
//assert.Equal(t, -1, Array.In(1234, []string{"1234", "123", "1"}))
|
||||
assert.Equal(t, 0, Array.In(1234, []interface{}{1234, "123", "1"}))
|
||||
//assert.Equal(t, -1, Array.In(nil, []interface{}{1234, "123", "1"}))
|
||||
//assert.Equal(t, 3, Array.In(nil, []interface{}{1234, "123", "1", nil}))
|
||||
}
|
33
browser.go
33
browser.go
@ -1,33 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-04-09 19:02
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type browser struct {
|
||||
commandTable map[string]string
|
||||
}
|
||||
|
||||
// Open 使用系统默认浏览器打开链接
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:05 2023/4/9
|
||||
func (b *browser) Open(link string) error {
|
||||
run, ok := b.commandTable[runtime.GOOS]
|
||||
if !ok {
|
||||
return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
|
||||
}
|
||||
|
||||
cmd := exec.Command(run, link)
|
||||
return cmd.Start()
|
||||
}
|
265
file.go
265
file.go
@ -1,265 +0,0 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : 文件相关工具
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-04-26 6:00 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util/define"
|
||||
|
||||
"github.com/go-ini/ini"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
yml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// file 文件相关操作
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:08 2022/5/14
|
||||
type file struct {
|
||||
}
|
||||
|
||||
// GetProjectPath 获取项目路径(可执行文件所在目录)
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:32 下午 2021/4/26
|
||||
func (f *file) GetProjectPath() (string, error) {
|
||||
rootPath, err := os.Getwd()
|
||||
if nil != err {
|
||||
return "", err
|
||||
}
|
||||
pathArr := strings.Split(rootPath, "/")
|
||||
if len(pathArr) > 0 {
|
||||
if pathArr[len(pathArr)-1] == "test" {
|
||||
rootPath = strings.Join(pathArr[0:len(pathArr)-1], "/")
|
||||
}
|
||||
}
|
||||
return rootPath, nil
|
||||
}
|
||||
|
||||
// ReadYmlContent 读取yml配置问价,并解析到指定的结构体中
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:35 下午 2021/4/26
|
||||
func (f *file) ReadYmlContent(filePath string, result interface{}) error {
|
||||
if nil == result {
|
||||
return errors.New("接收读取结果的数据指针为NIL")
|
||||
}
|
||||
var (
|
||||
fileContent []byte
|
||||
err error
|
||||
)
|
||||
if fileContent, err = f.ReadFileContent(filePath); nil != err {
|
||||
return err
|
||||
}
|
||||
return yml.Unmarshal(fileContent, result)
|
||||
}
|
||||
|
||||
// ReadJSONContent 读取JSON内容,并解析到指定的结构体中
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:23 2022/6/9
|
||||
func (f *file) ReadJSONContent(filePath string, result interface{}) error {
|
||||
if nil == result {
|
||||
return errors.New("接收读取结果的数据指针为NIL")
|
||||
}
|
||||
var (
|
||||
fileContent []byte
|
||||
err error
|
||||
)
|
||||
if fileContent, err = f.ReadFileContent(filePath); nil != err {
|
||||
return err
|
||||
}
|
||||
return JSON.UnmarshalWithNumber(fileContent, result)
|
||||
}
|
||||
|
||||
// ReadIniContent 读取并解析ini文件
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 13:21 2022/7/2
|
||||
func (f *file) ReadIniContent(filePath string, result interface{}) error {
|
||||
if nil == result {
|
||||
return errors.New("接收读取结果的数据指针为NIL")
|
||||
}
|
||||
return ini.MapTo(result, filePath)
|
||||
}
|
||||
|
||||
// ReadTomlContent 读取toml格式文件
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:08 2023/7/31
|
||||
func (f *file) ReadTomlContent(filePath string, result interface{}) error {
|
||||
if nil == result {
|
||||
return errors.New("接收读取结果的数据指针为NIL")
|
||||
}
|
||||
if _, err := toml.DecodeFile("example.toml", result); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadAnyFileContent 读取任意类型的文件并解析
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 13:11 2022/7/2
|
||||
func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error {
|
||||
fileInfoArr := strings.Split(filePath, ".")
|
||||
if len(fileInfoArr) < 2 {
|
||||
return errors.New("未知的文件类型")
|
||||
}
|
||||
var (
|
||||
parseFunc func(filePath string, receiver interface{}) error
|
||||
)
|
||||
|
||||
fileExt := strings.ToLower(fileInfoArr[len(fileInfoArr)-1])
|
||||
switch fileExt {
|
||||
case "json":
|
||||
parseFunc = f.ReadJSONContent
|
||||
case "yml":
|
||||
fallthrough
|
||||
case "yaml":
|
||||
parseFunc = f.ReadYmlContent
|
||||
case "ini":
|
||||
parseFunc = f.ReadIniContent
|
||||
default:
|
||||
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
|
||||
}
|
||||
return parseFunc(filePath, receiver)
|
||||
}
|
||||
|
||||
// ReadFileContent 读取文件内容
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:37 下午 2021/4/26
|
||||
func (f *file) ReadFileContent(filePath string) ([]byte, error) {
|
||||
if exist, isFile := f.IsFileExist(filePath); !exist || !isFile {
|
||||
//文件不存在或者是一个目录
|
||||
return nil, errors.New(filePath + " 文件不存在或者是一个目录!")
|
||||
}
|
||||
//打开文件
|
||||
var (
|
||||
fileHandler *os.File
|
||||
err error
|
||||
)
|
||||
if fileHandler, err = os.Open(filePath); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ioutil.ReadAll(fileHandler)
|
||||
}
|
||||
|
||||
// IsFileExist 判断文件是否存在
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:37 下午 2021/4/26
|
||||
func (f *file) IsFileExist(filePath string) (bool, bool) {
|
||||
fileStat, err := os.Stat(filePath)
|
||||
return nil == err || os.IsExist(err), (nil == err || os.IsExist(err)) && !fileStat.IsDir()
|
||||
}
|
||||
|
||||
// GetFileMIMEType 获取本地文件的MIME类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:15 2023/2/7
|
||||
func (f *file) GetFileMIMEType(filePath string) (string, error) {
|
||||
var (
|
||||
err error
|
||||
fileHandler *os.File
|
||||
)
|
||||
if fileHandler, err = os.Open(filePath); nil != err {
|
||||
return "", err
|
||||
}
|
||||
// 只需要前 512 个字节就可以了
|
||||
buffer := make([]byte, 512)
|
||||
if _, err = fileHandler.Read(buffer); nil != err {
|
||||
return "", err
|
||||
}
|
||||
contentType := http.DetectContentType(buffer)
|
||||
return contentType, nil
|
||||
}
|
||||
|
||||
// ReadDirFileList 读取目录下的文件列表, 支持递归
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:42 2023/7/31
|
||||
func (f *file) ReadDirFileList(dirPath string, ignoreHiddenFile bool, isRecurve bool) ([]*define.FileInfo, error) {
|
||||
var (
|
||||
err error
|
||||
fileList []os.DirEntry
|
||||
itemFileInfo os.FileInfo
|
||||
)
|
||||
|
||||
result := make([]*define.FileInfo, 0)
|
||||
if fileList, err = os.ReadDir(dirPath); nil != err {
|
||||
return result, err
|
||||
}
|
||||
for _, itemFile := range fileList {
|
||||
if ignoreHiddenFile && strings.HasPrefix(itemFile.Name(), ".") {
|
||||
// 或略隐藏文件、文件夹
|
||||
continue
|
||||
}
|
||||
if itemFileInfo, err = itemFile.Info(); nil != err {
|
||||
return result, err
|
||||
}
|
||||
fileInfo := &define.FileInfo{
|
||||
IsDir: itemFile.IsDir(),
|
||||
Name: itemFile.Name(),
|
||||
AbsolutePath: filepath.Join(dirPath, itemFile.Name()),
|
||||
Format: "",
|
||||
Size: itemFileInfo.Size(),
|
||||
ModifyTime: itemFileInfo.ModTime().Unix(),
|
||||
Mode: itemFileInfo.Mode(),
|
||||
FileList: make([]*define.FileInfo, 0),
|
||||
}
|
||||
if !fileInfo.IsDir {
|
||||
fileArr := strings.Split(fileInfo.Name, ".")
|
||||
if len(fileArr) >= 2 {
|
||||
fileInfo.Format = fileArr[len(fileArr)-1]
|
||||
} else {
|
||||
fileInfo.Format = "unknown"
|
||||
}
|
||||
} else {
|
||||
fileInfo.Format = "dir"
|
||||
}
|
||||
fileInfo.Format = strings.ToLower(fileInfo.Format)
|
||||
if fileInfo.IsDir && isRecurve {
|
||||
if fileInfo.FileList, err = f.ReadDirFileList(fileInfo.AbsolutePath, ignoreHiddenFile, isRecurve); nil != err {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
result = append(result, fileInfo)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ReadFileListRecurve 递归读取文件内容
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:26 2023/7/31
|
||||
func (f *file) ReadFileListRecurve(rootDir string) ([]*define.FileInfo, error) {
|
||||
return nil, nil
|
||||
}
|
20
file_test.go
20
file_test.go
@ -1,20 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-07-31 15:15
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_file_ReadDirFileList(t *testing.T) {
|
||||
res, err := File.ReadDirFileList("./", false, true)
|
||||
if nil != err {
|
||||
panic(err.Error())
|
||||
}
|
||||
JSON.ConsoleOutput(res)
|
||||
}
|
137
filter.go
137
filter.go
@ -1,137 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022-07-04 11:45
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Jeffail/gabs"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// MapRule 映射规则
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:21 2022/7/4
|
||||
type MapRule struct {
|
||||
SourcePath string `json:"source_path"` // 原路径
|
||||
MapPath string `json:"map_path"` // 映射路径
|
||||
Required bool `json:"required"` // 必须存在
|
||||
DataType string `json:"data_type"` // 数据类型
|
||||
DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType
|
||||
}
|
||||
|
||||
// NewFilter 过滤器实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:54 2022/7/4
|
||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
return &filter{
|
||||
sourceData: sourceData,
|
||||
filterRuleList: filterRuleList,
|
||||
jsonObj: &gabs.Container{},
|
||||
}
|
||||
}
|
||||
|
||||
// filter 数据过滤
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:58 2022/7/4
|
||||
type filter struct {
|
||||
sourceData string
|
||||
filterRuleList []MapRule
|
||||
jsonObj *gabs.Container // 生成的json对象实例
|
||||
}
|
||||
|
||||
// Deal ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:59 2022/7/4
|
||||
func (f *filter) Deal() ([]byte, error) {
|
||||
for _, rule := range f.filterRuleList {
|
||||
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
||||
sourceVal := sourceResult.String()
|
||||
if !sourceResult.Exists() {
|
||||
// 不存在, 使用默认值
|
||||
sourceVal = rule.DefaultValue
|
||||
}
|
||||
formatVal, err := f.getValue(rule.DataType, sourceVal)
|
||||
if nil != err {
|
||||
return nil, fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
|
||||
}
|
||||
if _, err := f.jsonObj.SetP(formatVal, rule.MapPath); nil != err {
|
||||
return nil, fmt.Errorf("%s set val = %v fail : %s", rule.MapPath, formatVal, err.Error())
|
||||
}
|
||||
}
|
||||
return f.jsonObj.EncodeJSON(), nil
|
||||
}
|
||||
|
||||
// getValue 获取值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:25 2022/7/4
|
||||
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
|
||||
switch dataType {
|
||||
case "int8":
|
||||
fallthrough
|
||||
case "int16":
|
||||
fallthrough
|
||||
case "int32":
|
||||
fallthrough
|
||||
case "int64":
|
||||
fallthrough
|
||||
case "int":
|
||||
var (
|
||||
err error
|
||||
val int64
|
||||
)
|
||||
err = ConvertAssign(&val, defaultValue)
|
||||
return val, err
|
||||
case "uint8":
|
||||
fallthrough
|
||||
case "uint16":
|
||||
fallthrough
|
||||
case "uint32":
|
||||
fallthrough
|
||||
case "uint64":
|
||||
fallthrough
|
||||
case "uint":
|
||||
var (
|
||||
err error
|
||||
val uint64
|
||||
)
|
||||
err = ConvertAssign(&val, defaultValue)
|
||||
return val, err
|
||||
case "bool":
|
||||
var (
|
||||
err error
|
||||
val bool
|
||||
)
|
||||
err = ConvertAssign(&val, defaultValue)
|
||||
return val, err
|
||||
case "float32":
|
||||
fallthrough
|
||||
case "float64":
|
||||
var (
|
||||
err error
|
||||
val float64
|
||||
)
|
||||
err = ConvertAssign(&val, defaultValue)
|
||||
return val, err
|
||||
case "string":
|
||||
return defaultValue, nil
|
||||
default:
|
||||
return nil, errors.New(dataType + " is not support!")
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022-07-04 12:44
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_filter_Deal(t *testing.T) {
|
||||
sourceData := `{
|
||||
"name":"zhangsan",
|
||||
"age":"18",
|
||||
"extension":{
|
||||
"sex":"man",
|
||||
"height":"180"
|
||||
}
|
||||
}`
|
||||
ruleList := []MapRule{
|
||||
{
|
||||
SourcePath: "name",
|
||||
MapPath: "user_name",
|
||||
Required: true,
|
||||
DataType: "string",
|
||||
DefaultValue: "lalala",
|
||||
},
|
||||
{
|
||||
SourcePath: "age",
|
||||
MapPath: "user_age",
|
||||
Required: true,
|
||||
DataType: "int",
|
||||
DefaultValue: "280",
|
||||
},
|
||||
{
|
||||
SourcePath: "extension.height",
|
||||
MapPath: "user_height",
|
||||
Required: true,
|
||||
DataType: "int",
|
||||
DefaultValue: "359",
|
||||
},
|
||||
{
|
||||
SourcePath: "extension.sex",
|
||||
MapPath: "user_sex",
|
||||
Required: true,
|
||||
DataType: "string",
|
||||
DefaultValue: "lalala",
|
||||
},
|
||||
}
|
||||
instance := NewFilter(sourceData, ruleList)
|
||||
result, err := instance.Deal()
|
||||
fmt.Println(string(result), err)
|
||||
}
|
19
go.mod
19
go.mod
@ -1,26 +1,9 @@
|
||||
module git.zhangdeman.cn/zhangdeman/util
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2
|
||||
github.com/Jeffail/gabs v1.4.0
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
|
||||
github.com/go-ini/ini v1.67.0
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/mozillazg/go-pinyin v0.20.0
|
||||
github.com/mssola/user_agent v0.6.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/spaolacci/murmur3 v1.1.0
|
||||
github.com/stretchr/testify v1.8.2
|
||||
github.com/tidwall/gjson v1.17.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
)
|
||||
|
44
go.sum
44
go.sum
@ -1,50 +1,6 @@
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
|
||||
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
|
||||
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
|
||||
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
|
||||
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.15.0 h1:5n/pM+v3r5ujuNl4YLZLsQ+UE5jlkLVm7jMzT5Mpolw=
|
||||
github.com/tidwall/gjson v1.15.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
|
||||
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
48
init.go
48
init.go
@ -10,71 +10,23 @@ package util
|
||||
var (
|
||||
// Cli ...
|
||||
Cli *cli
|
||||
// File ...
|
||||
File *file
|
||||
// Hash ...
|
||||
Hash *hash
|
||||
// IP ...
|
||||
IP *ip
|
||||
// JSON ...
|
||||
JSON *ownJSON
|
||||
// String ...
|
||||
String *stringOperate
|
||||
// Struct ...
|
||||
Struct *ownStruct
|
||||
// Time ...
|
||||
Time *ownTime
|
||||
// URL ...
|
||||
URL *ownURL
|
||||
// Map ...
|
||||
Map *ownMap
|
||||
// Calculate ...
|
||||
Calculate *calculate
|
||||
// Project ...
|
||||
Project *project
|
||||
// 数组操作
|
||||
Array *array
|
||||
// JWT ...
|
||||
JWT *ownJwt
|
||||
// UserAgent ...
|
||||
UserAgent *userAgent
|
||||
// Console 控制台输出数据
|
||||
Console *console
|
||||
// Browser 浏览器操作实例
|
||||
Browser *browser
|
||||
// StringConvert 字符串转为任意类型
|
||||
StringConvert *stringConvert
|
||||
// PinYin 汉字转拼音
|
||||
PinYin *pinYin
|
||||
)
|
||||
|
||||
func init() {
|
||||
Cli = &cli{}
|
||||
File = &file{}
|
||||
Hash = &hash{}
|
||||
IP = &ip{}
|
||||
JSON = &ownJSON{}
|
||||
String = &stringOperate{}
|
||||
Struct = &ownStruct{}
|
||||
Time = &ownTime{}
|
||||
URL = &ownURL{}
|
||||
Map = &ownMap{}
|
||||
Calculate = &calculate{}
|
||||
Project = &project{}
|
||||
Array = &array{}
|
||||
JWT = &ownJwt{}
|
||||
UserAgent = &userAgent{
|
||||
list: defaultUserAgentList,
|
||||
}
|
||||
Console = &console{}
|
||||
Browser = &browser{
|
||||
commandTable: map[string]string{
|
||||
"windows": "start",
|
||||
"darwin": "open",
|
||||
"linux": "xdg-open",
|
||||
},
|
||||
}
|
||||
// StringConvert
|
||||
StringConvert = &stringConvert{}
|
||||
PinYin = &pinYin{}
|
||||
}
|
||||
|
59
ip.go
59
ip.go
@ -1,59 +0,0 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : util...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-03-09 5:56 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ip ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:47 2022/5/14
|
||||
type ip struct {
|
||||
}
|
||||
|
||||
// GetHostIP 获取本机IP地址
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:58 下午 2021/3/9
|
||||
func (i *ip) GetHostIP() string {
|
||||
hostIP := "127.0.0.1"
|
||||
addrs, _ := net.InterfaceAddrs()
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
hostIP = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return hostIP
|
||||
}
|
||||
|
||||
// GetRemoteIP 获取远端IP
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:35 下午 2021/11/22
|
||||
func (i *ip) GetRemoteIP(req *http.Request) string {
|
||||
// Try via request
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
|
||||
if err != nil {
|
||||
return "::1"
|
||||
}
|
||||
userIP := net.ParseIP(remoteIP)
|
||||
if userIP == nil {
|
||||
return "::1"
|
||||
}
|
||||
return userIP.String()
|
||||
}
|
128
json.go
128
json.go
@ -1,128 +0,0 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : json 工具函数
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-09-14 8:38 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// ownJSON ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:01 2022/5/14
|
||||
type ownJSON struct {
|
||||
}
|
||||
|
||||
// UnmarshalWithNumber 解析json
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:39 下午 2021/9/14
|
||||
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(byteData))
|
||||
decoder.UseNumber()
|
||||
return decoder.Decode(receiver)
|
||||
}
|
||||
|
||||
// UnmarshalWithNumberForIOReader ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:43 下午 2021/9/14
|
||||
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver interface{}) error {
|
||||
decoder := json.NewDecoder(ioReader)
|
||||
decoder.UseNumber()
|
||||
return decoder.Decode(receiver)
|
||||
}
|
||||
|
||||
// UnmarshalWithNumberForString 字符串转结构体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:50 2023/7/22
|
||||
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error {
|
||||
return oj.UnmarshalWithNumber([]byte(input), receiver)
|
||||
}
|
||||
|
||||
// MarshalForByte 序列化并返回字节数组
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:56 2023/7/22
|
||||
func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
|
||||
byteData, _ := json.Marshal(input)
|
||||
return byteData
|
||||
}
|
||||
|
||||
// MarshalForString 序列化并返回字符串
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:56 2023/7/22
|
||||
func (oj *ownJSON) MarshalForString(input interface{}) string {
|
||||
byteData := oj.MarshalForByte(input)
|
||||
return string(byteData)
|
||||
}
|
||||
|
||||
// ConsoleOutput ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:45 下午 2021/11/5
|
||||
func (oj *ownJSON) ConsoleOutput(data interface{}) {
|
||||
var out bytes.Buffer
|
||||
switch reflect.TypeOf(data).Kind() {
|
||||
case reflect.Slice:
|
||||
fallthrough
|
||||
case reflect.Array:
|
||||
fallthrough
|
||||
case reflect.Map:
|
||||
fallthrough
|
||||
case reflect.Ptr:
|
||||
byteData, _ := json.Marshal(data)
|
||||
_ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t")
|
||||
_, _ = out.WriteTo(os.Stdout)
|
||||
return
|
||||
case reflect.Int:
|
||||
fallthrough
|
||||
case reflect.Int8:
|
||||
fallthrough
|
||||
case reflect.Int16:
|
||||
fallthrough
|
||||
case reflect.Int32:
|
||||
fallthrough
|
||||
case reflect.Int64:
|
||||
fallthrough
|
||||
case reflect.Uint:
|
||||
fallthrough
|
||||
case reflect.Uint8:
|
||||
fallthrough
|
||||
case reflect.Uint16:
|
||||
fallthrough
|
||||
case reflect.Uint32:
|
||||
fallthrough
|
||||
case reflect.Uint64:
|
||||
fallthrough
|
||||
case reflect.Float32:
|
||||
fallthrough
|
||||
case reflect.Float64:
|
||||
fallthrough
|
||||
case reflect.String:
|
||||
_ = json.Indent(&out, []byte(fmt.Sprintf("%v\n", data)), "", "\t")
|
||||
_, _ = out.WriteTo(os.Stdout)
|
||||
return
|
||||
default:
|
||||
fmt.Println("")
|
||||
}
|
||||
}
|
66
jwt.go
66
jwt.go
@ -1,66 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : go-jwt具体实现
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-02-11 20:09
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dgrijalva/jwt-go/v4"
|
||||
)
|
||||
|
||||
type ownJwt struct {
|
||||
}
|
||||
|
||||
type MyClaims struct {
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
// Generate 生成 token
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:13 2023/2/11
|
||||
func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jwt.Claims) (string, error) {
|
||||
if nil == signMethod {
|
||||
signMethod = jwt.SigningMethodHS256
|
||||
}
|
||||
var (
|
||||
token *jwt.Token
|
||||
)
|
||||
|
||||
if nil == claims {
|
||||
token = jwt.New(signMethod)
|
||||
} else {
|
||||
token = jwt.NewWithClaims(signMethod, claims)
|
||||
}
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
// Validate 验证token
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:13 2023/2/11
|
||||
func (j *ownJwt) Validate(inputToken string, secret string, claims jwt.Claims) (jwt.Claims, error) {
|
||||
|
||||
var (
|
||||
token *jwt.Token
|
||||
err error
|
||||
)
|
||||
|
||||
if token, err = jwt.ParseWithClaims(inputToken, claims, func(tokenInfo *jwt.Token) (interface{}, error) {
|
||||
return []byte(secret), nil
|
||||
}); nil != err {
|
||||
return nil, errors.New("token parse fail : " + err.Error())
|
||||
}
|
||||
if !token.Valid {
|
||||
return nil, errors.New("token is valid : " + err.Error())
|
||||
}
|
||||
|
||||
return token.Claims, nil
|
||||
}
|
229
map.go
229
map.go
@ -1,229 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : 从map中读取数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-16 4:28 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ownMap ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:53 2022/5/14
|
||||
type ownMap struct {
|
||||
}
|
||||
|
||||
// Exist 检测一个key在map中是否存在
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:52 下午 2021/11/16
|
||||
func (om *ownMap) Exist(source map[interface{}]interface{}, key interface{}) bool {
|
||||
if nil == source {
|
||||
return false
|
||||
}
|
||||
if _, exist := source[key]; !exist {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetInt 获取int值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:30 下午 2021/11/16
|
||||
func (om *ownMap) GetInt(source map[interface{}]interface{}, key interface{}, defaultVal int) int {
|
||||
var result int
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetInt8 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:59 下午 2021/11/16
|
||||
func (om *ownMap) GetInt8(source map[interface{}]interface{}, key interface{}, defaultVal int8) int8 {
|
||||
var result int8
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetInt16 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:59 下午 2021/11/16
|
||||
func (om *ownMap) GetInt16(source map[interface{}]interface{}, key interface{}, defaultVal int16) int16 {
|
||||
var result int16
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetInt32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:00 下午 2021/11/16
|
||||
func (om *ownMap) GetInt32(source map[interface{}]interface{}, key interface{}, defaultVal int32) int32 {
|
||||
var result int32
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetInt64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:00 下午 2021/11/16
|
||||
func (om *ownMap) GetInt64(source map[interface{}]interface{}, key interface{}, defaultVal int64) int64 {
|
||||
var result int64
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetUint ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:01 下午 2021/11/16
|
||||
func (om *ownMap) GetUint(source map[interface{}]interface{}, key interface{}, defaultVal uint) uint {
|
||||
var result uint
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetUint8 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:01 下午 2021/11/16
|
||||
func (om *ownMap) GetUint8(source map[interface{}]interface{}, key interface{}, defaultVal uint8) uint8 {
|
||||
var result uint8
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetUint16 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:02 下午 2021/11/16
|
||||
func (om *ownMap) GetUint16(source map[interface{}]interface{}, key interface{}, defaultVal uint16) uint16 {
|
||||
var result uint16
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetUint32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:02 下午 2021/11/16
|
||||
func (om *ownMap) GetUint32(source map[interface{}]interface{}, key interface{}, defaultVal uint32) uint32 {
|
||||
var result uint32
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetUint64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:03 下午 2021/11/16
|
||||
func (om *ownMap) GetUint64(source map[interface{}]interface{}, key interface{}, defaultVal uint64) uint64 {
|
||||
var result uint64
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetFloat32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:03 下午 2021/11/16
|
||||
func (om *ownMap) GetFloat32(source map[interface{}]interface{}, key interface{}, defaultVal float32) float32 {
|
||||
var result float32
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetFloat64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:04 下午 2021/11/16
|
||||
func (om *ownMap) GetFloat64(source map[interface{}]interface{}, key interface{}, defaultVal float64) float64 {
|
||||
var result float64
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetString ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:07 下午 2021/11/16
|
||||
func (om *ownMap) GetString(source map[interface{}]interface{}, key interface{}, defaultVal string) string {
|
||||
var result string
|
||||
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
||||
return defaultVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetDataWithReceiver 使用制定的数据指针接受结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:54 下午 2021/11/16
|
||||
func (om *ownMap) GetDataWithReceiver(source map[interface{}]interface{}, key interface{}, receiver interface{}) error {
|
||||
if !om.Exist(source, key) {
|
||||
return errors.New("key is not found")
|
||||
}
|
||||
return ConvertAssign(receiver, source[key])
|
||||
}
|
||||
|
||||
// ToStruct map转结构体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:48 2023/7/22
|
||||
func (om *ownMap) ToStruct(source map[string]interface{}, receiver interface{}) error {
|
||||
if nil == source {
|
||||
return nil
|
||||
}
|
||||
byteData := JSON.MarshalForByte(source)
|
||||
return JSON.UnmarshalWithNumber(byteData, receiver)
|
||||
}
|
203
string.go
203
string.go
@ -1,203 +0,0 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : 字符串相关的工具
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-03-09 6:00 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/axgle/mahonia"
|
||||
)
|
||||
|
||||
var (
|
||||
// LETTER_LIST 字母列表
|
||||
LETTER_LIST = []string{
|
||||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
|
||||
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||||
}
|
||||
)
|
||||
|
||||
// stringOperate ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:09 2022/5/14
|
||||
type stringOperate struct {
|
||||
}
|
||||
|
||||
// GenRandom 获取随机长度的字符串
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 6:01 下午 2021/3/9
|
||||
func (s *stringOperate) GenRandom(source string, length uint) string {
|
||||
if length == 0 {
|
||||
return ""
|
||||
}
|
||||
if len(source) == 0 {
|
||||
//字符串为空,默认字符源为如下(去除易混淆的i/l):
|
||||
source = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
|
||||
}
|
||||
strByte := []byte(source)
|
||||
var genStrByte = make([]byte, 0)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < int(length); i++ {
|
||||
genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))])
|
||||
}
|
||||
return string(genStrByte)
|
||||
}
|
||||
|
||||
// Md5 对字符串进行md5加密
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 6:01 下午 2021/3/9
|
||||
func (s *stringOperate) Md5(str string) string {
|
||||
h := md5.New()
|
||||
_, _ = h.Write([]byte(str))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Md5FromByte 从字节数组计算签名
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022/10/21 14:12:16
|
||||
func (s *stringOperate) Md5FromByte(data []byte) string {
|
||||
h := md5.New()
|
||||
_, _ = h.Write(data)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// GenRandomMd5 生成随机md5
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:11 2022/7/12
|
||||
func (s *stringOperate) GenRandomMd5() string {
|
||||
return s.Md5(s.GenRandom("", 16))
|
||||
}
|
||||
|
||||
// SnakeCaseToCamel 蛇形字符串转换为驼峰
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:58 下午 2021/10/25
|
||||
func (s *stringOperate) SnakeCaseToCamel(str string) string {
|
||||
if len(str) == 0 {
|
||||
return ""
|
||||
}
|
||||
builder := strings.Builder{}
|
||||
index := 0
|
||||
if str[0] >= 'a' && str[0] <= 'z' {
|
||||
builder.WriteByte(str[0] - ('a' - 'A'))
|
||||
index = 1
|
||||
}
|
||||
for i := index; i < len(str); i++ {
|
||||
if str[i] == '_' && i+1 < len(str) {
|
||||
if str[i+1] >= 'a' && str[i+1] <= 'z' {
|
||||
builder.WriteByte(str[i+1] - ('a' - 'A'))
|
||||
i++
|
||||
continue
|
||||
}
|
||||
}
|
||||
// 将ID转为大写
|
||||
if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') {
|
||||
builder.WriteByte('d' - ('a' - 'A'))
|
||||
continue
|
||||
}
|
||||
builder.WriteByte(str[i])
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// Convert 字符串编码转换
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:38 2022/7/9
|
||||
func (s *stringOperate) Convert(str string, sourceCode string, targetCode string) string {
|
||||
sourceCoder := mahonia.NewDecoder(sourceCode)
|
||||
sourceResult := sourceCoder.ConvertString(str)
|
||||
targetCoder := mahonia.NewDecoder(targetCode)
|
||||
_, cdata, _ := targetCoder.Translate([]byte(sourceResult), true)
|
||||
return string(cdata)
|
||||
}
|
||||
|
||||
// RemoveDuplicates 对列表数据去重
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:12 2022/7/23
|
||||
func (s *stringOperate) RemoveDuplicates(sourceList []string) []string {
|
||||
result := make([]string, 0)
|
||||
hasDeal := make(map[string]bool)
|
||||
if len(sourceList) == 0 {
|
||||
return result
|
||||
}
|
||||
for _, val := range sourceList {
|
||||
if _, exist := hasDeal[val]; exist {
|
||||
continue
|
||||
}
|
||||
result = append(result, val)
|
||||
hasDeal[val] = true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Map2Query map参数转换为url query
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:51 2022/10/10
|
||||
func (s *stringOperate) Map2Query(data map[string]string) string {
|
||||
list := make([]string, 0)
|
||||
for k, v := range data {
|
||||
list = append(list, k+"="+v)
|
||||
}
|
||||
return strings.Join(list, "&")
|
||||
}
|
||||
|
||||
// ClearChar 清理指定字符
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:53 2023/1/13
|
||||
func (s *stringOperate) ClearChar(src string, charList ...string) string {
|
||||
if len(charList) == 0 {
|
||||
return src
|
||||
}
|
||||
for _, item := range charList {
|
||||
src = strings.ReplaceAll(src, item, "")
|
||||
}
|
||||
return src
|
||||
}
|
||||
|
||||
// ReplaceChineseChar 替换常见的中文符号
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:59 2023/4/4
|
||||
func (s *stringOperate) ReplaceChineseChar(str string) string {
|
||||
charTable := map[string]string{
|
||||
"(": "(",
|
||||
")": ")",
|
||||
":": ":",
|
||||
",": ",",
|
||||
"。": ".",
|
||||
"【": "]",
|
||||
"】": "]",
|
||||
}
|
||||
for k, v := range charTable {
|
||||
str = strings.ReplaceAll(str, k, v)
|
||||
}
|
||||
return str
|
||||
}
|
@ -1,653 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-05-04 18:25
|
||||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type stringConvert struct {
|
||||
}
|
||||
|
||||
// ToFloat32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:26 2023/5/4
|
||||
func (sc *stringConvert) ToFloat32(str string) (float32, error) {
|
||||
var (
|
||||
err error
|
||||
res float32
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToFloat64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:26 2023/5/4
|
||||
func (sc *stringConvert) ToFloat64(str string) (float64, error) {
|
||||
var (
|
||||
err error
|
||||
res float64
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToDouble 转double
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:33 2023/5/4
|
||||
func (sc *stringConvert) ToDouble(str string) (float64, error) {
|
||||
return sc.ToFloat64(str)
|
||||
}
|
||||
|
||||
// ToNumber 转数字, 使用最高精度的float64
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:33 2023/5/4
|
||||
func (sc *stringConvert) ToNumber(str string) (float64, error) {
|
||||
return sc.ToFloat64(str)
|
||||
}
|
||||
|
||||
// ToInt8 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToInt8(str string) (int8, error) {
|
||||
var (
|
||||
err error
|
||||
res int8
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToInt16 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToInt16(str string) (int16, error) {
|
||||
var (
|
||||
err error
|
||||
res int16
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToInt32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToInt32(str string) (int32, error) {
|
||||
var (
|
||||
err error
|
||||
res int32
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToInt64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToInt64(str string) (int64, error) {
|
||||
var (
|
||||
err error
|
||||
res int64
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToInt ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:47 2023/5/5
|
||||
func (sc *stringConvert) ToInt(str string) (int, error) {
|
||||
var (
|
||||
err error
|
||||
res int
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToUint8 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:30 2023/5/4
|
||||
func (sc *stringConvert) ToUint8(str string) (uint8, error) {
|
||||
var (
|
||||
err error
|
||||
res uint8
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToUint16 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToUint16(str string) (uint16, error) {
|
||||
var (
|
||||
err error
|
||||
res uint16
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToUint32 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToUint32(str string) (uint32, error) {
|
||||
var (
|
||||
err error
|
||||
res uint32
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToUint64 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToUint64(str string) (uint64, error) {
|
||||
var (
|
||||
err error
|
||||
res uint64
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToUint ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:29 2023/5/4
|
||||
func (sc *stringConvert) ToUint(str string) (uint, error) {
|
||||
var (
|
||||
err error
|
||||
res uint
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToBool ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:34 2023/5/4
|
||||
func (sc *stringConvert) ToBool(str string) (bool, error) {
|
||||
var (
|
||||
err error
|
||||
res bool
|
||||
)
|
||||
err = ConvertAssign(&res, str)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// ToObject ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:35 2023/5/4
|
||||
func (sc *stringConvert) ToObject(str string) (map[string]interface{}, error) {
|
||||
var (
|
||||
err error
|
||||
result map[string]interface{}
|
||||
)
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &result); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ToStruct ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:38 2023/5/4
|
||||
func (sc *stringConvert) ToStruct(str string, receiver interface{}) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
return JSON.UnmarshalWithNumber([]byte(str), receiver)
|
||||
}
|
||||
|
||||
// ToInt8Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:55 2023/5/4
|
||||
func (sc *stringConvert) ToInt8Slice(str string, splitChar ...string) ([]int8, error) {
|
||||
var (
|
||||
err error
|
||||
res []int8
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]int8, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToInt8(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToInt16Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:01 2023/5/4
|
||||
func (sc *stringConvert) ToInt16Slice(str string, splitChar ...string) ([]int16, error) {
|
||||
var (
|
||||
err error
|
||||
res []int16
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]int16, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToInt16(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToInt32Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:03 2023/5/4
|
||||
func (sc *stringConvert) ToInt32Slice(str string, splitChar ...string) ([]int32, error) {
|
||||
var (
|
||||
err error
|
||||
res []int32
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]int32, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToInt32(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToInt64Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:04 2023/5/4
|
||||
func (sc *stringConvert) ToInt64Slice(str string, splitChar ...string) ([]int64, error) {
|
||||
var (
|
||||
err error
|
||||
res []int64
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]int64, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToInt64(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToIntSlice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:04 2023/5/4
|
||||
func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, error) {
|
||||
var (
|
||||
err error
|
||||
res []int
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]int, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToInt(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToUint8Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:50 2023/5/5
|
||||
func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) {
|
||||
var (
|
||||
err error
|
||||
res []uint8
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]uint8, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToUint8(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToUint16Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:52 2023/5/5
|
||||
func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) {
|
||||
var (
|
||||
err error
|
||||
res []uint16
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]uint16, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToUint16(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToUint32Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:53 2023/5/5
|
||||
func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) {
|
||||
var (
|
||||
err error
|
||||
res []uint32
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]uint32, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToUint32(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToUint64Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:54 2023/5/5
|
||||
func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) {
|
||||
var (
|
||||
err error
|
||||
res []uint64
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]uint64, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToUint64(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToUintSlice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:54 2023/5/5
|
||||
func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) {
|
||||
var (
|
||||
err error
|
||||
res []uint
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]uint, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToUint(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToBoolSlice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:55 2023/5/5
|
||||
func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) {
|
||||
var (
|
||||
err error
|
||||
res []bool
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]bool, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToBool(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToFloat32Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:56 2023/5/5
|
||||
func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) {
|
||||
var (
|
||||
err error
|
||||
res []float32
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]float32, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToFloat32(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToFloat64Slice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:56 2023/5/5
|
||||
func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) {
|
||||
var (
|
||||
err error
|
||||
res []float64
|
||||
)
|
||||
|
||||
isSplit := len(splitChar) > 0
|
||||
if !isSplit {
|
||||
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res = make([]float64, 0)
|
||||
arr := strings.Split(str, splitChar[0])
|
||||
for _, item := range arr {
|
||||
if itemVal, err := sc.ToFloat64(item); nil != err {
|
||||
return nil, err
|
||||
} else {
|
||||
res = append(res, itemVal)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToDoubleSlice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:57 2023/5/5
|
||||
func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) {
|
||||
return sc.ToFloat64Slice(str, splitChar...)
|
||||
}
|
||||
|
||||
// ToNumberSlice ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:57 2023/5/5
|
||||
func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) {
|
||||
return sc.ToFloat64Slice(str, splitChar...)
|
||||
}
|
38
struct.go
38
struct.go
@ -1,38 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-03-14 11:11 下午
|
||||
package util
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ownStruct ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:19 2022/5/14
|
||||
type ownStruct struct {
|
||||
}
|
||||
|
||||
// ToMap 结构体转为map
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:12 下午 2021/3/14
|
||||
func (os *ownStruct) ToMap(data interface{}) (map[string]interface{}, error) {
|
||||
var (
|
||||
byteData []byte
|
||||
err error
|
||||
result map[string]interface{}
|
||||
)
|
||||
if byteData, err = json.Marshal(data); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if err = json.Unmarshal(byteData, &result); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
91
time.go
91
time.go
@ -1,91 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : 时间相关操作
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-10-07 1:33 上午
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ownTime ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:24 2022/5/14
|
||||
type ownTime struct {
|
||||
format string
|
||||
}
|
||||
|
||||
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 1:34 上午 2021/10/7
|
||||
func (ot *ownTime) GetCurrentFormatTime(layout ...string) string {
|
||||
if len(layout) == 0 {
|
||||
layout = []string{"2006-01-02 15:04:05"}
|
||||
}
|
||||
return time.Now().In(time.Local).Format(layout[0])
|
||||
}
|
||||
|
||||
// FormatUnixNano 格式化纳秒时间戳
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:54 2022/7/14
|
||||
func (ot *ownTime) FormatUnixNano(timestamp int64, layout ...string) string {
|
||||
if len(layout) == 0 {
|
||||
layout = []string{"2006-01-02 15:04:05.000"}
|
||||
}
|
||||
nano := timestamp % 1e6
|
||||
milli := timestamp / 1e6
|
||||
return ot.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano)
|
||||
}
|
||||
|
||||
// FormatUnixMilli 格式化毫秒时间戳
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:55 2022/7/14
|
||||
func (ot *ownTime) FormatUnixMilli(timestamp int64, layout ...string) string {
|
||||
if len(layout) == 0 {
|
||||
layout = []string{"2006-01-02 15:04:05.000"}
|
||||
}
|
||||
return time.UnixMilli(timestamp).In(time.Local).Format(layout[0])
|
||||
}
|
||||
|
||||
// FormatUnixSec ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:06 2022/7/14
|
||||
func (ot *ownTime) FormatUnixSec(timestamp int64, layout ...string) string {
|
||||
if len(layout) == 0 {
|
||||
layout = []string{"2006-01-02 15:04:05"}
|
||||
}
|
||||
return time.Unix(timestamp, 0).In(time.Local).Format(layout[0])
|
||||
}
|
||||
|
||||
// ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 13:48 2022/7/14
|
||||
func (ot *ownTime) ParseISO8601Time(parseTime string) string {
|
||||
var (
|
||||
timeLayout = "2006-01-02T15:04:05+08:00"
|
||||
formatTime time.Time
|
||||
err error
|
||||
)
|
||||
|
||||
if formatTime, err = time.Parse(timeLayout, parseTime); nil != err {
|
||||
return ""
|
||||
}
|
||||
|
||||
return formatTime.In(time.Local).Format("2006-01-02 15:04:05")
|
||||
}
|
97
url.go
97
url.go
@ -1,97 +0,0 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : util...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-04 2:38 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// URLParseResult url解析
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:51 下午 2021/11/4
|
||||
type URLParseResult struct {
|
||||
Scheme string `json:"scheme"`
|
||||
Domain string `json:"domain"`
|
||||
URI string `json:"uri"`
|
||||
Parameter map[string]string `json:"parameter"`
|
||||
}
|
||||
|
||||
// ownURL ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:38 2022/5/14
|
||||
type ownURL struct {
|
||||
}
|
||||
|
||||
// Encode ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:39 下午 2021/11/4
|
||||
func (ou *ownURL) Encode(inputURL string) string {
|
||||
return url.QueryEscape(inputURL)
|
||||
}
|
||||
|
||||
// Decode ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:39 下午 2021/11/4
|
||||
func (ou *ownURL) Decode(inputURL string) (string, error) {
|
||||
return url.QueryUnescape(inputURL)
|
||||
}
|
||||
|
||||
// Parse url解析
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:39 下午 2021/11/4
|
||||
func (ou *ownURL) Parse(inputURL string) (*URLParseResult, error) {
|
||||
var (
|
||||
parseResult *url.URL
|
||||
err error
|
||||
)
|
||||
if parseResult, err = url.Parse(inputURL); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
detail := &URLParseResult{
|
||||
Scheme: parseResult.Scheme,
|
||||
Domain: parseResult.Host,
|
||||
URI: parseResult.Path,
|
||||
Parameter: make(map[string]string),
|
||||
}
|
||||
for k, v := range parseResult.Query() {
|
||||
if len(v) > 1 {
|
||||
detail.Parameter[k] = "[" + strings.Join(v, ",") + "]"
|
||||
} else {
|
||||
detail.Parameter[k] = v[0]
|
||||
}
|
||||
}
|
||||
return detail, nil
|
||||
}
|
||||
|
||||
// BuildQueryURL 构建GET链接
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:43 下午 2021/11/4
|
||||
func (ou *ownURL) BuildQueryURL(apiURL string, parameter map[string]string) string {
|
||||
u := url.Values{}
|
||||
for k, v := range parameter {
|
||||
u.Set(k, v)
|
||||
}
|
||||
apiURL = strings.Trim(apiURL, "?")
|
||||
if strings.Contains(apiURL, "?") {
|
||||
return apiURL + "&" + u.Encode()
|
||||
}
|
||||
return apiURL + "?" + u.Encode()
|
||||
}
|
176
user_agent.go
176
user_agent.go
@ -1,176 +0,0 @@
|
||||
// Package util ...
|
||||
//
|
||||
// Description : util ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-03-31 18:51
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/mssola/user_agent"
|
||||
)
|
||||
|
||||
var defaultUserAgentList = []string{
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
|
||||
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
|
||||
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
|
||||
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.3 Mobile/14E277 Safari/603.1.30",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1",
|
||||
"(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
|
||||
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11",
|
||||
"(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6",
|
||||
"(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
|
||||
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6",
|
||||
"(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
|
||||
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1",
|
||||
"(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
|
||||
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
|
||||
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
|
||||
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24",
|
||||
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
|
||||
}
|
||||
|
||||
type userAgent struct {
|
||||
list []string
|
||||
}
|
||||
|
||||
// Set 设置ua池
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:55 2023/3/31
|
||||
func (ua *userAgent) Set(agentList []string) {
|
||||
if len(agentList) == 0 {
|
||||
return
|
||||
}
|
||||
ua.list = agentList
|
||||
}
|
||||
|
||||
// GetRandomUA 随机获取UA
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:56 2023/3/31
|
||||
func (ua *userAgent) GetRandomUA() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
idx := rand.Intn(len(ua.list))
|
||||
return ua.list[idx]
|
||||
}
|
||||
|
||||
// Parse 解析UA
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:31 2023/4/20
|
||||
func (ua *userAgent) Parse(inputUA string) *UAInfo {
|
||||
uaInstance := user_agent.New(inputUA)
|
||||
uaInfo := &UAInfo{
|
||||
Mozilla: uaInstance.Mozilla(),
|
||||
Platform: uaInstance.Platform(),
|
||||
OS: uaInstance.OS(),
|
||||
Localization: uaInstance.Localization(),
|
||||
Model: uaInstance.Model(),
|
||||
Browser: &BrowserInfo{
|
||||
Engine: "",
|
||||
EngineVersion: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
},
|
||||
OSInfo: &OSInfo{
|
||||
FullName: uaInstance.OSInfo().FullName,
|
||||
Name: uaInstance.OSInfo().Name,
|
||||
Version: uaInstance.OSInfo().Version,
|
||||
},
|
||||
Bot: false,
|
||||
Mobile: false,
|
||||
Undecided: false,
|
||||
}
|
||||
uaInfo.Browser.Engine, uaInfo.Browser.EngineVersion = uaInstance.Engine()
|
||||
uaInfo.Browser.Name, uaInfo.Browser.Version = uaInstance.Browser()
|
||||
return uaInfo
|
||||
}
|
||||
|
||||
// UAInfo ua解析结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:34 2023/4/20
|
||||
type UAInfo struct {
|
||||
Mozilla string `json:"mozilla"`
|
||||
Platform string `json:"platform"`
|
||||
OS string `json:"os"`
|
||||
OSInfo *OSInfo `json:"os_info"`
|
||||
Localization string `json:"localization"`
|
||||
Model string `json:"model"`
|
||||
Browser *BrowserInfo `json:"browser"`
|
||||
Bot bool `json:"bot"`
|
||||
Mobile bool `json:"mobile"`
|
||||
Undecided bool `json:"undecided"`
|
||||
}
|
||||
|
||||
// BrowserInfo 浏览器信息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:34 2023/4/20
|
||||
type BrowserInfo struct {
|
||||
Engine string `json:"engine"` // 浏览器引擎
|
||||
EngineVersion string `json:"engine_version"` // 浏览器引擎版本
|
||||
Name string `json:"name"` // 浏览器名称
|
||||
Version string `json:"version"` // 浏览器版本
|
||||
}
|
||||
|
||||
// OSInfo 系统信息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:34 2023/4/20
|
||||
type OSInfo struct {
|
||||
FullName string `json:"full_name"` // 操作系统全称
|
||||
Name string `json:"name"` // 操作系统名称
|
||||
Version string `json:"version"` // 操作系统版本
|
||||
}
|
Loading…
Reference in New Issue
Block a user