Compare commits
80 Commits
cfe1f4c04d
...
master
Author | SHA1 | Date | |
---|---|---|---|
6ee2c90464 | |||
7eb5cdbf92 | |||
dceb0bf682 | |||
445c6407db | |||
a61f85b4e7 | |||
b34984be74 | |||
d6a489d586 | |||
2e36e7b1ac | |||
5984d6194c | |||
ccb4045065 | |||
ddd2413386 | |||
024a0e5e96 | |||
4ba9ec7c01 | |||
8425926115 | |||
17efb38b18 | |||
f83b4208ad | |||
c318ed9f36 | |||
773ac512f3 | |||
e92246d281 | |||
d9fac952a8 | |||
223d5ab374 | |||
1d8e8d546b | |||
090ab99af2 | |||
4a2cf51a9c | |||
7ed8ccbd87 | |||
0c25220b9a | |||
a0e8f3b7c2 | |||
96532aff00 | |||
6741dcf033 | |||
0c884c1f65 | |||
8dbec65861 | |||
3e27982702 | |||
0054da0620 | |||
8d9c4231c3 | |||
0a75d06083 | |||
5cfbbbb832 | |||
82a530d779 | |||
f5fddb92e8 | |||
812faa2e21 | |||
32f50cce5b | |||
33ce7169d0 | |||
5114db00c6 | |||
b5add69124 | |||
662152cb68 | |||
8aee2fb907 | |||
faba0a5a9e | |||
6cee12a850 | |||
6710e345cc | |||
2560949681 | |||
1a6a3e1d31 | |||
661eed5f0a | |||
cccfc05ea7 | |||
fcdda09503 | |||
c886ff6b20 | |||
b4b3e261e0 | |||
8f8476eed0 | |||
58140add3d | |||
8d464bb896 | |||
67e1cd1934 | |||
3529f2b113 | |||
e68b22a6e1 | |||
1fcde5ff76 | |||
9931590d80 | |||
63f5b99d40 | |||
f5086c77e0 | |||
1c9c8be53c | |||
e95f609c0f | |||
29b0f171ff | |||
3cfcad0c65 | |||
ad77044f1e | |||
24edcae2b3 | |||
2936ec4ba5 | |||
ec9271c0f3 | |||
d52af7e3f4 | |||
a94d99cd14 | |||
267a813e66 | |||
b5ae4cf809 | |||
fc651441ab | |||
cbac0b676f | |||
9d7c4713d0 |
32
calculate.go
Normal file
32
calculate.go
Normal 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
4
cli.go
@ -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
103
console.go
Normal 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
23
console_test.go
Normal 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
26
define/file.go
Normal 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
114
file.go
@ -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
17
go.mod
@ -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
25
go.sum
@ -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
36
init.go
@ -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
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()
|
||||
}
|
99
json.go
99
json.go
@ -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
216
map.go
@ -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
102
project.go
Normal 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
27
project_test.go
Normal 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())
|
||||
}
|
90
string.go
90
string.go
@ -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()
|
||||
}
|
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
|
||||
}
|
27
time.go
27
time.go
@ -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
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()
|
||||
}
|
208
util.go
Normal file
208
util.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user