randompkg

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

random

生成随机字符串、验证码、密码、订单号、trace ID,或随机选取/打乱切片。

基础用法

code := randompkg.VerifyCode(6)
token := randompkg.Token(32)
password := randompkg.Password(12)
secureToken, err := randompkg.SecureToken(32)
secureURLToken, err := randompkg.SecureBase64URL(32)

注意事项

  • TokenPasswordVerifyCode 等历史函数基于 math/rand,只适合非安全随机场景。
  • 安全 token、重置密码链接、外部认证随机串等场景使用 SecureTokenSecureHexSecureBase64URL
  • 安全随机函数基于 crypto/rand,会返回 error,调用方必须处理。

验证

go test ./random

Documentation

Index

Constants

View Source
const (
	CharsetLowercase = "abcdefghijklmnopqrstuvwxyz"
	CharsetAlphabet  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	CharsetNumeral   = "1234567890"
	CharsetHex       = "1234567890abcdef"
)
View Source
const (
	CharsetAlphanumeric = CharsetAlphabet + CharsetNumeral
	CharsetUppercase    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharsetSpecial      = "!@#$%^&*()-_=+[]{}|;:,.<>?"
	CharsetPassword     = CharsetAlphanumeric + "!@#$%^&*"
)

扩展字符集常量

Variables

This section is empty.

Functions

func AlphabetLower

func AlphabetLower(n int) string

AlphabetLower 从小写字符集生成指定长度的随机字符串

func Bool

func Bool() bool

Bool 随机返回 true 或 false

func Element

func Element[T any](slice []T) T

Element 从切片中随机选取一个元素

func Hex

func Hex(n int) string

Hex 生成十六进制随机字符串

func Int32Between

func Int32Between(min, max int32) int32

Int32Between random number between min-max

func Int64Between

func Int64Between(min, max int64) int64

Int64Between random number between min-max

func IntBetween

func IntBetween(min, max int) int

IntBetween random number between min-max

func Letter

func Letter(size int) string

Letter : A-Z a-z

func Name

func Name() string

Name random name

func NewRandHandler

func NewRandHandler() *rand.Rand

NewRandHandler 创建随机数生成器 Deprecated: Go 1.20+ 全局 rand 已自动随机种子且并发安全,建议直接使用 rand 包全局函数

func Numeric

func Numeric(size int) string

Numeric 0-9

func NumericBetween

func NumericBetween(min, max int64) int64

NumericBetween random number between min-max

func OrderNo

func OrderNo(randomSuffixLen int) string

OrderNo 生成订单号:时间戳前缀 + 随机数字后缀

func Password

func Password(length int) string

Password 生成随机密码(包含大小写字母、数字、特殊字符)

func Sample

func Sample[T any](slice []T, n int) []T

Sample 从切片中随机选取 n 个不重复元素

func SecureBase64URL

func SecureBase64URL(length int) (string, error)

SecureBase64URL 生成 URL 安全的 base64 token。 length 表示随机字节长度,不是最终字符串长度。

func SecureBytes

func SecureBytes(length int) ([]byte, error)

SecureBytes 使用 crypto/rand 生成指定长度的安全随机字节。

func SecureHex

func SecureHex(length int) (string, error)

SecureHex 生成十六进制安全随机字符串。

func SecureString

func SecureString(length int, charset string) (string, error)

SecureString 使用 crypto/rand 从指定字符集中生成安全随机字符串。

func SecureToken

func SecureToken(length int) (string, error)

SecureToken 生成 URL 安全的随机 token。

func Shuffle

func Shuffle[T any](slice []T)

Shuffle 随机打乱切片(原地修改)

func String

func String(n int, charset string) string

String returns a random string n characters long, composed of entities from charset.

func Strings

func Strings(size int) string

Strings : A-Z a-z 0-9

func Token

func Token(length int) string

Token 生成 URL 安全的随机 token

func TraceID

func TraceID() string

TraceID 生成 32 位十六进制 trace ID

func VerifyCode

func VerifyCode(length int) string

VerifyCode 生成纯数字验证码(短信/邮箱验证码)

func WeightedIndex

func WeightedIndex(weights []int) int

WeightedIndex 按权重随机选择索引

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL