rands

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 3 Imported by: 2

README

rands Build Status license codecov Go Reference

rands 为一个随机字符串生成工具。

// 生成一个长度为 [8,10) 之间的随机字符串
str := rands.String(8, 10, []byte("1234567890abcdefg"))


// 生成一个带缓存功能的随机字符串生成器
r, err := rands.New(time.Now().Unix(), 100, 5, 7, []byte("asdfghijklmn"))
ctx,cancel := context.WithCancel(context.Background())
go r.Serve(ctx)
defer cancel()
str1 := r.String()
str2 := r.String()

安装

go get github.com/issue9/rands/v2

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package rands 生成各种随机字符串

// 生成一个长度介于 [6,9) 之间的随机字符串
str := rands.String(6, 9, "1343567")

// 生成一个带缓存功能的随机字符串生成器
r := rands.New(time.Now().Unix(), 100, 5, 7, "adbcdefgadf;dfe1334")
ctx,cancel := context.WithCancel(context.Background())
go r.Serve(ctx)
defer cancel()
str1 := r.String()
str2 := r.String()

NOTE: 仅是随机字符串,不保证唯一性。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alpha

func Alpha() []byte

Alpha 返回所有的字母

func AlphaNumber

func AlphaNumber() []byte

AlphaNumber Alpha + Number

func AlphaNumberPunct

func AlphaNumberPunct() []byte

AlphaNumberPunct Alpha + Number + Punct

func Bytes

func Bytes(min, max int, bs []byte) []byte

Bytes 产生随机字符数组

其长度为[min, max),bs 所有的随机字符串从此处取。

func Number

func Number() []byte

Number 返回所有的数字

func Punct

func Punct() []byte

Punct 返回所有的标点符号

func Seed

func Seed(seed int64)

Seed 手动指定一个随机种子

默认情况下使用当前包初始化时的时间戳作为随机种子。 BytesString 依赖此项。但是 Rands 有专门的随机函数,不受此影响。

func String

func String(min, max int, bs []byte) string

String 产生一个随机字符串

其长度为[min, max),bs 可用的随机字符。

Types

type Rands

type Rands struct {
	// contains filtered or unexported fields
}

Rands 提供随机字符串的生成

func New

func New(seed int64, bufferSize, min, max int, bs []byte) *Rands

New 声明 Rands

seed 随机种子,若为 0 表示使用当前时间作为随机种子。 bufferSize 缓存的随机字符串数量,若为 0,表示不缓存。

func (*Rands) Bytes

func (r *Rands) Bytes() []byte

Bytes 产生随机字符数组

功能与全局函数 Bytes 相同,但参数通过 New 预先指定。

func (*Rands) Seed

func (r *Rands) Seed(seed int64)

Seed 重新指定随机种子

func (*Rands) Serve

func (r *Rands) Serve(ctx context.Context) error

func (*Rands) String

func (r *Rands) String() string

String 产生一个随机字符串

功能与全局函数 String 相同,但参数通过 New 预先指定。

Jump to

Keyboard shortcuts

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