2021-03-09 18:02:43 +08:00
|
|
|
// Package util...
|
|
|
|
//
|
|
|
|
// Description : 字符串相关的工具
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-09 18:02:43 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-09 6:00 下午
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
2021-10-25 18:06:26 +08:00
|
|
|
"strings"
|
2021-03-09 18:02:43 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenRandomString 获取随机长度的字符串
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-09 18:02:43 +08:00
|
|
|
//
|
|
|
|
// Date : 6:01 下午 2021/3/9
|
|
|
|
func GenRandomString(source string, length uint) string {
|
|
|
|
if length == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(source) == 0 {
|
2021-08-14 23:51:41 +08:00
|
|
|
//字符串为空,默认字符源为如下(去除易混淆的i/l):
|
|
|
|
source = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
|
2021-03-09 18:02:43 +08:00
|
|
|
}
|
|
|
|
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加密
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-09 18:02:43 +08:00
|
|
|
//
|
|
|
|
// Date : 6:01 下午 2021/3/9
|
|
|
|
func Md5(str string) string {
|
|
|
|
h := md5.New()
|
2021-04-01 20:17:30 +08:00
|
|
|
_, _ = h.Write([]byte(str))
|
2021-03-09 18:02:43 +08:00
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
2021-10-25 18:06:26 +08:00
|
|
|
|
|
|
|
// SnakeCaseToCamel 蛇形字符串转换为驼峰
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:58 下午 2021/10/25
|
|
|
|
func SnakeCaseToCamel(str string) string {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.WriteByte(str[i])
|
|
|
|
}
|
|
|
|
return builder.String()
|
|
|
|
}
|