1 Star 0 Fork 0

xiedongji/antgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
xgen.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
xiedongji 提交于 2023-03-17 21:02 . 框架
package xgen
import (
"crypto/rand"
"github.com/lithammer/shortuuid/v3"
uuid "github.com/satori/go.uuid"
"math/big"
"strings"
)
func Uuid() string {
return uuid.NewV4().String()
}
func UuidShort() string {
return shortuuid.New()
}
func UuidNoSeparator() string {
return strings.Replace(Uuid(), "-", "", -1)
}
func RandInt(min, max int64) int64 {
// calculate the max we will be using
bg := big.NewInt(max - min)
// get big.Int between 0 and bg
// in this case 0 to 20
n, err := rand.Int(rand.Reader, bg)
if err != nil {
panic(err)
}
// add n to min to support the passed in range
return n.Int64() + min
}
const (
LettersLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
LettersUpperCaseLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LettersNumber = "0123456789"
LettersNumberNoZero = "23456789"
LettersSymbol = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/"
)
func RandString(n int, letters ...string) (string, error) {
lettersDefaultValue := LettersLetter + LettersNumber + LettersSymbol
if len(letters) > 0 {
lettersDefaultValue = ""
for _, letter := range letters {
lettersDefaultValue = lettersDefaultValue + letter
}
}
bytes := make([]byte, n)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
for i, b := range bytes {
bytes[i] = lettersDefaultValue[b%byte(len(lettersDefaultValue))]
}
return string(bytes), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xiedongji/antgo.git
git@gitee.com:xiedongji/antgo.git
xiedongji
antgo
antgo
v1.0.17

搜索帮助