goProject/trunk/goutil/stringUtil/guid.go
皮蛋13361098506 1b77f62820 初始化项目
2025-01-06 16:01:02 +08:00

61 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package stringUtil
import (
"crypto/rand"
"encoding/base64"
"io"
"strings"
"goutil/securityUtil"
)
// 获取新的GUID字符串
// 返回值:
// 新的GUID字符串
func GetNewGUID() string {
b := make([]byte, 48)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return ""
}
return securityUtil.Md5String(base64.URLEncoding.EncodeToString(b), true)
}
// 生成空的GUID字符串
// 返回值:
// 空的GUID字符串
func GetEmptyGUID() string {
return "00000000-0000-0000-0000-000000000000"
}
// 判断GUID是否为空
// guidGUID
// 返回值:
// 是否为空
func IsGUIDEmpty(guid string) bool {
if guid == "" || guid == "00000000-0000-0000-0000-000000000000" {
return true
}
return false
}
// 获取新的GUID字符串
// 返回值:
// 新的GUID字符串
func GetNewUUID() string {
str := GetNewGUID()
var builder strings.Builder
builder.WriteString(Substring(str, 0, 8))
builder.WriteString("-")
builder.WriteString(Substring(str, 8, 4))
builder.WriteString("-")
builder.WriteString(Substring(str, 12, 4))
builder.WriteString("-")
builder.WriteString(Substring(str, 16, 4))
builder.WriteString("-")
builder.WriteString(Substring(str, 20, 12))
return strings.ToLower(builder.String())
}