fix both modify

This commit is contained in:
白茶清欢 2022-10-21 14:14:34 +08:00
commit e68b22a6e1
6 changed files with 233 additions and 1 deletions

29
calculate.go Normal file
View File

@ -0,0 +1,29 @@
// 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 {
totalPage := total / pageSize
if total%pageSize > 0 {
totalPage++
}
return totalPage
}

View File

@ -28,6 +28,10 @@ var (
URL *ownURL
// Map ...
Map *ownMap
// Calculate ...
Calculate *calculate
// Project ...
Project = &project{}
)
func init() {
@ -41,4 +45,5 @@ func init() {
Time = &ownTime{}
URL = &ownURL{}
Map = &ownMap{}
Calculate = &calculate{}
}

80
project.go Normal file
View File

@ -0,0 +1,80 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-10-14 12:39
package util
import (
"os"
"path/filepath"
"runtime"
"strings"
)
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())
}

23
project_test.go Normal file
View File

@ -0,0 +1,23 @@
// 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\\"))
}

View File

@ -69,6 +69,15 @@ func (s *stringOperate) Md5FromByte(data []byte) string {
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<白茶清欢>
@ -114,3 +123,37 @@ func (s *stringOperate) Convert(str string, sourceCode string, targetCode string
_, 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, "&")
}

54
time.go
View File

@ -7,7 +7,10 @@
// Date : 2021-10-07 1:33 上午
package util
import "time"
import (
"fmt"
"time"
)
// ownTime ...
//
@ -25,3 +28,52 @@ type ownTime struct {
func (ot *ownTime) GetCurrentFormatTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// FormatUnixNano 格式化纳秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/14
func (ot *ownTime) FormatUnixNano(timestamp int64) string {
nano := timestamp % 1e6
milli := timestamp / 1e6
return ot.FormatUnixMilli(milli) + fmt.Sprintf(" %v", nano)
}
// FormatUnixMilli 格式化毫秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:55 2022/7/14
func (ot *ownTime) FormatUnixMilli(timestamp int64) string {
time.Now().UnixMilli()
return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05.000")
}
// FormatUnixSec ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2022/7/14
func (ot *ownTime) FormatUnixSec(timestamp int64) string {
return time.UnixMilli(timestamp).Format("2006-01-02 15:04:05")
}
// 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.Format("2006-01-02 15:04:05")
}