Compare commits

...

80 Commits

Author SHA1 Message Date
6ee2c90464 update cli 2024-06-18 12:24:05 +08:00
7eb5cdbf92 remove code 2023-12-27 17:53:34 +08:00
dceb0bf682 增加版本号比较方法 2023-12-27 17:46:04 +08:00
445c6407db update go mod 2023-10-14 22:28:40 +08:00
a61f85b4e7 页码计算适配每页数量为0 2023-10-14 22:27:50 +08:00
b34984be74 update go mod 2023-08-15 12:25:59 +08:00
d6a489d586 remove code 2023-08-11 15:04:56 +08:00
2e36e7b1ac remove code 2023-08-11 13:50:14 +08:00
5984d6194c code clean 2023-08-11 10:55:51 +08:00
ccb4045065 update go mod 2023-08-10 17:33:04 +08:00
ddd2413386 remove jwt 2023-08-10 17:32:31 +08:00
024a0e5e96 r移除struct 2023-08-10 16:59:48 +08:00
4ba9ec7c01 Map相关操作转到wrapper.Map 2023-08-10 15:51:56 +08:00
8425926115 remove network code 2023-08-10 14:39:45 +08:00
17efb38b18 remove string conver 2023-08-10 14:21:10 +08:00
f83b4208ad remove Time1 2023-08-10 14:18:47 +08:00
c318ed9f36 update 2023-08-10 11:57:09 +08:00
773ac512f3 update go mod 2023-08-01 17:23:44 +08:00
e92246d281 增加toml文件读取 2023-07-31 21:10:05 +08:00
d9fac952a8 文件格式大小写脱敏 2023-07-31 15:46:59 +08:00
223d5ab374 支持读取目录文件列表 2023-07-31 15:39:02 +08:00
1d8e8d546b update go mod 2023-07-30 19:24:09 +08:00
090ab99af2 更新jwt-go 2023-07-30 19:23:26 +08:00
4a2cf51a9c 增加JSON操作以及map操作 2023-07-22 21:59:15 +08:00
7ed8ccbd87 update 2023-07-01 23:30:50 +08:00
0c25220b9a add letter list 2023-07-01 22:53:14 +08:00
a0e8f3b7c2 增加拼音处理 2023-06-25 18:05:43 +08:00
96532aff00 第一版string2other 2023-05-05 10:59:24 +08:00
6741dcf033 基础数据类型转换 2023-05-05 10:48:55 +08:00
0c884c1f65 字符串转int相关的slice 2023-05-04 19:06:10 +08:00
8dbec65861 规划string转任意类型 2023-05-04 18:40:25 +08:00
3e27982702 update 2023-04-20 16:58:12 +08:00
0054da0620 增加ua信息获取 2023-04-20 16:36:09 +08:00
8d9c4231c3 增加获取家目录方法 2023-04-18 22:32:57 +08:00
0a75d06083 增加使用系统默认浏览器打开连接方法 2023-04-09 19:06:07 +08:00
5cfbbbb832 升级go版本go1.17 -> go1.20 2023-04-09 02:37:42 +08:00
82a530d779 时间工具支持传入layout 2023-04-08 14:48:53 +08:00
f5fddb92e8 时间工具支持传入layout 2023-04-08 14:38:13 +08:00
812faa2e21 增加常见的中文字符替换成英文 2023-04-04 17:01:46 +08:00
32f50cce5b 优化JSON输出方式 2023-04-04 16:37:03 +08:00
33ce7169d0 格式化控制台输出 2023-04-04 16:30:40 +08:00
5114db00c6 增加控制台数据输出控制 2023-04-04 16:26:02 +08:00
b5add69124 增加获取随机UA方法 2023-03-31 18:58:14 +08:00
662152cb68 增加in函数的简化写法 2023-03-30 16:26:19 +08:00
8aee2fb907 in_array优化类型检测 2023-03-30 16:22:56 +08:00
faba0a5a9e fix both modify 2023-03-30 14:50:32 +08:00
6cee12a850 增加in array能力 2023-03-30 14:49:38 +08:00
6710e345cc 时间格式化增加时区 2023-03-19 14:13:11 +08:00
2560949681 优化jwt token解析方法 2023-02-12 00:42:27 +08:00
1a6a3e1d31 优化jwt token解析方法 2023-02-12 00:36:24 +08:00
661eed5f0a 修复秒级时间戳转格式化时间错误问题 2023-02-12 00:06:39 +08:00
cccfc05ea7 修复 jwt secret 类型错误问题 2023-02-12 00:00:50 +08:00
fcdda09503 增加jwt能力 2023-02-11 21:14:57 +08:00
c886ff6b20 增加解析文件类型的方法 2023-02-07 17:29:29 +08:00
b4b3e261e0 update 2023-01-13 17:59:43 +08:00
8f8476eed0 fix 2023-01-13 17:16:24 +08:00
58140add3d fix 2023-01-13 17:07:41 +08:00
8d464bb896 fix 2023-01-13 16:59:51 +08:00
67e1cd1934 增加字符串去空格方法 2023-01-13 16:56:31 +08:00
3529f2b113 增加获取项目目录方法 2023-01-13 14:41:13 +08:00
e68b22a6e1 fix both modify 2022-10-21 14:14:34 +08:00
1fcde5ff76 持己数组接计算签名 2022-10-21 14:13:29 +08:00
9931590d80 增加构建wenjian路径方法 2022-10-14 13:46:33 +08:00
63f5b99d40 增加获取当前文件系统路径分隔符的方法 2022-10-14 12:53:07 +08:00
f5086c77e0 增加获取可执行文件绝对路径的方法 2022-10-14 12:50:03 +08:00
1c9c8be53c 增加map数组转换为url query方法 2022-10-10 20:52:06 +08:00
e95f609c0f 字符串列表去重能力 2022-07-23 21:15:33 +08:00
29b0f171ff 增加计算相关 2022-07-17 15:51:53 +08:00
3cfcad0c65 增加各种规则的时间格式化 2022-07-14 13:49:40 +08:00
ad77044f1e Merge branch 'master' of git.zhangdeman.cn:zhangdeman/util 2022-07-12 20:12:06 +08:00
24edcae2b3 支持生成随机md5 2022-07-12 20:12:04 +08:00
2936ec4ba5 增加字符串编码转换 2022-07-09 14:42:31 +08:00
ec9271c0f3 增加简单的数据映射能力 2022-07-04 13:37:16 +08:00
d52af7e3f4 增加获取默认值方法 2022-07-04 12:30:53 +08:00
a94d99cd14 增加数据映射规则的定义 2022-07-04 12:26:02 +08:00
267a813e66 规划过滤方法 2022-07-04 11:58:34 +08:00
b5ae4cf809 支持解析ini文件 2022-07-02 13:24:29 +08:00
fc651441ab 支持任意格式的文件解析 2022-07-02 13:18:40 +08:00
cbac0b676f 升级 yaml.v2 -> yaml.v3 2022-06-26 16:11:30 +08:00
9d7c4713d0 update time 2022-06-26 16:09:15 +08:00
19 changed files with 570 additions and 773 deletions

32
calculate.go Normal file
View File

@ -0,0 +1,32 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-17 15:49
package util
// calculate 各种计算相关
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:49 2022/7/17
type calculate struct {
}
// GetTotalPage 获取总页码数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:50 2022/7/17
func (c *calculate) GetTotalPage(total int64, pageSize int64) int64 {
if pageSize <= 0 {
return 1
}
totalPage := total / pageSize
if total%pageSize > 0 {
totalPage++
}
return totalPage
}

4
cli.go
View File

@ -23,7 +23,9 @@ type cli struct {
func (c *cli) ParseCLIParameter(parameterNameList []string) map[string]string {
cliParameterTable := make(map[string]*string)
for _, parameterName := range parameterNameList {
cliParameterTable[parameterName] = flag.String(parameterName, "", parameterName)
var val string
flag.StringVar(&val, parameterName, "", parameterName)
cliParameterTable[parameterName] = &val
}
// 这里有一个非常中的操作,转换,必须调用该方法
flag.Parse()

103
console.go Normal file
View File

@ -0,0 +1,103 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-04-04 16:09
package util
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
)
// console 终端数据输出的一些操作
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:10 2023/4/4
type console struct {
enable bool
formatJson bool
}
// Enable 启用
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:10 2023/4/4
func (c *console) Enable() {
c.enable = true
}
// Disable 禁用
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:11 2023/4/4
func (c *console) Disable() {
c.enable = false
}
// FormatJson 是否格式化json
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:35 2023/4/4
func (c *console) FormatJson(format bool) {
c.formatJson = format
}
// Output 禁用终端输出
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:13 2023/4/4
func (c *console) Output(dataList ...interface{}) {
if !c.enable {
return
}
for _, item := range dataList {
fmt.Println(c.getDataStr(item))
}
}
// getDataStr 数据转换为字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:14 2023/4/4
func (c *console) getDataStr(data interface{}) string {
if nil == data {
return "nil"
}
dataValue := reflect.ValueOf(data)
dataType := reflect.TypeOf(data).Kind()
if dataType == reflect.Ptr {
dataValue = dataValue.Elem()
}
switch dataValue.Kind() {
case reflect.Map:
fallthrough
case reflect.Slice:
fallthrough
case reflect.Array:
fallthrough
case reflect.Struct:
byteData, _ := json.Marshal(data)
if !c.formatJson {
return string(byteData)
}
var out bytes.Buffer
_ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t")
return string(out.Bytes())
case reflect.Func:
return dataValue.String()
default:
return fmt.Sprintf("%v", data)
}
}

23
console_test.go Normal file
View File

@ -0,0 +1,23 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-04-04 16:21
package util
import "testing"
func Test_console_Output(t *testing.T) {
Console.Enable()
var a = map[string]interface{}{
"name": "baicha",
}
type U struct {
Name string
}
b := &U{Name: "qinghuan"}
c := U{Name: "test"}
Console.Output(a, b, c)
}

26
define/file.go Normal file
View File

@ -0,0 +1,26 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-07-31 14:52
package define
import "io/fs"
// FileInfo 文件信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:53 2023/7/31
type FileInfo struct {
IsDir bool `json:"is_dir"` // 是否目录
Name string `json:"name"` // 文件名
AbsolutePath string `json:"absolute_path"` // 绝对路径
Format string `json:"format"` // 文件格式
Size int64 `json:"size"` // 文件大小
ModifyTime int64 `json:"modify_time"` // 修改时间, 秒级时间戳
Mode fs.FileMode `json:"mode"` // 文件权限
FileList []*FileInfo `json:"file_list"` // 递归读取, 且当前文件为目录
}

114
file.go
View File

@ -1,114 +0,0 @@
// Package util...
//
// Description : 文件相关工具
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-04-26 6:00 下午
package util
import (
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
yml "gopkg.in/yaml.v2"
)
// 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)
}
// 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()
}

17
go.mod
View File

@ -1,9 +1,20 @@
module git.zhangdeman.cn/zhangdeman/util
go 1.17
go 1.21
require (
github.com/pkg/errors v0.9.1
github.com/mitchellh/go-homedir v1.1.0
github.com/mozillazg/go-pinyin v0.20.0
github.com/spaolacci/murmur3 v1.1.0
gopkg.in/yaml.v2 v2.4.0
)
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/go-ini/ini v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

25
go.sum
View File

@ -1,8 +1,23 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b h1:C7KftnLh7dOqzNRs5dn/9yqMDvuqMn5RCglvV6bY758=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 h1:nvVSH+Ju8EmoPiPHTae5lxHo4kDjROYChs19Yayz+NY=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d h1:TV0BCQQewBEtLsv3i9gXkxLFd5A5bWBTiNd3D/I5o4Q=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 h1:ulssHpUMNoxws/fdJD+bhw8tuMPXp+jB54WwLdJNTsU=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000/go.mod h1:rt9wlUf/Y6lpZBXb0KK8n0JxEbI1J5JB73XgQmGHJqw=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
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/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/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

36
init.go
View File

@ -10,35 +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
// Console 控制台输出数据
Console *console
// 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{}
Console = &console{}
PinYin = &pinYin{}
}

59
ip.go
View File

@ -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()
}

99
json.go
View File

@ -1,99 +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)
}
// 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("")
}
}

216
map.go
View File

@ -1,216 +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])
}

102
project.go Normal file
View File

@ -0,0 +1,102 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-10-14 12:39
package util
import (
"os"
"path/filepath"
"runtime"
"strings"
"github.com/mitchellh/go-homedir"
)
type project struct {
}
// GetExecutablePath 获取可执行文件的路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 2022/10/14
func (p *project) GetExecutablePath() (string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
if err != nil {
return "", err
}
return dir, nil
}
// GetFileSystemSeparator 获取当前文件系统的路径分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:51 2022/10/14
func (p *project) GetFileSystemSeparator() string {
return string(filepath.Separator)
}
// BuildPath 构建路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:57 2022/10/14
func (p *project) BuildPath(filePathList ...string) string {
projectRootPath, _ := p.GetExecutablePath()
if len(filePathList) == 0 {
// 降为获取项目根目录
return projectRootPath
}
// 第一个特殊处理
if filePathList[0] == "." || len(filePathList[0]) == 0 {
filePathList[0] = projectRootPath
} else {
if strings.ToLower(runtime.GOOS) == "windows" {
// windows
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
// 相对路径
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.Contains(filePathList[0], ":") {
// 不包含 :, 是 相对路径,拼成绝对路径
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + strings.TrimLeft(filePathList[0], p.GetFileSystemSeparator())
}
} else {
// unix/ linux
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + filePathList[0]
}
}
}
filePathList[0] = strings.TrimRight(filePathList[0], p.GetFileSystemSeparator())
for idx := 1; idx < len(filePathList); idx++ {
filePathList[idx] = strings.Trim(filePathList[idx], p.GetFileSystemSeparator())
}
return strings.Join(filePathList, p.GetFileSystemSeparator())
}
// GetProjectPath 获取项目目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:39 2023/1/13
func (p *project) GetProjectPath() string {
dir, _ := os.Getwd()
return dir
}
// GetHomeDir 获取家目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:31 2023/4/18
func (p *project) GetHomeDir() string {
homePath, _ := homedir.Dir()
return homePath
}

27
project_test.go Normal file
View File

@ -0,0 +1,27 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-10-14 12:47
package util
import (
"fmt"
"testing"
)
func Test_project_GetExecutablePath(t *testing.T) {
fmt.Println(Project.GetExecutablePath())
}
func Test_project_BuildPath(t *testing.T) {
fmt.Println(Project.GetExecutablePath())
fmt.Println(Project.BuildPath("a", "b", "c"))
fmt.Println(Project.BuildPath("\\a\\", "\\b\\", "\\c\\"))
}
func Test_project_GetGetProjectPath(t *testing.T) {
fmt.Println(Project.GetProjectPath())
}

View File

@ -1,90 +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"
)
// 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))
}
// 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()
}

View File

@ -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
}

27
time.go
View File

@ -1,27 +0,0 @@
// Package util ...
//
// Description : 时间相关操作
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-07 1:33 上午
package util
import "time"
// ownTime ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:24 2022/5/14
type ownTime struct {
}
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:34 上午 2021/10/7
func GetCurrentFormatTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}

97
url.go
View File

@ -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()
}

208
util.go Normal file
View File

@ -0,0 +1,208 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-25 17:22
package util
import "github.com/mozillazg/go-pinyin"
// PinYinArg 设置选项方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:33 2023/6/25
type PinYinArg func(arg *pinyin.Args)
// 拼音风格的设置
// PinYinStyleNormal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:37 2023/6/25
func PinYinStyleNormal() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Normal
}
}
// PinYinStyleToneStand 声调风格1拼音声调在韵母第一个字母上。如 zhōng guó
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:38 2023/6/25
func PinYinStyleToneStand() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone
}
}
// PinYinStyleToneAfterYunMu 声调风格2即拼音声调在各个韵母之后用数字 [1-4] 进行表示。如: zho1ng guo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:39 2023/6/25
func PinYinStyleToneAfterYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone2
}
}
// PinYinStyleToneAtEnding 声调风格3即拼音声调在各个拼音之后用数字 [1-4] 进行表示。如: zhong1 guo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:40 2023/6/25
func PinYinStyleToneAtEnding() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Tone3
}
}
// PinYinStyleShengMu 声母风格,只返回各个拼音的声母部分。如: zh g 。注意:不是所有的拼音都有声母
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:41 2023/6/25
func PinYinStyleShengMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Initials
}
}
// PinYinStyleFirstLetter 首字母风格,只返回拼音的首字母部分。如: z g
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2023/6/25
func PinYinStyleFirstLetter() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FirstLetter
}
}
// PinYinStyleYunMu 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:43 2023/6/25
func PinYinStyleYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.Finals
}
}
// PinYinStyleToneYunMu 韵母风格1带声调声调在韵母第一个字母上。如 ōng uó
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2023/6/25
func PinYinStyleToneYunMu() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone
}
}
// PinYinStyleToneYunMuAtStart 韵母风格2带声调声调在各个韵母之后用数字 [1-4] 进行表示。如: o1ng uo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:45 2023/6/25
func PinYinStyleToneYunMuAtStart() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone2
}
}
// PinYinStyleToneYunMuAtEnding 韵母风格3带声调声调在各个拼音之后用数字 [1-4] 进行表示。如: ong1 uo2
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:40 2023/6/25
func PinYinStyleToneYunMuAtEnding() PinYinArg {
return func(arg *pinyin.Args) {
arg.Style = pinyin.FinalsTone3
}
}
// 拼音风格设置结束
// PinYinStyleWithSeparator 设置拼音的分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:59 2023/6/25
func PinYinStyleWithSeparator(separator string) PinYinArg {
return func(arg *pinyin.Args) {
if len(separator) == 0 {
return
}
arg.Separator = separator
}
}
// PinYinStyleWithHeteronym 开启多音字模式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:03 2023/6/25
func PinYinStyleWithHeteronym() PinYinArg {
return func(arg *pinyin.Args) {
arg.Heteronym = true
}
}
// PinYin 汉字转拼音
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:23 2023/6/25
type pinYin struct {
}
// Convert 汉字生成拼音
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:26 2023/6/25
func (py *pinYin) Convert(text string, argFuncList ...PinYinArg) string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.Slug(text, arg)
}
// ConvertSingle 每个字一个读音, 支持多音字
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/6/25
func (py *pinYin) ConvertSingle(text string, argFuncList ...PinYinArg) []string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.LazyPinyin(text, arg)
}
// ConvertWithHeteronym 多音字模式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/6/25
func (py *pinYin) ConvertWithHeteronym(text string, argFuncList ...PinYinArg) [][]string {
arg := pinyin.NewArgs()
arg.Separator = " "
for _, argFunc := range argFuncList {
argFunc(&arg)
}
return pinyin.Pinyin(text, arg)
}