From d47b08dd418213c9b5f9a45dc96c6038bf2052af Mon Sep 17 00:00:00 2001 From: xxj <346944475@qq.com> Date: Tue, 29 Dec 2020 11:59:27 +0800 Subject: [PATCH] new --- tools/check.go | 4 ++-- tools/convert.go | 21 ++++++++++---------- tools/mybase64.go | 2 ++ tools/reflect.go | 2 +- tools/str.go | 14 +++++++------- tools/timeTools.go | 48 ++++++++++++++++++++++++---------------------- tools/tools.go | 25 ++++++++++++------------ tools/zip.go | 8 ++++---- 8 files changed, 65 insertions(+), 59 deletions(-) diff --git a/tools/check.go b/tools/check.go index b6e873c..b516237 100644 --- a/tools/check.go +++ b/tools/check.go @@ -42,8 +42,8 @@ func IsRunTesting() bool { return false } -// IsIdCard 判断是否是18或15位身份证 -func IsIdCard(cardNo string) bool { +// IsIDCard 判断是否是18或15位身份证 +func IsIDCard(cardNo string) bool { //18位身份证 ^(\d{17})([0-9]|X)$ if m, _ := regexp.MatchString(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)`, cardNo); !m { return false diff --git a/tools/convert.go b/tools/convert.go index b068e22..e0b7c86 100644 --- a/tools/convert.go +++ b/tools/convert.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "encoding/hex" "encoding/json" - "fmt" "regexp" "strconv" "strings" @@ -16,6 +15,7 @@ import ( "github.com/xxjwxc/public/mylog" ) +// RawBytes ... type RawBytes []byte var errNilPtr = errors.New("destination pointer is nil") @@ -23,13 +23,14 @@ var errNilPtr = errors.New("destination pointer is nil") func cloneBytes(b []byte) []byte { if b == nil { return nil - } else { - c := make([]byte, len(b)) - copy(c, b) - return c } + + c := make([]byte, len(b)) + copy(c, b) + return c } +// AsString 转成string func AsString(src interface{}) string { switch v := src.(type) { case string: @@ -56,10 +57,10 @@ func AsString(src interface{}) string { return string(b) } } - return fmt.Sprintf("%v", src) + // return fmt.Sprintf("%v", src) } -//编码二进制 +// EncodeByte 编码二进制 func EncodeByte(data interface{}) ([]byte, error) { buf := bytes.NewBuffer(nil) enc := gob.NewEncoder(buf) @@ -71,14 +72,14 @@ func EncodeByte(data interface{}) ([]byte, error) { return buf.Bytes(), nil } -//解码二进制 +// DecodeByte 解码二进制 func DecodeByte(data []byte, to interface{}) error { buf := bytes.NewBuffer(data) dec := gob.NewDecoder(buf) return dec.Decode(to) } -//byte转16进制字符串 +// ByteToHex byte转16进制字符串 func ByteToHex(data []byte) string { return hex.EncodeToString(data) @@ -96,7 +97,7 @@ func ByteToHex(data []byte) string { // return buffer.String() } -//16进制字符串转[]byte +// HexToBye 16进制字符串转[]byte func HexToBye(hexStr string) []byte { hr, _ := hex.DecodeString(hexStr) return hr diff --git a/tools/mybase64.go b/tools/mybase64.go index bab185f..993a107 100644 --- a/tools/mybase64.go +++ b/tools/mybase64.go @@ -4,10 +4,12 @@ import ( "encoding/base64" ) +// Base64Encode base64 编码 func Base64Encode(src []byte) string { return base64.StdEncoding.EncodeToString(src) } +// Base64Decode base64 解码 func Base64Decode(src []byte) ([]byte, error) { return base64.StdEncoding.DecodeString(string(src)) } diff --git a/tools/reflect.go b/tools/reflect.go index 0811907..f2a2f64 100644 --- a/tools/reflect.go +++ b/tools/reflect.go @@ -5,7 +5,7 @@ import ( "runtime" ) -//获取函数名 +// GetFuncName 获取函数名 func GetFuncName(f interface{}) string { return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() } diff --git a/tools/str.go b/tools/str.go index ffdcd6a..b5deb84 100644 --- a/tools/str.go +++ b/tools/str.go @@ -7,7 +7,7 @@ import ( "github.com/axgle/mahonia" ) -//int数组转字符串 +// FormatInt int数组转字符串 func FormatInt(list []int, seg string) string { s := make([]string, len(list)) for i := range list { @@ -24,7 +24,7 @@ func FormatInt(list []int, seg string) string { // return s //} -//截取字符串 不包括str +// Substr 截取字符串 不包括str func Substr(str string, start, length int) string { rs := []rune(str) rl := len(rs) @@ -54,7 +54,7 @@ func Substr(str string, start, length int) string { return string(rs[start:end]) } -//查找字符串最小值 +// MinimumString 查找字符串最小值 func MinimumString(rest []string) string { minimum := rest[0] for _, v := range rest { @@ -67,7 +67,7 @@ func MinimumString(rest []string) string { return minimum } -//字符集转换 +// ConvertToString 字符集转换 func ConvertToString(src string, srcCode string, tagCode string) string { srcCoder := mahonia.NewDecoder(srcCode) srcResult := srcCoder.ConvertString(src) @@ -76,18 +76,18 @@ func ConvertToString(src string, srcCode string, tagCode string) string { return string(cdata) } -//系统转其他 +// ConvertString 系统转其他 func ConvertString(src string, tagCode string) string { enc := mahonia.NewEncoder(tagCode) return enc.ConvertString(src) } -// +// GetGBK 获取gbk func GetGBK(src string) string { return string(ConvertString(src, "gbK")) } -//反转字符串 +// Reverse 反转字符串 func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { diff --git a/tools/timeTools.go b/tools/timeTools.go index fb8da69..b0591ed 100644 --- a/tools/timeTools.go +++ b/tools/timeTools.go @@ -6,26 +6,26 @@ import ( "time" ) -//获取本地时间戳 +// GetUtcTime 获取本地时间戳 func GetUtcTime(tm time.Time) int64 { return tm.Unix() //- 8*60*60 } -//当前时间向上取整点 +// GetHour 当前时间向上取整点 func GetHour(timestamp int64) int { // formaTime := time.Format("2006-01-02 15:04:05") tm := time.Unix(timestamp, 0) return tm.Hour() } -//获取offset天的现在时间:注意时区 +// GetLastDayCurrentTime 获取offset天的现在时间:注意时区 func GetLastDayCurrentTime(timestamp int64, offset int) time.Time { tm := time.Unix(timestamp, 0) yesDay := tm.AddDate(0, 0, 1*offset) return yesDay } -//获取给定时间的星期 +// GetTimeWeek 获取给定时间的星期 func GetTimeWeek(timestamp int64) int { tm := time.Unix(timestamp, 0) weekDay := tm.Weekday().String() @@ -49,14 +49,14 @@ func GetTimeWeek(timestamp int64) int { return week } -//获取向上整时时间 +// GetHour0 获取向上整时时间 func GetHour0(timestamp int64) time.Time { tm := time.Unix(timestamp, 0) tStr := tm.Format("2006-01-02 15") + ":00:00" return StrToTime(tStr, "2006-01-02 15:04:05", nil) } -//获取给定日期的零点时间 +// GetDay0 获取给定日期的零点时间 func GetDay0(timestamp int64) time.Time { tm := time.Unix(timestamp, 0) tStr := tm.Format("2006-01-02") + " 00:00:00" @@ -64,13 +64,13 @@ func GetDay0(timestamp int64) time.Time { return StrToTime(tStr, "2006-01-02 15:04:05", nil) } -//获取offset 0点时间 +// GetUtcDay0 获取offset 0点时间 func GetUtcDay0(now time.Time, timeZone *time.Location) int64 { tm := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) return tm.Unix() } -//字符串转时间 +// StrToTime 字符串转时间 func StrToTime(tStr, format string, timeZone *time.Location) time.Time { if len(format) == 0 { format = "2006-01-02 15:04:05" @@ -84,21 +84,19 @@ func StrToTime(tStr, format string, timeZone *time.Location) time.Time { return ti } -/* - 给定字符串时间转换成本地时间戳 -*/ +// StringTimetoUnix 给定字符串时间转换成本地时间戳 func StringTimetoUnix(timestr string) int64 { return StrToTime(timestr, "2006-01-02 15:04:05", time.Local).Unix() } -//获取最近上个星期天的零点日期 +// GetWeek0 获取最近上个星期天的零点日期 func GetWeek0(timestamp int64) time.Time { weekday := GetTimeWeek(timestamp) tm0 := GetDay0(timestamp) return tm0.AddDate(0, 0, -1*weekday) } -//获取最近上个星期天的零点日期 +// GetUtcWeek0 获取最近上个星期天的零点日期 func GetUtcWeek0(timestamp int64) int64 { weekday := GetTimeWeek(timestamp) tm0 := GetDay0(timestamp) @@ -107,9 +105,7 @@ func GetUtcWeek0(timestamp int64) int64 { return tm0.Unix() } -/* - 获取给定时间的当月1号零点时间 -*/ +// GetMonth0 获取给定时间的当月1号零点时间 func GetMonth0(timestamp int64) time.Time { tm0 := GetDay0(timestamp) @@ -118,7 +114,7 @@ func GetMonth0(timestamp int64) time.Time { return tm0 } -//整点执行操作 +// TimerByHour 整点执行操作 func TimerByHour(f func()) { for { now := time.Now() @@ -132,42 +128,46 @@ func TimerByHour(f func()) { } } -//时间戳转换为time +// UnixToTime 时间戳转换为time func UnixToTime(timestamp int64) time.Time { return time.Unix(timestamp, 0) } -//获取本地时间 +// GetLocalTime 获取本地时间 func GetLocalTime(tm time.Time) time.Time { local, _ := time.LoadLocation("Local") return tm.In(local) //return tm.Add(8 * 60 * 60 * time.Second) } -//获取系统时间的格式 +// GetSysTimeLayout 获取系统时间的格式 func GetSysTimeLayout() string { t := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC) strLayout := strings.Replace(t.String(), "+0000 UTC", "", -1) return strings.TrimSpace(strLayout) } -func FormatTime(tm time.Time, for_str string) string { - return tm.Format(for_str) +// FormatTime 格式化时间 +func FormatTime(tm time.Time, forStr string) string { + return tm.Format(forStr) } +// GetTimeStr 获取时间字符串 func GetTimeStr(tm time.Time) string { return FormatTime(tm, "2006-01-02 15:04:05") } +// GetDayStr 获取日期字符串 func GetDayStr(tm time.Time) string { return FormatTime(tm, "2006-01-02") } -//json marsh 重写 +// Time json marsh 重写 type Time struct { time.Time } +// UnmarshalJSON ... func (t *Time) UnmarshalJSON(data []byte) (err error) { tmp := string(data) str := `"2006-01-02 15:04:05"` @@ -183,11 +183,13 @@ func (t *Time) UnmarshalJSON(data []byte) (err error) { return } +// MarshalJSON ... func (t Time) MarshalJSON() ([]byte, error) { var stamp = fmt.Sprintf(`"%s"`, t.Format("2006-01-02 15:04:05")) return []byte(stamp), nil } +// String ... func (t Time) String() string { return t.Format("2006-01-02 15:04:05") } diff --git a/tools/tools.go b/tools/tools.go index 3c3ee4b..7b3354e 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -15,6 +15,7 @@ import ( "github.com/xxjwxc/public/errors" ) +// Md5Encoder md5 加密 func Md5Encoder(src string) string { h := md5.New() h.Write([]byte(src)) // 需要加密的字符串 @@ -23,7 +24,7 @@ func Md5Encoder(src string) string { return strings.ToUpper(ret) } -//合并数组 +// Copy 合并数组 func Copy(dest []interface{}, src []interface{}) (result []interface{}) { result = make([]interface{}, len(dest)+len(src)) copy(result, dest) @@ -31,26 +32,26 @@ func Copy(dest []interface{}, src []interface{}) (result []interface{}) { return } -//删除数组 +// DeleteArray 删除数组 func DeleteArray(src []interface{}, index int) (result []interface{}) { result = append(src[:index], src[(index+1):]...) return } -//生成32位md5字串 +// GetMd5String 生成32位md5字串 func GetMd5String(s string) string { h := md5.New() h.Write([]byte(s)) return hex.EncodeToString(h.Sum(nil)) } -//获取总页数 +// GetTotalPageNum 获取总页数 func GetTotalPageNum(pageSize, totalCount int) int { return (totalCount + pageSize - 1) / pageSize } -//生成32位guid -func UniqueId() string { +// UniqueID 生成32位guid +func UniqueID() string { b := make([]byte, 48) if _, err := io.ReadFull(crand.Reader, b); err != nil { return "" @@ -67,12 +68,12 @@ func DeleteSlice(slice interface{}, index int) (interface{}, error) { } if length-1 == index { return sliceValue.Slice(0, index).Interface(), nil - } else { - return reflect.AppendSlice(sliceValue.Slice(0, index), sliceValue.Slice(index+1, length)).Interface(), nil } + + return reflect.AppendSlice(sliceValue.Slice(0, index), sliceValue.Slice(index+1, length)).Interface(), nil } -//查找int最小值 +// MinimumInt 查找int最小值 func MinimumInt(rest []int) int { minimum := rest[0] for _, v := range rest { @@ -113,7 +114,7 @@ func MinimumInt(rest []int) int { // } // } -//按字典顺序排序 +// DictSort 按字典顺序排序 func DictSort(res []string) (str string) { sort.Strings(res) if len(res) > 0 { @@ -124,7 +125,7 @@ func DictSort(res []string) (str string) { return } -//SHA1加密 +// Sha1Encrypt SHA1加密 func Sha1Encrypt(str string) string { h := sha1.New() h.Write([]byte(str)) @@ -132,7 +133,7 @@ func Sha1Encrypt(str string) string { return fmt.Sprintf("%x", bs) } -//中文字符切割时有问题。采用此方式不会有问题 +// GetUtf8Str 中文字符切割时有问题。采用此方式不会有问题 func GetUtf8Str(str string) []rune { return []rune(str) } diff --git a/tools/zip.go b/tools/zip.go index 6ee9902..331e40d 100644 --- a/tools/zip.go +++ b/tools/zip.go @@ -8,6 +8,7 @@ import ( "path/filepath" ) +// IsZip 是否zip文件 func IsZip(zipPath string) bool { f, err := os.Open(zipPath) if err != nil { @@ -23,6 +24,7 @@ func IsZip(zipPath string) bool { return bytes.Equal(buf, []byte("PK\x03\x04")) } +// Unzip 解压 func Unzip(archive, target string) error { reader, err := zip.OpenReader(archive) if err != nil { @@ -73,7 +75,7 @@ func Unzip(archive, target string) error { return nil } -//压缩文件 +// Compress 压缩文件 //files 文件数组,可以是不同dir下的文件或者文件夹 //dest 压缩文件存放地址 func Compress(files []*os.File, dest string) error { @@ -117,9 +119,7 @@ func compress(file *os.File, prefix string, zw *zip.Writer) error { } } else { header, err := zip.FileInfoHeader(info) - if len(prefix) == 0 { - header.Name = header.Name - } else { + if len(prefix) != 0 { header.Name = prefix + "/" + header.Name } if err != nil {